Author: jdonnerstag
Date: Fri Sep 10 09:18:30 2010
New Revision: 995709

URL: http://svn.apache.org/viewvc?rev=995709&view=rev
Log:
fixed WICKET-3039 and added test case: WicketServlet failes to initialise with 
NullPointerException in WebXmlFile.getFilterPath()
Issue: WICKET-3029

Modified:
    wicket/trunk/wicket-examples/src/main/webapp/WEB-INF/web.xml
    
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
    
wicket/trunk/wicket/src/main/disabled/org/apache/wicket/protocol/http/MockWebApplication.java
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/ReloadingWicketFilter.java
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/WicketFilter.java
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/WicketServlet.java
    
wicket/trunk/wicket/src/test/java/org/apache/wicket/protocol/http/WicketFilterTest.java

Modified: wicket/trunk/wicket-examples/src/main/webapp/WEB-INF/web.xml
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-examples/src/main/webapp/WEB-INF/web.xml?rev=995709&r1=995708&r2=995709&view=diff
==============================================================================
--- wicket/trunk/wicket-examples/src/main/webapp/WEB-INF/web.xml (original)
+++ wicket/trunk/wicket-examples/src/main/webapp/WEB-INF/web.xml Fri Sep 10 
09:18:30 2010
@@ -707,18 +707,18 @@
                  
<param-value>org.apache.wicket.examples.events.EventsApplication</param-value>
                </init-param>
        </filter>
+       
        <filter-mapping>
                <filter-name>EventsApplication</filter-name>
         <url-pattern>/events/*</url-pattern>
        </filter-mapping>
 
-
-
-
+       <!-- LISTENER -->
        <listener>
                
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
 
+       <!-- SERVLET -->
        <servlet>
                <servlet-name>HelloWorldServlet</servlet-name>
                
<servlet-class>org.apache.wicket.examples.HelloWorldServlet</servlet-class>
@@ -729,6 +729,22 @@
                <url-pattern>/helloworldservlet/*</url-pattern>
        </servlet-mapping>
 
+       <servlet>
+               <servlet-name>ServletTest</servlet-name>
+               
<servlet-class>org.apache.wicket.protocol.http.WicketServlet</servlet-class>
+               <init-param>
+               <param-name>applicationClassName</param-name>
+               
<param-value>org.apache.wicket.examples.helloworld.HelloWorldApplication</param-value>
+               </init-param>
+               <load-on-startup>0</load-on-startup>
+       </servlet>
+
+       <servlet-mapping>
+               <servlet-name>ServletTest</servlet-name>
+               <url-pattern>/servlettest/*</url-pattern>
+       </servlet-mapping>
+
+
        <session-config>
                <session-timeout>5</session-timeout>
        </session-config>

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=995709&r1=995708&r2=995709&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
 Fri Sep 10 09:18:30 2010
@@ -52,29 +52,35 @@ public class WebXmlFile
        /**
         * Gets 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 FilterConfig filterConfig)
+       public final String getFilterPath(final boolean isServlet, final 
FilterConfig filterConfig)
        {
-               return getFilterPath(filterConfig.getServletContext(), 
filterConfig.getFilterName());
+               return getFilterPath(isServlet, 
filterConfig.getServletContext(),
+                       filterConfig.getFilterName());
        }
 
        /**
         * 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 ServletContext servletContext, 
final String filterName)
+       public final String getFilterPath(final boolean isServlet, final 
ServletContext servletContext,
+               final String filterName)
        {
                InputStream is = 
servletContext.getResourceAsStream("/WEB-INF/web.xml");
                if (is != null)
                {
                        try
                        {
-                               return getFilterPath(filterName, is);
+                               return getFilterPath(isServlet, filterName, is);
                        }
                        catch (ParserConfigurationException ex)
                        {
@@ -124,6 +130,8 @@ public class WebXmlFile
         * </code>
         * </pre>
         * 
+        * @param isServlet
+        *            true if Servlet, false if Filter
         * @param filterName
         * @param is
         *            The web.xml file
@@ -132,17 +140,31 @@ public class WebXmlFile
         * @throws IOException
         * @throws SAXException
         */
-       public final String getFilterPath(final String filterName, final 
InputStream is)
-               throws ParserConfigurationException, SAXException, IOException
+       public final String getFilterPath(final boolean isServlet, final String 
filterName,
+               final InputStream is) throws ParserConfigurationException, 
SAXException, IOException
        {
                DocumentBuilderFactory factory = 
DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                Document document = builder.parse(is);
 
-               String mapping = "filter-mapping";
-               String name = "filter-name";
+               String tag = (isServlet ? "servlet" : "filter");
+               String mapping = tag + "-mapping";
+               String name = tag + "-name";
 
                String urlPattern = getFilterPath(filterName, mapping, name, 
document.getChildNodes());
+               if (urlPattern == null)
+               {
+                       if (log.isWarnEnabled())
+                       {
+                               log.warn("web.xml: No url-pattern found for " + 
tag + " with name " + filterName);
+                       }
+                       return null;
+               }
+               else if (log.isInfoEnabled())
+               {
+                       log.info("web.xml: found " + tag + " with name " + 
filterName + ". url-pattern=" +
+                               urlPattern);
+               }
 
                // remove leading "/" and trailing "*"
                return urlPattern.substring(1, urlPattern.length() - 1);

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=995709&r1=995708&r2=995709&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
 Fri Sep 10 09:18:30 2010
@@ -31,29 +31,87 @@ import org.xml.sax.SAXException;
  */
 public class WebXmlFileTest extends TestCase
 {
-       public void test_1() throws ParserConfigurationException, SAXException, 
IOException
+       /**
+        * 
+        * @throws ParserConfigurationException
+        * @throws SAXException
+        * @throws IOException
+        */
+       public void test_filter() throws ParserConfigurationException, 
SAXException, IOException
+       {
+               filterOrServlet(false);
+       }
+
+       /**
+        * 
+        * @throws ParserConfigurationException
+        * @throws SAXException
+        * @throws IOException
+        */
+       public void test_servlet() throws ParserConfigurationException, 
SAXException, IOException
+       {
+               filterOrServlet(true);
+       }
+
+       /**
+        * 
+        * @param servlet
+        * @throws ParserConfigurationException
+        * @throws SAXException
+        * @throws IOException
+        */
+       public void filterOrServlet(boolean servlet) throws 
ParserConfigurationException, SAXException,
+               IOException
+       {
+               String tag = servlet ? "servlet" : "filter";
+
+               String webxml = getWebXml(tag, "/*");
+               String path = new WebXmlFile().getFilterPath(servlet, 
"HelloWorldApplication",
+                       new ByteArrayInputStream(webxml.toString().getBytes()));
+               assertEquals("", path);
+
+               webxml = getWebXml(tag, "/test/*");
+               path = new WebXmlFile().getFilterPath(servlet, 
"HelloWorldApplication",
+                       new ByteArrayInputStream(webxml.toString().getBytes()));
+               assertEquals("test/", path);
+
+               path = new WebXmlFile().getFilterPath(servlet, "xxx", new 
ByteArrayInputStream(
+                       webxml.toString().getBytes()));
+               assertNull(path);
+
+               path = new WebXmlFile().getFilterPath(!servlet, 
"HelloWorldApplication",
+                       new ByteArrayInputStream(webxml.toString().getBytes()));
+               assertNull(path);
+       }
+
+       /**
+        * 
+        * @param filter
+        * @param url
+        * @return webxml
+        */
+       private String getWebXml(String filter, String url)
        {
                StringBuffer webxml = new StringBuffer();
                webxml.append("<web-app>");
-               webxml.append("<filter>");
-               webxml.append(" 
<filter-name>HelloWorldApplication</filter-name>");
-               webxml.append(" 
<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>");
+               webxml.append("<" + filter + ">");
+               webxml.append(" <" + filter + "-name>HelloWorldApplication</" + 
filter + "-name>");
+               webxml.append(" <" + filter + 
"-class>org.apache.wicket.protocol.http.WicketFilter</" +
+                       filter + "-class>");
                webxml.append(" <init-param>");
                webxml.append("  
<param-name>applicationClassName</param-name>");
                webxml.append("  
<param-value>org.apache.wicket.examples.helloworld.HelloWorldApplication</param-value>");
                webxml.append(" </init-param>");
-               webxml.append("</filter>");
+               webxml.append("</" + filter + ">");
                webxml.append("");
-               webxml.append("<filter-mapping>");
-               webxml.append(" 
<filter-name>HelloWorldApplication</filter-name>");
-               webxml.append(" <url-pattern>/*</url-pattern>");
+               webxml.append("<" + filter + "-mapping>");
+               webxml.append(" <" + filter + "-name>HelloWorldApplication</" + 
filter + "-name>");
+               webxml.append(" <url-pattern>" + url + "</url-pattern>");
                webxml.append(" <dispatcher>REQUEST</dispatcher>");
                webxml.append(" <dispatcher>INCLUDE</dispatcher>");
-               webxml.append("</filter-mapping>");
+               webxml.append("</" + filter + "-mapping>");
                webxml.append("</web-app>");
 
-               String path = new 
WebXmlFile().getFilterPath("HelloWorldApplication",
-                       new ByteArrayInputStream(webxml.toString().getBytes()));
-               assertEquals("", path);
+               return webxml.toString();
        }
 }

Modified: 
wicket/trunk/wicket/src/main/disabled/org/apache/wicket/protocol/http/MockWebApplication.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/disabled/org/apache/wicket/protocol/http/MockWebApplication.java?rev=995709&r1=995708&r2=995709&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/main/disabled/org/apache/wicket/protocol/http/MockWebApplication.java
 (original)
+++ 
wicket/trunk/wicket/src/main/disabled/org/apache/wicket/protocol/http/MockWebApplication.java
 Fri Sep 10 09:18:30 2010
@@ -37,6 +37,7 @@ import org.apache.wicket.Page;
 import org.apache.wicket.PageParameters;
 import org.apache.wicket.Session;
 import org.apache.wicket.markup.html.pages.ExceptionErrorPage;
+import org.apache.wicket.protocol.http.WicketFilter;
 import org.apache.wicket.protocol.http.request.WebErrorCodeResponseHandler;
 import org.apache.wicket.protocol.http.request.WebRequestCodingStrategy;
 import org.apache.wicket.request.target.coding.WebRequestEncoder;
@@ -172,7 +173,6 @@ public class MockWebApplication
                {
                        filter.init(new FilterConfig()
                        {
-
                                public ServletContext getServletContext()
                                {
                                        return context;
@@ -185,12 +185,6 @@ public class MockWebApplication
 
                                public String getInitParameter(String name)
                                {
-                                       if 
(name.equals(WicketFilter.FILTER_MAPPING_PARAM))
-                                       {
-                                               return 
WicketFilter.SERVLET_PATH_HOLDER;
-                                               // return "/" + 
MockWebApplication.this.getName() +
-                                               // "/*";
-                                       }
                                        return null;
                                }
 

Modified: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/ReloadingWicketFilter.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/ReloadingWicketFilter.java?rev=995709&r1=995708&r2=995709&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/ReloadingWicketFilter.java
 (original)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/ReloadingWicketFilter.java
 Fri Sep 10 09:18:30 2010
@@ -143,10 +143,11 @@ public class ReloadingWicketFilter exten
        }
 
        /**
-        * @see 
org.apache.wicket.protocol.http.WicketFilter#init(javax.servlet.FilterConfig)
+        * @see org.apache.wicket.protocol.http.WicketFilter#init(boolean, 
javax.servlet.FilterConfig)
         */
        @Override
-       public void init(final FilterConfig filterConfig) throws 
ServletException
+       public void init(final boolean isServlet, final FilterConfig 
filterConfig)
+               throws ServletException
        {
                reloadingClassLoader.setListener(new IChangeListener()
                {
@@ -172,6 +173,6 @@ public class ReloadingWicketFilter exten
                        }
                });
 
-               super.init(filterConfig);
+               super.init(isServlet, filterConfig);
        }
 }

Modified: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/WicketFilter.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/WicketFilter.java?rev=995709&r1=995708&r2=995709&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/WicketFilter.java
 (original)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/WicketFilter.java
 Fri Sep 10 09:18:30 2010
@@ -67,8 +67,6 @@ public class WicketFilter implements Fil
        /** The name of the context parameter that specifies application 
factory class */
        public static final String APP_FACT_PARAM = 
"applicationFactoryClassName";
 
-       static final String SERVLET_PATH_HOLDER = "<servlet>";
-
        // Wicket's Application object
        private WebApplication application;
 
@@ -245,9 +243,28 @@ public class WicketFilter implements Fil
        }
 
        /**
+        * If you do have a need to subclass, you may subclass {...@link 
#init(boolean, FilterConfig)}
+        * 
         * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
         */
-       public void init(final FilterConfig filterConfig) throws 
ServletException
+       public final void init(final FilterConfig filterConfig) throws 
ServletException
+       {
+               init(false, filterConfig);
+       }
+
+       /**
+        * Servlets and Filters are treated essentially the same with Wicket. 
This is the entry point
+        * for both of them.
+        * 
+        * @see #init(FilterConfig)
+        * 
+        * @param isServlet
+        *            True if Servlet, false of Filter
+        * @param filterConfig
+        * @throws ServletException
+        */
+       public void init(final boolean isServlet, final FilterConfig 
filterConfig)
+               throws ServletException
        {
                this.filterConfig = filterConfig;
 
@@ -259,8 +276,8 @@ public class WicketFilter implements Fil
                // Allow the filterPath to tbe preset via setFilterPath()
                if (filterPath == null)
                {
-                       filterPath = new 
WebXmlFile().getFilterPath(filterConfig);
-                       if (filterPath == null)
+                       filterPath = new WebXmlFile().getFilterPath(isServlet, 
filterConfig);
+                       if ((filterPath == null) && log.isInfoEnabled())
                        {
                                log.info("Unable to parse filter mapping 
web.xml for " +
                                        filterConfig.getFilterName() + ". " + 
"Configure with init-param " +

Modified: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/WicketServlet.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/WicketServlet.java?rev=995709&r1=995708&r2=995709&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/WicketServlet.java
 (original)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/WicketServlet.java
 Fri Sep 10 09:18:30 2010
@@ -162,7 +162,12 @@ public class WicketServlet extends HttpS
                }
        }
 
-       private static String getURL(HttpServletRequest httpServletRequest)
+       /**
+        * 
+        * @param httpServletRequest
+        * @return URL
+        */
+       private static String getURL(final HttpServletRequest 
httpServletRequest)
        {
                /*
                 * Servlet 2.3 specification :
@@ -183,8 +188,7 @@ public class WicketServlet extends HttpS
                        url += pathInfo;
                }
 
-               final String queryString = httpServletRequest.getQueryString();
-
+               String queryString = httpServletRequest.getQueryString();
                if (queryString != null)
                {
                        url += ("?" + queryString);
@@ -199,7 +203,13 @@ public class WicketServlet extends HttpS
                return url;
        }
 
-       private void fallback(HttpServletRequest request, HttpServletResponse 
response)
+       /**
+        * 
+        * @param request
+        * @param response
+        * @throws IOException
+        */
+       private void fallback(final HttpServletRequest request, final 
HttpServletResponse response)
                throws IOException
        {
                // The ServletWebRequest is created here to avoid code 
duplication. The getURL call doesn't
@@ -251,7 +261,7 @@ public class WicketServlet extends HttpS
        public void init() throws ServletException
        {
                wicketFilter = newWicketFilter();
-               wicketFilter.init(new FilterConfig()
+               wicketFilter.init(true, new FilterConfig()
                {
                        /**
                         * @see javax.servlet.FilterConfig#getServletContext()
@@ -273,12 +283,8 @@ public class WicketServlet extends HttpS
                        /**
                         * @see 
javax.servlet.FilterConfig#getInitParameter(java.lang.String)
                         */
-                       public String getInitParameter(String name)
+                       public String getInitParameter(final String name)
                        {
-                               if 
(WicketFilter.FILTER_MAPPING_PARAM.equals(name))
-                               {
-                                       return WicketFilter.SERVLET_PATH_HOLDER;
-                               }
                                return 
WicketServlet.this.getInitParameter(name);
                        }
 
@@ -309,5 +315,4 @@ public class WicketServlet extends HttpS
                wicketFilter.destroy();
                wicketFilter = null;
        }
-
 }

Modified: 
wicket/trunk/wicket/src/test/java/org/apache/wicket/protocol/http/WicketFilterTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/protocol/http/WicketFilterTest.java?rev=995709&r1=995708&r2=995709&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/test/java/org/apache/wicket/protocol/http/WicketFilterTest.java
 (original)
+++ 
wicket/trunk/wicket/src/test/java/org/apache/wicket/protocol/http/WicketFilterTest.java
 Fri Sep 10 09:18:30 2010
@@ -143,7 +143,7 @@ public class WicketFilterTest extends Te
        {
                try
                {
-                       return new WebXmlFile().getFilterPath(string, in);
+                       return new WebXmlFile().getFilterPath(false, string, 
in);
                }
                catch (ParserConfigurationException ex)
                {


Reply via email to