tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1004006 - in /tomcat/trunk: java/org/apache/catalina/deploy/ java/org/apache/catalina/startup/ test/org/apache/catalina/core/ test/org/apache/catalina/startup/ test/webapp-3.0/WEB-INF/
Date Sun, 03 Oct 2010 17:27:42 GMT
Author: markt
Date: Sun Oct  3 17:27:42 2010
New Revision: 1004006

URL: http://svn.apache.org/viewvc?rev=1004006&view=rev
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=49922
Don't map filter twice if filter matches multiple mapping.

Modified:
    tomcat/trunk/java/org/apache/catalina/deploy/FilterMap.java
    tomcat/trunk/java/org/apache/catalina/deploy/WebXml.java
    tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java
    tomcat/trunk/test/org/apache/catalina/core/TestStandardContext.java
    tomcat/trunk/test/org/apache/catalina/startup/TestContextConfigAnnotation.java
    tomcat/trunk/test/webapp-3.0/WEB-INF/web.xml

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=1004006&r1=1004005&r2=1004006&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/deploy/FilterMap.java (original)
+++ tomcat/trunk/java/org/apache/catalina/deploy/FilterMap.java Sun Oct  3 17:27:42 2010
@@ -80,7 +80,11 @@ public class FilterMap implements Serial
     private String[] servletNames = new String[0];
 
     public String[] getServletNames() {
-        return (this.servletNames);
+        if (matchAllServletNames) {
+            return new String[] {};
+        } else {
+            return (this.servletNames);
+        }
     }
 
     public void addServletName(String servletName) {
@@ -121,7 +125,11 @@ public class FilterMap implements Serial
     private String[] urlPatterns = new String[0];
 
     public String[] getURLPatterns() {
-        return (this.urlPatterns);
+        if (matchAllUrlPatterns) {
+            return new String[] {};
+        } else {
+            return (this.urlPatterns);
+        }
     }
 
     public void addURLPattern(String urlPattern) {

Modified: tomcat/trunk/java/org/apache/catalina/deploy/WebXml.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/deploy/WebXml.java?rev=1004006&r1=1004005&r2=1004006&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/deploy/WebXml.java (original)
+++ tomcat/trunk/java/org/apache/catalina/deploy/WebXml.java Sun Oct  3 17:27:42 2010
@@ -278,13 +278,37 @@ public class WebXml {
     public Map<String,FilterDef> getFilters() { return filters; }
     
     // filter-mapping
-    private Set<FilterMap> filterMaps = new LinkedHashSet<FilterMap>();
-    private Set<String> filterMappingNames = new HashSet<String>();
+    private Map<String,FilterMap> filterMaps =
+        new LinkedHashMap<String,FilterMap>();
     public void addFilterMapping(FilterMap filterMap) {
-        filterMaps.add(filterMap);
-        filterMappingNames.add(filterMap.getFilterName());
+        FilterMap fm = filterMaps.get(filterMap.getFilterName());
+        if (fm == null) {
+            filterMaps.put(filterMap.getFilterName(), filterMap);
+        } else {
+            for (String dispatcher : filterMap.getDispatcherNames()) {
+                fm.setDispatcher(dispatcher);
+            }
+            if (!fm.getMatchAllServletNames()) {
+                if (filterMap.getMatchAllServletNames()) {
+                    fm.addServletName("*");
+                } else {
+                    for (String servletName : filterMap.getServletNames()) {
+                        fm.addServletName(servletName);
+                    }
+                }
+            }
+            if (!fm.getMatchAllUrlPatterns()) {
+                if (filterMap.getMatchAllUrlPatterns()) {
+                    fm.addURLPattern("*");
+                } else {
+                    for (String urlPattern : filterMap.getURLPatterns()) {
+                        fm.addURLPattern(urlPattern);
+                    }
+                }
+            }
+        }
     }
-    public Set<FilterMap> getFilterMappings() { return filterMaps; }
+    public Map<String,FilterMap> getFilterMappings() { return filterMaps; }
     
     // listener
     // TODO: description (multiple with language) is ignored
@@ -627,7 +651,7 @@ public class WebXml {
         }
         sb.append('\n');
 
-        for (FilterMap filterMap : filterMaps) {
+        for (FilterMap filterMap : filterMaps.values()) {
             sb.append("  <filter-mapping>\n");
             appendElement(sb, INDENT4, "filter-name",
                     filterMap.getFilterName());
@@ -1176,7 +1200,7 @@ public class WebXml {
             }
             context.addFilterDef(filter);
         }
-        for (FilterMap filterMap : filterMaps) {
+        for (FilterMap filterMap : filterMaps.values()) {
             context.addFilterMap(filterMap);
         }
         for (JspPropertyGroup jspPropertyGroup : jspPropertyGroups) {
@@ -1418,17 +1442,16 @@ public class WebXml {
         // main web.xml override those in fragments and those in fragments
         // override mappings in annotations
         for (WebXml fragment : fragments) {
-            Iterator<FilterMap> iterFilterMaps =
-                fragment.getFilterMappings().iterator();
+            Iterator<String> iterFilterMaps =
+                fragment.getFilterMappings().keySet().iterator();
             while (iterFilterMaps.hasNext()) {
-                FilterMap filterMap = iterFilterMaps.next();
-                if (filterMappingNames.contains(filterMap.getFilterName())) {
+                if (filterMaps.containsKey(iterFilterMaps.next())) {
                     iterFilterMaps.remove();
                 }
             }
         }
         for (WebXml fragment : fragments) {
-            for (FilterMap filterMap : fragment.getFilterMappings()) {
+            for (FilterMap filterMap : fragment.getFilterMappings().values()) {
                 // Additive
                 addFilterMapping(filterMap);
             }

Modified: tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java?rev=1004006&r1=1004005&r2=1004006&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java (original)
+++ tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java Sun Oct  3 17:27:42 2010
@@ -33,6 +33,7 @@ import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLConnection;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -2192,7 +2193,7 @@ public class ContextConfig
             fragment.addFilterMapping(filterMap);
         }
         if (urlPatternsSet || dispatchTypesSet) {
-            Set<FilterMap> fmap = fragment.getFilterMappings();
+            Collection<FilterMap> fmap = fragment.getFilterMappings().values();
             FilterMap descMap = null;
             for (FilterMap map : fmap) {
                 if (filterName.equals(map.getFilterName())) {

Modified: tomcat/trunk/test/org/apache/catalina/core/TestStandardContext.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/core/TestStandardContext.java?rev=1004006&r1=1004005&r2=1004006&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/core/TestStandardContext.java (original)
+++ tomcat/trunk/test/org/apache/catalina/core/TestStandardContext.java Sun Oct  3 17:27:42
2010
@@ -26,6 +26,9 @@ import javax.servlet.FilterConfig;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 
 import org.apache.catalina.Context;
 import org.apache.catalina.deploy.FilterDef;
@@ -33,6 +36,7 @@ import org.apache.catalina.deploy.Filter
 import org.apache.catalina.startup.SimpleHttpClient;
 import org.apache.catalina.startup.Tomcat;
 import org.apache.catalina.startup.TomcatBaseTest;
+import org.apache.tomcat.util.buf.ByteChunk;
 
 public class TestStandardContext extends TomcatBaseTest {
 
@@ -109,4 +113,69 @@ public class TestStandardContext extends
         }
         
     }
+
+
+    public void testBug49922() throws Exception {
+        
+        // Set up a container
+        Tomcat tomcat = getTomcatInstance();
+        
+        // Must have a real docBase - just use temp
+        // Use the normal Tomcat ROOT context
+        File root = new File("test/webapp-3.0");
+        tomcat.addWebapp("", root.getAbsolutePath());
+        
+        tomcat.start();
+
+        // Check path mapping works
+        ByteChunk result = getUrl("http://localhost:" + getPort() +
+        "/bug49922/foo");
+        // Filter should only have been called once
+        assertEquals("Filter", result.toString());
+
+        // Check extension mapping works
+        result = getUrl("http://localhost:" + getPort() +
+        "/foo.do");
+        // Filter should only have been called once
+        assertEquals("Filter", result.toString());
+
+        result = getUrl("http://localhost:" + getPort() +
+                "/bug49922/index.do");
+        // Filter should only have been called once
+        assertEquals("Filter", result.toString());
+    }
+
+    
+    public static final class Bug49922Filter implements Filter {
+        
+        @Override
+        public void destroy() {
+            // NOOP
+        }
+
+        @Override
+        public void doFilter(ServletRequest request, ServletResponse response,
+                FilterChain chain) throws IOException, ServletException {
+            response.setContentType("text/plain");
+            response.getWriter().print("Filter");
+            chain.doFilter(request, response);
+        }
+
+        @Override
+        public void init(FilterConfig filterConfig) throws ServletException {
+            // NOOP
+        }
+    }
+    
+    public static final class Bug49922Servlet extends HttpServlet {
+
+        private static final long serialVersionUID = 1L;
+
+        @Override
+        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
+                throws ServletException, IOException {
+            // NOOP
+        }
+        
+    }
 }

Modified: tomcat/trunk/test/org/apache/catalina/startup/TestContextConfigAnnotation.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/TestContextConfigAnnotation.java?rev=1004006&r1=1004005&r2=1004006&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/startup/TestContextConfigAnnotation.java (original)
+++ tomcat/trunk/test/org/apache/catalina/startup/TestContextConfigAnnotation.java Sun Oct
 3 17:27:42 2010
@@ -18,7 +18,7 @@ package org.apache.catalina.startup;
 
 import java.io.File;
 import java.net.URL;
-import java.util.Set;
+import java.util.Collection;
 
 import javax.servlet.DispatcherType;
 
@@ -201,7 +201,8 @@ public class TestContextConfigAnnotation
         assertNotNull(fdef);
         assertEquals(filterDef,fdef);
         assertEquals("tomcat",fdef.getParameterMap().get("message"));
-        Set<FilterMap> filterMappings = webxml.getFilterMappings();
+        Collection<FilterMap> filterMappings =
+            webxml.getFilterMappings().values();
         assertTrue(filterMappings.contains(filterMap));
         // annotation mapping not added s. Servlet Spec 3.0 (Nov 2009)
         // 8.2.3.3.vi page 81

Modified: tomcat/trunk/test/webapp-3.0/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/webapp-3.0/WEB-INF/web.xml?rev=1004006&r1=1004005&r2=1004006&view=diff
==============================================================================
--- tomcat/trunk/test/webapp-3.0/WEB-INF/web.xml (original)
+++ tomcat/trunk/test/webapp-3.0/WEB-INF/web.xml Sun Oct  3 17:27:42 2010
@@ -27,6 +27,37 @@
      Used as part of the Tomcat unit tests when a full web application is
      required.
   </description>
+  
+  <!--  Bug 49922 -->
+  <filter>
+    <filter-name>Bug49922</filter-name>
+    <filter-class>
+      org.apache.catalina.core.TestStandardContext$Bug49922Filter
+    </filter-class>
+  </filter>
+  <filter-mapping>
+    <filter-name>Bug49922</filter-name>
+    <url-pattern>/bug49922/*</url-pattern>
+  </filter-mapping>
+  <filter-mapping>
+    <filter-name>Bug49922</filter-name>
+    <url-pattern>*.do</url-pattern>
+  </filter-mapping>
+  <servlet>
+    <servlet-name>Bug49922</servlet-name>
+    <servlet-class>
+      org.apache.catalina.core.TestStandardContext$Bug49922Servlet
+    </servlet-class>
+  </servlet>
+  <servlet-mapping>
+    <servlet-name>Bug49922</servlet-name>
+    <url-pattern>/bug49922/*</url-pattern>
+  </servlet-mapping>
+  <servlet-mapping>
+    <servlet-name>Bug49922</servlet-name>
+    <url-pattern>*.do</url-pattern>
+  </servlet-mapping>
+  
   <jsp-config>
     <jsp-property-group>
       <default-content-type>text/plain</default-content-type>



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


Mime
View raw message