Skip to content

Commit

Permalink
Fixes piranhacloud#3705 - Add Tomcat10xExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
mnriem committed Apr 14, 2024
1 parent 280c816 commit cf7ce0f
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 0 deletions.
44 changes: 44 additions & 0 deletions extension/compat-tomcat10x/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cloud.piranha.extension</groupId>
<artifactId>project</artifactId>
<version>24.5.0-SNAPSHOT</version>
</parent>
<artifactId>piranha-extension-compat-tomcat10x</artifactId>
<packaging>jar</packaging>
<name>Piranha - Extension - Tomcat 10.x Compatibility</name>
<dependencies>
<!-- compile -->
<dependency>
<groupId>cloud.piranha.core</groupId>
<artifactId>piranha-core-api</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<!-- test -->
<dependency>
<groupId>cloud.piranha.core</groupId>
<artifactId>piranha-core-impl</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package cloud.piranha.extension.tomcat10x;

import cloud.piranha.core.api.WebApplication;
import cloud.piranha.core.api.WebApplicationExtension;
import java.io.IOException;
import java.io.InputStream;
import static java.lang.System.Logger.Level.INFO;
import static java.lang.System.Logger.Level.WARNING;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

/**
* The WebApplicationExtension that delivers Tomcat 10.x compatibility.
*
* @author Manfred Riem (mriem@manorrock.com)
*/
public class Tomcat10xExtension implements WebApplicationExtension {

/**
* Stores the logger.
*/
private static final System.Logger LOGGER = System.getLogger(Tomcat10xExtension.class.getName());

@Override
public void configure(WebApplication webApplication) {

InputStream inputStream = webApplication.getResourceAsStream("/META-INF/context.xml");
if (inputStream != null) {
try {
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = documentBuilder.parse(inputStream);
XPath xPath = XPathFactory.newInstance().newXPath();

String contextPath = xPath.evaluate("//Context/@path", (Node) document);
if (contextPath != null) {
LOGGER.log(INFO, "Found Tomcat 10.x Context path: {0}", contextPath);
LOGGER.log(WARNING, "Found Tomcat 10.x Context path, please replace with command-line parameter", this);
LOGGER.log(INFO, "Setting context path to: {0}", contextPath);
webApplication.setContextPath(contextPath);
}

} catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException e) {
}
}
}
}
42 changes: 42 additions & 0 deletions extension/compat-tomcat10x/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

/**
* This module delivers an extension to support Tomcat 10.x compatibility.
*
* @author Manfred Riem (mriem@manorrock.com)
*/
module cloud.piranha.extension.tomcat10x {

exports cloud.piranha.extension.tomcat10x;

opens cloud.piranha.extension.tomcat10x;

requires cloud.piranha.core.api;
requires java.xml;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package cloud.piranha.extension.tomcat10x.tests;

import cloud.piranha.core.impl.DefaultWebApplication;
import cloud.piranha.extension.tomcat10x.Tomcat10xExtension;
import cloud.piranha.resource.impl.DirectoryResource;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* The JUnit tests for the Tomcat10xExtension class.
*
* @author Manfred Riem (mriem@manorrock.com)
*/
public class Tomcat10xExtensionTest {

/**
* Test of configure method, of class Tomcat10xExtension.
*/
@Test
public void testConfigure() {
DefaultWebApplication webApplication = new DefaultWebApplication();
webApplication.addResource(new DirectoryResource("src/test/tomcat/contextpath"));
Tomcat10xExtension extension = new Tomcat10xExtension();
extension.configure(webApplication);
assertEquals("/mypath", webApplication.getContextPath());
}
}
39 changes: 39 additions & 0 deletions extension/compat-tomcat10x/src/test/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

/**
* The JUnit tests for the Tomcat 10.x extension.
*
* @author Manfred Riem (mriem@manorrock.com)
*/
open module cloud.piranha.extension.tomcat10x.tests {

requires cloud.piranha.extension.tomcat10x;
requires cloud.piranha.core.impl;
requires org.junit.jupiter.api;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>

<Context path="/mypath">
</Context>
1 change: 1 addition & 0 deletions extension/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<module>webprofile</module>
<module>webxml</module>
<module>welcomefile</module>
<module>compat-tomcat10x</module>
</modules>

<dependencyManagement>
Expand Down
6 changes: 6 additions & 0 deletions extension/servlet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cloud.piranha.extension</groupId>
<artifactId>piranha-extension-compat-tomcat10x</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cloud.piranha.extension</groupId>
<artifactId>piranha-extension-eclipse-expressly</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import cloud.piranha.extension.security.servlet.ServletSecurityManagerExtension;
import cloud.piranha.extension.servletannotations.ServletAnnotationsExtension;
import cloud.piranha.extension.tempdir.TempDirExtension;
import cloud.piranha.extension.tomcat10x.Tomcat10xExtension;
import cloud.piranha.extension.wasp.WaspExtension;
import cloud.piranha.extension.wasp.WaspJspManagerExtension;
import cloud.piranha.extension.webxml.WebXmlExtension;
Expand Down Expand Up @@ -66,6 +67,7 @@ public void extend(WebApplicationExtensionContext context) {
context.add(WaspExtension.class); // WaSP
context.add(ServletContainerInitializerExtension.class); // ServletContainerInitializer
context.add(ServletSecurityExtension.class); // Security implementation
context.add(Tomcat10xExtension.class); // Tomcat 10x compatbility
}

private static Class<? extends WebApplicationExtension> getAnnotationScanExtensionClass() {
Expand Down
1 change: 1 addition & 0 deletions extension/servlet/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
requires cloud.piranha.extension.annotationscan;
requires cloud.piranha.extension.annotationscan.classfile;
requires cloud.piranha.extension.apache.fileupload;
requires cloud.piranha.extension.tomcat10x;
requires cloud.piranha.extension.expressly;
requires cloud.piranha.extension.naming;
requires cloud.piranha.extension.policy;
Expand Down

0 comments on commit cf7ce0f

Please sign in to comment.