Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The request handler implementation can either modify the input Message and let the request to proceed or block the request
by returning a non-null Response.

A response filter implementation can get an access to OperationResourceInfo object representing a method to be invoked on a resource class :

Code Block
java
java

OperationResourceInfo ori = exchange.get(OperationResourceInfo.class);

Use OperationResourceInfo in your filter with care. In principle a given request chain may have filters which may want to overwrite Accept or ContentType message headers which might lead to another method be selected. However if you know no such filters (will) exist in your application then you might want to check an OperationResourceInfo instance as part of your filter logic.

When modifying an input message, one would typically want to replace a message input stream or one of its headers, such as ContentType :

Code Block
java
java

InputStream is = message.getContent(InputStream.class);
message.setContent(new MyFilterInputStream(is));
message.put(m.put(Message.ACCEPT_CONTENT_TYPE, "custom/media"); 
Code Block
java
java

public interface ResponseHandler {
    
    Response handleResponse(Message outputMessage,
                           OperationResourceInfo invokedOperation, 
               
public interface ResponseHandler {
    
    Response handleResponse(Message outputMessage,
            Response response);

}

The response handler implementation can optionally overwrite or modify the application Response or modify the output message. When modifying an output message, one may want to either replace an output stream before message body providers attempt to write to it or replace the actual response object :

Code Block
java
java

// replace an output stream
OutputStream os = message.getContent(OutputStream.class);
message.setContent(new MyFilterOutputStream(os));

// replace an actual object
response.setEntity(new MyWrapper(response.getEntity()))
// or OperationResourceInfousing invokedOperation, 
               a low-level Message api if needed
MessageContentsList objs = MessageContentsList.getContentsList(message);
if (objs !== null && objs.size() == 1) {
    Object responseObj = objs.remove(0);
     Response responseobj.add(new MyWrapper(responseObj));

}

The response handler implementation can optionally overwrite or modify the application ResponsePlease see this blog entry for another example of when response filters can be useful.

Multiple request and response handlers are supported.

...