From 57eda459d98a1c4c681654bce103f54cede43f58 Mon Sep 17 00:00:00 2001 From: markt Date: Wed, 3 Feb 2010 19:10:48 +0000 Subject: [PATCH] Servlet 3.0 Effective major/minor version support git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@906183 13f79535-47bb-0310-9956-ffa450edef68 --- java/org/apache/catalina/Context.java | 28 +++++++ .../apache/catalina/core/ApplicationContext.java | 6 +- java/org/apache/catalina/core/StandardContext.java | 20 +++++ .../apache/catalina/deploy/LocalStrings.properties | 4 +- java/org/apache/catalina/deploy/WebXml.java | 87 +++++++++++++++++++++- test/org/apache/TestAll.java | 4 +- test/org/apache/catalina/deploy/TestWebXml.java | 83 +++++++++++++++++++++ 7 files changed, 223 insertions(+), 9 deletions(-) create mode 100644 test/org/apache/catalina/deploy/TestWebXml.java diff --git a/java/org/apache/catalina/Context.java b/java/org/apache/catalina/Context.java index ef7ebe77e..83d391352 100644 --- a/java/org/apache/catalina/Context.java +++ b/java/org/apache/catalina/Context.java @@ -1128,5 +1128,33 @@ public interface Context extends Container { * @param path The path to the desired resource */ public String getRealPath(String path); + + + /** + * Return the effective major version of the Servlet spec used by this + * context. + */ + public int getEffectiveMajorVersion(); + + + /** + * Set the effective major version of the Servlet spec used by this + * context. + */ + public void setEffectiveMajorVersion(int major); + + + /** + * Return the effective minor version of the Servlet spec used by this + * context. + */ + public int getEffectiveMinorVersion(); + + + /** + * Set the effective minor version of the Servlet spec used by this + * context. + */ + public void setEffectiveMinorVersion(int minor); } diff --git a/java/org/apache/catalina/core/ApplicationContext.java b/java/org/apache/catalina/core/ApplicationContext.java index a1e90abf6..c55330be9 100644 --- a/java/org/apache/catalina/core/ApplicationContext.java +++ b/java/org/apache/catalina/core/ApplicationContext.java @@ -1348,15 +1348,13 @@ public class ApplicationContext @Override public int getEffectiveMajorVersion() { - // TODO SERVLET3 - return 0; + return context.getEffectiveMajorVersion(); } @Override public int getEffectiveMinorVersion() { - // TODO SERVLET3 - return 0; + return context.getEffectiveMinorVersion(); } diff --git a/java/org/apache/catalina/core/StandardContext.java b/java/org/apache/catalina/core/StandardContext.java index 05575579a..d14675cb8 100644 --- a/java/org/apache/catalina/core/StandardContext.java +++ b/java/org/apache/catalina/core/StandardContext.java @@ -763,8 +763,28 @@ public class StandardContext */ private boolean logEffectiveWebXml = false; + private int effectiveMajorVersion = 3; + + private int effectiveMinorVersion = 0; + // ----------------------------------------------------- Context Properties + public int getEffectiveMajorVersion() { + return effectiveMajorVersion; + } + + public void setEffectiveMajorVersion(int effectiveMajorVersion) { + this.effectiveMajorVersion = effectiveMajorVersion; + } + + public int getEffectiveMinorVersion() { + return effectiveMinorVersion; + } + + public void setEffectiveMinorVersion(int effectiveMinorVersion) { + this.effectiveMinorVersion = effectiveMinorVersion; + } + public void setLogEffectiveWebXml(boolean logEffectiveWebXml) { this.logEffectiveWebXml = logEffectiveWebXml; } diff --git a/java/org/apache/catalina/deploy/LocalStrings.properties b/java/org/apache/catalina/deploy/LocalStrings.properties index 382417fd7..cb6d1ff9b 100644 --- a/java/org/apache/catalina/deploy/LocalStrings.properties +++ b/java/org/apache/catalina/deploy/LocalStrings.properties @@ -38,4 +38,6 @@ webXml.mergeConflictSessionCookieMaxAge=The session cookie max-age was defined i webXml.mergeConflictSessionTimeout=The session timeout was defined inconsistently in multiple fragments with different values including fragment with name [{0}] located at [{1}] webXml.mergeConflictSessionTrackingMode=The session tracking modes were defined inconsistently in multiple fragments including fragment with name [{0}] located at [{1}] webXml.mergeConflictString=The [{0}] with name [{1}] was defined inconsistently in multiple fragments including fragment with name [{2}] located at [{3}] -webXml.multipleOther=Multiple others entries in ordering \ No newline at end of file +webXml.multipleOther=Multiple others entries in ordering +webxml.unrecognisedPublicId=The public ID [{0}] did not match any of the known public ID's for web.xml files so the version could not be identified +webXml.version.nfe=Unable to parse [{0}] from the version string [{1}]. This component of the version string will be ignored. diff --git a/java/org/apache/catalina/deploy/WebXml.java b/java/org/apache/catalina/deploy/WebXml.java index 90f5264a7..facc43691 100644 --- a/java/org/apache/catalina/deploy/WebXml.java +++ b/java/org/apache/catalina/deploy/WebXml.java @@ -110,12 +110,85 @@ public class WebXml { // Required attribute of web-app element private String version = null; public String getVersion() { return version; } - public void setVersion(String version) { this.version = version; } + /** + * Set the version for this web.xml file + * @param version Values of null will be ignored + */ + public void setVersion(String version) { + if (version == null) return; + + this.version = version; + // Update major and minor version + // Expected format is n.n - allow for any number of digits just in case + String major = null; + String minor = null; + int split = version.indexOf('.'); + if (split < 0) { + // Major only + major = version; + } else { + major = version.substring(0, split); + minor = version.substring(split + 1); + } + if (major == null || major.length() == 0) { + majorVersion = 0; + } else { + try { + majorVersion = Integer.parseInt(major); + } catch (NumberFormatException nfe) { + log.warn(sm.getString("webXml.version.nfe", major, version), + nfe); + majorVersion = 0; + } + } + + if (minor == null || minor.length() == 0) { + minorVersion = 0; + } else { + try { + minorVersion = Integer.parseInt(minor); + } catch (NumberFormatException nfe) { + log.warn(sm.getString("webXml.version.nfe", minor, version), + nfe); + minorVersion = 0; + } + } + } + // Optional publicId attribute private String publicId = null; public String getPublicId() { return publicId; } - public void setPublicId(String publicId) { this.publicId = publicId; } + public void setPublicId(String publicId) { + this.publicId = publicId; + // Update major and minor version + if (org.apache.catalina.startup.Constants.WebSchemaPublicId_30. + equalsIgnoreCase(publicId) || + org.apache.catalina.startup.Constants.WebFragmentSchemaPublicId_30. + equalsIgnoreCase(publicId)) { + majorVersion = 3; + minorVersion = 0; + } else if (org.apache.catalina.startup.Constants.WebSchemaPublicId_25. + equalsIgnoreCase(publicId)) { + majorVersion = 2; + minorVersion = 5; + } else if (org.apache.catalina.startup.Constants.WebSchemaPublicId_24. + equalsIgnoreCase(publicId)) { + majorVersion = 2; + minorVersion = 4; + } else if (org.apache.catalina.startup.Constants.WebDtdPublicId_23. + equalsIgnoreCase(publicId)) { + majorVersion = 2; + minorVersion = 3; + } else if (org.apache.catalina.startup.Constants.WebDtdPublicId_22. + equalsIgnoreCase(publicId)) { + majorVersion = 2; + minorVersion = 2; + } else { + // Unrecognised publicId + log.warn(sm.getString("webxml.unrecognisedPublicId", publicId)); + } + } // Optional metadata-complete attribute private boolean metadataComplete = false; @@ -135,6 +208,13 @@ public class WebXml { } } + // Derived major and minor version attributes + // Default to 3.0 until we know otherwise + private int majorVersion = 3; + private int minorVersion = 0; + public int getMajorVersion() { return majorVersion; } + public int getMinorVersion() { return minorVersion; } + // web-app elements // TODO: Ignored elements: // - description @@ -1055,6 +1135,9 @@ public class WebXml { context.setPublicId(publicId); // Everything else in order + context.setEffectiveMajorVersion(getMajorVersion()); + context.setEffectiveMinorVersion(getMinorVersion()); + for (Entry entry : contextParams.entrySet()) { context.addParameter(entry.getKey(), entry.getValue()); } diff --git a/test/org/apache/TestAll.java b/test/org/apache/TestAll.java index d2185817c..357056795 100644 --- a/test/org/apache/TestAll.java +++ b/test/org/apache/TestAll.java @@ -23,7 +23,7 @@ import junit.framework.TestSuite; import org.apache.catalina.connector.TestKeepAliveCount; import org.apache.catalina.connector.TestRequest; import org.apache.catalina.core.TestStandardContext; -import org.apache.catalina.deploy.TestWebXml; +import org.apache.catalina.deploy.TestWebXmlOrdering; import org.apache.catalina.ha.session.TestSerializablePrincipal; import org.apache.catalina.startup.TestTomcat; import org.apache.catalina.startup.TestTomcatSSL; @@ -51,7 +51,7 @@ public class TestAll { // startup suite.addTestSuite(TestTomcat.class); suite.addTestSuite(TestTomcatSSL.class); - suite.addTestSuite(TestWebXml.class); + suite.addTestSuite(TestWebXmlOrdering.class); // tribes // suite.addTest(TribesTestSuite.suite()); diff --git a/test/org/apache/catalina/deploy/TestWebXml.java b/test/org/apache/catalina/deploy/TestWebXml.java new file mode 100644 index 000000000..d8c5ca6c2 --- /dev/null +++ b/test/org/apache/catalina/deploy/TestWebXml.java @@ -0,0 +1,83 @@ +/* + * 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 junit.framework.TestCase; + +/** + * Test case for {@link WebXml}. + */ +public class TestWebXml extends TestCase { + + public void testParseVersion() { + + WebXml webxml = new WebXml(); + + // Defaults + assertEquals(3, webxml.getMajorVersion()); + assertEquals(0, webxml.getMinorVersion()); + + // Both get changed + webxml.setVersion("2.5"); + assertEquals(2, webxml.getMajorVersion()); + assertEquals(5, webxml.getMinorVersion()); + + // Reset + webxml.setVersion("0.0"); + assertEquals(0, webxml.getMajorVersion()); + assertEquals(0, webxml.getMinorVersion()); + + // null input should be ignored + webxml.setVersion(null); + assertEquals(0, webxml.getMajorVersion()); + assertEquals(0, webxml.getMinorVersion()); + + // major only + webxml.setVersion("3"); + assertEquals(3, webxml.getMajorVersion()); + assertEquals(0, webxml.getMinorVersion()); + + // no minor digit + webxml.setVersion("0.0"); // reset + webxml.setVersion("3."); + assertEquals(3, webxml.getMajorVersion()); + assertEquals(0, webxml.getMinorVersion()); + + // minor only + webxml.setVersion("0.0"); // reset + webxml.setVersion(".5"); + assertEquals(0, webxml.getMajorVersion()); + assertEquals(5, webxml.getMinorVersion()); + + // leading & training zeros + webxml.setVersion("0.0"); // reset + webxml.setVersion("002.500"); + assertEquals(2, webxml.getMajorVersion()); + assertEquals(500, webxml.getMinorVersion()); + } + + public void testParsePublicIdVersion() { + + WebXml webxml = new WebXml(); + + webxml.setPublicId( + org.apache.catalina.startup.Constants.WebSchemaPublicId_25); + assertEquals(2, webxml.getMajorVersion()); + assertEquals(5, webxml.getMinorVersion()); + } +} -- 2.11.0