best way to resolve an "absolute" URI
Weijun Wang
weijun.wang at oracle.com
Wed Apr 6 03:31:47 PDT 2011
It seems I shouldn't use t.getRequestURI() as a real URI, its getPath()
is much better.
Thanks
Max
On 04/06/2011 05:09 PM, Chris Hegarty wrote:
> 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