com.alogic.together2.service.SessionTogetherServant Maven / Gradle / Ivy
package com.alogic.together2.service;
import com.logicbus.backend.message.YamlMessage;
import org.apache.commons.lang3.StringUtils;
import com.alogic.auth.Constants;
import com.alogic.auth.CookieManager;
import com.alogic.auth.Session;
import com.alogic.auth.SessionManager;
import com.alogic.auth.SessionManagerFactory;
import com.alogic.together.service.SevantLogicletContext;
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.alogic.xscript.doc.xml.XmlObject;
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.JsonMessage;
import com.logicbus.backend.message.XMLMessage;
import com.logicbus.backend.server.http.HttpContext;
import com.logicbus.models.servant.ServiceDescription;
/**
* 基于会话和cookie的服务实现
* @author yyduan
* @since 1.6.12.8
*
* @version 1.6.12.27 [20190403 duanyy]
* - 增加对yaml的支持
*
* @version 1.6.12.39 [20190725]
* - 增加$privilege上下文变量
*/
public class SessionTogetherServant 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);
}
}
}
}
@Override
protected int onJson(Context ctx) {
if (script != null){
JsonMessage msg = (JsonMessage) ctx.asMessage(JsonMessage.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);
try {
XsObject doc = new JsonObject("root",msg.getRoot());
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);
}
String outputPath = logicletContext.GetValue("$outpath", "");
if (StringUtils.isNotEmpty(outputPath)){
msg.setOutputPath(outputPath);
}
}
}else{
ctx.asMessage(JsonMessage.class);
}
return 0;
}
protected int onYaml(Context ctx){ // NOSONAR
if (script != null){
YamlMessage msg = (YamlMessage) ctx.asMessage(YamlMessage.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.setObject(Constants.ID_SESSION, session);
logicletContext.setObject(Constants.ID_COOKIES, cm);
try {
XsObject doc = new JsonObject("root",msg.getRoot());
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);
}
String outputPath = logicletContext.GetValue("$outpath", "");
if (StringUtils.isNotEmpty(outputPath)){
msg.setOutputPath(outputPath);
}
}
}else{
ctx.asMessage(YamlMessage.class);
}
return 0;
}
protected int onXml(Context ctx) {
if (script != null){
XMLMessage msg = (XMLMessage) ctx.asMessage(XMLMessage.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 SevantLogicletContext(ctx);
logicletContext.setObject("$context", ctx);
logicletContext.SetValue("$service", service);
logicletContext.setObject(Constants.ID_SESSION, session);
logicletContext.setObject(Constants.ID_COOKIES, cm);
try {
XsObject doc = new XmlObject("root",msg.getRoot());
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{
ctx.asMessage(XMLMessage.class);
}
return 0;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy