1 Reply Latest reply on Nov 22, 2009 5:23 PM by Nicklas Karlsson

    Servlets and Conversation Scope

    Nicklas Karlsson Master

      Is there any special reason why the conversation scope is not active during servlet requests? Considering the conversation is propagated with the cid request parameter and I could reconstruct the conversation scope myself in a servlet (well, didn't try but shouldn't be a problem)...

        • 1. Re: Servlets and Conversation Scope
          Nicklas Karlsson Master

          Here is a small utility class that you can use as an anonymous class and wrap the request with if you want conversation scope support in servlets...



          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServletRequest;
          
          import org.jboss.weld.Container;
          import org.jboss.weld.context.ContextLifecycle;
          import org.jboss.weld.context.ConversationContext;
          import org.jboss.weld.servlet.ConversationBeanStore;
          
          public abstract class ConversationalHttpRequest {
               protected HttpServletRequest request;
               
               public ConversationalHttpRequest(HttpServletRequest request) {
                    this.request = request;
               }
               
               public abstract void process() throws Exception;
               
               public void run() throws ServletException {
                    try {
                         initConversationContext();
                         process();
                    } catch (Exception e) {
                         throw new ServletException("Error processing conversational request", e);
                    } finally {
                         cleanupConversationContext();
                    }
               }
          
               private void initConversationContext() {
                    ConversationContext conversationContext = Container.instance().deploymentServices().get(ContextLifecycle.class).getConversationContext();
                    conversationContext.setBeanStore(new ConversationBeanStore(request.getSession(), request.getParameter("cid")));
                    conversationContext.setActive(true);
               }
          
               private void cleanupConversationContext() {
                    ConversationContext conversationContext = Container.instance().deploymentServices().get(ContextLifecycle.class).getConversationContext();
                    conversationContext.setBeanStore(null);
                    conversationContext.setActive(false);
               }
          
               
          }