Getting the path (context root) to the application in Restlet

I need to get the root of the application in the Restlet resource class (it extends ServerResource). My ultimate goal is to try to return the full explicit path to another resource.

I am currently using getRequest().getResourceRef().getPath() and it almost gives me what I need. This does not return the full URL (e.g. http://example.com/app ), it returns me / resourceName. So, the two problems that I encountered are the lack of a scheme (part of http or https) and the server name, and the other is returning to where the application was installed.

So, set the person resource to http://dev.example.com/app_name/person ', I would like to find a way to return' http://dev.example.com/app_name '.

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

+4
source share
4 answers

It looks like getRequest().getRootRef().toString() gives me what I want. I tried using a combination of method calls getRequest().getRootRef() (for example, getPath or getRelativePart), but either they gave me what I did not want, or null.

+2
source

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

+1
source

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

 MyServlet.init(): String contextPath = getServletContext().getContextPath(); getApplication().getContext().getAttributes().put("contextPath", contextPath); MyResource: String contextPath = getContext().getAttributes().get("contextPath"); 
+1
source

The servlet context is accessible from the restart 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(); 
0
source

Source: https://habr.com/ru/post/1308374/


All Articles