泽西岛Web服务代理 [英] Jersey web service proxy

查看:49
本文介绍了泽西岛Web服务代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个Web服务,该Web服务替代我想向API的外部用户隐藏的另一个服务.基本上,我想扮演中间人,以便能够向solr隐藏的api添加功能.

I am trying to implement a web service that proxies another service that I want to hide from external users of the API. Basically I want to play the middle man to have ability to add functionality to the hidden api which is solr.

我必须遵循以下代码:

@POST
@Path("/update/{collection}")
public Response update(@PathParam("collection") String collection,
        @Context Request request) {
      //extract URL params
      //update URL to target internal web service
      //put body from incoming request to outgoing request
      //send request and relay response back to original requestor
}

我知道我需要重写URL以指向内部可用的服务,并添加来自URL或正文的参数.

I know that I need to rewrite the URL to point to the internally available service adding the parameters coming from either the URL or the body.

在这里,我很困惑如何才能访问原始请求正文并将其传递给内部Web服务,而不必解组内容. Request对象似乎没有给我执行这些操作的方法.

This is where I am confused how can I access the original request body and pass it to the internal web service without having to unmarshall the content? Request object does not seem to give me the methods to performs those actions.

我正在寻找应该使用对我有帮助的潜在方法的对象.如果有人知道我还没有真正找到针对类似或便携式行为的东西,我也希望获得一些文档.

I am looking for Objects I should be using with potential methods that would help me. I would also like to get some documentation if someone knows any I have not really found anything targeting similar or portable behaviour.

推荐答案

根据JSR-311规范的第4.2.4节,所有JAX-RS实现都必须以字节[],字符串或InputStream的形式提供对请求正文的访问

Per section 4.2.4 of the JSR-311 spec, all JAX-RS implementations must provide access to the request body as byte[], String, or InputStream.

您可以使用UriInfo来获取有关查询参数的信息.看起来像这样:

You can use UriInfo to get information on the query parameters. It would look something like this:

@POST
@Path("/update/{collection}")
public Response update(@PathParam("collection") String collection, @Context UriInfo info, InputStream inputStream) 
{
     String fullPath = info.getAbsolutePath().toASCIIString();
     System.out.println("full request path: " + fullPath);

     // query params are also available from a map.  query params can be repeated,
     // so the Map values are actually Lists.  getFirst is a convenience method
     // to get the value of the first occurrence of a given query param
     String foo = info.getQueryParameters().getFirst("bar");

     // do the rewrite...
     String newURL = SomeOtherClass.rewrite(fullPath);

     // the InputStream will have the body of the request.  use your favorite
     // HTTP client to make the request to Solr.
     String solrResponse = SomeHttpLibrary.post(newURL, inputStream);

     // send the response back to the client
     return Response.ok(solrResponse).build();

另一个想法.看起来您只是在重写请求并传递给Solr.您可以通过其他几种方式来做到这一点.

One other thought. It looks like you're simply rewriting the requests and passing through to Solr. There are a few others ways that you could do this.

如果您恰好在Java应用程序服务器或Servlet容器的前面有一个Web服务器,则有可能无需编写任何Java代码即可完成您的任务.除非重写条件极其复杂,否则我个人的喜好是尝试使用Apache mod_proxy和mod_rewrite.

If you happen to have a web server in front of your Java app server or Servlet container, you could potentially accomplish your task without writing any Java code. Unless the rewrite conditions were extremely complex, my personal preference would be to try doing this with Apache mod_proxy and mod_rewrite.

还有一些可用的Java库,这些库将在URL到达应用服务器后但在到达您的代码之前重写URL.例如, https://code.google.com/p/urlrewritefilter/.使用类似的方法,您只需要编写一个调用Solr的非常简单的方法,因为在到达您的REST资源之前,URL将被重写.作为记录,我实际上并没有尝试过将特定库与Jersey一起使用.

There are also libraries for Java available that will rewrite URLs after they hit the app server but before they reach your code. For instance, https://code.google.com/p/urlrewritefilter/. With something like that, you'd only need to write a very simple method that invoked Solr because the URL would be rewritten before it hits your REST resource. For the record, I haven't actually tried using that particular library with Jersey.

这篇关于泽西岛Web服务代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆