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

radcortez pushed a commit to branch TOMEE-2408
in repository https://gitbox.apache.org/repos/asf/tomee.git

commit ba02c9470fcd0440eff367538509b1ea735177fa
Author: Roberto Cortez <radcor...@yahoo.com>
AuthorDate: Mon Jan 21 23:36:15 2019 +0000

    TOMEE-2408 - MicroProfileListener to removed duplicated endpoints and 
prevent servlet mapping clash.
---
 .../apache/tomee/catalina/TomcatWebAppBuilder.java |  5 +-
 .../catalina/event/AfterApplicationCreated.java    | 11 +++-
 tomee/tomee-microprofile/mp-common/pom.xml         |  7 +++
 .../microprofile/TomEEMicroProfileListener.java    | 58 ++++++++++++++++++++++
 .../META-INF/org.apache.openejb.extension          |  1 +
 5 files changed, 80 insertions(+), 2 deletions(-)

diff --git 
a/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomcatWebAppBuilder.java
 
b/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomcatWebAppBuilder.java
index 0a79755..493cb90 100644
--- 
a/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomcatWebAppBuilder.java
+++ 
b/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomcatWebAppBuilder.java
@@ -1780,7 +1780,10 @@ public class TomcatWebAppBuilder implements 
WebAppBuilder, ContextListener, Pare
                 for (final WebAppInfo webAppInfo : 
contextInfo.appInfo.webApps) {
                     final String wId = getId(webAppInfo.host, 
webAppInfo.contextRoot, contextInfo.version);
                     if (id.equals(wId)) {
-                        SystemInstance.get().fireEvent(new 
AfterApplicationCreated(contextInfo.appInfo, webAppInfo));
+                        SystemInstance.get().fireEvent(
+                                new 
AfterApplicationCreated(contextInfo.appInfo,
+                                                            webAppInfo,
+                                                            
standardContext.getServletContext()));
                         break;
                     }
                 }
diff --git 
a/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/event/AfterApplicationCreated.java
 
b/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/event/AfterApplicationCreated.java
index ced0c15..2c06fe3 100644
--- 
a/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/event/AfterApplicationCreated.java
+++ 
b/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/event/AfterApplicationCreated.java
@@ -20,14 +20,20 @@ import org.apache.openejb.assembler.classic.AppInfo;
 import org.apache.openejb.assembler.classic.WebAppInfo;
 import org.apache.openejb.observer.Event;
 
+import javax.servlet.ServletContext;
+
 @Event
 public class AfterApplicationCreated {
     private final AppInfo app;
     private final WebAppInfo web;
+    private final ServletContext context;
 
-    public AfterApplicationCreated(final AppInfo appInfo, final WebAppInfo 
webApp) {
+    public AfterApplicationCreated(final AppInfo appInfo,
+                                   final WebAppInfo webApp,
+                                   final ServletContext servletContext) {
         app = appInfo;
         web = webApp;
+        context = servletContext;
     }
 
     public AppInfo getApp() {
@@ -38,6 +44,9 @@ public class AfterApplicationCreated {
         return web;
     }
 
+    public ServletContext getContext() {
+        return context;
+    }
 
     @Override
     public String toString() {
diff --git a/tomee/tomee-microprofile/mp-common/pom.xml 
b/tomee/tomee-microprofile/mp-common/pom.xml
index da9f024..582741a 100644
--- a/tomee/tomee-microprofile/mp-common/pom.xml
+++ b/tomee/tomee-microprofile/mp-common/pom.xml
@@ -36,6 +36,13 @@
       <scope>provided</scope>
     </dependency>
 
+    <dependency>
+      <groupId>${project.groupId}</groupId>
+      <artifactId>tomee-catalina</artifactId>
+      <version>${project.version}</version>
+      <scope>provided</scope>
+    </dependency>
+
     <!-- MicroProfile -->
     <dependency>
       <groupId>org.eclipse.microprofile.config</groupId>
diff --git 
a/tomee/tomee-microprofile/mp-common/src/main/java/org/apache/tomee/microprofile/TomEEMicroProfileListener.java
 
b/tomee/tomee-microprofile/mp-common/src/main/java/org/apache/tomee/microprofile/TomEEMicroProfileListener.java
new file mode 100644
index 0000000..353ac8e
--- /dev/null
+++ 
b/tomee/tomee-microprofile/mp-common/src/main/java/org/apache/tomee/microprofile/TomEEMicroProfileListener.java
@@ -0,0 +1,58 @@
+/*
+ * 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.tomee.microprofile;
+
+import org.apache.geronimo.microprofile.common.jaxrs.HealthChecksEndpoint;
+import 
org.apache.geronimo.microprofile.impl.health.cdi.CdiHealthChecksEndpoint;
+import org.apache.geronimo.microprofile.metrics.common.jaxrs.MetricsEndpoints;
+import org.apache.geronimo.microprofile.metrics.jaxrs.CdiMetricsEndpoints;
+import org.apache.geronimo.microprofile.openapi.jaxrs.OpenAPIEndpoint;
+import org.apache.openejb.assembler.classic.WebAppInfo;
+import org.apache.openejb.observer.Observes;
+import org.apache.openejb.observer.event.BeforeEvent;
+import org.apache.tomee.catalina.event.AfterApplicationCreated;
+
+import javax.servlet.ServletContext;
+import javax.servlet.ServletRegistration;
+import java.util.Collection;
+
+public class TomEEMicroProfileListener {
+    public void processApplication(@Observes final 
BeforeEvent<AfterApplicationCreated> afterApplicationCreated) {
+        final ServletContext context = 
afterApplicationCreated.getEvent().getContext();
+        final WebAppInfo webApp = afterApplicationCreated.getEvent().getWeb();
+
+        // These remove duplicated REST API endpoints.
+        webApp.restClass.removeIf(className -> 
className.equals(HealthChecksEndpoint.class.getName()));
+        webApp.restClass.removeIf(className -> 
className.equals(MetricsEndpoints.class.getName()));
+
+        // There remove all of MP REST API endpoint if there is a servlet 
already registered in /*. The issue here is
+        // that REST path has priority over servlet and there may override old 
applications that have servlets
+        // with /* mapping.
+        context.getServletRegistrations()
+               .values()
+               .stream()
+               .map(ServletRegistration::getMappings)
+               .flatMap(Collection::stream)
+               .filter(mapping -> mapping.equals("/*"))
+               .findFirst()
+               .ifPresent(mapping -> {
+                   webApp.restClass.removeIf(className -> 
className.equals(CdiHealthChecksEndpoint.class.getName()));
+                   webApp.restClass.removeIf(className -> 
className.equals(CdiMetricsEndpoints.class.getName()));
+                   webApp.restClass.removeIf(className -> 
className.equals(OpenAPIEndpoint.class.getName()));
+               });
+    }
+}
diff --git 
a/tomee/tomee-microprofile/mp-common/src/main/resources/META-INF/org.apache.openejb.extension
 
b/tomee/tomee-microprofile/mp-common/src/main/resources/META-INF/org.apache.openejb.extension
new file mode 100644
index 0000000..3192713
--- /dev/null
+++ 
b/tomee/tomee-microprofile/mp-common/src/main/resources/META-INF/org.apache.openejb.extension
@@ -0,0 +1 @@
+org.apache.tomee.microprofile.TomEEMicroProfileListener

Reply via email to