Author: markt
Date: Wed Jan 14 13:55:59 2009
New Revision: 734527

URL: http://svn.apache.org/viewvc?rev=734527&view=rev
Log:
Implement dynamically adding servlets and filters. This compiles but is 
untested.

Modified:
    tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java
    tomcat/trunk/java/org/apache/catalina/core/ApplicationContextFacade.java
    tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties
    tomcat/trunk/java/org/apache/catalina/core/StandardContext.java
    tomcat/trunk/java/org/apache/catalina/deploy/FilterMap.java

Modified: tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java?rev=734527&r1=734526&r2=734527&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java 
(original)
+++ tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java Wed Jan 
14 13:55:59 2009
@@ -49,6 +49,8 @@
 import org.apache.catalina.Wrapper;
 import org.apache.catalina.connector.Connector;
 import org.apache.catalina.deploy.ApplicationParameter;
+import org.apache.catalina.deploy.FilterDef;
+import org.apache.catalina.deploy.FilterMap;
 import org.apache.catalina.util.Enumerator;
 import org.apache.catalina.util.ResourceSet;
 import org.apache.catalina.util.ServerInfo;
@@ -836,26 +838,82 @@
     public void addFilter(String filterName, String description,
             String className, Map<String, String> initParameters,
             boolean isAsyncSupported) {
-        // TODO SERVLET3
+
+        if (context.initialized) {
+            //TODO Spec breaking enhancement to ignore this restriction
+            throw new IllegalStateException(
+                    sm.getString("applicationContext.addFilter.ise",
+                            getContextPath()));
+        }
+        FilterDef filterDef = new FilterDef();
+        filterDef.setFilterName(filterName);
+        filterDef.setDescription(description);
+        filterDef.setFilterClass(className);
+        filterDef.getParameterMap().putAll(initParameters);
+        context.addFilterDef(filterDef);
+        // TODO SERVLET3 - ASync support
     }
 
 
     public void addFilterMappingForServletNames(String filterName,
             EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter,
             String... servletNames) {
-        // TODO SERVLET3
+        if (context.initialized) {
+            //TODO Spec breaking enhancement to ignore this restriction
+            throw new IllegalStateException(sm.getString(
+                    "applicationContext.addFilterMapping", getContextPath()));
+        }
+        FilterMap filterMap = new FilterMap(); 
+        for (String servletName : servletNames) {
+            filterMap.addServletName(servletName);
+        }
+        filterMap.setFilterName(filterName);
+        for (DispatcherType dispatcherType: dispatcherTypes) {
+            filterMap.setDispatcher(dispatcherType.name());
+        }
+        if (isMatchAfter) {
+            context.addFilterMap(filterMap);
+        } else {
+            context.addFilterMapBefore(filterMap);
+        }
     }
 
 
     public void addFilterMappingForUrlPatterns(String filterName,
             EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter,
             String... urlPatterns) {
-        // TODO SERVLET3
+        
+        if (context.initialized) {
+            //TODO Spec breaking enhancement to ignore this restriction
+            throw new IllegalStateException(sm.getString(
+                    "applicationContext.addFilterMapping", getContextPath()));
+        }
+        FilterMap filterMap = new FilterMap(); 
+        for (String urlPattern : urlPatterns) {
+            filterMap.addURLPattern(urlPattern);
+        }
+        filterMap.setFilterName(filterName);
+        for (DispatcherType dispatcherType: dispatcherTypes) {
+            filterMap.setDispatcher(dispatcherType.name());
+        }
+        if (isMatchAfter) {
+            context.addFilterMap(filterMap);
+        } else {
+            context.addFilterMapBefore(filterMap);
+        }
     }
 
 
     public void addServletMapping(String servletName, String[] urlPatterns) {
-        // TODO SERVLET3
+        if (context.initialized) {
+            //TODO Spec breaking enhancement to ignore this restriction
+            throw new IllegalStateException(sm.getString(
+                    "applicationContext.addServletMapping", getContextPath()));
+        }
+        for (String urlPattern : urlPatterns) {
+            boolean jspWildCard = ("*.jsp".equals(urlPattern));
+            context.addServletMapping(servletName, urlPattern, jspWildCard);
+        }
     }
 
 
@@ -882,10 +940,7 @@
         // Context > Host > Engine > Service
         Connector[] connectors = ((Engine) context.getParent().getParent())
                 .getService().findConnectors();
-        // Need at least one SSL enabled connector to use the SSL session ID.
-        // has to be SSL enabled so we can close the SSL session.
-        // TODO extend this for SSL sessions managed by accelerators, web
-        // servers etc
+        // Need at least one secure connector to use the SSL session ID.
         for (Connector connector : connectors) {
             if (Boolean.TRUE.equals(connector.getAttribute("secure"))) {
                 defaultSessionTrackingModes.add(SessionTrackingMode.SSL);
@@ -1123,4 +1178,4 @@
     }
 
 
-}
\ No newline at end of file
+}

Modified: 
tomcat/trunk/java/org/apache/catalina/core/ApplicationContextFacade.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/ApplicationContextFacade.java?rev=734527&r1=734526&r2=734527&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/ApplicationContextFacade.java 
(original)
+++ tomcat/trunk/java/org/apache/catalina/core/ApplicationContextFacade.java 
Wed Jan 14 13:55:59 2009
@@ -390,26 +390,52 @@
     public void addFilter(String filterName, String description,
             String className, Map<String, String> initParameters,
             boolean isAsyncSupported) {
-        // TODO SERVLET3
+        if (SecurityUtil.isPackageProtectionEnabled()) {
+            doPrivileged("addFilter", new Object[]{filterName, description,
+                    className, initParameters,
+                    Boolean.valueOf(isAsyncSupported)});
+        } else {
+            context.addFilter(filterName, description, className,
+                    initParameters, isAsyncSupported);
+        }
     }
 
 
     public void addFilterMappingForServletNames(String filterName,
             EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter,
             String... servletNames) {
-        // TODO SERVLET3
+        if (SecurityUtil.isPackageProtectionEnabled()) {
+            doPrivileged("addFilterMappingForServletNames",
+                    new Object[]{filterName, dispatcherTypes,
+                    Boolean.valueOf(isMatchAfter), servletNames});
+        } else {
+            context.addFilterMappingForServletNames(filterName, 
dispatcherTypes,
+                    isMatchAfter, servletNames);
+        }
     }
 
 
     public void addFilterMappingForUrlPatterns(String filterName,
             EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter,
             String... urlPatterns) {
-        // TODO SERVLET3
+        if (SecurityUtil.isPackageProtectionEnabled()) {
+            doPrivileged("addFilterMappingForUrlPatterns",
+                    new Object[]{filterName, dispatcherTypes,
+                    Boolean.valueOf(isMatchAfter), urlPatterns});
+        } else {
+            context.addFilterMappingForUrlPatterns(filterName, dispatcherTypes,
+                    isMatchAfter, urlPatterns);
+        }
     }
 
 
     public void addServletMapping(String servletName, String[] urlPatterns) {
-        // TODO SERVLET3
+        if (SecurityUtil.isPackageProtectionEnabled()) {
+            doPrivileged("addServletMapping",
+                    new Object[]{servletName, urlPatterns});
+        } else {
+            context.addServletMapping(servletName, urlPatterns);
+        }
     }
 
 
@@ -434,13 +460,22 @@
 
 
     public SessionCookieConfig getSessionCookieConfig() {
-        // TODO SERVLET3
-        return null;
+        if (SecurityUtil.isPackageProtectionEnabled()) {
+            return (SessionCookieConfig)
+                doPrivileged("getSessionCookieConfig", null);
+        } else {
+            return context.getSessionCookieConfig();
+        }
     }
 
 
     public void setSessionCookieConfig(SessionCookieConfig 
sessionCookieConfig) {
-        // TODO SERVLET3
+        if (SecurityUtil.isPackageProtectionEnabled()) {
+            doPrivileged("setSessionCookieConfig",
+                    new Object[]{sessionCookieConfig});
+        } else {
+            context.setSessionCookieConfig(sessionCookieConfig);
+        }
     }
 
 

Modified: tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties?rev=734527&r1=734526&r2=734527&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties 
(original)
+++ tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties Wed Jan 
14 13:55:59 2009
@@ -13,6 +13,9 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+applicationContext.addFilter.ise=Filters can not be added to context {0} at 
this time. See SRV.4.4.
+applicationContext.addFilterMapping.ise=Filter mappings can not be added to 
context {0} at this time. See SRV.4.4.
+applicationContext.addServletMapping.ise=Servlet mappings can not be added to 
context {0} at this time. See SRV.4.4.
 applicationContext.attributeEvent=Exception thrown by attributes event listener
 applicationContext.mapping.error=Error during mapping
 applicationContext.requestDispatcher.iae=Path {0} does not start with a "/" 
character

Modified: tomcat/trunk/java/org/apache/catalina/core/StandardContext.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/StandardContext.java?rev=734527&r1=734526&r2=734527&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/StandardContext.java (original)
+++ tomcat/trunk/java/org/apache/catalina/core/StandardContext.java Wed Jan 14 
13:55:59 2009
@@ -360,10 +360,22 @@
 
     /**
      * The set of filter mappings for this application, in the order
-     * they were defined in the deployment descriptor.
+     * they were defined in the deployment descriptor with additional mappings
+     * added via the {...@link ServletContext} possibly both before and after 
those
+     * defined in the deployment descriptor.
      */
     private FilterMap filterMaps[] = new FilterMap[0];
 
+    /**
+     * Filter mappings added via {...@link ServletContext} may have to be 
inserted
+     * before the mappings in the deploymenmt descriptor but must be inserted 
in
+     * the order the {...@link ServletContext} methods are called. This isn't 
an
+     * issue for the mappings added after the deployment descriptor - they are
+     * just added to the end - but correctly the adding mappings before the
+     * deployment descriptor mappings requires knowing where the last 'before'
+     * mapping was added.
+     */
+    private int filterMapInsertPoint = 0;
 
     /**
      * Ignore annotations.
@@ -2173,7 +2185,8 @@
 
 
     /**
-     * Add a filter mapping to this Context.
+     * Add a filter mapping to this Context at the end of the current set
+     * of filter mappings.
      *
      * @param filterMap The filter mapping to be added
      *
@@ -2183,6 +2196,54 @@
      */
     public void addFilterMap(FilterMap filterMap) {
 
+        validateFilterMap(filterMap);
+        // Add this filter mapping to our registered set
+        synchronized (filterMaps) {
+            FilterMap results[] =new FilterMap[filterMaps.length + 1];
+            System.arraycopy(filterMaps, 0, results, 0, filterMaps.length);
+            results[filterMaps.length] = filterMap;
+            filterMaps = results;
+        }
+        fireContainerEvent("addFilterMap", filterMap);
+    }
+
+    
+    /**
+     * Add a filter mapping to this Context before the mappings defined in the
+     * deployment descriptor but after any other mappings added via this 
method.
+     *
+     * @param filterMap The filter mapping to be added
+     *
+     * @exception IllegalArgumentException if the specified filter name
+     *  does not match an existing filter definition, or the filter mapping
+     *  is malformed
+     */
+    public void addFilterMapBefore(FilterMap filterMap) {
+
+        validateFilterMap(filterMap);
+
+        // Add this filter mapping to our registered set
+        synchronized (filterMaps) {
+            FilterMap results[] = new FilterMap[filterMaps.length + 1];
+            System.arraycopy(filterMaps, 0, results, 0, filterMapInsertPoint);
+            results[filterMapInsertPoint] = filterMap;
+            System.arraycopy(filterMaps, filterMapInsertPoint, results,
+                    filterMaps.length - filterMapInsertPoint+1,
+                    filterMapInsertPoint);
+            
+            filterMapInsertPoint++;
+            
+            results[filterMaps.length] = filterMap;
+            filterMaps = results;
+        }
+        fireContainerEvent("addFilterMap", filterMap);
+    }
+
+
+    /**
+     * Validate the supplied FilterMap.
+     */
+    private void validateFilterMap(FilterMap filterMap) {
         // Validate the proposed filter mapping
         String filterName = filterMap.getFilterName();
         String[] servletNames = filterMap.getServletNames();
@@ -2190,10 +2251,7 @@
         if (findFilterDef(filterName) == null)
             throw new IllegalArgumentException
                 (sm.getString("standardContext.filterMap.name", filterName));
-//      <= Servlet API 2.4
-//      if ((servletNames.length == 0) && (urlPatterns.length == 0))
-//      Servlet API 2.5 (FIX 43338)
-//      SRV 6.2.5 says supporting for '*' as the servlet-name in 
filter-mapping.
+
         if (!filterMap.getMatchAllServletNames() && 
             !filterMap.getMatchAllUrlPatterns() && 
             (servletNames.length == 0) && (urlPatterns.length == 0))
@@ -2205,8 +2263,6 @@
             throw new IllegalArgumentException
                 (sm.getString("standardContext.filterMap.either"));
         */
-        // Because filter-pattern is new in 2.3, no need to adjust
-        // for 2.2 backwards compatibility
         for (int i = 0; i < urlPatterns.length; i++) {
             if (!validateURLPattern(urlPatterns[i])) {
                 throw new IllegalArgumentException
@@ -2214,19 +2270,8 @@
                             urlPatterns[i]));
             }
         }
-
-        // Add this filter mapping to our registered set
-        synchronized (filterMaps) {
-            FilterMap results[] =new FilterMap[filterMaps.length + 1];
-            System.arraycopy(filterMaps, 0, results, 0, filterMaps.length);
-            results[filterMaps.length] = filterMap;
-            filterMaps = results;
-        }
-        fireContainerEvent("addFilterMap", filterMap);
-
     }
 
-
     /**
      * Add the classname of an InstanceListener to be added to each
      * Wrapper appended to this Context.
@@ -3339,6 +3384,9 @@
             System.arraycopy(filterMaps, 0, results, 0, n);
             System.arraycopy(filterMaps, n + 1, results, n,
                              (filterMaps.length - 1) - n);
+            if (n < filterMapInsertPoint) {
+                filterMapInsertPoint--;
+            }
             filterMaps = results;
 
         }

Modified: tomcat/trunk/java/org/apache/catalina/deploy/FilterMap.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/deploy/FilterMap.java?rev=734527&r1=734526&r2=734527&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/deploy/FilterMap.java (original)
+++ tomcat/trunk/java/org/apache/catalina/deploy/FilterMap.java Wed Jan 14 
13:55:59 2009
@@ -22,6 +22,8 @@
 import org.apache.catalina.util.RequestUtil;
 import java.io.Serializable;
 
+import javax.servlet.DispatcherType;
+
 
 /**
  * Representation of a filter mapping for a web application, as represented
@@ -161,7 +163,7 @@
     public void setDispatcher(String dispatcherString) {
         String dispatcher = dispatcherString.toUpperCase();
         
-        if (dispatcher.equals("FORWARD")) {
+        if (dispatcher.equals(DispatcherType.FORWARD.name())) {
 
             // apply FORWARD to the global dispatcherMapping.
             switch (dispatcherMapping) {
@@ -174,7 +176,7 @@
                 case REQUEST_ERROR_INCLUDE : dispatcherMapping = 
REQUEST_ERROR_FORWARD_INCLUDE; break;
                 case REQUEST_INCLUDE : dispatcherMapping = 
REQUEST_FORWARD_INCLUDE; break;
             }
-        } else if (dispatcher.equals("INCLUDE")) {
+        } else if (dispatcher.equals(DispatcherType.INCLUDE.name())) {
             // apply INCLUDE to the global dispatcherMapping.
             switch (dispatcherMapping) {
                 case NOT_SET  :  dispatcherMapping = INCLUDE; break;
@@ -186,7 +188,7 @@
                 case REQUEST_ERROR_FORWARD : dispatcherMapping = 
REQUEST_ERROR_FORWARD_INCLUDE; break;
                 case REQUEST_FORWARD : dispatcherMapping = 
REQUEST_FORWARD_INCLUDE; break;
             }
-        } else if (dispatcher.equals("REQUEST")) {
+        } else if (dispatcher.equals(DispatcherType.REQUEST.name())) {
             // apply REQUEST to the global dispatcherMapping.
             switch (dispatcherMapping) {
                 case NOT_SET  :  dispatcherMapping = REQUEST; break;
@@ -198,7 +200,7 @@
                 case INCLUDE_FORWARD : dispatcherMapping = 
REQUEST_FORWARD_INCLUDE; break;
                 case INCLUDE_ERROR_FORWARD : dispatcherMapping = 
REQUEST_ERROR_FORWARD_INCLUDE; break;
             }
-        }  else if (dispatcher.equals("ERROR")) {
+        }  else if (dispatcher.equals(DispatcherType.ERROR.name())) {
             // apply ERROR to the global dispatcherMapping.
             switch (dispatcherMapping) {
                 case NOT_SET  :  dispatcherMapping = ERROR; break;



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to