[jboss-cvs] JBossAS SVN: r94392 - in branches/Branch_5_x: testsuite/src/main/org/jboss/test/web/test and 7 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Oct 5 17:14:06 EDT 2009


Author: anil.saldhana at jboss.com
Date: 2009-10-05 17:14:05 -0400 (Mon, 05 Oct 2009)
New Revision: 94392

Added:
   branches/Branch_5_x/testsuite/src/main/org/jboss/test/web/test/ActiveRequestResponseCacheValveUnitTestCase.java
   branches/Branch_5_x/testsuite/src/resources/web/valve-requestcaching/
   branches/Branch_5_x/testsuite/src/resources/web/valve-requestcaching/testCachedRequest.jsp
   branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/service/request/
   branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/service/request/ActiveRequestResponseCacheValve.java
Modified:
   branches/Branch_5_x/testsuite/imports/sections/web.xml
   branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/security/JBossWebRealm.java
   branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/security/JaccContextValve.java
   branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/security/SecurityAssociationValve.java
   branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/security/login/WebAuthentication.java
   branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/service/deployers/TomcatDeployment.java
Log:
JBAS-7311: valve at the engine level to cache active request/response

Modified: branches/Branch_5_x/testsuite/imports/sections/web.xml
===================================================================
--- branches/Branch_5_x/testsuite/imports/sections/web.xml	2009-10-05 20:39:29 UTC (rev 94391)
+++ branches/Branch_5_x/testsuite/imports/sections/web.xml	2009-10-05 21:14:05 UTC (rev 94392)
@@ -913,5 +913,11 @@
          </classes>
       </war>
 
+      <!-- war to test the valve for active request/response caching at engine level -->
+      <jar destfile="${build.lib}/valve-requestcaching.war">
+         <fileset dir="${build.resources}/web/valve-requestcaching">
+           <include name="**"/>
+         </fileset>
+      </jar>
    </target>
 </project>

Added: branches/Branch_5_x/testsuite/src/main/org/jboss/test/web/test/ActiveRequestResponseCacheValveUnitTestCase.java
===================================================================
--- branches/Branch_5_x/testsuite/src/main/org/jboss/test/web/test/ActiveRequestResponseCacheValveUnitTestCase.java	                        (rev 0)
+++ branches/Branch_5_x/testsuite/src/main/org/jboss/test/web/test/ActiveRequestResponseCacheValveUnitTestCase.java	2009-10-05 21:14:05 UTC (rev 94392)
@@ -0,0 +1,59 @@
+/*
+ * 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.test.web.test;
+
+import java.net.URL;
+
+import junit.framework.Test;
+
+import org.jboss.test.JBossTestCase;
+import org.jboss.test.util.web.HttpUtils;
+
+/**
+ * JBAS-7311: Valve at Engine level caches active request/response
+ * @author Anil.Saldhana at redhat.com
+ * @since Oct 5, 2009
+ */
+public class ActiveRequestResponseCacheValveUnitTestCase extends JBossTestCase
+{
+   public ActiveRequestResponseCacheValveUnitTestCase(String name)
+   {
+      super(name); 
+   }
+   
+   public static Test suite() throws Exception
+   {
+      return getDeploySetup(ActiveRequestResponseCacheValveUnitTestCase.class, 
+            "valve-requestcaching.war");
+   }
+   
+   /**
+    * Test a jsp page that internally checks whether the {@link ActiveRequestResponseCacheValve}
+    * is caching the active request
+    * @throws Exception
+    */
+   public void testActiveRequestCaching() throws Exception
+   {
+      URL url = new URL(HttpUtils.getBaseURL() + "valve-requestcaching/testCachedRequest.jsp");
+      HttpUtils.accessURL(url);   
+   }
+}
\ No newline at end of file

Added: branches/Branch_5_x/testsuite/src/resources/web/valve-requestcaching/testCachedRequest.jsp
===================================================================
--- branches/Branch_5_x/testsuite/src/resources/web/valve-requestcaching/testCachedRequest.jsp	                        (rev 0)
+++ branches/Branch_5_x/testsuite/src/resources/web/valve-requestcaching/testCachedRequest.jsp	2009-10-05 21:14:05 UTC (rev 94392)
@@ -0,0 +1,8 @@
+<%
+   org.apache.catalina.connector.Request catrequest =
+      org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.activeRequest.get();
+   if(catrequest == null)
+      response.setStatus(500);
+   else
+      response.setStatus(200);
+%>

Modified: branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/security/JBossWebRealm.java
===================================================================
--- branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/security/JBossWebRealm.java	2009-10-05 20:39:29 UTC (rev 94391)
+++ branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/security/JBossWebRealm.java	2009-10-05 21:14:05 UTC (rev 94392)
@@ -69,6 +69,7 @@
 import org.jboss.security.authorization.ResourceKeys;
 import org.jboss.security.javaee.AbstractWebAuthorizationHelper;
 import org.jboss.security.javaee.SecurityHelperFactory;
+import org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve;
 
 //$Id$
 
@@ -366,7 +367,7 @@
       }
       Principal principal = null; 
 
-      Principal caller = (Principal) SecurityAssociationValve.userPrincipal.get();
+      Principal caller = SecurityAssociationValve.userPrincipal.get();
       if (caller == null && username == null && credentials == null)
       {
          return null;
@@ -533,7 +534,7 @@
       String servletName = null;
       //WebProgrammaticAuthentication does not go through hasResourcePermission
       //and hence the activeRequest thread local may not be set
-      Request req = (Request) SecurityAssociationValve.activeRequest.get();
+      Request req = ActiveRequestResponseCacheValve.activeRequest.get();
       Wrapper servlet = req.getWrapper();
       if (servlet != null)
       {

Modified: branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/security/JaccContextValve.java
===================================================================
--- branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/security/JaccContextValve.java	2009-10-05 20:39:29 UTC (rev 94391)
+++ branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/security/JaccContextValve.java	2009-10-05 21:14:05 UTC (rev 94392)
@@ -37,6 +37,7 @@
 import org.jboss.metadata.web.jboss.JBossWebMetaData;
 import org.jboss.security.SecurityRolesAssociation;
 import org.jboss.servlet.http.HttpEvent;
+import org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve;
 
 /**
  * A Valve that sets the JACC context id and HttpServletRequest policy
@@ -86,15 +87,15 @@
          PolicyContext.setContextID(contextID);
          // Set the JACC HttpServletRequest PolicyContextHandler data
          HttpServletRequestPolicyContextHandler.setRequest(httpRequest);
-         if(SecurityAssociationValve.activeRequest.get() == null)
-            SecurityAssociationValve.activeRequest.set(request);
+         if(ActiveRequestResponseCacheValve.activeRequest.get() == null)
+            ActiveRequestResponseCacheValve.activeRequest.set(request);
          // Perform the request
          getNext().invoke(request, response);
       }
       finally
       {
          SecurityAssociationValve.activeWebMetaData.set(null);
-         SecurityAssociationValve.activeRequest.set(null);
+         ActiveRequestResponseCacheValve.activeRequest.set(null);
          SecurityAssociationActions.clear();
          activeCS.set(null);
          SecurityRolesAssociation.setSecurityRoles(null);
@@ -121,15 +122,15 @@
          PolicyContext.setContextID(contextID);
          // Set the JACC HttpServletRequest PolicyContextHandler data
          HttpServletRequestPolicyContextHandler.setRequest(httpRequest);
-         if(SecurityAssociationValve.activeRequest.get() == null)
-            SecurityAssociationValve.activeRequest.set(request);
+         if(ActiveRequestResponseCacheValve.activeRequest.get() == null)
+            ActiveRequestResponseCacheValve.activeRequest.set(request);
          // Perform the request
          getNext().event(request, response, event);
       }
       finally
       {
          SecurityAssociationValve.activeWebMetaData.set(null);
-         SecurityAssociationValve.activeRequest.set(null);
+         ActiveRequestResponseCacheValve.activeRequest.set(null);
          SecurityAssociationActions.clear();
          activeCS.set(null);
          SecurityRolesAssociation.setSecurityRoles(null);

Modified: branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/security/SecurityAssociationValve.java
===================================================================
--- branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/security/SecurityAssociationValve.java	2009-10-05 20:39:29 UTC (rev 94391)
+++ branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/security/SecurityAssociationValve.java	2009-10-05 21:14:05 UTC (rev 94392)
@@ -59,10 +59,6 @@
    public static ThreadLocal<Principal> userPrincipal = new ThreadLocal<Principal>();
    /** Maintain the active WebMetaData for request security checks */
    public static ThreadLocal<JBossWebMetaData> activeWebMetaData = new ThreadLocal<JBossWebMetaData>();
-   /** Maintain the Catalina Request for programmatic web login */
-   public static ThreadLocal<Request> activeRequest = new ThreadLocal<Request>();
-   /** Maintain the Catalina Response for programmatic web login */
-   public static ThreadLocal<Response> activeResponse = new ThreadLocal<Response>();
    
    /** The web app metadata */
    private JBossWebMetaData metaData;
@@ -106,9 +102,6 @@
          log.trace("Begin invoke, caller="+caller);
       // Set the active meta data
       activeWebMetaData.set(metaData); 
-      //Set the active request and response objects
-      activeRequest.set(request);
-      activeResponse.set(response);
       
       try
       {
@@ -226,8 +219,6 @@
             log.trace("End invoke, caller="+caller);
          activeWebMetaData.set(null);
          userPrincipal.set(null);
-         activeRequest.set(null);
-         activeResponse.set(null);
       }
    }
 
@@ -260,10 +251,7 @@
       if( trace )
          log.trace("Begin invoke, caller="+caller);
       // Set the active meta data
-      activeWebMetaData.set(metaData); 
-      //Set the active request and response objects
-      activeRequest.set(request);
-      activeResponse.set(response);
+      activeWebMetaData.set(metaData);  
       
       try
       {
@@ -380,9 +368,7 @@
          if( trace )
             log.trace("End invoke, caller="+caller);
          activeWebMetaData.set(null);
-         userPrincipal.set(null);
-         activeRequest.set(null);
-         activeResponse.set(null);
+         userPrincipal.set(null); 
       }
    }
       

Modified: branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/security/login/WebAuthentication.java
===================================================================
--- branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/security/login/WebAuthentication.java	2009-10-05 20:39:29 UTC (rev 94391)
+++ branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/security/login/WebAuthentication.java	2009-10-05 21:14:05 UTC (rev 94392)
@@ -36,7 +36,7 @@
 import org.apache.catalina.authenticator.SingleSignOn;
 import org.apache.catalina.connector.Request;
 import org.apache.catalina.connector.Response;
-import org.jboss.web.tomcat.security.SecurityAssociationValve;
+import org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve;
 
 //$Id$
 
@@ -62,7 +62,7 @@
    public boolean login(X509Certificate[] certs)
    {
       //Get the active request
-      Request request = SecurityAssociationValve.activeRequest.get();
+      Request request = ActiveRequestResponseCacheValve.activeRequest.get();
       if (request == null)
          throw new IllegalStateException("request is null");
       Principal p = request.getContext().getRealm().authenticate(certs);
@@ -83,7 +83,7 @@
    public boolean login(String username, Object credential)
    {
       //Get the active request
-      Request request = SecurityAssociationValve.activeRequest.get();
+      Request request = ActiveRequestResponseCacheValve.activeRequest.get();
       if (request == null)
          throw new IllegalStateException("request is null");
 
@@ -110,7 +110,7 @@
    public void logout()
    {
       //Get the active request
-      Request request = SecurityAssociationValve.activeRequest.get();
+      Request request = ActiveRequestResponseCacheValve.activeRequest.get();
       if (request == null)
          throw new IllegalStateException("request is null");
       unregister(request);
@@ -171,7 +171,7 @@
             cookie.setDomain(ssoDomain);
          }
 
-         Response response = (Response) SecurityAssociationValve.activeResponse.get();
+         Response response = ActiveRequestResponseCacheValve.activeResponse.get();
          response.addCookie(cookie);
 
          // Register this principal with our SSO valve

Modified: branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/service/deployers/TomcatDeployment.java
===================================================================
--- branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/service/deployers/TomcatDeployment.java	2009-10-05 20:39:29 UTC (rev 94391)
+++ branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/service/deployers/TomcatDeployment.java	2009-10-05 21:14:05 UTC (rev 94392)
@@ -44,6 +44,9 @@
 import java.util.Set;
 import java.util.zip.ZipFile;
 
+import org.apache.catalina.Container;
+import org.apache.catalina.Engine;
+import org.apache.catalina.Host;
 import org.apache.catalina.LifecycleEvent;
 import org.apache.catalina.LifecycleListener;
 import org.apache.catalina.Loader;
@@ -68,6 +71,7 @@
 import org.jboss.web.tomcat.security.SecurityContextEstablishmentValve;
 import org.jboss.web.tomcat.service.TomcatInjectionContainer;
 import org.jboss.web.tomcat.service.WebCtxLoader;
+import org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve;
 import org.jboss.web.tomcat.service.session.AbstractJBossManager;
 import org.jboss.web.tomcat.service.session.distributedcache.spi.ClusteringNotSupportedException;
 import org.omg.CORBA.ORB;
@@ -292,6 +296,14 @@
       if (metaDataSecurityDomain != null)
          metaDataSecurityDomain = metaDataSecurityDomain.trim();
 
+      //Add a valve to cache the active request/response
+      Engine engine = this.getCatalinaEngine(context);
+      if(engine != null)
+      {
+         ActiveRequestResponseCacheValve activeReqValve = new ActiveRequestResponseCacheValve();
+         engine.getPipeline().addValve(activeReqValve); 
+      }
+      
       // Add a valve to establish security context
       SecurityContextEstablishmentValve scevalve = new SecurityContextEstablishmentValve(metaDataSecurityDomain, SecurityUtil.unprefixSecurityDomain(config
             .getDefaultSecurityDomain()), SecurityActions.loadClass(config.getSecurityContextClassName()), getSecurityManagement());
@@ -684,5 +696,17 @@
       }
       return defaultHosts.iterator();
    }
-
-}
+   
+   /**
+    * Traverse the parent chain of the context to reach the Catalina Engine
+    * @param context Context of the web application
+    * @return
+    */
+   private Engine getCatalinaEngine(org.apache.catalina.Context context)
+   {
+      Container parentContainer = context.getParent();
+      while(parentContainer != null && !(parentContainer instanceof Engine))
+         parentContainer = parentContainer.getParent();
+      return (Engine) parentContainer;
+   }
+}
\ No newline at end of file

Added: branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/service/request/ActiveRequestResponseCacheValve.java
===================================================================
--- branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/service/request/ActiveRequestResponseCacheValve.java	                        (rev 0)
+++ branches/Branch_5_x/tomcat/src/main/org/jboss/web/tomcat/service/request/ActiveRequestResponseCacheValve.java	2009-10-05 21:14:05 UTC (rev 94392)
@@ -0,0 +1,87 @@
+/*
+ * 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.web.tomcat.service.request;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+
+import org.apache.catalina.connector.Request;
+import org.apache.catalina.connector.Response;
+import org.apache.catalina.valves.ValveBase;
+import org.jboss.servlet.http.HttpEvent;
+
+/** JBAS-7311: Caches the active Catalina request/response
+ * @author Anil.Saldhana at redhat.com
+ * @since Oct 5, 2009
+ */
+public class ActiveRequestResponseCacheValve extends ValveBase
+{
+   /** Maintain the Catalina Request for programmatic web login */
+   public static ThreadLocal<Request> activeRequest = new ThreadLocal<Request>();
+   /** Maintain the Catalina Response for programmatic web login */
+   public static ThreadLocal<Response> activeResponse = new ThreadLocal<Response>();
+  
+   @Override
+   public void invoke(Request request, Response response) throws IOException, ServletException
+   {
+      internalProcess(request, response, null); 
+   }
+
+   @Override
+   public void event(Request request, Response response, HttpEvent event) throws IOException, ServletException
+   {
+      internalProcess(request, response, event);
+   } 
+   
+   /**
+    * Set the active request and response on the threadlocals
+    * @param request
+    * @param response
+    * @param event
+    * @throws IOException
+    * @throws ServletException
+    */
+   private void internalProcess(Request request, Response response, HttpEvent event) throws IOException, ServletException
+   {
+      //Set the active request and response objects
+      activeRequest.set(request);
+      activeResponse.set(response);
+      
+      try
+      {
+         if(event == null)
+         {
+            getNext().invoke(request, response);
+         }
+         else
+         {
+            getNext().event(request, response, event);
+         }
+      }
+      finally
+      {
+         activeRequest.set(null);
+         activeResponse.set(null);
+      }  
+   }
+}
\ No newline at end of file




More information about the jboss-cvs-commits mailing list