Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2008, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 
 package org.jboss.jsfunit.seam;
 
 import  javax.faces.context.FacesContext;
This listener prepares the Seam environment to be compatible with JSFUnit. After each request, the conversation scope is promoted so that it will be available to JSFUnit tests. Before each request, conversation scope is demoted to its normal pre-request state so that Seam applications will function normally.

Author(s):
Stan Silvert
Since:
1.0
 
 public class SeamRequestListener implements RequestListener
 {
    private boolean iPromotedTheConversation = false;
    private boolean iRestoredTheConversation = false;
    
    public void beforeRequest(WebRequestSettings webRequestSettings)
    {
       demoteConversation();
       tearDownConversation();
    }
 
    public void afterRequest(WebResponse webResponse)
    {
       promoteConversation();
       restoreConversation();
    }
    
    // code copied from org.jboss.seam.servlet.ContextualHttpServletRequest
    private void restoreConversation()
    {
       HttpServletRequest request = httpServletRequest();
       if (request == nullreturn;
       ServletLifecycle.beginRequest(request);
       ServletContexts.instance().setRequest(request);
       ConversationPropagation.instance().restoreConversationIdrequest.getParameterMap() );
       Manager.instance().restoreConversation();
       ServletLifecycle.resumeConversation(request);
       Manager.instance().handleConversationPropagationrequest.getParameterMap() );
       
       this. = true;
    }
    
    // code copied from org.jboss.seam.servlet.ContextualHttpServletRequest
    private void tearDownConversation()
    {
       if (!this.return;
       
       HttpServletRequest request = httpServletRequest();
       Manager.instance().endRequestnew ServletRequestSessionMap(request)  );
       ServletLifecycle.endRequest(request);
       
       this. = false;
    }
    
    private void demoteConversation()
    {
       if (this.)
       {
          Manager.instance().setLongRunningConversation(false);
          this. = false;
       }
    }
   
   private void promoteConversation()
   {
      try
      {
         if (!Manager.instance().isLongRunningConversation())
         {
            Manager.instance().setLongRunningConversation(true);
            this. = true;
         }
      }
      catch (IllegalStateException e)
      {
         // ignore
      }
   }
   
   {
      FacesContext facesContext = FacesContextBridge.getCurrentInstance();
      if (facesContext == nullreturn null;
      return (HttpServletRequest)facesContext.getExternalContext().getRequest();
   }
   
New to GrepCode? Check out our FAQ X