1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class DispatcherFilter implements Filter { ... public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpReq = (HttpServletRequest) req; HttpServletResponse httpResp = (HttpServletResponse) resp; String method = httpReq.getMethod(); if ("GET".equals(method) || "POST".equals(method)) { if (!dispatcher.service(httpReq, httpResp)) chain.doFilter(req, resp); return; } httpResp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); } } |
1 2 3 4 5 6 7 8 | <filter> <filter-name>dispatcher</servlet-name> <filter-class>org.expressme.webwind.DispatcherFilter</servlet-class> </filter> <filter-mapping> <filter-name>dispatcher</servlet-name> <url-pattern>/*</url-pattern> </filter-mapping> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | public final class ActionContext { private static final ThreadLocal<ActionContext> actionContextThreadLocal = new ThreadLocal<ActionContext>(); private ServletContext context; private HttpServletRequest request; private HttpServletResponse response; public ServletContext getServletContext() { return context; } public HttpServletRequest getHttpServletRequest() { return request; } public HttpServletResponse getHttpServletResponse() { return response; } public HttpSession getHttpSession() { return request.getSession(); } public static ActionContext getActionContext() { return actionContextThreadLocal.get(); } static void setActionContext(ServletContext context, HttpServletRequest request, HttpServletResponse response) { ActionContext ctx = new ActionContext(); ctx.context = context; ctx.request = request; ctx.response = response; actionContextThreadLocal.set(ctx); } static void removeActionContext() { actionContextThreadLocal.remove(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Dispatcher { ... void handleExecution(Execution execution, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ActionContext.setActionContext(servletContext, request, response); try { InterceptorChainImpl chains = new InterceptorChainImpl(interceptors); chains.doInterceptor(execution); handleResult(request, response, chains.getResult()); } catch (Exception e) { handleException(request, response, e); } finally { ActionContext.removeActionContext(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class MultipartHttpServletRequest extends HttpServletRequestWrapper { final HttpServletRequest target; final Map<String, List<FileItemStream>> fileItems; final Map<String, List<String>> formItems; public MultipartHttpServletRequest(HttpServletRequest request, long maxFileSize) throws IOException { super(request); this.target = request; this.fileItems = new HashMap<String, List<FileItemStream>>(); this.formItems = new HashMap<String, List<String>>(); ServletFileUpload upload = new ServletFileUpload(); upload.setFileSizeMax(maxFileSize); try { |
1 2 3 4 5 6 7 8 9 10 11 12 13 | } catch (FileUploadException e) { throw new IOException(e); } } public InputStream getFileInputStream(String fieldName) throws IOException { List<FileItemStream> list = fileItems.get(fieldName); if (list==null) throw new IOException("No file item with name '" + fieldName + "'."); return list.get(0).openStream(); }; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | public class MultipartHttpServletRequest extends HttpServletRequestWrapper { ... @Override public String getParameter(String name) { List<String> list = formItems.get(name); if (list==null) return null; return list.get(0); } @Override @SuppressWarnings("unchecked") public Map getParameterMap() { Map<String, String[]> map = new HashMap<String, String[]>(); Set<String> keys = formItems.keySet(); for (String key : keys) { List<String> list = formItems.get(key); map.put(key, list.toArray(new String[list.size()])); } return Collections.unmodifiableMap(map); } @Override @SuppressWarnings("unchecked") public Enumeration getParameterNames() { return Collections.enumeration(formItems.keySet()); } @Override public String[] getParameterValues(String name) { List<String> list = formItems.get(name); if (list==null) return null; return list.toArray(new String[list.size()]); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | class Dispatcher { private boolean multipartSupport = false; ... void initAll(Config config) throws Exception { try { Class.forName("org.apache.commons.fileupload.servlet.ServletFileUpload"); this.multipartSupport = true; } catch (ClassNotFoundException e) { log.info("CommonsFileUpload not found."); } ... } void handleExecution(Execution execution, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (this.multipartSupport) { if (MultipartHttpServletRequest.isMultipartRequest(request)) { request = new MultipartHttpServletRequest(request, maxFileSize); } } ... } ... } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |