2010-04-29 94 views
4

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

我目前正在使用getRequest().getResourceRef().getPath(),这几乎让我有我需要的东西。这不会返回完整的URL(如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。

回答

2

它看起来像getRequest().getRootRef().toString()给了我我想要的。我试着用getRequest().getRootRef()的方法调用(如的getPathgetRelativePart)的结合,不过他们给我的东西我不想要或

1

request.getRootRef()request.getHostRef()

1

只需从服务上下文获取基础URL,然后与资源共享并根据需要添加资源路径。

MyServlet.init(): 

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

MyResource: 

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

servlet的上下文是从的Restlet的应用程序访问:

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(); 
相关问题