6 Replies Latest reply: Mar 29, 2012 2:50 AM by Heiko Tappe RSS

Weld equivalence to ContextualHttpServletRequest

Tiago Rossi Newbie

Hi,


in seam we are able to start processing inside the servlet context
by creating a ContextualHttpServletRequest, so if i had a reference to the
HttpServletRequest outside of servlet context ( like a independent thread of application scope) we could
start the servlet context and inject components from session scope inside it.
How can i do something like that using weld ?


In seam i used:


new ContextualHttpServletRequest(req) {


  @Override
  public void process() throws Exception {
    ...


    Component.getInstance(cometControler,ScopeType.SESSION);


    ...
  }


  @Override
  protected void restoreConversationId(){
   ...
  }


  @Override
  protected void handleConversationPropagation(){
   ...
  }


}.run();


Thanks,
Tiago

  • 1. Re: Weld equivalence to ContextualHttpServletRequest
    Nicklas Karlsson Master

    Not tested but you could try something like


    public abstract class WeldRequest
    {
       protected HttpServletRequest request;
       protected ServletLifecycle servletLifecycle;
    
       public WeldRequest(HttpServletRequest request)
       {
          this.request = request;
          servletLifecycle = new ServletLifecycle(Container.instance().services().get(ContextLifecycle.class));
       }
    
       public abstract void process();
    
       public void run() throws ServletException
       {
          try
          {
             servletLifecycle.beginRequest(request);
             process();
          }
          catch (Exception e)
          {
             throw new ServletException("Error processing Weld request", e);
          }
          finally
          {
             servletLifecycle.endRequest(request);
          }
       }
    
    }
    



    You might need to fiddle with the setup/teardown process a bit but the code might get you started.

  • 2. Re: Weld equivalence to ContextualHttpServletRequest
    Tiago Rossi Newbie
    Thank you Nicklass, but i think i'll need to do something more, beacuse i'm getting an IllegalStateException on Container.getInstance.


    java.lang.IllegalStateException: Singleton not set for java.net.URLClassLoader@f7ba93
         at org.glassfish.weld.ACLSingletonProvider$ACLSingleton.get(ACLSingletonProvider.java:107)
         at org.jboss.weld.Container.instance(Container.java:98)
         at com.ati.sglweb.sglclient.server.comet.WeldContextualServletRequest.<init>(WeldContextualServletRequest.java:21)
         at com.ati.sglweb.sglclient.server.comet.GWTCometHandler$1.<init>(GWTCometHandler.java:80)
         at com.ati.sglweb.sglclient.server.comet.GWTCometHandler.onEvent(GWTCometHandler.java:80)
         at com.sun.grizzly.comet.DefaultNotificationHandler.notify0(DefaultNotificationHandler.java:189)
         at com.sun.grizzly.comet.DefaultNotificationHandler$1.run(DefaultNotificationHandler.java:133)
         at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
         at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
         at java.lang.Thread.run(Thread.java:619)
  • 3. Re: Weld equivalence to ContextualHttpServletRequest
    Nicklas Karlsson Master

    Hmm. It indicates that Weld hasn't booted or something. Are you sure  you have a beans.xml in your web app and that you see the Weld boot message at deployment? Some more info on your environment?

  • 4. Re: Weld equivalence to ContextualHttpServletRequest
    Tiago Rossi Newbie

    I'm using glassifish 3 with embeded weld.
    I have a servlet working in async mode with grizzly comet, so after the request is made, i enqueue it (plus HttpServletRequest and HttpServletResponse) in a CometContext and serlet is released(since i'm in async mode).
    A new thread created on Servlet init method is responsable for prepare the answers and notify the CometContext.


    In the CometHandler, that is started by the notification of the CometContext, i want to restore the SessionContext
    of the request and inject SessionScoped objets on it.
    Injection works fine inside the doPost method of the servlet, also i can get the Conteiner.instance inside it,
    but on the CometHandler it seams not to be instantiated.



    Ps.: From your code i have to replace the
    Container.instance().services() by Container.instance().deploymentServices().


    Thanks,
    Tiago.

  • 5. Re: Weld equivalence to ContextualHttpServletRequest
    Nicklas Karlsson Master

    The separate thread is probably the problem (the singleton is not set there).


    Perhaps you could register a custom context for the stuff and access the BeanManager from JNDI? It's a bit tricky to access the normal contexts in a portable way since you can't get non-active contexts from the BeanManager.


    There are probably better ways of doing this so if anyone has better suggestions, feel free...

  • 6. Re: Weld equivalence to ContextualHttpServletRequest
    Heiko Tappe Newbie

    I wanted to take this approach with weld 1.1.2 but I can't find the class ServletLifecycle.