eclipse-ee4j / eclipse-ee4j/jersey

Jersey 2 with guice bind is not able to bind resources

Open
#5,357 5 comments 0 reactions 0 assignees View on GitHub
help wanted
Dominant language
Java
Stars
730
Forks
382
PR merge metrics
No merged PRs in 30d

Description

As part of migrating Jersey from 1.19.4 to 2.39.1, we are facing some issues with guice bind.

Here is the draft PR: https://github.com/apache/hadoop/pull/5768

All updated dependencies are defined here: https://github.com/apache/hadoop/pull/5768/files#diff-df2aa66a3757d73c93849b3a279d42a4634b0aed3550cdd2ead32d2fa06bf49e (hadoop-project/pom.xml)

We have a test TestWebApp https://github.com/apache/hadoop/blob/trunk/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/TestWebApp.java#L245

```
@Test
void testCustomRoutes() throws Exception {
WebApp app =
WebApps.$for("test", TestWebApp.class, this, "ws").start(new WebApp() {
@Override
public void setup() {
bind(MyTestJAXBContextResolver.class);
bind(MyTestWebService.class);

route("/:foo", FooController.class);
route("/bar/foo", FooController.class, "bar");
route("/foo/:foo", DefaultController.class);
route("/foo/bar/:foo", DefaultController.class, "index");
}
});
String baseUrl = baseUrl(app);
try {
assertEquals("foo", getContent(baseUrl).trim());
assertEquals("foo", getContent(baseUrl + "test").trim());
assertEquals("foo1", getContent(baseUrl + "test/1").trim());
assertEquals("bar", getContent(baseUrl + "test/bar/foo").trim());
assertEquals("default", getContent(baseUrl + "test/foo/bar").trim());
assertEquals("default1", getContent(baseUrl + "test/foo/1").trim());
assertEquals("default2", getContent(baseUrl + "test/foo/bar/2").trim());
assertEquals(404, getResponseCode(baseUrl + "test/goo"));
assertEquals(200, getResponseCode(baseUrl + "ws/v1/test"));
assertTrue(getContent(baseUrl + "ws/v1/test").contains("myInfo"));
} finally {
app.stop();
}
}

```

The idea here is to ensure that custom routes are served with the above mentioned endpoints.

Moreover, MyTestJAXBContextResolver and MyTestWebService are used to bind requests starting with "ws/v1/test".

The changes done for MyTestJAXBContextResolver: https://github.com/apache/hadoop/pull/5768/files#diff-52936fdf3657b9acf3cffa439cf5c0b71737d51d8d473329843e66ca462aa306

and MyTestWebService (without changes): https://github.com/apache/hadoop/blob/trunk/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/MyTestWebService.java

Since guice bind is not able to bind the classes, `getResponseCode(baseUrl + "ws/v1/test")` always results into 404.

i have also tried adding the classes/packages with ResourceConfig but it is only able to instantiate MyTestJAXBContextResolver, it is still not able to redirect `ws/v1/test` GET to MyTestWebService.

```
diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/MyTestWebService.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/MyTestWebService.java
index 1d0a01ea53d..553389feca4 100644
--- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/MyTestWebService.java
+++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/MyTestWebService.java
@@ -31,7 +31,7 @@
import org.apache.hadoop.http.JettyUtils;

@Singleton
-@Path("/ws/v1/test")
+@Path("")
public class MyTestWebService {
@GET
@Produces({ MediaType.APPLICATION_XML + "; " + JettyUtils.UTF_8 })
diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/TestWebApp.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/TestWebApp.java
index 7d7a1575b47..cadf0e720ae 100644
--- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/TestWebApp.java
+++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/TestWebApp.java
@@ -22,14 +22,17 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
+import java.util.HashMap;

import com.google.inject.Inject;
+import org.glassfish.jersey.server.ResourceConfig;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.http.HttpServer2;
import org.apache.hadoop.net.ServerSocketUtil;
import org.apache.hadoop.yarn.MockApps;
import org.apache.hadoop.yarn.webapp.view.HtmlPage;
@@ -256,6 +259,16 @@ public void setup() {
route("/foo/:foo", DefaultController.class);
route("/foo/bar/:foo", DefaultController.class, "index");
}
+
+ @Override
+ public void addJerseyResourceConfigs(HttpServer2 server) {
+ ResourceConfig resourceConfig = new ResourceConfig();
+ resourceConfig.packages(MyTestWebService.class.getPackage().getName() + ";"
+ + MyTestJAXBContextResolver.class.getPackage().getName());
+ resourceConfig.register(MyTestJAXBContextResolver.class);
+ resourceConfig.register(MyTestWebService.class);
+ server.addJerseyResourceConfig(resourceConfig, "/ws/v1/test", new HashMap<>());
+ }
});
String baseUrl = baseUrl(app);
try {
```

guice-hk2 is in the classpath.
the test belongs to hadoop-yarn-common module.

Edit:
additional reference: we create embedded Jetty server to server http requests in [HttpServer2](https://github.com/apache/hadoop/blob/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/http/HttpServer2.java)
Changes for the class in the PR: https://github.com/apache/hadoop/pull/5768/files#diff-4e9d7dccc4530205e71b54fe7f967135aeca170cff5ace98b5b7f04304153813

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.