Chad Gorshing
Chad Gorshing

Reputation: 3118

Getting Path (context root) to the Application in Restlet

I am needing to get the application root within a Restlet resource class (it extends ServerResource). My end goal is trying to return a full explicit path to another Resource.

I am currently using getRequest().getResourceRef().getPath() and this almost gets me what I need. This does not return the full URL (like http://example.com/app), it returns to me /resourceName. So two problems I'm having with that, one is it is missing the schema (the http or https part) and server name, the other is it does not return where the application has been mounted to.

So given a person resource at 'http://dev.example.com/app_name/person', I would like to find a way to get back 'http://dev.example.com/app_name'.

I am using Restlet 2.0 RC3 and deploying it to GAE.

Upvotes: 4

Views: 6219

Answers (4)

Corin
Corin

Reputation: 2467

The servlet's context is accessible from the restlet's application:

org.restlet.Application app = org.restlet.Application.getCurrent();
javax.servlet.ServletContext ctx = ((javax.servlet.ServletContext) app.getContext().getAttributes().get("org.restlet.ext.servlet.ServletContext"));
String path = ctx.getResource("").toString();

Upvotes: 0

Paul
Paul

Reputation: 11

request.getRootRef() or request.getHostRef()?

Upvotes: 1

Melnosta
Melnosta

Reputation: 11

Just get the base url from service context, then share it with the resources and add resource path if needed.

MyServlet.init():

String contextPath = getServletContext().getContextPath();
getApplication().getContext().getAttributes().put("contextPath", contextPath);

MyResource:

String contextPath = getContext().getAttributes().get("contextPath");

Upvotes: 1

Chad Gorshing
Chad Gorshing

Reputation: 3118

It looks like getRequest().getRootRef().toString() gives me what I want. I tried using a combination of method calls of getRequest().getRootRef() (like getPath or getRelativePart) but either they gave me something I didn't want or null.

Upvotes: 2

Related Questions