[seam-commits] Seam SVN: r13490 - in branches/enterprise/JBPAPP_5_0/src/main/org/jboss/seam: web and 1 other directory.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Fri Jul 23 08:38:48 EDT 2010


Author: manaRH
Date: 2010-07-23 08:38:47 -0400 (Fri, 23 Jul 2010)
New Revision: 13490

Modified:
   branches/enterprise/JBPAPP_5_0/src/main/org/jboss/seam/servlet/ContextualHttpServletRequest.java
   branches/enterprise/JBPAPP_5_0/src/main/org/jboss/seam/web/AuthenticationFilter.java
Log:
JBPAPP-3985, JBPAPP-3713 modified AuthenticationFilter and ContextualHttpServletRequest

Modified: branches/enterprise/JBPAPP_5_0/src/main/org/jboss/seam/servlet/ContextualHttpServletRequest.java
===================================================================
--- branches/enterprise/JBPAPP_5_0/src/main/org/jboss/seam/servlet/ContextualHttpServletRequest.java	2010-07-23 12:23:58 UTC (rev 13489)
+++ branches/enterprise/JBPAPP_5_0/src/main/org/jboss/seam/servlet/ContextualHttpServletRequest.java	2010-07-23 12:38:47 UTC (rev 13490)
@@ -1,6 +1,7 @@
 package org.jboss.seam.servlet;
 
 import java.io.IOException;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
@@ -17,6 +18,7 @@
  * Perform work in a full set of Seam contexts
  * 
  * @author Gavin King
+ * @author Marek Novotny
  *
  */
 public abstract class ContextualHttpServletRequest
@@ -25,6 +27,8 @@
 
    private final HttpServletRequest request;
    
+   private static ThreadLocal<AtomicInteger> count = new ThreadLocal<AtomicInteger>();
+   
    public ContextualHttpServletRequest(HttpServletRequest request)
    {
       this.request = request;
@@ -35,13 +39,19 @@
    public void run() throws ServletException, IOException
    {
       log.debug("beginning request");
-      ServletLifecycle.beginRequest(request);
-      ServletContexts.instance().setRequest(request);
-      restoreConversationId();
-      Manager.instance().restoreConversation();
-      ServletLifecycle.resumeConversation(request);
-      handleConversationPropagation();
       
+      // Begin request and Seam life cycle only if it is not nested
+      // ContextualHttpServletRequest
+      if (getCounterValue() == 0)
+      {  
+         ServletLifecycle.beginRequest(request);
+         ServletContexts.instance().setRequest(request);
+         restoreConversationId();
+         Manager.instance().restoreConversation();
+         ServletLifecycle.resumeConversation(request);
+         handleConversationPropagation();
+      }
+      
       // Force creation of the session
       if (request.getSession(false) == null)
       {
@@ -50,25 +60,37 @@
       
       try
       {
+         incrementCounterValue();
+         
          process();
-         //TODO: conversation timeout
-         Manager.instance().endRequest( new ServletRequestSessionMap(request)  );
-         ServletLifecycle.endRequest(request);
+         
+         decrementCounterValue();
+
+         // End request only if it is not nested ContextualHttpServletRequest
+         if (getCounterValue() == 0)
+         {
+            //TODO: conversation timeout
+            Manager.instance().endRequest( new ServletRequestSessionMap(request)  );
+            ServletLifecycle.endRequest(request);
+         }
       }
       catch (IOException ioe)
       {
+         removeCounter();
          Lifecycle.endRequest();
          log.debug("ended request due to exception");
          throw ioe;
       }
       catch (ServletException se)
       {
+         removeCounter();
          Lifecycle.endRequest();
          log.debug("ended request due to exception");
          throw se;
       }
       catch (Exception e)
       {
+         removeCounter();
          Lifecycle.endRequest();
          log.debug("ended request due to exception");
          throw new ServletException(e);
@@ -89,4 +111,65 @@
       ConversationPropagation.instance().restoreConversationId( request.getParameterMap() );
    }
    
+   /*
+    * Getter for ThreadLocal counter value
+    */
+   private int getCounterValue()
+   {
+      AtomicInteger i = count.get();
+      if (i == null || i.intValue() < 0)
+      {
+         log.trace("Getting 0" );
+         return 0;
+      }
+      else
+      {
+         log.trace("Getting " + i.intValue());
+         return i.intValue();
+      }
+   }
+   
+   /*
+    * Increments ThreadLocal counter value 
+    */
+   private void incrementCounterValue()
+   {
+      AtomicInteger i = count.get();
+      if (i == null || i.intValue() < 0)
+      {
+         i = new AtomicInteger(0);
+         count.set(i);
+      }
+      i.incrementAndGet();
+      log.trace("Incrementing to " + count.get());
+   }
+   
+   /*
+    *  Decrements ThreadLocal counter value
+    */
+   private void decrementCounterValue()
+   {
+      AtomicInteger i = count.get();
+      if (i == null)
+      {
+         log.trace("OOps, something removed counter befor end of request!");
+         // we should never get here...
+         throw new IllegalStateException("Counter for nested ContextualHttpServletRequest was removed before it should be!");
+      }
+      if (i.intValue() > 0)
+      {
+         i.decrementAndGet();
+         log.trace("Decrementing to " + count.get());
+      }
+   }
+   
+   /*
+    * Removes ThreadLocal counter
+    */
+   private void removeCounter() 
+   {
+      log.trace("Removing ThreadLocal counter");
+      count.remove();
+   } 
+   
 }

Modified: branches/enterprise/JBPAPP_5_0/src/main/org/jboss/seam/web/AuthenticationFilter.java
===================================================================
--- branches/enterprise/JBPAPP_5_0/src/main/org/jboss/seam/web/AuthenticationFilter.java	2010-07-23 12:23:58 UTC (rev 13489)
+++ branches/enterprise/JBPAPP_5_0/src/main/org/jboss/seam/web/AuthenticationFilter.java	2010-07-23 12:38:47 UTC (rev 13490)
@@ -21,8 +21,6 @@
 import org.jboss.seam.annotations.Scope;
 import org.jboss.seam.annotations.intercept.BypassInterceptors;
 import org.jboss.seam.annotations.web.Filter;
-import org.jboss.seam.contexts.Context;
-import org.jboss.seam.contexts.SessionContext;
 import org.jboss.seam.log.Log;
 import org.jboss.seam.security.Credentials;
 import org.jboss.seam.security.Identity;
@@ -31,7 +29,6 @@
 import org.jboss.seam.security.digest.DigestUtils;
 import org.jboss.seam.security.digest.DigestValidationException;
 import org.jboss.seam.servlet.ContextualHttpServletRequest;
-import org.jboss.seam.servlet.ServletRequestSessionMap;
 import org.jboss.seam.util.Base64;
 
 /**
@@ -103,41 +100,46 @@
       this.nonceValiditySeconds = value;
    }
    
-   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
-      throws IOException, ServletException
+   public void doFilter(ServletRequest request, ServletResponse response, final FilterChain chain) throws IOException, ServletException
    {
-      if (!(request instanceof HttpServletRequest)) 
+      if (!(request instanceof HttpServletRequest))
       {
          throw new ServletException("This filter can only process HttpServletRequest requests");
       }
-
-      HttpServletRequest httpRequest = (HttpServletRequest) request;
-      HttpServletResponse httpResponse = (HttpServletResponse) response;
-
+      
+      final HttpServletRequest httpRequest = (HttpServletRequest) request;
+      final HttpServletResponse httpResponse = (HttpServletResponse) response;
+      
       // Force session creation
       httpRequest.getSession();
       
-      if (AUTH_TYPE_BASIC.equals(authType))
-         processBasicAuth(httpRequest, httpResponse, chain);
-      else if (AUTH_TYPE_DIGEST.equals(authType))
-         processDigestAuth(httpRequest, httpResponse, chain);
-      else
-         throw new ServletException("Invalid authentication type");
+      new ContextualHttpServletRequest(httpRequest)
+      {
+         @Override
+         public void process() throws ServletException, IOException, LoginException
+         {
+            if (AUTH_TYPE_BASIC.equals(authType))
+               processBasicAuth(httpRequest, httpResponse, chain);
+            else if (AUTH_TYPE_DIGEST.equals(authType))
+               processDigestAuth(httpRequest, httpResponse, chain);
+            else
+               throw new ServletException("Invalid authentication type");
+         }
+      }.run();
    }
    
    private void processBasicAuth(HttpServletRequest request, 
             HttpServletResponse response, FilterChain chain)
       throws IOException, ServletException
    {
-      Context ctx = new SessionContext( new ServletRequestSessionMap(request) );
-      Identity identity = (Identity) ctx.get(Identity.class);
+      Identity identity = Identity.instance();
 
       if (identity == null)
       {
          throw new ServletException("Identity not found - please ensure that the Identity component is created on startup.");
       }
       
-      Credentials credentials = (Credentials) ctx.get(Credentials.class);
+      Credentials credentials = identity.getCredentials();
       
       boolean requireAuth = false;
       
@@ -202,15 +204,14 @@
             HttpServletResponse response, FilterChain chain)
       throws IOException, ServletException
    {
-      Context ctx = new SessionContext( new ServletRequestSessionMap(request) );
-      Identity identity = (Identity) ctx.get(Identity.class);
+      Identity identity = Identity.instance();
       
       if (identity == null)
       {
          throw new ServletException("Identity not found - please ensure that the Identity component is created on startup.");
       }      
       
-      Credentials credentials = (Credentials) ctx.get(Credentials.class);
+      Credentials credentials = identity.getCredentials();
       
       boolean requireAuth = false;    
       boolean nonceExpired = false;
@@ -301,19 +302,11 @@
       }             
    }
    
-   private void authenticate(HttpServletRequest request, final String username)
-      throws ServletException, IOException
+   private void authenticate(HttpServletRequest request, final String username) throws ServletException, IOException, LoginException
    {
-      new ContextualHttpServletRequest(request)
-      {
-         @Override
-         public void process() throws ServletException, IOException, LoginException
-         {
-            Identity identity = Identity.instance();
-            identity.getCredentials().setUsername(username);
-            identity.authenticate();
-         }
-      }.run();  
+      Identity identity = Identity.instance();
+      identity.getCredentials().setUsername(username);
+      identity.authenticate();
    }
    
    private String[] split(String toSplit, String delimiter) 



More information about the seam-commits mailing list