Question

I'm trying to implement a custom Grizzly HttpHandler but fail miserably trying to simply extract the path info from the incoming requests. See the minimal example below:

public class PathInfoTest {
    public static void main(String[] args) throws IOException {
        final HttpServer httpServer = new HttpServer();
        final NetworkListener nl = new NetworkListener(
                "grizzly", "localhost", 8080);
        httpServer.addListener(nl);
        httpServer.getServerConfiguration().addHttpHandler(
                new HandlerImpl(), "/test");   
        httpServer.start();
        System.in.read();
    }

    private static class HandlerImpl extends HttpHandler {
        @Override
        public void service(Request request, Response response)
                throws Exception {

            System.out.println(request.getPathInfo());
            System.out.println(request.getContextPath());
            System.out.println(request.getDecodedRequestURI());
            System.out.println(request.getHttpHandlerPath());
    }
}

I thought this would tell Grizzly that all incoming requests where the URL starts with "/test" should be handled by HandlerImpl, which seems to work so far. However, when doing a GET to http://localhost:8080/test/foo, this code prints the following to stdout:

null
/test
/test/foo
null

My main concern is the first null, which should be the path info. I expect it to be foo in this example, not null. Can someone explain to me:

  1. why both, getHttpHandlerPath() and getPathInfo() return null in this example?
  2. Also, I suspect the latter being a consequence of the first, is this right?
  3. How can I get my hands on the "unrouted" part of the URL in Grizzly?
Was it helpful?

Solution

You have to use asterisk in mapping (similar to Servlets) to see the correct pathInfo value. For example please use the following mapping:

httpServer.getServerConfiguration().addHttpHandler(
     new HandlerImpl(), "/test/myhandler/*");

and make a request to http://localhost:8080/test/myhandler/foo/bar

and the result will be:

/foo/bar
/test
/test/myhandler/foo/bar
/myhandler
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top