All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alogic.sdep.client.DefaultSdepServer Maven / Gradle / Ivy

package com.alogic.sdep.client;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.anysoft.webloader.HttpClientTool;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;

import com.alogic.auth.CookieManager;
import com.alogic.auth.Session;
import com.alogic.auth.SessionManager;
import com.alogic.xscript.Logiclet;
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.JsonTools;
import com.anysoft.util.KeyGen;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import com.anysoft.util.Settings;
import com.anysoft.util.XmlElementProperties;
import com.anysoft.util.XmlTools;
import com.logicbus.backend.Context;
import com.logicbus.backend.server.http.HttpContext;
import com.logicbus.backend.bizlog.BizLog;

/**
 * 缺省实现
 * @author duanwaiwai
 * @since 1.6.12.36 [20190611 duanyy] 
*/ public class DefaultSdepServer implements SdepServer { /** * a logger of slf4j */ protected final static Logger LOG = LoggerFactory.getLogger(SdepServer.class); /** * Server id */ protected String id; /** * SDEP服务器的登录地址 */ protected String loginURL = "${sdep.server}/sdep/login"; /** * SDEP服务器的登出地址 */ protected String logoutURL = "${sdep.server}/logout"; /** * CAS服务器的回调路径 */ protected String callbackPath = "/sdepclient/sdep/${id}"; /** * 回调服务器地址 */ protected String callbackServer = ""; /** * service参数名 */ protected String arguService = "service"; /** * 当回调时 */ protected Logiclet onCallback = null; /** * 当注销时执行脚本 */ protected Logiclet onLogout = null; /** * 当登录时执行脚本 */ protected Logiclet onLogin = null; /** * 编码 */ protected String encoding = "utf-8"; /** * 时间戳 */ private long timestamp = System.currentTimeMillis(); protected String sessionGroup = "$sdep-client"; protected HttpClientTool httpClientTool = null; public void bizlog(String service,String clientIp,long time,long duration,String url,boolean error,String reason){ BizLog.log(KeyGen.uuid(8,0,15),service,clientIp,error?CODE_ERR:CODE_OK,reason,time,duration,url); } /** * 获取生存时间(毫秒) * @return 生存时间 */ protected long getTTL(){ return 5 * 60 * 1000L; } @Override public long getTimestamp() { return timestamp; } @Override public boolean isExpired() { return System.currentTimeMillis() - this.timestamp > getTTL(); } @Override public void expire() { this.timestamp = System.currentTimeMillis() - getTTL(); } @Override public void report(Element xml) { if (xml != null){ XmlTools.setString(xml,"module",getClass().getName()); } } @Override public void report(Map json) { if (json != null){ JsonTools.setString(json,"module",getClass().getName()); } } @Override public void configure(Properties props) { id = PropertiesConstants.getString(props, "id", ""); loginURL = PropertiesConstants.getString(props, "sdep.server.login", loginURL); logoutURL = PropertiesConstants.getString(props, "sdep.server.logout", logoutURL); callbackPath = PropertiesConstants.getString(props, "sdep.callback.path", callbackPath); callbackServer = PropertiesConstants.getString(props, "sdep.calback.server", callbackServer); arguService = PropertiesConstants.getString(props, "sdeo.para.service", arguService); encoding = PropertiesConstants.getString(props,"http.encoding",encoding); sessionGroup = PropertiesConstants.getString(props, "sdep.client.group",sessionGroup); httpClientTool = Settings.getToolkit(HttpClientTool.class); if (onCallback == null){ String script = PropertiesConstants.getString(props, "onCallback",""); if (StringUtils.isNotEmpty(script)){ onCallback = Script.createFromContent(script, props); } } if (onLogout == null){ String script = PropertiesConstants.getString(props, "onLogout",""); if (StringUtils.isNotEmpty(script)){ onLogout = Script.createFromContent(script, props); } } if (onLogin == null){ String script = PropertiesConstants.getString(props, "onLogin",""); if (StringUtils.isNotEmpty(script)){ onLogin = Script.createFromContent(script, props); } } } @Override public void configure(Element e, Properties p) { Properties props = new XmlElementProperties(e,p); Element elem = XmlTools.getFirstElementByPath(e, "on-callback"); if (elem != null){ onCallback = Script.create(elem, props); } elem = XmlTools.getFirstElementByPath(e, "on-logout"); if (elem != null){ onLogout = Script.create(elem, props); } elem = XmlTools.getFirstElementByPath(e, "on-login"); if (elem != null){ onLogin = Script.create(elem, props); } configure(props); } @Override public String getId() { return id; } @Override public boolean isOk() { return StringUtils.isNotEmpty(id); } @Override public void doCallback(HttpServletRequest httpReq,HttpServletResponse httpResp,SessionManager sm,Session session) { if (onCallback != null){ Context ctx = new HttpContext(httpReq,httpResp,encoding); Session sess = getSession(sm,httpReq,httpResp, true); LogicletContext logicletContext = new Context.ServantLogicletContext(ctx); CookieManager cm = new CookieManager.Default(sm, httpReq, httpResp); long start = System.nanoTime(); String clientIp = httpClientTool.getClientIp(httpReq); try { logicletContext.setObject(ID_SESSION, sess); logicletContext.setObject(ID_COOKIES, cm) ; logicletContext.SetValue("$service", "/sdep/client/Callback"); logicletContext.SetValue("$clientIp",clientIp); XsObject doc = new JsonObject("root",new HashMap()); onCallback.execute(doc,doc, logicletContext, null); }finally{ logicletContext.removeObject(ID_SESSION); logicletContext.removeObject(ID_COOKIES); bizlog("/sdep/Callback",clientIp,System.currentTimeMillis(),System.nanoTime() - start,httpReq.getRequestURL().toString(),false,""); } } } @Override public void doLogout(HttpServletRequest httpReq, HttpServletResponse httpResp, SessionManager sm, Session session) { String nextUrl = logoutURL; if (onLogout != null){ Context ctx = new HttpContext(httpReq,httpResp,encoding); Session sess = getSession(sm,httpReq,httpResp, true); LogicletContext logicletContext = new Context.ServantLogicletContext(ctx); CookieManager cm = new CookieManager.Default(sm, httpReq, httpResp); long start = System.nanoTime(); String clientIp = httpClientTool.getClientIp(httpReq); try { logicletContext.setObject(ID_SESSION, sess); logicletContext.setObject(ID_COOKIES, cm) ; logicletContext.SetValue("$service", "/sdep/client/Logout"); logicletContext.SetValue("$nextUrl", nextUrl); logicletContext.SetValue("$clientIp",clientIp); XsObject doc = new JsonObject("root",new HashMap()); onLogout.execute(doc,doc, logicletContext, null); nextUrl = PropertiesConstants.getString(logicletContext, "$nextUrl", nextUrl); }finally{ logicletContext.removeObject(ID_SESSION); logicletContext.removeObject(ID_COOKIES); bizlog("/sdep/Logout",clientIp,System.currentTimeMillis(),System.nanoTime() - start,httpReq.getRequestURL().toString(),false,""); } } try { //客户端模式,重定向到服务端 if (StringUtils.isNotEmpty(nextUrl)){ httpClientTool.sendRedirect(httpResp,nextUrl); } } catch (Exception ex){ LOG.error(ExceptionUtils.getStackTrace(ex)); } } @Override public void doLogin(HttpServletRequest httpReq, HttpServletResponse httpResp, SessionManager sm, Session session) { String callback = getSdepService(httpReq,httpResp,session); try { String nextUrl = String.format("%s?%s=%s",loginURL,arguService,URLEncoder.encode(callback,encoding)); if (onLogin != null){ Context ctx = new HttpContext(httpReq,httpResp,encoding); Session sess = getSession(sm,httpReq,httpResp, true); LogicletContext logicletContext = new Context.ServantLogicletContext(ctx); CookieManager cm = new CookieManager.Default(sm, httpReq, httpResp); long start = System.nanoTime(); String clientIp = httpClientTool.getClientIp(httpReq); try { logicletContext.setObject(ID_SESSION, sess); logicletContext.setObject(ID_COOKIES, cm) ; logicletContext.SetValue("$service", "/sdep/client/Login"); logicletContext.SetValue("$clientIp",clientIp); logicletContext.SetValue("$nextUrl", nextUrl); logicletContext.SetValue("$sdep-service", callback); XsObject doc = new JsonObject("root",new HashMap()); onLogin.execute(doc,doc, logicletContext, null); nextUrl = PropertiesConstants.getString(logicletContext, "$nextUrl", nextUrl); }finally{ logicletContext.removeObject(ID_SESSION); logicletContext.removeObject(ID_COOKIES); bizlog("/sdep/Login",clientIp,System.currentTimeMillis(),System.nanoTime() - start,httpReq.getRequestURL().toString(),false,""); } } LOG.info("User has not logged in,redirect to: " + nextUrl); httpClientTool.sendRedirect(httpResp,nextUrl); }catch (Exception ex){ LOG.error(ExceptionUtils.getStackTrace(ex)); } } protected Session getSession(SessionManager sm,HttpServletRequest request,HttpServletResponse response,boolean create){ return sm.getSession(request,response,create); } protected String getSdepService(HttpServletRequest httpReq,HttpServletResponse httpResp, Session session) { StringBuffer callback = new StringBuffer(); if (StringUtils.isNotEmpty(callbackServer)){ callback.append(callbackServer).append(httpReq.getContextPath()); }else{ callback.append(httpClientTool.getContextBase(httpReq)); } callback.append(callbackPath).append("/").append(session.getId()); return callback.toString(); } /** * 从Request中获取指定的参数 * @param request HttpServletRequest * @param id 参数id * @param dftValue 缺省值,当参数不存在时,返回 * @return 参数值,如果参数不存在,返回缺省值 */ protected String getParameter(HttpServletRequest request,String id,String dftValue){ String value = request.getParameter(id); return StringUtils.isEmpty(value)?dftValue:value; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy