6 Replies Latest reply on Apr 12, 2007 1:01 PM by Frederic Dubois

    Servlet Emulation and Request content problem

    Frederic Dubois Newbie

      Hi,

      I'm trying to emulate a servlet behavior with JSF and Seam.
      The goal of this, is to expose an URL in my application to which I could post data using a simple HTML Form or an ESB.

      I added the following in the pages.xml file :

      <page view-id="/connect.xhtml" action="#{connector.doRequest}"/>


      The HTML form I use to test this is as simple as :

      <html>
       <body>
       <form name="myForm" action="http://localhost:8080/myApp/connect.faces" method="POST' enctype="multipart/form-data">
       <input type="file" name="myFile"/>
       <input type="submit"/>
       </form>
       </body>
      </html>



      connector is a Stateless bean that exposes a doRequest method :
      In this method, I'm trying to analyze the content of the http request that has been posted

      public void doRequest(){
       FacesContext context = FacesContext.getCurrentInstance();
       HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();
       System.out.println("contentlength = "+request.getContentLength())
       byte[] data = new byte[2048];
       int count;
       InputStream is = request.getInputStream();
       count = is.read(data,0,2048);
       while(count != -1){
       System.out.write(data,0,count);
       count = is.read(data,0,2048);
       }
      }


      The 'request.getContentLength()' returns a value that seems to be correct (non-zero and approximately the size of the uploaded file)
      but when I try to read the request content, the first 'is.read(data,0,2048)' call returns '-1' as if the stream was empty :(

      I tried to add a SeamMultipartFilter but it didn't change anything

      Do you think the way I proceed is able to work ?
      Is there any configuration I could add somewhere to make things work ?

      Thanks

      Fred





        • 1. Re: Servlet Emulation and Request content problem
          Dustin Norlander Apprentice

          Its much easier if you just write a normal servlet and add the web context filter to your url pattern (so you can access seam components). Its in the documentation, but is essentially adding:

          <web:context-filter url-pattern="/media/*" />


          to components.xml

          hope that helps,
          Dustin

          • 2. Re: Servlet Emulation and Request content problem
            Frederic Dubois Newbie

            It seems like it's exactly what I need

            But how do I access Seam components ?

            With an @In annotation ?

            I've tried but the component I try to get is null :(
            I don't know if my configuration is correct...

            my web.xml contains :

            <servlet>
             <servlet-name>MyExporter</servlet-name>
             <servlet-class>org.fred.connector.MyExporter</servlet-class>
            </servlet>
            
            <servlet-mapping>
             <servlet-name>MyExporter</servlet-name>
             <url-pattern>/servlet/export</url-pattern>
            </servlet-mapping>


            and my components.xml contains :

            <web:context-filter url-pattern="*/servlet/*" />


            And finally, I access the servlet with this URL :

            http://localhost:8080/myApp/servlet/export


            I enter the doGet method but when I access a Seam component get with the @In annotation, it is null

            The servlet class must have any annotations ?

            Another question : It also seems that the Request must contain a parameter named conversationId. Can the value of this parameter be generated randomly ?

            Thanks

            Fred


            • 3. Re: Servlet Emulation and Request content problem
              Christian Bauer Master

              Components.getInstance("name")

              • 4. Re: Servlet Emulation and Request content problem
                Frederic Dubois Newbie

                Unfortunately, an exception is thrown when I invoke

                Component.getInstance("name") or Component.forName("name")


                ==> No application context active

                I think my <web:context-filter> instruction is wrong.

                Is there a way to see if this filter is processed ?

                Here's the stacktrace

                2007-04-10 19:02:56,354 INFO [com.nuxeo.project.eurocis.bri.connector.ExporterServlet] GET
                2007-04-10 19:02:56,354 DEBUG [org.jboss.seam.util.Naming] JNDI InitialContext properties:{java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory, java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces}
                2007-04-10 19:02:56,354 DEBUG [org.jboss.seam.contexts.Lifecycle] After request, destroying contexts
                2007-04-10 19:02:56,354 DEBUG [org.jboss.seam.Component] instantiating Seam component: org.jboss.seam.core.manager
                2007-04-10 19:02:56,354 DEBUG [org.jboss.seam.Component] initializing new instance of: org.jboss.seam.core.manager
                2007-04-10 19:02:56,354 DEBUG [org.jboss.seam.Component] done initializing: org.jboss.seam.core.manager
                2007-04-10 19:02:56,354 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.preSetVariable.org.jboss.seam.core.manager
                2007-04-10 19:02:56,354 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.postSetVariable.org.jboss.seam.core.manager
                2007-04-10 19:02:56,354 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.postCreate.org.jboss.seam.core.manager
                2007-04-10 19:02:56,354 DEBUG [org.jboss.seam.contexts.Lifecycle] flushing session context
                2007-04-10 19:02:56,354 DEBUG [org.jboss.seam.contexts.Lifecycle] destroying event context
                2007-04-10 19:02:56,354 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.preDestroyContext.EVENT
                2007-04-10 19:02:56,354 DEBUG [org.jboss.seam.contexts.Contexts] destroying: org.jboss.seam.core.manager
                2007-04-10 19:02:56,354 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.preDestroy.org.jboss.seam.core.manager
                2007-04-10 19:02:56,354 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.postDestroyContext.EVENT
                2007-04-10 19:02:56,354 DEBUG [org.jboss.seam.contexts.Lifecycle] <<< End web request
                2007-04-10 19:02:56,354 ERROR [org.jboss.seam.servlet.SeamExceptionFilter] uncaught exception handled by Seam
                java.lang.IllegalStateException: No application context active
                 at org.jboss.seam.Component.forName(Component.java:1586)
                 at com.nuxeo.project.eurocis.bri.connector.ExporterServlet.doGet(ExporterServlet.java:35)
                 at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
                 at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
                 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
                 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                 at org.nuxeo.ecm.webapp.shield.SessionExpirationFilter.doFilter(SessionExpirationFilter.java:63)
                 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                 at org.jboss.seam.servlet.SeamExceptionFilter.doFilter(SeamExceptionFilter.java:46)
                 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                 at org.nuxeo.ecm.platform.ui.web.rest.FancyURLFilter.doFilter(FancyURLFilter.java:75)
                 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                 at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
                 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
                 at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
                 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432)
                 at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
                 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
                 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
                 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
                 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
                 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
                 at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
                 at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
                 at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
                 at java.lang.Thread.run(Thread.java:595)
                2007-04-10 19:02:56,354 DEBUG [org.jboss.seam.servlet.SeamExceptionFilter] ended request
                2007-04-10 19:02:56,354 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/nuxeo].[BRIExporter]] Servlet.service() for servlet BRIExporter threw exception
                java.lang.IllegalStateException: No application context active
                 at org.jboss.seam.Component.forName(Component.java:1586)
                 at com.nuxeo.project.eurocis.bri.connector.ExporterServlet.doGet(ExporterServlet.java:35)
                 at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
                 at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
                 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
                 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                 at org.nuxeo.ecm.webapp.shield.SessionExpirationFilter.doFilter(SessionExpirationFilter.java:63)
                 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                 at org.jboss.seam.servlet.SeamExceptionFilter.doFilter(SeamExceptionFilter.java:46)
                 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                 at org.nuxeo.ecm.platform.ui.web.rest.FancyURLFilter.doFilter(FancyURLFilter.java:75)
                 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                 at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
                 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
                 at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
                 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432)
                 at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
                 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
                 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
                 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
                 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
                 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
                 at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
                 at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
                 at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
                 at java.lang.Thread.run(Thread.java:595)
                


                I think I'm getting close to have this code working....

                Thanks

                Fred

                • 5. Re: Servlet Emulation and Request content problem
                  Frederic Dubois Newbie

                  I have some good news and some bad news :

                  The good news : I managed to make things work !
                  The bad news : It's not working all the time :-(

                  With the same JBoss running, I tried to call my servlet several times. All I want to see in the logs is :

                  [com.nuxeo.project.eurocis.bri.connector.ExporterServlet] Principal : Administrator


                  first attempt : my servlet call works fine ! (Seam component successfully get and the method call on the resulting object named "documentManager" works)
                  second attempt (to see if I wasn't dreaming) : Same error as exposed in the previous post
                  third attempt : Error again
                  fourth attempt : error again
                  fifth attempt : servlet working !

                  I noticed in the traces that sometimes, the context is flushed before my code gets processed :-(

                  the working trace :
                  2007-04-10 19:36:53,530 INFO [com.nuxeo.project.eurocis.bri.connector.ExporterServlet] GET
                  2007-04-10 19:36:53,530 DEBUG [org.jboss.seam.contexts.Contexts] found in session context: documentManager
                  2007-04-10 19:36:53,530 DEBUG [org.jboss.seam.intercept.RootInterceptor] intercepted: documentManager.getDocumentManager
                  2007-04-10 19:36:53,530 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.preSetVariable.org.jboss.seam.outermostExceptionInterceptor
                  2007-04-10 19:36:53,530 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.postSetVariable.org.jboss.seam.outermostExceptionInterceptor
                  2007-04-10 19:36:53,530 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.preSetVariable.documentManager
                  2007-04-10 19:36:53,530 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.postSetVariable.documentManager
                  2007-04-10 19:36:53,530 DEBUG [org.jboss.seam.Component] instantiating Seam component: org.jboss.seam.core.manager
                  2007-04-10 19:36:53,530 DEBUG [org.jboss.seam.Component] initializing new instance of: org.jboss.seam.core.manager
                  2007-04-10 19:36:53,530 DEBUG [org.jboss.seam.Component] done initializing: org.jboss.seam.core.manager
                  2007-04-10 19:36:53,530 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.preSetVariable.org.jboss.seam.core.manager
                  2007-04-10 19:36:53,530 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.postSetVariable.org.jboss.seam.core.manager
                  2007-04-10 19:36:53,530 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.postCreate.org.jboss.seam.core.manager
                  2007-04-10 19:36:53,530 DEBUG [org.jboss.seam.interceptors.BijectionInterceptor] injecting dependencies of: documentManager
                  2007-04-10 19:36:53,530 DEBUG [org.jboss.seam.Component] trying to inject with hierarchical context search: currentServerLocation
                  2007-04-10 19:36:53,530 DEBUG [org.jboss.seam.contexts.Contexts] found in session context: currentServerLocation
                  2007-04-10 19:36:53,530 DEBUG [org.jboss.seam.interceptors.BijectionInterceptor] disinjecting dependencies of: documentManager
                  2007-04-10 19:36:53,530 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.preRemoveVariable.org.jboss.seam.outermostExceptionInterceptor
                  2007-04-10 19:36:53,530 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.postRemoveVariable.org.jboss.seam.outermostExceptionInterceptor
                  2007-04-10 19:36:53,530 DEBUG [org.jboss.ejb3.entity.ExtendedPersistenceContextPropagationInterceptor] ++++ LongLivedSessionPropagationInterceptor
                  2007-04-10 19:36:53,546 INFO [com.nuxeo.project.eurocis.bri.connector.ExporterServlet] Principal : Administrator
                  2007-04-10 19:36:53,546 DEBUG [org.jboss.seam.util.Naming] JNDI InitialContext properties:{java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory, java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces}
                  2007-04-10 19:36:53,546 DEBUG [org.jboss.seam.servlet.SeamExceptionFilter] ended request


                  the error trace :
                  2007-04-10 19:36:43,139 INFO [com.nuxeo.project.eurocis.bri.connector.ExporterServlet] GET
                  2007-04-10 19:36:43,139 DEBUG [org.jboss.seam.util.Naming] JNDI InitialContext properties:{java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory, java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces}
                  2007-04-10 19:36:43,139 DEBUG [org.jboss.seam.contexts.Lifecycle] After request, destroying contexts
                  2007-04-10 19:36:43,139 DEBUG [org.jboss.seam.Component] instantiating Seam component: org.jboss.seam.core.manager
                  2007-04-10 19:36:43,139 DEBUG [org.jboss.seam.Component] initializing new instance of: org.jboss.seam.core.manager
                  2007-04-10 19:36:43,139 DEBUG [org.jboss.seam.Component] done initializing: org.jboss.seam.core.manager
                  2007-04-10 19:36:43,139 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.preSetVariable.org.jboss.seam.core.manager
                  2007-04-10 19:36:43,139 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.postSetVariable.org.jboss.seam.core.manager
                  2007-04-10 19:36:43,139 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.postCreate.org.jboss.seam.core.manager
                  2007-04-10 19:36:43,139 DEBUG [org.jboss.seam.contexts.Lifecycle] flushing session context
                  2007-04-10 19:36:43,139 DEBUG [org.jboss.seam.contexts.Lifecycle] destroying event context
                  2007-04-10 19:36:43,139 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.preDestroyContext.EVENT
                  2007-04-10 19:36:43,139 DEBUG [org.jboss.seam.contexts.Contexts] destroying: org.jboss.seam.core.manager
                  2007-04-10 19:36:43,139 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.preDestroy.org.jboss.seam.core.manager
                  2007-04-10 19:36:43,139 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.postDestroyContext.EVENT
                  2007-04-10 19:36:43,139 DEBUG [org.jboss.seam.contexts.Lifecycle] <<< End web request
                  2007-04-10 19:36:43,139 ERROR [org.jboss.seam.servlet.SeamExceptionFilter] uncaught exception handled by Seam
                  java.lang.IllegalStateException: No application context active


                  It would be great if I could disable this Context flushing or maybe it's something else ...

                  Any idea ?

                  Thanks

                  Fred


                  • 6. Re: Servlet Emulation and Request content problem
                    Frederic Dubois Newbie

                    Hi,

                    I discovered another problem which seems to be linked :

                    When I post some data to my servlet, I try to read the input stream from the request and the Stream is empty.
                    But sometimes, it works : the Stream contains the posted data.
                    In all cases, the request.getContentLength returns the size of the posted data, but sometimes no bytes can be read from the stream.

                    The success rate is about 5%

                    I get the same behaviour even when I disable the Seam context filter set in components.xml

                    Important note : I use Seam v1.1.5GA

                    May be this unstability is a known bug that has been corrected since ?

                    Thanks for your help

                    Fred