Commit 69e4d0ae by fukai

支持无会话访问交易

parent 2f926f92
......@@ -6,14 +6,15 @@ import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.sss.presentation.noui.common.Constants;
import org.sss.presentation.noui.context.NoUiContextManager;
public class NoUiRequest {
private HttpServletRequest httpRequest;
private String token;
private String userId;
private String terminalType;// WEB APP
private String mappingUrl;
private boolean openSource;
private Map<String, ?> paramsMap = new HashMap<String, Object>();
private Map<String, ?> dataMap = new HashMap<String, Object>();
private Map<String, ?> saveDisplayMap = new HashMap<String, Object>();
......@@ -30,6 +31,10 @@ public class NoUiRequest {
this.userId = userId;
this.terminalType = terminalType;
this.mappingUrl = mappingUrl;
if(request.getRequestURI().startsWith(NoUiContextManager.openSourcePrefix))
{
this.openSource = true; //开放访问路径
}
if (requestData != null) {
this.paramsMap = (Map<String, ?>) requestData.get(Constants.PARAMS);
this.dataMap = (Map<String, ?>) requestData.get(Constants.DATA);
......@@ -43,6 +48,9 @@ public class NoUiRequest {
}
}
public boolean isOpenSource(){
return this.openSource;
}
public String getUserId() {
return userId;
}
......
......@@ -26,9 +26,14 @@ public class ErrorCodes {
public static final String DISPLAY_ERROR = "R0014";
public static final String UNCORRECT_DATA_FORMAT = "R0015";
public static final String UNSUPPORTED_DATA_TYPE = "R0016";
public static final String UNKNOEW_TRANS = "R0017";
public static final String FORBIDDEN_TRANS = "R0018";
public static final String INTERRUPTED_ERROR = "R9997";
public static final String REDIS_CONNECTION_ERROR = "R9998";
public static final String ERROR = "R9999";
public static final String BUSINESS_ERROR = "E0001";
}
package org.sss.presentation.noui.context;
import org.sss.common.impl.AbstractContext;
import org.sss.presentation.noui.jwt.RedisLoginInfo;
public class NoUiContext extends AbstractContext {
......@@ -22,4 +23,14 @@ public class NoUiContext extends AbstractContext {
return null;
}
private RedisLoginInfo redisLoginInfo;
public RedisLoginInfo getRedisLoginInfo()
{
return this.redisLoginInfo;
}
public void setRedisLoginInfo(RedisLoginInfo redisLoginInfo)
{
this.redisLoginInfo = redisLoginInfo;
}
}
package org.sss.presentation.noui.context;
import java.util.UUID;
import java.util.ArrayList;
import java.util.List;
import log.Log;
import log.LogFactory;
......@@ -18,11 +19,18 @@ public class NoUiContextManager {
private static final Log log = LogFactory.getLog(NoUiContextManager.class);
public static String dbType = "hibernate";
public static String loginContextName = "cn.com.brilliance.eibs.auth.DatabaseLoginContext";
public static String openSourcePrefix = "openservice";
public static String everybody = "#Everybody#";
public static List<String> openTransactions = new ArrayList<String>();
public static NoUiContext createNoUiContext(NoUiRequest noUiRequest) {
NoUiContext noUiContext = new NoUiContext();
ILoginContext loginContext = null;
if (StringUtils.isEmpty(noUiRequest.getToken())) {
if(noUiRequest.isOpenSource())
{
loginContext = new EmptyLoginContext(everybody);
}
else if (StringUtils.isEmpty(noUiRequest.getToken())) {
try {
loginContext = (ILoginContext) Class.forName(loginContextName).newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
......
......@@ -36,6 +36,7 @@ import org.sss.presentation.noui.api.request.NoUiRequest;
import org.sss.presentation.noui.api.response.ErrorCodes;
import org.sss.presentation.noui.common.Constants;
import org.sss.presentation.noui.util.NoUiUtils;
import org.sss.presentation.noui.util.RedisUtil;
import org.sss.util.ContainerUtils;
import com.google.gson.Gson;
......@@ -72,6 +73,38 @@ public class NoUiPresentation extends AbstractNullPresentation {
private String message = null; // 弹出的message
private String message_code = ErrorCodes.SUCCESS;
public IPanel popupPanel = null;
@Override
public void close(IPanel panel) throws ContainerException {
// TODO Auto-generated method stub
super.close(panel);
}
@Override
public void popupOnly(IPanel panel) throws ContainerException {
// TODO Auto-generated method stub
super.popupOnly(panel);
}
@Override
public Object readFromSession(String name) {
return ((NoUiContext)this.ctx).getRedisLoginInfo().getContextMap().get(name);
}
@Override
public void showMainPanel() throws ContainerException {
}
@Override
public void showPanel(IPanel panel) throws ContainerException {
super.showPanel(panel);
}
@Override
public void writeToSession(String name, Object value) {
((NoUiContext)this.ctx).getRedisLoginInfo().getContextMap().put(name, value);
}
public String transactionName = null;
public boolean clearFlag = false;
public boolean newTabFlag = false;
......
......@@ -9,12 +9,14 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import org.sss.common.model.EventType;
import org.sss.common.model.IBaseObject;
import org.sss.common.model.IDatafield;
import org.sss.common.model.IModule;
import org.sss.common.model.IModuleList;
import org.sss.common.model.IPanel;
import org.sss.common.model.IStream;
import org.sss.presentation.noui.api.exception.NoUiException;
import org.sss.presentation.noui.api.model.Alias;
......@@ -34,7 +36,7 @@ import org.sss.presentation.noui.util.StringUtil;
import log.Log;
import log.LogFactory;
public class AbstractCommonController {
public abstract class AbstractCommonController {
protected static final Log log = LogFactory.getLog(AbstractCommonController.class);
protected static String ON_CLICK = "ON_CLICK";
protected static String INIT = "INIT";
......@@ -43,38 +45,17 @@ public class AbstractCommonController {
protected static String ON_STREAM_UPLOAD = "ON_STREAM_UPLOAD";
protected static String ON_STREAM_DOWNLOAD = "ON_STREAM_DOWNLOAD";
public String getMainPanel(){
return "";
}
@SuppressWarnings("unchecked")
public Object event(String mappingUrl, String eventType,MultipartFile file, Map<String, Object> dataMap, HttpServletRequest request, HttpServletResponse response) {
public Object event(String mappingUrl, String eventType, Map<String, Object> dataMap, MultipartFile file,HttpServletRequest request, HttpServletResponse response) {
NoUiContext context = null;
Result ret = null;
try {
// isMultipart = ServletFileUpload.isMultipartContent(request);
// if (isMultipart) {
//
// ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
// // Parse the request
// List<FileItem> items = upload.parseRequest(request);
// Iterator<FileItem> iter = items.iterator();
//
// while (iter.hasNext()) {
// FileItem item = iter.next();
// // 普通表单域
// if (item.isFormField()) {
// String itemName = item.getFieldName();
// String itemValue = item.getString();
// log.info("文件表单 key:" + itemName + ",Value:" + itemValue);
// // 给dataMap赋值
// Map<String, ?> itemMap = new Gson().fromJson(itemValue, Map.class);
// dataMap.put(itemName, itemMap);
// } else { // 文件域
// lastFileItem = item;
// }
// }
// } else {
// log.info("普通表单key-Value:" + dataMap);
// }
NoUiRequest noUiRequest = new NoUiRequest(request, mappingUrl, dataMap);
context = NoUiContextManager.createNoUiContext(noUiRequest);
Alias alias = new Alias(mappingUrl);
......@@ -85,12 +66,24 @@ public class AbstractCommonController {
context.getSession().storeData(key, paramsMap.get(key));
}
// 设置old sysmod
RedisLoginInfo redisLoginInfo = (RedisLoginInfo) RedisUtil.get(StringUtil.userUniqueId(noUiRequest));
NoUiPresentationUtil.setSysmod(context, (byte[]) redisLoginInfo.getSysmod());
RedisLoginInfo redisLoginInfo = null;
if( !StringUtils.isEmpty(noUiRequest.getUserId()) ) //开放模式下
redisLoginInfo = (RedisLoginInfo) RedisUtil.get(StringUtil.userUniqueId(noUiRequest));
if(redisLoginInfo != null)
{
NoUiPresentationUtil.setSysmod(context, (byte[]) redisLoginInfo.getSysmod());
context.setRedisLoginInfo(redisLoginInfo);
}
// 交易跳转
context.getSession().chain(true, trnName);
//执行可能存在的主面板的初始化
if(this.getMainPanel().length() > 0)
{
IPanel mainPanel = (IPanel)context.getSession().getBaseObject(null, this.getMainPanel());
mainPanel.invokeDefaultRules(context);
}
// 模型赋值
NoUiPresentationUtil.hanleInput(context, noUiRequest, alias);
......@@ -115,9 +108,9 @@ public class AbstractCommonController {
@SuppressWarnings("rawtypes")
IDatafield dataField = (IDatafield) baseObject(context, noUiRequest, alias);
IStream stream = (IStream) dataField.getValue();
stream.setName(file.getName());
stream.setName(file.getOriginalFilename());
stream.setType(file.getContentType());
log.info("文件表单 key:" + file.getName() + ",size:" + file.getSize());
log.info("文件表单 key:" + file.getOriginalFilename() + ",size:" + file.getSize());
IOUtils.copy(file.getInputStream(), stream.getOutputStream(), 1024);
dataField.invokeEventRules(context, EventType.ON_STREAM_UPLOAD, null);
} else if (eventType.equals(ON_STREAM_DOWNLOAD)) {
......@@ -130,9 +123,13 @@ public class AbstractCommonController {
}
// 保存新的RedisLoginInfo
byte[] sysmodBytes = NoUiPresentationUtil.sysmodToBytes(context);
redisLoginInfo.setSysmod(sysmodBytes);
RedisUtil.set(StringUtil.userUniqueId(noUiRequest), redisLoginInfo);
if(redisLoginInfo!=null) //当为开放模式下,redisLoginInfo 为空
{
byte[] sysmodBytes = NoUiPresentationUtil.sysmodToBytes(context);
redisLoginInfo.setSysmod(sysmodBytes);
RedisUtil.set(StringUtil.userUniqueId(noUiRequest), redisLoginInfo);
}
Map<String, Object> afterReturnData = handleReturnData(eventType, context, noUiRequest, alias);
......
......@@ -112,4 +112,14 @@ public class LoginController {
getMenus(item, subMenuList);
}
}
@ResponseBody
@RequestMapping(value = "/logout", method = RequestMethod.POST)
public Object logout(@RequestBody Map<String, Object> dataMap, HttpServletRequest request, HttpSession session) {
return "ok";
}
}
package org.sss.presentation.noui.jwt;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.sss.presentation.noui.api.request.NoUiRequest;
import org.sss.presentation.noui.api.response.ErrorCodes;
import org.sss.presentation.noui.api.response.Result;
import org.sss.presentation.noui.context.NoUiContextManager;
import org.sss.presentation.noui.util.NumericUtil;
import org.sss.presentation.noui.util.RedisUtil;
import org.sss.presentation.noui.util.StringUtil;
import com.google.gson.Gson;
import log.Log;
import log.LogFactory;
public class OpenTransInterceptor implements HandlerInterceptor {
protected static final Log log = LogFactory.getLog(OpenTransInterceptor.class);
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception arg3) throws Exception {
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView model) throws Exception {
}
// 拦截每个请求
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
response.setCharacterEncoding("utf-8");
NoUiRequest noUiRequest = new NoUiRequest(request, "", null);
String token = noUiRequest.getToken();
String userId = noUiRequest.getUserId();
String terminalType = noUiRequest.getTerminalType(); // APP WEB
RedisLoginInfo redisLoginInfo = null;
if( !StringUtils.isEmpty(noUiRequest.getUserId())) //开放模式下
redisLoginInfo = (RedisLoginInfo) RedisUtil.get(StringUtil.userUniqueId(noUiRequest));
//如果是已登录状态,要刷新超时时间
if (redisLoginInfo != null && System.currentTimeMillis() <= redisLoginInfo.getExpiredTime()) {
// 重新刷入登陆时间
RedisLoginInfo nweRedisLoginInfo = new RedisLoginInfo(userId, token, NumericUtil.sessionTimeOut(), redisLoginInfo.getSysmod());
RedisUtil.set(userId + "." + terminalType, nweRedisLoginInfo);
}
String url = request.getRequestURI();
String trnnam = null;
int idx1 = url.indexOf(NoUiContextManager.openSourcePrefix);
int begpos = idx1 + NoUiContextManager.openSourcePrefix.length()+1;
int idx2 = url.indexOf("/", begpos);
if(idx2 > 0)
trnnam = url.substring(begpos, idx2);
if(trnnam==null)
{
Result rt = new Result(ErrorCodes.UNKNOEW_TRANS, "未知的交易", null);
responseMessage(response, response.getWriter(), rt);
return false;
}
//交易必须是配置允许开放的交易
log.debug("开放访问交易名:"+trnnam);
if(!NoUiContextManager.openTransactions.contains(trnnam))
{
Result rt = new Result(ErrorCodes.FORBIDDEN_TRANS, "非法访问", null);
responseMessage(response, response.getWriter(), rt);
return false;
}
return true;
}
// 请求不通过,返回错误信息给客户端
private void responseMessage(HttpServletResponse response, PrintWriter out, Result result) {
response.setContentType("application/json; charset=utf-8");
String json = new Gson().toJson(result);
out.print(json);
out.flush();
out.close();
}
}
\ No newline at end of file
package org.sss.presentation.noui.jwt;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public class RedisLoginInfo implements Serializable {
/**
......@@ -24,6 +26,16 @@ public class RedisLoginInfo implements Serializable {
private long expiredTime;
private Object sysmod;
private Map<String,Object> contextMap = new HashMap<String,Object>();
public Map<String, Object> getContextMap() {
return contextMap;
}
public void setContextMap(Map<String, Object> contextMap) {
this.contextMap = contextMap;
}
public RedisLoginInfo() {
......
package org.sss.presentation.noui.listener;
import java.util.Arrays;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.lang.StringUtils;
import org.sss.presentation.noui.context.NoUiContextManager;
import log.Log;
import log.LogFactory;
public class NoUiApiInitListener implements ServletContextListener {
protected static final Log log = LogFactory.getLog(NoUiApiInitListener.class);
private static final String DATABASE_SUPPORT_TYPE = "DbSupportType";
private static final String NOUI_TIMEOUT = "nouiTimeout";
private static final String LOGIN_CONTEXT_NAME = "loginContextName";
private static final String OPEN_PREFIX = "openPrefix";
private static final String OPEN_TRANS = "openTrans";
@Override
public void contextDestroyed(ServletContextEvent arg0) {
......@@ -24,5 +36,16 @@ public class NoUiApiInitListener implements ServletContextListener {
int timeout = Integer.valueOf(context.getInitParameter(NOUI_TIMEOUT));
NoUiContextManager.dbType = dbType;
NoUiContextManager.loginContextName = context.getInitParameter(LOGIN_CONTEXT_NAME);
String openSource = context.getInitParameter(OPEN_PREFIX);
if(openSource!=null)
NoUiContextManager.openSourcePrefix = openSource;
String trans = context.getInitParameter(OPEN_TRANS);
if(!StringUtils.isEmpty(trans))
{
List<String> openTrans = Arrays.asList(trans.split(","));
NoUiContextManager.openTransactions.addAll( openTrans);
log.debug("开放的交易:"+trans+";"+openTrans);
}
}
}
......@@ -244,7 +244,7 @@ public class NoUiPresentationUtil {
return null;
}
public static Object handIBaseObject(NoUiContext context, Object val, String path) {
public static Object handIBaseObject(IContext context, Object val, String path) {
if (val == null)
return null;
if (val instanceof IModuleList<?>) {
......@@ -269,19 +269,35 @@ public class NoUiPresentationUtil {
}
return null;
}
public static Map<String, Object> getWholeAliasData(IContext context,String mappingUrl)
{
Map<String, Object> data = new HashMap<String, Object>();
Alias alias = new Alias(mappingUrl);
for (String aliasKey : alias.getRel().keySet()) {
if (aliasKey.startsWith(Constants.MAPPING_PRE)) {
continue;
}
String realPath = alias.getRelPath(aliasKey);
IBaseObject baseObject = context.getSession().getBaseObject(context.getRoot(), realPath);
data.put(aliasKey, NoUiPresentationUtil.handIBaseObject(context, baseObject, realPath));
}
return data;
}
public static Map<String, Object> handleErrorReturnData(NoUiContext context, Alias alias) {
NoUiPresentation gui = (NoUiPresentation) context.getGui();
Map<String, Object> errorMap = gui.getError();
Map<String, Object> n_errorMap = new HashMap<String,Object>();
for (Map.Entry<String, Object> errorEntity : errorMap.entrySet()) {
String key = errorEntity.getKey();
String realKey = alias.getRevertRel().get(key);
if (!StringUtils.isEmpty(realKey))
errorMap.put(realKey, errorEntity.getValue());
n_errorMap.put(realKey, errorEntity.getValue());
else
errorMap.put(key, errorEntity.getValue());
n_errorMap.put(key, errorEntity.getValue());
}
return errorMap;
return n_errorMap;
}
public static Map<String, Object> handleCodeTableReturnData(NoUiContext context, Alias alias) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment