获取路径(上下文根)到Restlet中的应用程序

时间:2010-04-29 15:48:46

标签: restlet

我需要在Restlet资源类中获取应用程序根(它扩展了ServerResource)。我的最终目标是尝试返回另一个资源的完整显式路径。

我目前正在使用getRequest().getResourceRef().getPath(),这几乎可以满足我的需要。这不会返回完整的网址(如http://example.com/app),它会返回给我 / resourceName 。所以我遇到的两个问题是,一个是缺少模式(http或https部分)和服务器名称,另一个是它没有返回应用程序已安装到的位置。

因此,根据“http://dev.example.com/app_name/person”的人力资源,我想找到一种方法来取回“http://dev.example.com/app_name”。

我正在使用Restlet 2.0 RC3并将其部署到GAE。

4 个答案:

答案 0 :(得分:2)

看起来getRequest().getRootRef().toString()给了我想要的东西。我尝试使用getRequest().getRootRef()的方法调用的组合(如 getPath getRelativePart )但是他们给了我一些我不想要的东西或 null

答案 1 :(得分:1)

request.getRootRef()request.getHostRef()

答案 2 :(得分:1)

只需从服务上下文获取基本URL,然后与资源共享它,并在需要时添加资源路径。

MyServlet.init():

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

MyResource:

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

答案 3 :(得分:0)

可以从restlet的应用程序访问servlet的上下文:

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();