Author: markt Date: Sat Dec 31 22:29:36 2011 New Revision: 1226196 URL: http://svn.apache.org/viewvc?rev=1226196&view=rev Log: https://issues.apache.org/bugzilla/show_bug.cgi?id=52405 Servlet 3.0 Rev A updates Item 5 of 20 Tighter control of web.xml elements (name, ordering, absolute-ordering)
Added: tomcat/trunk/test/org/apache/catalina/startup/TestWebRuleSet.java tomcat/trunk/test/org/apache/catalina/startup/web-1ordering.xml tomcat/trunk/test/org/apache/catalina/startup/web-2ordering.xml tomcat/trunk/test/org/apache/catalina/startup/web-fragment-1name.xml tomcat/trunk/test/org/apache/catalina/startup/web-fragment-1ordering.xml tomcat/trunk/test/org/apache/catalina/startup/web-fragment-2name.xml tomcat/trunk/test/org/apache/catalina/startup/web-fragment-2ordering.xml Modified: tomcat/trunk/java/org/apache/catalina/startup/LocalStrings.properties tomcat/trunk/java/org/apache/catalina/startup/WebRuleSet.java Modified: tomcat/trunk/java/org/apache/catalina/startup/LocalStrings.properties URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/startup/LocalStrings.properties?rev=1226196&r1=1226195&r2=1226196&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/startup/LocalStrings.properties (original) +++ tomcat/trunk/java/org/apache/catalina/startup/LocalStrings.properties Sat Dec 31 22:29:36 2011 @@ -122,6 +122,9 @@ userConfig.error=Error deploying web app userConfig.start=UserConfig: Processing START userConfig.stop=UserConfig: Processing STOP webRuleSet.absoluteOrdering=<absolute-ordering> element not valid in web-fragment.xml and will be ignored +webRuleSet.absoluteOrderingCount=<absolute-ordering> element is limited to 1 occurrence +webRuleSet.nameCount=<name> element is limited to 1 occurrence webRuleSet.relativeOrdering=<ordering> element not valid in web.xml and will be ignored +webRuleSet.relativeOrderingCount=<ordering> element is limited to 1 occurrence xmlErrorHandler.error=Non-fatal error [{0}] reported processing [{1}]. xmlErrorHandler.warning=Warning [{0}] reported processing [{1}]. Modified: tomcat/trunk/java/org/apache/catalina/startup/WebRuleSet.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/startup/WebRuleSet.java?rev=1226196&r1=1226195&r2=1226196&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/startup/WebRuleSet.java (original) +++ tomcat/trunk/java/org/apache/catalina/startup/WebRuleSet.java Sat Dec 31 22:29:36 2011 @@ -92,6 +92,25 @@ public class WebRuleSet extends RuleSetB protected SetJspConfig jspConfig = new SetJspConfig(); + /** + * The <code>NameRule</code> rule used to parse the web.xml + */ + protected NameRule name = new NameRule(); + + + /** + * The <code>AbsoluteOrderingRule</code> rule used to parse the web.xml + */ + protected AbsoluteOrderingRule absoluteOrdering; + + + /** + * The <code>RelativeOrderingRule</code> rule used to parse the web.xml + */ + protected RelativeOrderingRule relativeOrdering; + + + // ------------------------------------------------------------ Constructor @@ -137,6 +156,8 @@ public class WebRuleSet extends RuleSetB fullPrefix = prefix + "web-app"; } + absoluteOrdering = new AbsoluteOrderingRule(fragment); + relativeOrdering = new RelativeOrderingRule(fragment); } // --------------------------------------------------------- Public Methods @@ -160,12 +181,13 @@ public class WebRuleSet extends RuleSetB digester.addRule(fullPrefix, new VersionRule()); + // Required for both fragments and non-fragments + digester.addRule(fullPrefix + "/absolute-ordering", absoluteOrdering); + digester.addRule(fullPrefix + "/ordering", relativeOrdering); + if (fragment) { // web-fragment.xml - digester.addCallMethod(fullPrefix + "/name", - "setName", 0); - digester.addRule(fullPrefix + "/absolute-ordering", - new AbsoluteOrderingRule()); + digester.addRule(fullPrefix + "/name", name); digester.addCallMethod(fullPrefix + "/ordering/after/name", "addAfterOrdering", 0); digester.addCallMethod(fullPrefix + "/ordering/after/others", @@ -176,8 +198,6 @@ public class WebRuleSet extends RuleSetB "addBeforeOrderingOthers"); } else { // web.xml - digester.addRule(fullPrefix + "/ordering", - new RelativeOrderingRule()); digester.addCallMethod(fullPrefix + "/absolute-ordering/name", "addAbsoluteOrdering", 0); digester.addCallMethod(fullPrefix + "/absolute-ordering/others", @@ -648,6 +668,9 @@ public class WebRuleSet extends RuleSetB jspConfig.isJspConfigSet = false; sessionConfig.isSessionConfigSet = false; loginConfig.isLoginConfigSet = false; + name.isNameSet = false; + absoluteOrdering.isAbsoluteOrderingSet = false; + relativeOrdering.isRelativeOrderingSet = false; } } @@ -1035,19 +1058,61 @@ final class VersionRule extends Rule { /** - * A rule that logs a warning if absolute ordering is configured. + * A rule that ensures only a single name element is present. */ -final class AbsoluteOrderingRule extends Rule { +final class NameRule extends Rule { - public AbsoluteOrderingRule() { + protected boolean isNameSet = false; + + public NameRule() { // NO-OP } @Override public void begin(String namespace, String name, Attributes attributes) + throws Exception { + if (isNameSet){ + throw new IllegalArgumentException(WebRuleSet.sm.getString( + "webRuleSet.nameCount")); + } + isNameSet = true; + } + + @Override + public void body(String namespace, String name, String text) + throws Exception { + super.body(namespace, name, text); + ((WebXml) digester.peek()).setName(text); + } +} + + +/** + * A rule that logs a warning if absolute ordering is configured for a fragment + * and fails if multiple absolute orders are configured. + */ +final class AbsoluteOrderingRule extends Rule { + + protected boolean isAbsoluteOrderingSet = false; + private final boolean fragment; + + public AbsoluteOrderingRule(boolean fragment) { + this.fragment = fragment; + } + + @Override + public void begin(String namespace, String name, Attributes attributes) throws Exception { - digester.getLogger().warn( - WebRuleSet.sm.getString("webRuleSet.absoluteOrdering")); + if (fragment) { + digester.getLogger().warn( + WebRuleSet.sm.getString("webRuleSet.absoluteOrdering")); + } + if (isAbsoluteOrderingSet) { + throw new IllegalArgumentException(WebRuleSet.sm.getString( + "webRuleSet.absoluteOrderingCount")); + } else { + isAbsoluteOrderingSet = true; + } } } @@ -1056,15 +1121,26 @@ final class AbsoluteOrderingRule extends */ final class RelativeOrderingRule extends Rule { - public RelativeOrderingRule() { - // NO-OP + protected boolean isRelativeOrderingSet = false; + private final boolean fragment; + + public RelativeOrderingRule(boolean fragment) { + this.fragment = fragment; } @Override public void begin(String namespace, String name, Attributes attributes) throws Exception { - digester.getLogger().warn( - WebRuleSet.sm.getString("webRuleSet.relativeOrdering")); + if (!fragment) { + digester.getLogger().warn( + WebRuleSet.sm.getString("webRuleSet.relativeOrdering")); + } + if (isRelativeOrderingSet) { + throw new IllegalArgumentException(WebRuleSet.sm.getString( + "webRuleSet.relativeOrderingCount")); + } else { + isRelativeOrderingSet = true; + } } } Added: tomcat/trunk/test/org/apache/catalina/startup/TestWebRuleSet.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/TestWebRuleSet.java?rev=1226196&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/catalina/startup/TestWebRuleSet.java (added) +++ tomcat/trunk/test/org/apache/catalina/startup/TestWebRuleSet.java Sat Dec 31 22:29:36 2011 @@ -0,0 +1,142 @@ +/* + * 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.startup; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.InputStream; + +import org.apache.catalina.deploy.WebXml; +import org.apache.tomcat.util.digester.Digester; +import org.junit.Test; + +public class TestWebRuleSet { + + private Digester fragmentDigester = new Digester(); + private WebRuleSet fragmentRuleSet = new WebRuleSet(true); + + private Digester webDigester = new Digester(); + private WebRuleSet webRuleSet = new WebRuleSet(false); + + public TestWebRuleSet() { + fragmentDigester.addRuleSet(fragmentRuleSet); + webDigester.addRuleSet(webRuleSet); + } + + + @Test + public void testSingleNameInWebFragmentXml() throws Exception { + + WebXml webXml = new WebXml(); + + assertTrue(parse(webXml, "web-fragment-1name.xml", true)); + assertEquals("name1", webXml.getName()); + } + + + @Test + public void testMultipleNameInWebFragmentXml() throws Exception { + assertFalse(parse(new WebXml(), "web-fragment-2name.xml", true)); + } + + + @Test + public void testSingleOrderingInWebFragmentXml() throws Exception { + + WebXml webXml = new WebXml(); + + assertTrue(parse(webXml, "web-fragment-1ordering.xml", true)); + assertEquals(1, webXml.getBeforeOrdering().size()); + assertTrue(webXml.getBeforeOrdering().contains("bar")); + } + + + @Test + public void testMultipleOrderingInWebFragmentXml() throws Exception { + assertFalse(parse(new WebXml(), "web-fragment-2ordering.xml", true)); + } + + + @Test + public void testSingleOrderingInWebXml() throws Exception { + + WebXml webXml = new WebXml(); + + assertTrue(parse(webXml, "web-1ordering.xml", false)); + assertEquals(1, webXml.getAbsoluteOrdering().size()); + assertTrue(webXml.getAbsoluteOrdering().contains("bar")); + } + + + @Test + public void testMultipleOrderingInWebXml() throws Exception { + assertFalse(parse(new WebXml(), "web-2ordering.xml", false)); + } + + + @Test + public void testRecycle() throws Exception { + // Name + assertFalse(parse(new WebXml(), "web-fragment-2name.xml", true)); + assertTrue(parse(new WebXml(), "web-fragment-1name.xml", true)); + assertFalse(parse(new WebXml(), "web-fragment-2name.xml", true)); + assertTrue(parse(new WebXml(), "web-fragment-1name.xml", true)); + + // Relative ordering + assertFalse(parse(new WebXml(), "web-fragment-2ordering.xml", true)); + assertTrue(parse(new WebXml(), "web-fragment-1ordering.xml", true)); + assertFalse(parse(new WebXml(), "web-fragment-2ordering.xml", true)); + assertTrue(parse(new WebXml(), "web-fragment-1ordering.xml", true)); + + // Absolute ordering + assertFalse(parse(new WebXml(), "web-2ordering.xml", false)); + assertTrue(parse(new WebXml(), "web-1ordering.xml", false)); + assertFalse(parse(new WebXml(), "web-2ordering.xml", false)); + assertTrue(parse(new WebXml(), "web-1ordering.xml", false)); +} + + + private synchronized boolean parse(WebXml webXml, String target, + boolean fragment) { + + Digester d; + if (fragment) { + d = fragmentDigester; + fragmentRuleSet.recycle(); + } else { + d = webDigester; + webRuleSet.recycle(); + } + + d.push(webXml); + + InputStream is = this.getClass().getClassLoader().getResourceAsStream( + "org/apache/catalina/startup/" + target); + + boolean result = true; + + try { + d.parse(is); + } catch (Exception e) { + result = false; + } + + return result; + } +} Added: tomcat/trunk/test/org/apache/catalina/startup/web-1ordering.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/web-1ordering.xml?rev=1226196&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/catalina/startup/web-1ordering.xml (added) +++ tomcat/trunk/test/org/apache/catalina/startup/web-1ordering.xml Sat Dec 31 22:29:36 2011 @@ -0,0 +1,27 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<!-- + 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. +--> +<web-app xmlns="http://java.sun.com/xml/ns/javaee" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee + http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" + version="3.0" + metadata-complete="true"> + <absolute-ordering> + <name>bar</name> + </absolute-ordering> +</web-app> \ No newline at end of file Added: tomcat/trunk/test/org/apache/catalina/startup/web-2ordering.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/web-2ordering.xml?rev=1226196&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/catalina/startup/web-2ordering.xml (added) +++ tomcat/trunk/test/org/apache/catalina/startup/web-2ordering.xml Sat Dec 31 22:29:36 2011 @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<!-- + 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. +--> +<web-app xmlns="http://java.sun.com/xml/ns/javaee" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee + http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" + version="3.0" + metadata-complete="true"> + <absolute-ordering> + <name>foo</name> + </absolute-ordering> + <absolute-ordering> + <name>bar</name> + </absolute-ordering> +</web-app> \ No newline at end of file Added: tomcat/trunk/test/org/apache/catalina/startup/web-fragment-1name.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/web-fragment-1name.xml?rev=1226196&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/catalina/startup/web-fragment-1name.xml (added) +++ tomcat/trunk/test/org/apache/catalina/startup/web-fragment-1name.xml Sat Dec 31 22:29:36 2011 @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<!-- + 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. +--> +<web-fragment xmlns="http://java.sun.com/xml/ns/javaee" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee + http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd" + version="3.0" + metadata-complete="true"> + <name>name1</name> +</web-fragment> \ No newline at end of file Added: tomcat/trunk/test/org/apache/catalina/startup/web-fragment-1ordering.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/web-fragment-1ordering.xml?rev=1226196&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/catalina/startup/web-fragment-1ordering.xml (added) +++ tomcat/trunk/test/org/apache/catalina/startup/web-fragment-1ordering.xml Sat Dec 31 22:29:36 2011 @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<!-- + 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. +--> +<web-fragment xmlns="http://java.sun.com/xml/ns/javaee" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee + http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd" + version="3.0" + metadata-complete="true"> + <ordering> + <before> + <name>bar</name> + </before> + </ordering> +</web-fragment> \ No newline at end of file Added: tomcat/trunk/test/org/apache/catalina/startup/web-fragment-2name.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/web-fragment-2name.xml?rev=1226196&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/catalina/startup/web-fragment-2name.xml (added) +++ tomcat/trunk/test/org/apache/catalina/startup/web-fragment-2name.xml Sat Dec 31 22:29:36 2011 @@ -0,0 +1,26 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<!-- + 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. +--> +<web-fragment xmlns="http://java.sun.com/xml/ns/javaee" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee + http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd" + version="3.0" + metadata-complete="true"> + <name>name1</name> + <name>name2</name> +</web-fragment> \ No newline at end of file Added: tomcat/trunk/test/org/apache/catalina/startup/web-fragment-2ordering.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/web-fragment-2ordering.xml?rev=1226196&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/catalina/startup/web-fragment-2ordering.xml (added) +++ tomcat/trunk/test/org/apache/catalina/startup/web-fragment-2ordering.xml Sat Dec 31 22:29:36 2011 @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<!-- + 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. +--> +<web-fragment xmlns="http://java.sun.com/xml/ns/javaee" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee + http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd" + version="3.0" + metadata-complete="true"> + <ordering> + <after> + <name>foo</name> + </after> + </ordering> + <ordering> + <before> + <name>bar</name> + </before> + </ordering> +</web-fragment> \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org