Author: markt Date: Mon Jan 7 19:08:58 2013 New Revision: 1429969 URL: http://svn.apache.org/viewvc?rev=1429969&view=rev Log: Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=54284 As per clarification from the Servlet EG, anonymous Filters and Servlets are not permitted. Patch by Violeta Georgieva.
Added: tomcat/trunk/test/org/apache/catalina/deploy/TestFilterDef.java (with props) tomcat/trunk/test/org/apache/catalina/deploy/TestServletDef.java (with props) Modified: tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties tomcat/trunk/java/org/apache/catalina/deploy/FilterDef.java tomcat/trunk/java/org/apache/catalina/deploy/LocalStrings.properties tomcat/trunk/java/org/apache/catalina/deploy/ServletDef.java tomcat/trunk/test/org/apache/catalina/core/TestApplicationContext.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=1429969&r1=1429968&r2=1429969&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java (original) +++ tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java Mon Jan 7 19:08:58 2013 @@ -884,6 +884,11 @@ public class ApplicationContext private FilterRegistration.Dynamic addFilter(String filterName, String filterClass, Filter filter) throws IllegalStateException { + if (filterName == null || filterName.equals("")) { + throw new IllegalArgumentException(sm.getString( + "applicationContext.invalidFilterName", filterName)); + } + if (!context.getState().equals(LifecycleState.STARTING_PREP)) { //TODO Spec breaking enhancement to ignore this restriction throw new IllegalStateException( @@ -1023,6 +1028,11 @@ public class ApplicationContext private ServletRegistration.Dynamic addServlet(String servletName, String servletClass, Servlet servlet) throws IllegalStateException { + if (servletName == null || servletName.equals("")) { + throw new IllegalArgumentException(sm.getString( + "applicationContext.invalidServletName", servletName)); + } + if (!context.getState().equals(LifecycleState.STARTING_PREP)) { //TODO Spec breaking enhancement to ignore this restriction throw new IllegalStateException( 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=1429969&r1=1429968&r2=1429969&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties (original) +++ tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties Mon Jan 7 19:08:58 2013 @@ -21,6 +21,8 @@ applicationContext.addListener.ise=Liste applicationContext.addRole.ise=Roles can not be added to context {0} as the context has been initialised applicationContext.addServlet.ise=Servlets can not be added to context {0} as the context has been initialised applicationContext.attributeEvent=Exception thrown by attributes event listener +applicationContext.invalidFilterName=Unable to add filter definition due to invalid filter name [{0}]. +applicationContext.invalidServletName=Unable to add servlet definition due to invalid servlet name [{0}]. applicationContext.mapping.error=Error during mapping applicationContext.requestDispatcher.iae=Path {0} does not start with a "/" character applicationContext.resourcePaths.iae=Path {0} does not start with a "/" character Modified: tomcat/trunk/java/org/apache/catalina/deploy/FilterDef.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/deploy/FilterDef.java?rev=1429969&r1=1429968&r2=1429969&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/deploy/FilterDef.java (original) +++ tomcat/trunk/java/org/apache/catalina/deploy/FilterDef.java Mon Jan 7 19:08:58 2013 @@ -25,6 +25,8 @@ import java.util.Map; import javax.servlet.Filter; +import org.apache.tomcat.util.res.StringManager; + /** * Representation of a filter definition for a web application, as represented @@ -38,6 +40,9 @@ public class FilterDef implements Serial private static final long serialVersionUID = 1L; + private static final StringManager sm = + StringManager.getManager(Constants.Package); + // ------------------------------------------------------------- Properties @@ -108,6 +113,10 @@ public class FilterDef implements Serial } public void setFilterName(String filterName) { + if (filterName == null || filterName.equals("")) { + throw new IllegalArgumentException( + sm.getString("filterDef.invalidFilterName", filterName)); + } this.filterName = filterName; } Modified: tomcat/trunk/java/org/apache/catalina/deploy/LocalStrings.properties URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/deploy/LocalStrings.properties?rev=1429969&r1=1429968&r2=1429969&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/deploy/LocalStrings.properties (original) +++ tomcat/trunk/java/org/apache/catalina/deploy/LocalStrings.properties Mon Jan 7 19:08:58 2013 @@ -12,6 +12,9 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +filterDef.invalidFilterName=Invalid <filter-name> [{0}] in filter definition. + +servletDef.invalidServletName=Invalid <servlet-name> [{0}] in servlet definition. webXml.duplicateEnvEntry=Duplicate env-entry name [{0}] webXml.duplicateFilter=Duplicate filter name [{0}] Modified: tomcat/trunk/java/org/apache/catalina/deploy/ServletDef.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/deploy/ServletDef.java?rev=1429969&r1=1429968&r2=1429969&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/deploy/ServletDef.java (original) +++ tomcat/trunk/java/org/apache/catalina/deploy/ServletDef.java Mon Jan 7 19:08:58 2013 @@ -25,6 +25,8 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import org.apache.tomcat.util.res.StringManager; + /** * Representation of a servlet definition for a web application, as represented @@ -35,6 +37,9 @@ public class ServletDef implements Seria private static final long serialVersionUID = 1L; + private static final StringManager sm = + StringManager.getManager(Constants.Package); + // ------------------------------------------------------------- Properties @@ -104,6 +109,10 @@ public class ServletDef implements Seria } public void setServletName(String servletName) { + if (servletName == null || servletName.equals("")) { + throw new IllegalArgumentException( + sm.getString("servletDef.invalidServletName", servletName)); + } this.servletName = servletName; } Modified: tomcat/trunk/test/org/apache/catalina/core/TestApplicationContext.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/core/TestApplicationContext.java?rev=1429969&r1=1429968&r2=1429969&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/catalina/core/TestApplicationContext.java (original) +++ tomcat/trunk/test/org/apache/catalina/core/TestApplicationContext.java Mon Jan 7 19:08:58 2013 @@ -18,6 +18,9 @@ package org.apache.catalina.core; import java.io.File; +import javax.servlet.Filter; +import javax.servlet.Servlet; +import javax.servlet.ServletContext; import javax.servlet.http.HttpServletResponse; import org.junit.Assert; @@ -69,4 +72,40 @@ public class TestApplicationContext exte Assert.assertEquals(HttpServletResponse.SC_OK, rc); Assert.assertTrue(res.toString().contains("<p>OK</p>")); } + + + @Test(expected = IllegalArgumentException.class) + public void testAddFilterWithFilterNameNull() { + getServletContext().addFilter(null, (Filter) null); + } + + + @Test(expected = IllegalArgumentException.class) + public void testAddFilterWithFilterNameEmptyString() { + getServletContext().addFilter("", (Filter) null); + } + + + @Test(expected = IllegalArgumentException.class) + public void testAddServletWithServletNameNull() { + getServletContext().addServlet(null, (Servlet) null); + } + + + @Test(expected = IllegalArgumentException.class) + public void testAddServletWithServletNameEmptyString() { + getServletContext().addServlet("", (Servlet) null); + } + + + private ServletContext getServletContext() { + Tomcat tomcat = getTomcatInstance(); + + File appDir = new File("test/webapp-3.0"); + // app dir is relative to server home + StandardContext standardContext = (StandardContext) tomcat.addWebapp( + null, "/test", appDir.getAbsolutePath()); + + return standardContext.getServletContext(); + } } Added: tomcat/trunk/test/org/apache/catalina/deploy/TestFilterDef.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/deploy/TestFilterDef.java?rev=1429969&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/catalina/deploy/TestFilterDef.java (added) +++ tomcat/trunk/test/org/apache/catalina/deploy/TestFilterDef.java Mon Jan 7 19:08:58 2013 @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.catalina.deploy; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Test case for {@link FilterDef}. + */ +public class TestFilterDef { + + @Test(expected = IllegalArgumentException.class) + public void testSetFilterNameNull() { + new FilterDef().setFilterName(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testSetFilterNameEmptyString() { + new FilterDef().setFilterName(""); + } + + @Test + public void testSetFilterName() { + FilterDef filterDef = new FilterDef(); + filterDef.setFilterName("test"); + Assert.assertEquals("'test' is expected as filter name", + "test", filterDef.getFilterName()); + } + +} \ No newline at end of file Propchange: tomcat/trunk/test/org/apache/catalina/deploy/TestFilterDef.java ------------------------------------------------------------------------------ svn:eol-style = native Added: tomcat/trunk/test/org/apache/catalina/deploy/TestServletDef.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/deploy/TestServletDef.java?rev=1429969&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/catalina/deploy/TestServletDef.java (added) +++ tomcat/trunk/test/org/apache/catalina/deploy/TestServletDef.java Mon Jan 7 19:08:58 2013 @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.catalina.deploy; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Test case for {@link ServletDef} + */ +public class TestServletDef { + + @Test(expected = IllegalArgumentException.class) + public void testSetServletNameNull() { + new ServletDef().setServletName(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testSetServletNameEmptyString() { + new ServletDef().setServletName(""); + } + + @Test + public void testSetServletName() { + ServletDef servletDef = new ServletDef(); + servletDef.setServletName("test"); + Assert.assertEquals("'test' is expected as servlet name", + "test", servletDef.getServletName()); + } + +} \ No newline at end of file Propchange: tomcat/trunk/test/org/apache/catalina/deploy/TestServletDef.java ------------------------------------------------------------------------------ svn:eol-style = native --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org