1 package com.openinventions.webappfilter; 2 3 import org.apache.commons.logging.*; 4 import com.openinventions.metaframework.*; 5 import java.io.*; 6 import java.util.*; 7 import javax.servlet.*; 8 import javax.servlet.http.*; 9 10 public class InitServletFilter implements javax.servlet.Filter { 11 private static final Log log = LogFactory.getLog(ServletFilter.class); 12 13 public void init(FilterConfig filterConfig) throws ServletException { 14 } 15 16 public void destroy() { 17 } 18 19 public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException { 20 try { 21 ElementFactory factory = new JXPathElementFileFactory(); 22 Element req = factory.createElement("request"); 23 Element res = factory.createElement("response"); 24 populateRequest((HttpServletRequest) request, req); 25 request.setAttribute("com.openinventions.webappsecurity.Request", req); 26 request.setAttribute("com.openinventions.webappsecurity.Response", res); 27 request.setAttribute("com.openinventions.webappsecurity.Direction", "forward"); 28 chain.doFilter(request, response); 29 populateResponse((HttpServletResponse) response, res); 30 } catch (Exception e) { 31 log.error(e); 32 throw new ServletException(e); 33 } 34 } 35 36 public void populateRequest(HttpServletRequest request, Element req) throws Exception { 37 req.setValue("url", request.getRequestURL().toString()); 39 req.setValue("path", request.getServletPath()); 40 req.setValue("ip", request.getRemoteAddr()); 41 42 Enumeration headers = request.getHeaderNames(); 43 44 int i = 1; 45 while (headers.hasMoreElements()) { 46 String name = (String ) headers.nextElement(); 47 req.setValue("header[" + i + "]", ""); 48 req.setValue("header[" + i + "]/name", name); 49 String value = request.getHeader(name); 50 req.setValue("header[" + i + "]/value", value); 51 i++; 52 } 53 54 Enumeration params = request.getParameterNames(); 55 56 i = 1; 57 while (params.hasMoreElements()) { 58 String name = (String ) params.nextElement(); 59 req.setValue("param[" + i + "]", ""); 60 req.setValue("param[" + i + "]/name", name); 61 62 String [] values = request.getParameterValues(name); 63 64 for (int j = 0; j < values.length; j++) { 65 req.setValue("param[" + i + "]/value[" + (j + 1) + "]", values[j]); 66 } 67 68 i++; 69 } 70 } 71 72 public void populateResponse(HttpServletResponse response, Element res) throws Exception { 73 if (log.isDebugEnabled()) { 74 log.debug("res.getValue(body) = " + res.getValue("body")); 75 } 76 77 response.getWriter().write(res.getValue("body")); 78 } 79 } 80 131 132 133 | Popular Tags |