views:

116

answers:

1

I'm unsure what the proper way is to access parts of the requested URL.

In this case, I want to get the requested path without the query variables. This is the only way I found to do it:

String path = getRequest().getResourceRef().getHostIdentifier() + 
     getRequest().getResourceRef().getPath();

The result would be the bold part of this url: http://stackoverflow.com/questions/ask?query=value

I also found about 6 different ways to get the server name (http://stackoverflow.com) but I'm worried that some of them would fail in certain cases that I am unaware of (why would there be 6 different ways to do one thing):

getRequest().getHostRef().getHostIdentifier();
getRequest().getHostRef().getIdentifier();
getRequest().getRootRef().getHostIdentifier();
getRequest().getRootRef().getIdentifier();
getRequest().getResourceRef().getHostIdentifier();

And this seems to get the complete URL with query parameters:

getRequest().getResourceRef().getIdentifier();

Any further explanation would be much appreciated.

+1  A: 

If you're in a UniformResource (or subclass) I think you might be looking for the method getReference(), which returns the URI reference. There are a number of other convenience methods in that class you might be interested in so you don't have to go through the request. See UniformResource (Restlet 2.0).

Everett Toews