11

We have built some REST (jax-rs) web services using Apache CXF. They return a JSON response.

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:

  1. Use a Servlet filter
  2. Use a CXF Interceptor
  3. Use a JAX-RS Filter
  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. 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. 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? What is the format parameter in that case, just String.class?

1 Answer 1

9

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. I used the (rather sparse) documentation from 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).

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. I couldn't include that source here (in fact my implementation has a different name) due to security considerations.

5
  • 2
    It would be nice if you could explain the reason behind making your choice. Nov 30, 2012 at 14:02
  • i have same question,if CXF has provided interceptor,why still need to have the filter?
    – Chailie
    Dec 19, 2013 at 7:43
  • 3
    @Chailie -- there was no strong reason for going with JAX-RS filter over CXF Interceptor, I think the main reason I chose it was that it was JAX-RS specific, but a CXF Interceptor would have worked also. (It still would've required the same amount of custom coding though.) Note that JAX-RS 2.0 is out now, so JAX-RS filters and interceptors have become part of the spec and would definitely be the way to go with current technology. Jan 2, 2014 at 14:51
  • @andthereitgoes see above comment; note that I threw out option 4 as I really preferred an interceptor/filter type solution (separation of concerns). Also option 1 was not very specific to the CXF environment we were in. Jan 2, 2014 at 14:53
  • Note that with CXF 3.0.0 RequestHandler and ResponseHandler have been removed in favor of the JAX-RS 2.0 ContainerRequestFilter and ContainerResponseFilter classes (see CXF-4882)
    – dpr
    Jan 29, 2020 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.