Outjecting session-scope object from servlet filter
roger_goldman2000 Sep 12, 2008 9:24 PMHi everyone.
Over the past several days, I've attempted to cause an APPLICATION-scoped Seam-enabled servlet filter to outject a session scope object. After many attempts and hours of searching the web and reading seam-related posts and documentation, I seem to have exhausted my options (and possibly patience).
I'm hoping someone with expertise in this particular area can assist me in getting over this hurdle. Please note that we have been successfully using seam for the past 4 months developing this application and I suspect that my problem stems from a lack of understanding of how Seam's framework was designed to deal with Servlet Filters.
Some facts about my environment
- Tomcat 6.0.14
- Seam 2.0.3.CR1 (although we had the same problems with 2.0.2.SP1 and 2.1.0.BETA1)
- Persistence settings
components.xml
<transaction:entity-transaction entity-manager="#{entityManager}" /> <persistence:entity-manager-factory name="MyDatabase" /> <persistence:managed-persistence-context name="entityManager" auto-create="true" scope="conversation" entity-manager-factory="#{MyDatabase}" />
persistence.xml
<persistence-unit name="MyDatabase" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:comp/env/jdbc/TestDB</jta-data-source> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/> </properties> </persistence-unit>
The Seam-enabled Servlet Filter
@Startup @Scope(ScopeType.APPLICATION) @Name("userAuthenticationFilter") @Filter(within="org.jboss.seam.servlet.SeamFilter") public class UserAuthenticationFilter implements javax.servlet.Filter { @Out(scope=ScopeType.SESSION) private User user; @SuppressWarnings("unchecked") public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { user = detectUser(); chain.doFilter(request, response ); } public void destroy() { } private User detectUser() { ... } }
The Problem
The problem I'm encountering is the following exception when the init() method is called:
INFO: Initializing filter: userAuthenticationFilter Sep 12, 2008 3:20:44 PM org.apache.catalina.core.StandardContext filterStart SEVERE: Exception starting filter Seam Filter java.lang.IllegalStateException: No session context active at org.jboss.seam.ScopeType.getContext(ScopeType.java:132) at org.jboss.seam.Component.outjectAttribute(Component.java:1667) at org.jboss.seam.Component.outjectAttributes(Component.java:1620) at org.jboss.seam.Component.outject(Component.java:1473) at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:47) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:42) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107) at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:166) at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:102) at test.UserAuthenticationFilter_$$_javassist_0.init(UserAuthenticationFilter_$$_javassist_0.java) at org.jboss.seam.servlet.SeamFilter.init(SeamFilter.java:97) at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275) at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397) at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108) at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3696) at org.apache.catalina.core.StandardContext.start(StandardContext.java:4343) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) at org.apache.catalina.core.StandardHost.start(StandardHost.java:719) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443) at org.apache.catalina.core.StandardService.start(StandardService.java:516) at org.apache.catalina.core.StandardServer.start(StandardServer.java:710) at org.apache.catalina.startup.Catalina.start(Catalina.java:566) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413) Sep 12, 2008 3:20:44 PM org.apache.catalina.core.StandardContext start SEVERE: Error filterStart
Call for Help
I can understand the error message: the init() method runs while there is only an APPLICATION context active. OK, but I'm not trying to outject the User object in the init() method. I'm trying to do the outjection in the doFilter() method, which runs while servicing a REQUEST -- therefore both REQUEST and SESSION contexts should be active, no?
If Seam doesn't support this kind of situation and I need to take an entirely different approach that would be OK with me. Would you please help me understand the right way of doing something like this is with the Seam framework.
Many thanks in advance.