2

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?
Waldheinz
  • 10,399
  • 3
  • 31
  • 61

1 Answers1

3

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
alexey
  • 1,959
  • 10
  • 9
  • Thanks a bunch, this does the trick. This should definitively be part of the documentation. – Waldheinz Sep 24 '13 at 09:46
  • This mapping corresponds to the Servlet mapping, but I agree we have to better document addHttpHandler() method to make it clear. Thank you for the feedback :) – alexey Sep 25 '13 at 00:30