best way to resolve an "absolute" URI

Weijun Wang weijun.wang at oracle.com
Tue Apr 5 21:02:32 PDT 2011


Hi Guys

I am writing a test that starts a small HttpServer, and I write my 
HttpHandler like this:

     static class MyHttpHandler implements HttpHandler {
         private final URI root;
         MyHttpHandler(String fileroot) {
             root = new File(fileroot).toURI();
         }
         public void handle(HttpExchange t) throws IOException {
             URI uri = root.resolve(t.getRequestURI());
             ...

Here, root is the root of the webserver, and when a request comes in, 
I'd like to resolve the request URI to the real URI in a file system.

Unfortunately, t.getRequestURI() already returns an absolute URI (say, 
"/"), so the result uri is the same of it. We all know the "/" in "GET 
/" is in fact not an absolute URI.

What is the elegant way to resolve it? I'm now using

   URI uri = root.resolve(t.getRequestURI().toString().substring(1));

but it looks so ugly.

Thanks
Max



More information about the net-dev mailing list