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

发布于 2024-08-30 23:16:49 字数 603 浏览 3 评论 0原文

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

我目前正在使用 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。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

抽个烟儿 2024-09-06 23:16:49

看起来 getRequest().getRootRef().toString() 给了我我想要的东西。我尝试使用 getRequest().getRootRef() 的方法调用组合(例如 getPathgetRelativePart),但它们要么给了我一些我想要的东西不想要或无效

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.

多情癖 2024-09-06 23:16:49

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

MyServlet.init():

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

MyResource:

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

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");
黄昏下泛黄的笔记 2024-09-06 23:16:49

request.getRootRef() 还是 request.getHostRef()

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

若言繁花未落 2024-09-06 23:16:49

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文