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 ServletFilter extends DefaultFilter implements javax.servlet.Filter { 11 private static final Log log = LogFactory.getLog(ServletFilter.class); 12 private static String rootDir = ""; 13 14 public void init(FilterConfig filterConfig) throws ServletException { 15 try { 16 String file = filterConfig.getInitParameter("file"); 17 this.rootDir = filterConfig.getServletContext().getRealPath("/"); 18 if (this.rootDir.endsWith("/") || this.rootDir.endsWith("\\")) { 19 this.rootDir = this.rootDir.substring(0, this.rootDir.length() - 1); 20 } 21 if (log.isDebugEnabled()) { 22 log.debug("file = " + file); 23 } 24 ElementFactory factory = new JXPathElementFileFactory(); 25 Element context = factory.createElement("context"); 26 context.setValue("file", file); 27 context.setValue("rootDir", rootDir); 28 super.init(null, context); 29 } catch (Exception e) { 30 log.error(e); 31 throw new ServletException(e); 32 } 33 } 34 35 public void destroy() { 36 try { 37 ElementFactory factory = new JXPathElementFileFactory(); 38 Element context = factory.createElement("context"); 39 context.setValue("rootDir", rootDir); 40 super.destroy(null, context); 41 } catch (Exception e) { 42 log.error(e); 43 } 44 } 45 46 public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException { 47 try { 48 State state = new DefaultState(); 49 initState(state); 50 Element req = (Element) request.getAttribute("com.openinventions.webappsecurity.Request"); 51 Element res = (Element) request.getAttribute("com.openinventions.webappsecurity.Response"); 52 String direction = (String ) request.getAttribute("com.openinventions.webappsecurity.Direction"); 53 if (log.isDebugEnabled()) { 54 log.debug("req = " + req); 55 log.debug("res = " + res); 56 log.debug("direction = " + direction); 57 } 58 state.set("nextCommand", ""); 59 state.set("direction", direction); 60 state.set("req", req); 61 state.set("res", res); 62 state.set("rootDir", this.rootDir); 63 super.process(state, null); 64 request.setAttribute("com.openinventions.webappsecurity.Request", state.get("req")); 65 request.setAttribute("com.openinventions.webappsecurity.Response", state.get("res")); 66 request.setAttribute("com.openinventions.webappsecurity.Direction", state.get("direction")); 67 68 direction = (String ) state.get("direction"); 69 70 if (log.isDebugEnabled()) { 71 log.debug("filterDirection = " + (String ) state.get("filterDirection")); 72 log.debug("direction = " + (String ) state.get("direction")); 73 log.debug("nextCommand = " + (String ) state.get("nextCommand")); 74 } 75 76 if (direction.equals("forward")) { 77 chain.doFilter(request, response); 78 } 79 80 direction = (String ) request.getAttribute("com.openinventions.webappsecurity.Direction"); 81 state.set("direction", direction); 82 83 if (direction.equals("backward")) { 84 super.process(state, null); 85 request.setAttribute("com.openinventions.webappsecurity.Request", state.get("req")); 86 request.setAttribute("com.openinventions.webappsecurity.Response", state.get("res")); 87 request.setAttribute("com.openinventions.webappsecurity.Direction", direction); 88 } 89 } catch (Exception e) { 90 log.error(e); 91 throw new ServletException(e); 92 } 93 } 94 } 95 146 147 148 | Popular Tags |