Skip to content

Commit

Permalink
refactor: decouple jersey and jetty (#4068)
Browse files Browse the repository at this point in the history
* refactor: decouple jersey and jetty

Signed-off-by: Masatake Iwasaki <[email protected]>

* addressed checkstyle error.

* updated DEPENDENCIES.

* prefering jetty-jakarta-servlet-api over standard servlet-api.

---------

Signed-off-by: Masatake Iwasaki <[email protected]>
  • Loading branch information
iwasakims authored Apr 1, 2024
1 parent 2880828 commit 44f5eef
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion extensions/common/http/jersey-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,16 +40,16 @@
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;

private final Map<String, List<Object>> controllers = new HashMap<>();
private final JerseyConfiguration configuration;
private final List<Supplier<Object>> 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;
Expand Down Expand Up @@ -101,7 +101,7 @@ private void registerContext(String contextAlias, List<Object> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions spi/common/web-spi/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies {
api(project(":spi:common:validator-spi"))

api(libs.jakarta.rsApi)
api(libs.jetty.jakarta.servlet.api)
}


Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package org.eclipse.edc.web.spi;

import jakarta.servlet.Servlet;
import org.eclipse.edc.runtime.metamodel.annotation.ExtensionPoint;

/**
Expand All @@ -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
Expand Down

0 comments on commit 44f5eef

Please sign in to comment.