Author: pete
Date: Thu Mar 10 00:54:07 2011
New Revision: 1080076

URL: http://svn.apache.org/viewvc?rev=1080076&view=rev
Log:
WICKET-3518: web.xml can contain more than one filter or servlet mapping for a 
filter or servlet. fixed WebXmlFile class to honor that fact. also added some 
checking to give the user an error in case he tries to setup more than one 
wicket filter path since we do not support that (and probably never will).

Modified:
    
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/WicketFilter.java
    
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/protocol/http/WicketFilterTest.java
    
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/file/WebXmlFile.java
    
wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/file/WebXmlFileTest.java

Modified: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/WicketFilter.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/WicketFilter.java?rev=1080076&r1=1080075&r2=1080076&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/WicketFilter.java
 (original)
+++ 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/WicketFilter.java
 Thu Mar 10 00:54:07 2011
@@ -388,7 +388,7 @@ public class WicketFilter implements Fil
        protected String getFilterPathFromWebXml(final boolean isServlet,
                final FilterConfig filterConfig)
        {
-               return new WebXmlFile().getFilterPath(isServlet, filterConfig);
+               return new WebXmlFile().getUniqueFilterPath(isServlet, 
filterConfig);
        }
 
        /**

Modified: 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/protocol/http/WicketFilterTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/org/apache/wicket/protocol/http/WicketFilterTest.java?rev=1080076&r1=1080075&r2=1080076&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/protocol/http/WicketFilterTest.java
 (original)
+++ 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/protocol/http/WicketFilterTest.java
 Thu Mar 10 00:54:07 2011
@@ -139,11 +139,11 @@ public class WicketFilterTest extends Te
                request.addDateHeader("If-Modified-Since", 
nextWeek.getTimeInMillis());
        }
 
-       private String getFilterPath(String string, InputStream in)
+       private String getFilterPath(String filterName, InputStream in)
        {
                try
                {
-                       return new WebXmlFile().getFilterPath(false, string, 
in);
+                       return new WebXmlFile().getUniqueFilterPath(false, 
filterName, in);
                }
                catch (ParserConfigurationException ex)
                {

Modified: 
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/file/WebXmlFile.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/file/WebXmlFile.java?rev=1080076&r1=1080075&r2=1080076&view=diff
==============================================================================
--- 
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/file/WebXmlFile.java
 (original)
+++ 
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/file/WebXmlFile.java
 Thu Mar 10 00:54:07 2011
@@ -18,6 +18,8 @@ package org.apache.wicket.util.file;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.HashSet;
+import java.util.Set;
 
 import javax.servlet.FilterConfig;
 import javax.servlet.ServletContext;
@@ -35,7 +37,7 @@ import org.xml.sax.SAXException;
 
 /**
  * A utility class providing helper methods in dealing with web.xml
- * 
+ *
  * @author jcompagner
  * @author Juergen Donnerstag
  */
@@ -51,29 +53,30 @@ public class WebXmlFile
        }
 
        /**
-        * Gets Wicket filter path via FilterConfig
-        * 
+        * Gets unique Wicket filter path via FilterConfig
+        *
         * @param isServlet
         *            true if Servlet, false if Filter
         * @param filterConfig
         * @return Filter path retrieved from "url-pattern". Null if not found 
or error occured
         */
-       public final String getFilterPath(final boolean isServlet, final 
FilterConfig filterConfig)
+       public final String getUniqueFilterPath(final boolean isServlet, final 
FilterConfig filterConfig)
        {
-               return getFilterPath(isServlet, 
filterConfig.getServletContext(),
-                       filterConfig.getFilterName());
+               String filterName = filterConfig.getFilterName();
+               Set<String> paths = getFilterPath(isServlet, 
filterConfig.getServletContext(), filterName);
+               return uniquePath(paths, isServlet, filterName);
        }
 
        /**
         * Gets Wicket filter path via ServletContext and the filter name
-        * 
+        *
         * @param isServlet
         *            true if Servlet, false if Filter
         * @param servletContext
         * @param filterName
         * @return Filter path retrieved from "url-pattern". Null if not found 
or error occured
         */
-       public final String getFilterPath(final boolean isServlet, final 
ServletContext servletContext,
+       public final Set<String> getFilterPath(final boolean isServlet, final 
ServletContext servletContext,
                final String filterName)
        {
                InputStream is = 
servletContext.getResourceAsStream("/WEB-INF/web.xml");
@@ -106,11 +109,62 @@ public class WebXmlFile
        }
 
        /**
+        * Returns unique mapping for filter / servlet.
+        *
+        * @param isServlet
+        * @param filterName
+        * @param is
+        * @return
+        * @throws ParserConfigurationException
+        * @throws SAXException
+        * @throws IOException
+        */
+
+       public final String getUniqueFilterPath(final boolean isServlet, final 
String filterName,
+               final InputStream is) throws ParserConfigurationException, 
SAXException, IOException
+       {
+               return uniquePath(getFilterPath(isServlet, filterName, is), 
isServlet, filterName);
+       }
+
+       /**
+        * return unique path from set of paths
+        *
+        *
+        * @param paths <code>null</code> if set was empty, unique path if set 
was of length == 1
+        * @param isServlet
+        *@param filterName  @return
+        * @throws RuntimeException in case length > 1
+        */
+       private String uniquePath(Set<String> paths, boolean isServlet, String 
filterName)
+       {
+               if(paths.size() > 1)
+               {
+                       StringBuilder err = new StringBuilder();
+                       err.append("web.xml: expected one ");
+                       err.append(isServlet ? "servlet" : "filter");
+                       err.append(" path for [");
+                       err.append(filterName);
+                       err.append("] but found multiple:");
+
+                       for (String path : paths)
+                       {
+                               err.append(" [" + path + "]");
+                       }
+                       throw new RuntimeException(err.toString());
+               }
+               if(paths.size() == 1)
+               {
+                       return paths.iterator().next();
+               }
+               return null;
+       }
+
+       /**
         * Gets Wicket filter path via filter name and InputStream. The 
InputStream is assumed to be an
         * web.xml file.
         * <p>
         * A typical Wicket web.xml entry looks like:
-        * 
+        *
         * <pre>
         * <code>
         * &lt;filter&gt;
@@ -121,7 +175,7 @@ public class WebXmlFile
         *     
&lt;param-value&gt;org.apache.wicket.examples.helloworld.HelloWorldApplication&lt;/param-value&gt;
         *   &lt;/init-param&gt;
         * &lt;/filter&gt;
-        * 
+        *
         * &lt;filter-mapping&gt;
         *   &lt;filter-name&gt;HelloWorldApplication&lt;/filter-name&gt;
         *   &lt;url-pattern&gt;/helloworld/*&lt;/url-pattern&gt;
@@ -130,7 +184,7 @@ public class WebXmlFile
         * &lt;/filter-mapping&gt;
         * </code>
         * </pre>
-        * 
+        *
         * @param isServlet
         *            true if Servlet, false if Filter
         * @param filterName
@@ -141,7 +195,7 @@ public class WebXmlFile
         * @throws IOException
         * @throws SAXException
         */
-       public final String getFilterPath(final boolean isServlet, final String 
filterName,
+       public final Set<String> getFilterPath(final boolean isServlet, final 
String filterName,
                final InputStream is) throws ParserConfigurationException, 
SAXException, IOException
        {
                DocumentBuilderFactory factory = 
DocumentBuilderFactory.newInstance();
@@ -154,42 +208,53 @@ public class WebXmlFile
                String mapping = tag + "-mapping";
                String name = tag + "-name";
 
-               String urlPattern = getFilterPath(filterName, mapping, name, 
document.getChildNodes());
-               if (urlPattern == null)
+               Set<String> urlPatterns = getFilterPaths(filterName, mapping, 
name, document.getChildNodes());
+
+               if (urlPatterns.size() == 0)
+               {
+                       log.warn("web.xml: No url-pattern found for " + tag + " 
with name " + filterName);
+               }
+
+               if(log.isInfoEnabled())
                {
-                       if (log.isWarnEnabled())
+                       StringBuilder msg = new StringBuilder();
+                       msg.append("web.xml: url mapping found for " + tag + " 
with name " + filterName + ':');
+                       for (String urlPattern : urlPatterns)
                        {
-                               log.warn("web.xml: No url-pattern found for " + 
tag + " with name " + filterName);
+                               msg.append(" [");
+                               msg.append(urlPattern);
+                               msg.append(']');
                        }
-                       return null;
+                       log.info(msg.toString());
                }
-               else if (log.isInfoEnabled())
+               Set<String> stripped = new HashSet<String>(urlPatterns.size());
+
+               for (String urlPattern : urlPatterns)
                {
-                       log.info("web.xml: found " + tag + " with name " + 
filterName + ". url-pattern=" +
-                               urlPattern);
+                       stripped.add(urlPattern.substring(1, 
urlPattern.length() - 1));
                }
-
-               // remove leading "/" and trailing "*"
-               return urlPattern.substring(1, urlPattern.length() - 1);
+               return stripped;
        }
 
        /**
         * Iterate through all children of 'node' and search for a node with 
name "filterName". Return
         * the value of node "url-pattern" if "filterName" was found.
-        * 
+        *
         * @param filterName
         * @param name
         * @param node
         * @return value of node "url-pattern"
         */
-       private String getFilterPath(final String filterName, final String 
name, final Node node)
+       private Set<String> getFilterPaths(final String filterName, final 
String name, final Node node)
        {
+               Set<String> paths = new HashSet<String>();
                String foundUrlPattern = null;
                String foundFilterName = null;
 
                for (int i = 0; i < node.getChildNodes().getLength(); ++i)
                {
                        Node n = node.getChildNodes().item(i);
+
                        if (name.equals(n.getNodeName()))
                        {
                                foundFilterName = n.getTextContent();
@@ -198,49 +263,48 @@ public class WebXmlFile
                        {
                                foundUrlPattern = n.getTextContent();
                        }
+                       if (foundFilterName != null)
+                       {
+                               foundFilterName = foundFilterName.trim();
+                       }
+                       if (filterName.equals(foundFilterName))
+                       {
+                               if (foundUrlPattern != null)
+                                       paths.add(foundUrlPattern.trim());
+                       }
                }
-
-               if (foundFilterName != null)
-               {
-                       foundFilterName = foundFilterName.trim();
-               }
-
-               if (filterName.equals(foundFilterName))
-               {
-                       return (foundUrlPattern != null) ? 
foundUrlPattern.trim() : null;
-               }
-               else
-               {
-                       return null;
-               }
+               return paths;
        }
 
        /**
         * Find a node with name 'mapping' within 'nodeList' and if found 
continue to search amongst its
         * children for a node with 'filterName' and "url-pattern'
-        * 
+        *
+        *
         * @param filterName
         * @param mapping
         * @param name
         * @param nodeList
         * @return The value assigned to node "url-pattern"
         */
-       private String getFilterPath(final String filterName, final String 
mapping, final String name,
+       private Set<String> getFilterPaths(final String filterName, final 
String mapping, final String name,
                final NodeList nodeList)
        {
-               String path = null;
-               for (int i = 0; (i < nodeList.getLength()) && (path == null); 
++i)
+               Set<String> paths = new HashSet<String>(1);
+
+               for (int i = 0; i < nodeList.getLength(); ++i)
                {
                        Node node = nodeList.item(i);
+
                        if (mapping.equals(node.getNodeName()))
                        {
-                               path = getFilterPath(filterName, name, node);
+                               paths.addAll(getFilterPaths(filterName, name, 
node));
                        }
                        else
                        {
-                               path = getFilterPath(filterName, mapping, name, 
node.getChildNodes());
+                               paths.addAll(getFilterPaths(filterName, 
mapping, name, node.getChildNodes()));
                        }
                }
-               return path;
+               return paths;
        }
 }

Modified: 
wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/file/WebXmlFileTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/file/WebXmlFileTest.java?rev=1080076&r1=1080075&r2=1080076&view=diff
==============================================================================
--- 
wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/file/WebXmlFileTest.java
 (original)
+++ 
wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/file/WebXmlFileTest.java
 Thu Mar 10 00:54:07 2011
@@ -66,21 +66,21 @@ public class WebXmlFileTest extends Test
                String tag = servlet ? "servlet" : "filter";
 
                String webxml = getWebXml(tag, "/*");
-               String path = new WebXmlFile().getFilterPath(servlet, 
"HelloWorldApplication",
-                       new ByteArrayInputStream(webxml.toString().getBytes()));
+               String path = new WebXmlFile().getUniqueFilterPath(servlet, 
"HelloWorldApplication",
+                       new ByteArrayInputStream(webxml.getBytes()));
                assertEquals("", path);
 
                webxml = getWebXml(tag, "/test/*");
-               path = new WebXmlFile().getFilterPath(servlet, 
"HelloWorldApplication",
-                       new ByteArrayInputStream(webxml.toString().getBytes()));
+               path = new WebXmlFile().getUniqueFilterPath(servlet, 
"HelloWorldApplication",
+                       new ByteArrayInputStream(webxml.getBytes()));
                assertEquals("test/", path);
 
-               path = new WebXmlFile().getFilterPath(servlet, "xxx", new 
ByteArrayInputStream(
-                       webxml.toString().getBytes()));
+               path = new WebXmlFile().getUniqueFilterPath(servlet, "xxx", new 
ByteArrayInputStream(
+                       webxml.getBytes()));
                assertNull(path);
 
-               path = new WebXmlFile().getFilterPath(!servlet, 
"HelloWorldApplication",
-                       new ByteArrayInputStream(webxml.toString().getBytes()));
+               path = new WebXmlFile().getUniqueFilterPath(!servlet, 
"HelloWorldApplication",
+                       new ByteArrayInputStream(webxml.getBytes()));
                assertNull(path);
        }
 


Reply via email to