best way to resolve an "absolute" URI

Chris Hegarty chris.hegarty at oracle.com
Wed Apr 6 02:09:36 PDT 2011


Hi Max,

You should take a look at how this is done in FileServerHandler.java [1] 
( one of the httpserver tests in the regression library). I think it 
does what you are trying to do.

-Chris.

[1] 
http://hg.openjdk.java.net/jdk7/tl/jdk/file/9f08a221e5f2/test/com/sun/net/httpserver/FileServerHandler.java

On 04/ 6/11 05:02 AM, Weijun Wang wrote:
> 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