1 package org.apache.beehive.controls.runtime.servlet; 2 19 20 import javax.servlet.Filter ; 21 import javax.servlet.FilterChain ; 22 import javax.servlet.FilterConfig ; 23 import javax.servlet.ServletException ; 24 import javax.servlet.ServletRequest ; 25 import javax.servlet.ServletResponse ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 import javax.servlet.http.HttpSession ; 29 30 39 public class ControlFilter implements Filter 40 { 41 public static String BEAN_CONTEXT_ATTRIBUTE = ServletBeanContext.class.getName(); 42 43 46 47 52 public static String INIT_PARAM_USE_SESSION = "useSession"; 53 54 59 public static String INIT_PARAM_CONTEXT_CLASS = "contextClass"; 60 61 public void init(FilterConfig filterConfig) throws ServletException 62 { 63 _filterConfig = filterConfig; 64 _useSessionContext = new Boolean (filterConfig.getInitParameter(INIT_PARAM_USE_SESSION)).booleanValue(); 65 String contextClassName = filterConfig.getInitParameter(INIT_PARAM_CONTEXT_CLASS); 66 if(contextClassName != null) 67 { 68 try 69 { 70 _contextClass = Class.forName(contextClassName); 71 } 72 catch(Exception e) 73 { 74 throw new ServletException ("Cannot load container context class '"+contextClassName+"'"); 75 } 76 if(!ServletBeanContext.class.isAssignableFrom(_contextClass)) 77 { 78 throw new ServletException ("'"+contextClassName+"' is not a ServletBeanContext sub-class"); 79 } 80 81 } 82 } 83 84 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 85 throws java.io.IOException , ServletException 86 { 87 if (request instanceof HttpServletRequest ) 88 { 89 ServletBeanContext beanContext = null; 90 HttpSession httpSession = null; 91 92 if (_useSessionContext) 97 { 98 httpSession = ((HttpServletRequest )request).getSession(true); 99 beanContext = (ServletBeanContext)httpSession.getAttribute(BEAN_CONTEXT_ATTRIBUTE); 100 } 101 102 if (beanContext == null) 108 { 109 try 110 { 111 beanContext = (ServletBeanContext)_contextClass.newInstance(); 112 } 113 catch(Exception e) 114 { 115 throw new ServletException ("Cannot construct BeanContext class instance: " + 116 _contextClass.getName()); 117 } 118 if (httpSession != null) 119 httpSession.setAttribute(BEAN_CONTEXT_ATTRIBUTE, beanContext); 120 } 121 122 beanContext.beginContext(_filterConfig.getServletContext(), request, response); 126 try 127 { 128 chain.doFilter(request, response); 132 } 133 finally 134 { 135 beanContext.endContext(); 139 } 140 } 141 else 142 { 143 chain.doFilter(request, response); 148 } 149 } 150 151 public void destroy() {} 152 153 156 FilterConfig _filterConfig; 157 158 161 private boolean _useSessionContext = false; 162 163 166 private Class _contextClass = ServletBeanContext.class; 167 } 168 | Popular Tags |