2018-06-03 22:26:41 +08:00

94 lines
3.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cn.coyoteam.aweresf.web.servlet;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.coyoteam.aweresf.consts.XWConst;
import cn.coyoteam.aweresf.util.StringHelper;
import cn.coyoteam.aweresf.web.xw.render.XWRenderAsJson;
import cn.coyoteam.aweresf.web.xw.render.XWRenderIntercept;
import com.amarsoft.awe.dw.ASObjectModel;
import com.amarsoft.awe.dw.ASObjectWindow;
/***
* DW或OW重新定向渲染处理Servlet
* @author EX-YANGSONG001
* @date 2015/5/14
*/
public class XWRenderServlet extends HttpServlet {
private static final long serialVersionUID = 1836559134168560992L;
private Properties xwInterceptProperties = new Properties();
public XWRenderServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ASObjectModel doTemp = (ASObjectModel)request.getAttribute("doTemp");
ASObjectWindow dwTemp = (ASObjectWindow)request.getAttribute("dwTemp");
@SuppressWarnings("unchecked")
XWRenderAsJson<ASObjectModel,ASObjectWindow> owRender = (XWRenderAsJson<ASObjectModel,ASObjectWindow>)request.getAttribute("owRender");
//对象不存在,则抛出错误
if(doTemp == null || dwTemp == null) {
throw new ServletException("owRender not found!");
}
//如果相应的URI有拦截器存在则设置拦截器以备在数据渲染时调用
String xwUri = (String)request.getAttribute(XWConst.XWURI);
XWRenderIntercept xwRenderIntercept = lookupXWRenderIntercept(xwUri);
if(xwRenderIntercept!=null)owRender.setXwRenderIntercept(xwRenderIntercept);
//执行渲染
owRender.initXw(doTemp, dwTemp);
owRender.render(request, response);
}
protected XWRenderIntercept lookupXWRenderIntercept(String xwUri){
String className = xwInterceptProperties.getProperty(xwUri);
if(className==null)return null;
try {
Object object = Class.forName(className).newInstance();
if(object instanceof XWRenderIntercept){
XWRenderIntercept renderIntercept = (XWRenderIntercept)object;
return renderIntercept;
}
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
return null;
}
public void init(ServletConfig filterConfig) throws ServletException {
super.init(filterConfig);
String interceptConfig = StringHelper.nvl(filterConfig.getInitParameter(XWConst.KEY_XW_INTERCEPT_CONFIG),"");
interceptConfig = interceptConfig.replaceAll("\\s+", "");
if(interceptConfig!=null&&interceptConfig.startsWith("classpath:")){
interceptConfig = interceptConfig.substring(10);
InputStream in = getClass().getResourceAsStream(interceptConfig);
try {
xwInterceptProperties.load(in);
} catch (IOException e) {
throw new ServletException(String.format("load %s file error", XWConst.KEY_XW_INTERCEPT_CONFIG));
}
}
}
}