1

I'm developing a server-client application. The server one is essentially an embed grizzly server exposing REST services and the client should be an android app. For exposing REST services I'm using spring.My resource is this:

@Component
@Path("/info")
public class InfoResource {

private static Logger logger = Logger.getLogger(InfoResource.class);

@SuppressWarnings("unchecked")
@Path("/")
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response info(){

    Response response = null;

    JSONObject jsonObject = new JSONObject();

    jsonObject.put("Server Name", "Woghetto Server");
    jsonObject.put("Date", new Date());

    response = Response.ok(jsonObject).build();

    logger.debug(jsonObject.toJSONString());

    response = Response.ok(jsonObject).build();

    return response;
}

The server is not meant for being deployed on an external domain so it should be accessible from the public ip. I'm using jTCPfwd-lite-0.5 for tunneling my private ip to my public IP @ port 80. For testing purpose, I need to access the exposed resources from the same laptop where the server is deployed. Basically, I would like to reach my server, deployed at localhost:8080, from a jersey 2 client using my public ip as target.I tried several ways but mainly I build the client either like this:

Response response = client.target("http://" + targetAddress + ":80/info")  
.request().get();

and what I get is this stacktrace

javax.ws.rs.ProcessingException: java.net.UnknownHostException: 109.134.124.94%0A 
at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:244)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:254)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:671)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:668)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:668)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:402)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:302)
at be.pcab.wonghetto.wonghettoserver.resources.InfoResourceTest.testTunnelingInfoResource(InfoResourceTest.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.net.UnknownHostException: 109.134.124.94%0A
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
at sun.net.www.http.HttpClient.New(HttpClient.java:308)
at sun.net.www.http.HttpClient.New(HttpClient.java:326)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1168)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1104)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:998)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:932)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1512)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:335)
at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:242)
... 36 more

or like this

Response response = client.target(basicURI)
            .request().get();

getting

javax.ws.rs.ProcessingException: URI is not absolute
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:263)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:671)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:668)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:668)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:402)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:302)
at be.pcab.wonghetto.wonghettoserver.resources.InfoResourceTest.testTunnelingInfoResource(InfoResourceTest.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.IllegalArgumentException: URI is not absolute
at java.net.URI.toURL(URI.java:1088)
at org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:272)
at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:242)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:254)
... 35 more

where

basicURI = UriBuilder.fromUri(targetAddress)
            .port(80) .path("/info").build();

and targetAddress is my public IP, so something like 123.456.789.11

The server should be normally reached from outside my router so I guess it should work but, as I told before, I need to test the application and I would like to do it from a JUnit test case. I suppose that I could test it also from the android app but doesn't seem to me neither a good practice nor a quick one.

Any help is appreciated!!

volbollo
  • 11
  • 2
  • 1
    Note `109.134.124.94%0A` - %0A is not a valid component of an IP address. e.g. you have a `\n` character in the IP address. – Marc B Jul 16 '15 at 14:24
  • I noticed that but I hadn't realized it was because of `\n` – volbollo Jul 16 '15 at 14:39
  • I made the change...I currently get a 404 answer from the server which is better but doesn't solve the problem...thanks anyway! – volbollo Jul 16 '15 at 14:47
  • some times port 8080 is used by other services, try another port like 8081 or some thing else – Maytham Fahmi Jul 16 '15 at 15:18
  • I'm trying also with 8082...actually I use more often 8082 instead of 8080 – volbollo Jul 16 '15 at 15:22
  • can you share the testcase (on github)? – alexey Jul 17 '15 at 06:15
  • @alexey here you are, the project is [Wonghetto-project](https://github.com/pcabras/WonghettoRepo/tree/PerformingTests) and the right branch is "PerformingTest". The right module is wonghetto-server and InfoResourceTest is under src/test-->be.pcab.wonghetto.wonghettoserver.resources – volbollo Jul 17 '15 at 08:13

1 Answers1

0

I found several problems with your app:

  1. The InfoResource is not registered in Configuration, so /info is never available.
  2. In the InfoResourceTest you don't register servlets:

    WebContainer.registerServlet(webappContext);
    
  3. The InfoResource return JSON object, so probably you wanted to declare it as:

    @Produces(MediaType.APPLICATION_JSON)
    
  4. Once all the changes above applied, the application is accessible directly on localhost:8082, but still not via forwarder server, so there should be something wrong with that part as well.

alexey
  • 1,959
  • 10
  • 9
  • First of thanks for replying...Actually while implementing tests I forgot several step. So, I registered the InfoResource (1), I registered the servlet(2), (3) was not really a problem, it was more related to what I pushed. The idea is to use json as data format to exchange but I locally downgraded for the moment. In my workspace I am trying to retrieve a string and the resource "produces" plain text. And I agree with you, once everything is correct the server is available at localhost:8082 but still not reachable through the public IP. – volbollo Jul 18 '15 at 08:42
  • But AFAIU you're using some forwarder server on the public address, which doesn't forward request to your jersey server. So I don't see any problem with the Jersey server so far. – alexey Jul 18 '15 at 15:46
  • I know the problem is not related to the jersey server (errors were more careless mistake than in dealing with jersey-grizzly API)...I posted the InfoResource for giving a background (and it was a good idea cause you pointed out existing errors ) but the real problem is exactly the forwarder server...sorry if it wasn't clear – volbollo Jul 18 '15 at 16:37
  • It seems to work quite well from command line but I wonder if I can really debug my application as I'like to do meaning with junit instead of making requests from an android client. Anyway for everyone interested, the forwarder application is jTCPfwd-lite, available on sourceforge – volbollo Jul 18 '15 at 16:45