简体   繁体   English

Servlet过滤器与CXF拦截器之间用于修改请求和响应内容?

[英]Servlet filter vs. CXF interceptor for modifying request & response content?

We have built some REST (jax-rs) web services using Apache CXF. 我们使用Apache CXF构建了一些REST(jax-rs)Web服务。 They return a JSON response. 他们返回JSON响应。

I now need to modify some of the request parameters, and response content. 我现在需要修改一些请求参数和响应内容。 (Basically we need to encode/encrypt some of the data that is returned by the service; and decode/decrypt the same data when it is used as a parameter in a subsequent service call.) (基本上我们需要对服务返回的一些数据进行编码/加密;当在后续服务调用中将其用作参数时,解码/解密相同的数据。)

It seems I have at least 4 options here: 看来我这里至少有4个选项:

  1. Use a Servlet filter 使用Servlet过滤器
  2. Use a CXF Interceptor 使用CXF拦截器
  3. Use a JAX-RS Filter 使用JAX-RS过滤器
  4. Don't use any particular pattern, and perform the encode/decode within the actual service logic. 不要使用任何特定模式,并在实际服务逻辑中执行编码/解码。

I've used Servlet Filters before, and understand exactly how to modify request params and response body, so I'm leaning toward that. 我之前使用过Servlet过滤器,并且确切地了解如何修改请求参数和响应体,所以我倾向于此。 However, I'm open to using a CXF Interceptor or JAX-RS filter if that is the more 'correct' way to solve this when using CXF. 但是,我愿意使用CXF Interceptor或JAX-RS过滤器,如果这是使用CXF时解决此问题的更“正确”的方法。 But based on the documentation, I don't really understand how to do this. 但根据文档,我真的不明白如何做到这一点。 For example, do I use the setContent method of the Message object to change the JSON response? 例如,我是否使用Message对象的setContent方法来更改JSON响应? What is the format parameter in that case, just String.class? 在这种情况下,格式参数是什么,只是String.class?

Answering my own question here ... I ended up using a JAX-RS filter, and it worked well, once I got past the lack of documentation. 在这里回答我自己的问题...我最终使用了一个JAX-RS过滤器,一旦我找到了缺少文档,它运行良好。 I used the (rather sparse) documentation from http://cxf.apache.org/docs/jax-rs-filters.html . 我使用了http://cxf.apache.org/docs/jax-rs-filters.html中的(相当稀疏的)文档。 Note despite it's name, a JAX-RS filter is a CXF-specific beast, not part of the JAX-RS standard (as far as I can tell). 注意尽管它的名字,JAX-RS过滤器是特定于CXF的野兽,不是JAX-RS标准的一部分(据我所知)。

Here is some example code: 这是一些示例代码:

@Context
private HttpServletRequest httpRequest;
@Context
private UriInfo uriInfo;

/**
 * @see org.apache.cxf.jaxrs.ext.ResponseHandler#handleResponse(org.apache.cxf.message.Message, org.apache.cxf.jaxrs.model.OperationResourceInfo, javax.ws.rs.core.Response)
 */
public Response handleResponse(Message message, OperationResourceInfo opResourceInfo, Response response) {
    try {

        // log the injected context data; useful for debugging CXF problems
        logContextData(httpRequest, uriInfo);

        OutputStream os = message.getContent(OutputStream.class);
        String relevantData = getDataFromRequest(httpRequest);
        message.setContent(OutputStream.class, new MyOutputStreamWrapper(os, relevantData));

    } catch (CustomException e) {
            // return some status that is related to CustomException
        return Response.status(Status.UNAUTHORIZED).build();
    } catch (Exception e) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).build();
    }

    return response;
}

/**
 * @see org.apache.cxf.jaxrs.ext.RequestHandler#handleRequest(org.apache.cxf.message.Message, org.apache.cxf.jaxrs.model.ClassResourceInfo)
 */
public Response handleRequest(Message message, ClassResourceInfo classResourceInfo) {
    try {

        // log the injected context data; useful for debugging CXF problems
        logContextData();

        String updatedQueryString = buildNewQueryString(this.uriInfo, httpRequest);

        message.put(Message.QUERY_STRING, updatedQueryString);


        // returning null tells CXF to continue the request (i.e. a non-null value would halt the request)
        return null;

    } catch (CustomException e) {
        // return some status that is related to CustomException
        return Response.status(Status.UNAUTHORIZED).build();
    } catch (Exception e) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).build();
    }
}

I should note that the implementation of MyOutputStreamWrapper is the important part in modifying the response content. 我应该注意,MyOutputStreamWrapper的实现是修改响应内容的重要部分。 I couldn't include that source here (in fact my implementation has a different name) due to security considerations. 由于安全考虑,我无法在此处包含该源(实际上我的实现具有不同的名称)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM