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

com.logicbus.backend.server.http.MessageRouterServletHandler Maven / Gradle / Ivy

There is a newer version: 1.6.16
Show newest version
package com.logicbus.backend.server.http;

import java.io.IOException;

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

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.anysoft.util.PropertiesConstants;
import com.anysoft.util.Settings;
import com.anysoft.webloader.ServletConfigProperties;
import com.anysoft.webloader.ServletHandler;
import com.logicbus.backend.AccessController;
import com.logicbus.backend.DefaultNormalizer;
import com.logicbus.backend.Normalizer;
import com.logicbus.backend.server.MessageRouter;
import com.logicbus.models.catalog.Path;

/**
 * 基于anyWebLoader的ServletHandler
 * 
 * @author duanyy
 * 
 * @version 1.0.5 [20140412 duanyy] 
* - 修改消息传递模型。
* * @version 1.0.7 [20140418 duanyy]
* - 增加全局序列号功能,从Http头的GlobalSerial变量获取前端传入的全局序列号 * * @version 1.2.1 [20140614 duanyy]
* - 支持跨域服务调用 * * @version 1.2.6 [20140807 duanyy]
* - ServantPool和ServantFactory插件化 * * @version 1.2.7.2 [20140910 duanyy]
* - Normalizer降级为Servlet级别对象 * * @version 1.3.0.1 [20141031 duanyy]
* - 解决问题:框架截获了post方式的body数据,导致post过来的form数据无法获取 * * @version 1.4.0 [20141117 duanyy]
* - 将MessageDoc和Context进行合并整合
* * @version 1.6.1.2 [20141118 duanyy]
* - 支持HttpContext的数据截取,通过Servlet的变量intercept.mode来控制
* * @version 1.6.3.28 [20150708 duanyy]
* - 允许设置为缓存模式
* * @version 1.6.4.8 [20151013 duanyy]
* - CORS成了可选配置
* * @version 1.6.5.6 [20160523 duanyy]
* - 在MessageRouter中提前写出报文
* * @version 1.6.7.9 [20170201 duanyy]
* - 采用SLF4j日志框架输出日志
* * @version 1.6.8.6 [20170410 duanyy]
* - 增加Options方法的实现
* * @version 1.6.9.8 [20170821 duanyy]
* - 优化代码
*/ public class MessageRouterServletHandler implements ServletHandler { /** * 访问控制器 */ protected AccessController ac = null; /** * 路径标准化 */ protected Normalizer normalizer = null; /** * 是否开启拦截模式 */ protected boolean interceptMode = false; /** * a logger of log4j */ protected static Logger logger = LoggerFactory.getLogger(MessageRouterServletHandler.class); /** * 是否已经获取服务器信息 */ protected static boolean getServerInfo = false; /** * 编码 */ protected static String encoding = "utf-8"; /** * Access-Control-Allow-Origin */ protected static String defaultAllowOrigin = "*"; protected String methodAllow = "GET,PUT,POST"; protected boolean cacheAllowed = false; protected boolean corsSupport = true; public void init(ServletConfig servletConfig) throws ServletException { ServletConfigProperties props = new ServletConfigProperties(servletConfig); encoding = PropertiesConstants.getString(props,"http.encoding", encoding); defaultAllowOrigin = PropertiesConstants.getString(props,"http.alloworigin",defaultAllowOrigin); corsSupport = PropertiesConstants.getBoolean(props, "http.cors", corsSupport); methodAllow = PropertiesConstants.getString(props, "http.method.allow", methodAllow); cacheAllowed = PropertiesConstants.getBoolean(props, "cacheAllowed", cacheAllowed); interceptMode = PropertiesConstants.getBoolean(props, "intercept.mode", interceptMode); String normalizerClass = PropertiesConstants.getString(props, "normalizer", "com.logicbus.backend.DefaultNormalizer"); logger.info("Normalizer is initializing,module:" + normalizerClass); try { Normalizer.TheFactory ncf = new Normalizer.TheFactory( Settings.getClassLoader()); normalizer = ncf.newInstance(normalizerClass,props); } catch (Throwable t) { normalizer = new DefaultNormalizer(props); logger.error("Failed to initialize Normalizer.Using default:" + DefaultNormalizer.class.getName()); } Settings settings = Settings.get(); ac = (AccessController) settings.get("accessController"); } public void doService(HttpServletRequest request, HttpServletResponse response, String method) throws ServletException, IOException { if (!getServerInfo){ Settings settings = Settings.get(); settings.SetValue("server.host", request.getLocalAddr()); settings.SetValue("server.port", String.valueOf(request.getLocalPort())); logger.info("Get server info:" + settings.GetValue("server.host", "") + ":" + settings.GetValue("server.port","")); getServerInfo = true; } if (cacheAllowed){ response.setHeader("Cache-Control", "public"); }else{ response.setHeader("Expires", "Mon, 26 Jul 1970 05:00:00 GMT"); response.setHeader("Last-Modified", "Mon, 26 Jul 1970 05:00:00 GMT"); response.setHeader("Cache-Control", "no-cache, must-revalidate"); response.setHeader("Pragma", "no-cache"); } // 1.2.1 duanyy // to support CORS if (corsSupport){ String origin = request.getHeader("Origin"); response.setHeader("Access-Control-Allow-Origin", StringUtils.isEmpty(origin) ? defaultAllowOrigin : origin); response.setHeader("Access-Control-Allow-Credentials", "true"); } if (method.equals("options")){ response.setHeader("Allow", methodAllow); }else{ HttpContext ctx = new HttpContext(request,response,encoding,interceptMode); Path id = normalizer.normalize(ctx, request); MessageRouter.action(id,ctx,ac); } } public void destroy() { } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy