com.alogic.together2.service.SSEServant Maven / Gradle / Ivy
package com.alogic.together2.service;
import com.alogic.auth.*;
import com.alogic.together2.TogetherServiceDescription;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.Script;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.doc.json.JsonObject;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import com.logicbus.backend.AbstractServant;
import com.logicbus.backend.Context;
import com.logicbus.backend.message.SSEMessage;
import com.logicbus.backend.server.http.HttpContext;
import com.logicbus.models.servant.ServiceDescription;
import org.apache.commons.lang3.StringUtils;
import java.util.HashMap;
/**
* SSEServant
*
* @since 1.6.13.32 [20210128 duanyy]
*/
public class SSEServant extends AbstractServant {
protected Script script = null;
protected String service;
protected String privilege;
@Override
protected void onDestroy() {
}
@Override
protected void onCreate(ServiceDescription sd) {
service = sd.getPath();
privilege = sd.getPrivilege();
if (sd instanceof TogetherServiceDescription){
script = ((TogetherServiceDescription)sd).getScript();
}else{
Properties p = sd.getProperties();
String bootstrap = PropertiesConstants.getString(p,"bootstrap","",true);
if (StringUtils.isEmpty(bootstrap)){
String config = PropertiesConstants.getString(p,"script","");
if (StringUtils.isNotEmpty(config)){
script = Script.create(config, p);
}
}else{
String config = PropertiesConstants.getString(p,"script","");
if (StringUtils.isNotEmpty(config)){
script = Script.create(bootstrap, config, p);
}
}
}
}
public int doAction(Context ctx){
if (script != null){
SSEMessage msg = (SSEMessage) ctx.asMessage(SSEMessage.class);
HttpContext httpCtx = (HttpContext)ctx;
SessionManager sm = SessionManagerFactory.getDefault();
Session session = sm.getSession(httpCtx.getRequest(),httpCtx.getResponse(),true);
CookieManager cm = new CookieManager.Default(sm, httpCtx.getRequest(), httpCtx.getResponse());
LogicletContext logicletContext = new Context.ServantLogicletContext(ctx);
logicletContext.setObject("$context", ctx);
logicletContext.SetValue("$service", service);
logicletContext.SetValue("$privilege",StringUtils.isEmpty(privilege)?service:privilege);
logicletContext.setObject(Constants.ID_SESSION, session);
logicletContext.setObject(Constants.ID_COOKIES, cm);
logicletContext.setObject(SSEMessage.ID,msg);
try {
XsObject doc = new JsonObject("root",new HashMap());
script.execute(doc,doc, logicletContext, null);
}finally{
logicletContext.removeObject(Constants.ID_SESSION);
logicletContext.removeObject(Constants.ID_COOKIES);
logicletContext.removeObject("$context");
String keyword = logicletContext.GetValue("$keyword", "");
if (StringUtils.isNotEmpty(keyword)){
ctx.setKeyword(keyword);
}
}
}else{
SSEMessage msg = (SSEMessage) ctx.asMessage(SSEMessage.class);
}
return 0;
}
@Override
protected int onJson(Context ctx) {
return doAction(ctx);
}
protected int onYaml(Context ctx){ // NOSONAR
return doAction(ctx);
}
protected int onXml(Context ctx) {
return doAction(ctx);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy