package org.theonefx.wcframework.mvc.wcweb; import java.io.UnsupportedEncodingException; import java.util.Enumeration; import java.util.Map; import java.util.Stack; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.theonefx.wcframework.mvc.MVCConfig; import org.theonefx.wcframework.mvc.WebApplicationContext; import org.theonefx.wcframework.mvc.exception.ActionInvocationException; import org.theonefx.wcframework.utils.StringUtils; /** * @File : ActionContext.java * @ClassName : ActionContext * @Author : 陈曦 * @Date : 2012-6-8 下午02:11:49 * @Version : v1.0 * @Description : Action执行的上下文 */ public final class ActionInvocationContext extends ArgsHolder { private static final ThreadLocal<ActionInvocationContext> CONTEXT_LOCAL = new ThreadLocal<ActionInvocationContext>(); private final RequestPather pather; private final WebApplicationContext atx; private final HttpServletRequest req; private final HttpServletResponse res; private final ServletContext sctx; private final RequestDispatcher dispatcher; private boolean inited = false; private int deep = 0; private Stack<ActionProxy> stack = null; private boolean isMultipart = false; private MultipartDateWrapper multipartDateWrapper = null; private Map<String, String> pathParams = null; public static final ActionInvocationContext getContext() { return CONTEXT_LOCAL.get(); } public ActionInvocationContext(WebApplicationContext atx, RequestPather pather, HttpServletRequest req, HttpServletResponse res, ServletContext sctx, RequestDispatcher dispatcher) { this.pather = pather; this.atx = atx; this.req = req; this.res = res; this.sctx = sctx; this.dispatcher = dispatcher; CONTEXT_LOCAL.set(this); } public void init(PathMatcher pathMatcher, String encoding) { if (inited) { return; } inited = true; if (pathMatcher instanceof SmartPathMatcher) { pathParams = ((SmartPathMatcher)pathMatcher).getParamsMap(pather.getPath()); } // 设置请求编码 try { if (encoding == null) { req.setCharacterEncoding(MVCConfig.defaultEncoding); } else { req.setCharacterEncoding(encoding); } } catch (UnsupportedEncodingException e) { throw new ActionInvocationException("设置Http请求编码出现异常", e); } String contextType = req.getContentType(); if (req.getMethod().equalsIgnoreCase("post") && contextType != null && contextType.indexOf("multipart/form-data") > -1) { isMultipart = true; multipartDateWrapper = new MultipartDateWrapper(req, encoding); } } public int getDeep() { return deep; } public void increase() { deep++; } public RequestPather getPather() { return pather; } public WebApplicationContext getWebApplicationContext() { return atx; } public RequestDispatcher getDispatcher() { return dispatcher; } public HttpSession getSession() { return req.getSession(); } public HttpServletRequest getRequest() { return req; } public HttpServletResponse getResponse() { return res; } public ServletContext getServletContext() { return sctx; } @SuppressWarnings("unchecked") public Enumeration<String> getStringParameterNames() { if (!isMultipart) { return req.getParameterNames(); } else { return multipartDateWrapper.getStringParameterNames(); } } public String getStringParameter(String name) { if (!isMultipart) { return req.getParameter(name); } else { return multipartDateWrapper.getStringParameter(name); } } public String[] getStringParameterValues(String name) { if (!isMultipart) { return req.getParameterValues(name); } else { return multipartDateWrapper.getParameterValues(name); } } @SuppressWarnings("unchecked") public Map<String, String[]> getStringParameterMap() { if (!isMultipart) { return req.getParameterMap(); } else { return multipartDateWrapper.getParameterMap(); } } public UploadFile getFileParameter(String name){ if (!isMultipart) { return null; } else { return multipartDateWrapper.getFileParameter(name); } } public UploadFile[] getFileParameters(String name){ if (!isMultipart) { return null; } else { return multipartDateWrapper.getFileParameterValues(name); } } public Map<String, UploadFile[]> getFileParameterMap() { if (!isMultipart) { return null; } else { return multipartDateWrapper.getFileParameterMap(); } } public Enumeration<String> getFileParameterNames() { if (!isMultipart) { return null; } else { return multipartDateWrapper.getFileParameterNames(); } } public Stack<ActionProxy> getStack() { if(stack == null){ stack = new Stack<ActionProxy>(); } return stack; } public String base(String url) { if (StringUtils.isNotBlank(url) && url.startsWith("/")) { url = url.substring(1, url.length()); } String basePath = MVCConfig.base + "/"; String result = basePath + url; return result; } public String getPathParam(String name) { if (pathParams != null) { return pathParams.get(name); } else { return null; } } }