You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by jg...@apache.org on 2019/11/15 12:26:10 UTC

[tomee] 02/11: TOMEE-2739 use webcontext and the path to evaluate which context to apply the filter to

This is an automated email from the ASF dual-hosted git repository.

jgallimore pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomee.git

commit a11829520f6c1798de3b1e6074a7369e8e9293d3
Author: Jonathan Gallimore <jo...@jrg.me.uk>
AuthorDate: Wed Nov 13 17:42:50 2019 +0000

    TOMEE-2739 use webcontext and the path to evaluate which context to apply the filter to
---
 .../arquillian/tests/jaxrs/ear/EarTest.java        | 70 ++++++++++++++++++++++
 .../tests/jaxrs/ear/HelloApplication.java          |  8 +++
 .../arquillian/tests/jaxrs/ear/HelloEndpoint.java  | 22 +++++++
 .../arquillian/tests/jaxrs/ear/HelloServlet.java   | 18 ++++++
 .../apache/tomee/webservices/TomcatRsRegistry.java | 27 ++++-----
 5 files changed, 131 insertions(+), 14 deletions(-)

diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/ear/EarTest.java b/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/ear/EarTest.java
new file mode 100644
index 0000000..cb21aa5
--- /dev/null
+++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/ear/EarTest.java
@@ -0,0 +1,70 @@
+/*
+ * 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.openejb.arquillian.tests.jaxrs.ear;
+
+import org.apache.ziplock.IO;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.arquillian.test.api.ArquillianResource;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.StringAsset;
+import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.jboss.shrinkwrap.descriptor.api.Descriptors;
+import org.jboss.shrinkwrap.descriptor.api.application6.ApplicationDescriptor;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.net.URL;
+
+/**
+ * @version $Rev$ $Date$
+ */
+@RunWith(Arquillian.class)
+public class EarTest {
+
+    @ArquillianResource
+    private URL url;
+
+    @Deployment(testable = false)
+    public static EnterpriseArchive createDeployment() {
+
+        final WebArchive servletWar = ShrinkWrap.create(WebArchive.class, "servlet.war").addClass(HelloServlet.class);
+        final WebArchive jaxRSWar = ShrinkWrap.create(WebArchive.class, "rest.war").addClasses(HelloApplication.class, HelloEndpoint.class);
+
+        final EnterpriseArchive ear = ShrinkWrap.create(EnterpriseArchive.class, "test.ear")
+                .addAsModule(servletWar)
+                .addAsModule(jaxRSWar);
+
+
+        final ApplicationDescriptor descriptor = Descriptors.create(ApplicationDescriptor.class);
+        descriptor.id("TestApplication").description("My Test Application")
+                .createModule().getOrCreateWeb().webUri("servlet.war").contextRoot("TestApplication").up().up()
+                .createModule().getOrCreateWeb().webUri("rest.war").contextRoot("TestApplication-REST").up().up();
+
+        ear.setApplicationXML(new StringAsset(descriptor.exportAsString()));
+
+        return ear;
+    }
+
+    @Test
+    public void test() throws Exception {
+        Assert.assertEquals("Hello rest!", IO.slurp(new URL(url, "/TestApplication-REST/api/")));
+        Assert.assertEquals("Hello servlets!", IO.slurp(new URL(url, "/TestApplication/")));
+    }
+}
diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/ear/HelloApplication.java b/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/ear/HelloApplication.java
new file mode 100644
index 0000000..ac16adb
--- /dev/null
+++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/ear/HelloApplication.java
@@ -0,0 +1,8 @@
+package org.apache.openejb.arquillian.tests.jaxrs.ear;
+
+import javax.ws.rs.ApplicationPath;
+import javax.ws.rs.core.Application;
+
+@ApplicationPath("")
+public class HelloApplication extends Application {
+}
diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/ear/HelloEndpoint.java b/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/ear/HelloEndpoint.java
new file mode 100644
index 0000000..fea4246
--- /dev/null
+++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/ear/HelloEndpoint.java
@@ -0,0 +1,22 @@
+package org.apache.openejb.arquillian.tests.jaxrs.ear;
+
+import javax.ejb.Lock;
+import javax.ejb.LockType;
+import javax.ejb.Singleton;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+@Singleton
+@Lock(LockType.READ)
+@Path("api")
+public class HelloEndpoint {
+
+    @GET
+    @Produces(MediaType.TEXT_PLAIN)
+    public String greet() {
+        return "Hello rest!";
+    }
+
+}
diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/ear/HelloServlet.java b/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/ear/HelloServlet.java
new file mode 100644
index 0000000..1740d61
--- /dev/null
+++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/ear/HelloServlet.java
@@ -0,0 +1,18 @@
+package org.apache.openejb.arquillian.tests.jaxrs.ear;
+
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+@WebServlet(urlPatterns = "/*")
+public class HelloServlet extends HttpServlet {
+
+    @Override
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+        resp.getWriter().print("Hello servlets!");
+        resp.getWriter().flush();
+    }
+}
diff --git a/tomee/tomee-jaxrs/src/main/java/org/apache/tomee/webservices/TomcatRsRegistry.java b/tomee/tomee-jaxrs/src/main/java/org/apache/tomee/webservices/TomcatRsRegistry.java
index 4203215..86d4bd1 100644
--- a/tomee/tomee-jaxrs/src/main/java/org/apache/tomee/webservices/TomcatRsRegistry.java
+++ b/tomee/tomee-jaxrs/src/main/java/org/apache/tomee/webservices/TomcatRsRegistry.java
@@ -82,7 +82,7 @@ public class TomcatRsRegistry implements RsRegistry {
 
         if (host == null) {
             for (final Host h : hosts) {
-                context = findContext(h, appId, webContext);
+                context = findContext(h, webContext);
                 if (context != null) {
                     host = h;
                     if (classLoader != null && classLoader.equals(context.getLoader().getClassLoader())) {
@@ -95,7 +95,7 @@ public class TomcatRsRegistry implements RsRegistry {
                 throw new IllegalArgumentException("Invalid virtual host '" + virtualHost + "'.  Do you have a matching Host entry in the server.xml?");
             }
         } else {
-            context = findContext(host, appId, webContext);
+            context = findContext(host, webContext);
         }
 
         if (context == null) {
@@ -155,21 +155,20 @@ public class TomcatRsRegistry implements RsRegistry {
         return completePath.substring((webContext.length() > 0 && !webContext.startsWith("/") ? 1 : 0) + webContext.length());
     }
 
-    private static Context findContext(final Container host, final String appId, final String webContext) {
-        if (appId != null) { // when using versioning appId is like context#1235 but not the context itself so ensure to test appId first
-            final Context ctx = Context.class.cast(host.findChild('/' + appId));
-            if (ctx != null) {
-                return ctx;
+    private static Context findContext(final Container host, final String webContext) {
+        final Container[] children = host.findChildren();
+
+        for (final Container child : children) {
+            if (! Context.class.isInstance(child)) continue;
+
+            final Context context = (Context) child;
+
+            if (context.getPath().equals(webContext)) {
+                return context;
             }
         }
 
-        Context webapp = Context.class.cast(host.findChild(webContext));
-        if (webapp == null && "/".equals(webContext)) { // ROOT
-            webapp = Context.class.cast(host.findChild(""));
-        } else if (webapp == null && webContext.length() > 0 && !webContext.startsWith("/")) {
-            webapp = Context.class.cast(host.findChild("/" + webContext));
-        }
-        return webapp;
+        return null;
     }
 
     private static String address(final Collection<Connector> connectors, final String host, final String path) {