From 44f5eefad876e907cf15d843be72be47a79d311c Mon Sep 17 00:00:00 2001 From: Masatake Iwasaki Date: Mon, 1 Apr 2024 21:49:19 +0900 Subject: [PATCH] refactor: decouple jersey and jetty (#4068) * refactor: decouple jersey and jetty Signed-off-by: Masatake Iwasaki * addressed checkstyle error. * updated DEPENDENCIES. * prefering jetty-jakarta-servlet-api over standard servlet-api. --------- Signed-off-by: Masatake Iwasaki --- extensions/common/http/jersey-core/build.gradle.kts | 2 +- .../org/eclipse/edc/web/jersey/JerseyExtension.java | 6 +++--- .../org/eclipse/edc/web/jersey/JerseyRestService.java | 10 +++++----- .../java/org/eclipse/edc/web/jetty/JettyService.java | 1 + spi/common/web-spi/build.gradle.kts | 1 + .../main/java/org/eclipse/edc/web/spi/WebServer.java | 8 ++++++++ 6 files changed, 19 insertions(+), 9 deletions(-) diff --git a/extensions/common/http/jersey-core/build.gradle.kts b/extensions/common/http/jersey-core/build.gradle.kts index 49370ec750a..8d4b9227768 100644 --- a/extensions/common/http/jersey-core/build.gradle.kts +++ b/extensions/common/http/jersey-core/build.gradle.kts @@ -19,7 +19,6 @@ plugins { dependencies { api(project(":spi:common:web-spi")) - api(project(":extensions:common:http:jetty-core")) implementation(project(":extensions:common:lib:jersey-providers-lib")) @@ -33,6 +32,7 @@ dependencies { testFixturesApi(project(":core:common:lib:json-ld-lib")) testFixturesApi(project(":core:common:junit")) + testFixturesApi(project(":extensions:common:http:jetty-core")) testFixturesApi(project(":extensions:common:json-ld")) testFixturesImplementation(project(":extensions:common:lib:jersey-providers-lib")) testFixturesApi(libs.jakarta.rsApi) diff --git a/extensions/common/http/jersey-core/src/main/java/org/eclipse/edc/web/jersey/JerseyExtension.java b/extensions/common/http/jersey-core/src/main/java/org/eclipse/edc/web/jersey/JerseyExtension.java index c4f88a19ff4..d96fa290afd 100644 --- a/extensions/common/http/jersey-core/src/main/java/org/eclipse/edc/web/jersey/JerseyExtension.java +++ b/extensions/common/http/jersey-core/src/main/java/org/eclipse/edc/web/jersey/JerseyExtension.java @@ -23,7 +23,7 @@ import org.eclipse.edc.spi.types.TypeManager; import org.eclipse.edc.web.jersey.validation.ResourceInterceptorBinder; import org.eclipse.edc.web.jersey.validation.ResourceInterceptorProvider; -import org.eclipse.edc.web.jetty.JettyService; +import org.eclipse.edc.web.spi.WebServer; import org.eclipse.edc.web.spi.WebService; import org.eclipse.edc.web.spi.validation.InterceptorFunctionRegistry; @@ -41,7 +41,7 @@ public class JerseyExtension implements ServiceExtension { public static final String CORS_CONFIG_METHODS_SETTING = "edc.web.rest.cors.methods"; @Inject - private JettyService jettyService; + private WebServer webServer; @Inject private TypeManager typeManager; @@ -64,7 +64,7 @@ public void initialize(ServiceExtensionContext context) { .corsEnabled(context.getSetting(CORS_CONFIG_ENABLED_SETTING, false)) .build(); - jerseyRestService = new JerseyRestService(jettyService, typeManager, configuration, monitor); + jerseyRestService = new JerseyRestService(webServer, typeManager, configuration, monitor); provider = new ResourceInterceptorProvider(); jerseyRestService.registerInstance(() -> new ResourceInterceptorBinder(provider)); diff --git a/extensions/common/http/jersey-core/src/main/java/org/eclipse/edc/web/jersey/JerseyRestService.java b/extensions/common/http/jersey-core/src/main/java/org/eclipse/edc/web/jersey/JerseyRestService.java index e36f52476ed..d49928e14c3 100644 --- a/extensions/common/http/jersey-core/src/main/java/org/eclipse/edc/web/jersey/JerseyRestService.java +++ b/extensions/common/http/jersey-core/src/main/java/org/eclipse/edc/web/jersey/JerseyRestService.java @@ -21,7 +21,7 @@ import org.eclipse.edc.web.jersey.jsonld.ObjectMapperProvider; import org.eclipse.edc.web.jersey.mapper.EdcApiExceptionMapper; import org.eclipse.edc.web.jersey.mapper.UnexpectedExceptionMapper; -import org.eclipse.edc.web.jetty.JettyService; +import org.eclipse.edc.web.spi.WebServer; import org.eclipse.edc.web.spi.WebService; import org.glassfish.hk2.utilities.binding.AbstractBinder; import org.glassfish.jersey.media.multipart.MultiPartFeature; @@ -40,7 +40,7 @@ public class JerseyRestService implements WebService { private static final String DEFAULT_CONTEXT_ALIAS = "default"; - private final JettyService jettyService; + private final WebServer webServer; private final TypeManager typeManager; private final Monitor monitor; @@ -48,8 +48,8 @@ public class JerseyRestService implements WebService { private final JerseyConfiguration configuration; private final List> additionalInstances = new ArrayList<>(); - public JerseyRestService(JettyService jettyService, TypeManager typeManager, JerseyConfiguration configuration, Monitor monitor) { - this.jettyService = jettyService; + public JerseyRestService(WebServer webServer, TypeManager typeManager, JerseyConfiguration configuration, Monitor monitor) { + this.webServer = webServer; this.typeManager = typeManager; this.configuration = configuration; this.monitor = monitor; @@ -101,7 +101,7 @@ private void registerContext(String contextAlias, List controllers) { resourceConfig.register(MultiPartFeature.class); var servletContainer = new ServletContainer(resourceConfig); - jettyService.registerServlet(contextAlias, servletContainer); + webServer.registerServlet(contextAlias, servletContainer); monitor.info("Registered Web API context alias: " + contextAlias); } diff --git a/extensions/common/http/jetty-core/src/main/java/org/eclipse/edc/web/jetty/JettyService.java b/extensions/common/http/jetty-core/src/main/java/org/eclipse/edc/web/jetty/JettyService.java index 54446575251..6ca945fef9c 100644 --- a/extensions/common/http/jetty-core/src/main/java/org/eclipse/edc/web/jetty/JettyService.java +++ b/extensions/common/http/jetty-core/src/main/java/org/eclipse/edc/web/jetty/JettyService.java @@ -123,6 +123,7 @@ public void shutdown() { } } + @Override public void registerServlet(String contextName, Servlet servlet) { var servletHolder = new ServletHolder(Source.EMBEDDED); servletHolder.setName("EDC-" + contextName); diff --git a/spi/common/web-spi/build.gradle.kts b/spi/common/web-spi/build.gradle.kts index 66e8dce0775..0afddbdd64e 100644 --- a/spi/common/web-spi/build.gradle.kts +++ b/spi/common/web-spi/build.gradle.kts @@ -22,6 +22,7 @@ dependencies { api(project(":spi:common:validator-spi")) api(libs.jakarta.rsApi) + api(libs.jetty.jakarta.servlet.api) } diff --git a/spi/common/web-spi/src/main/java/org/eclipse/edc/web/spi/WebServer.java b/spi/common/web-spi/src/main/java/org/eclipse/edc/web/spi/WebServer.java index 14ff56f8326..31f42528a51 100644 --- a/spi/common/web-spi/src/main/java/org/eclipse/edc/web/spi/WebServer.java +++ b/spi/common/web-spi/src/main/java/org/eclipse/edc/web/spi/WebServer.java @@ -14,6 +14,7 @@ package org.eclipse.edc.web.spi; +import jakarta.servlet.Servlet; import org.eclipse.edc.runtime.metamodel.annotation.ExtensionPoint; /** @@ -34,6 +35,13 @@ public interface WebServer { */ void addPortMapping(String contextName, int port, String path); + /** + * Adds a new servlet to the specified context name.. + * + * @param contextName the name of the API context. + * @param servlet servlet implementation to add. + */ + void registerServlet(String contextName, Servlet servlet); /** * Returns the default context name