55.2. Processing messages
Overview
handleMessage() method is invoked. It receives that message data as a Message object. Along with the actual contents of the message, the Message object may contain a number of properties related to the message or the message processing state. The exact contents of the Message object depends on the interceptors preceding the current interceptor in the chain.
Getting the message contents
Message interface provides two methods that can be used in extracting the message contents:
public <T> T getContent(java.lang.Class<T> format);ThegetContent()method returns the content of the message in an object of the specified class. If the contents are not available as an instance of the specified class, null is returned. The list of available content types is determined by the interceptor's location on the interceptor chain and the direction of the interceptor chain.public Collection<Attachment> getAttachments();ThegetAttachments()method returns a JavaCollectionobject containing any binary attachments associated with the message. The attachments are stored inorg.apache.cxf.message.Attachmentobjects.Attachmentobjects provide methods for managing the binary data.ImportantAttachments are only available after the attachment processing interceptors have executed.
Determining the message's direction
getExchange() method. As shown in Example 55.1, “Getting the message exchange”, getExchange() does not take any parameters and returns the message exchange as a org.apache.cxf.message.Exchange object.
Example 55.1. Getting the message exchange
Exchange getExchange();Exchange object has four methods, shown in Example 55.2, “Getting messages from a message exchange”, for getting the messages associated with an exchange. Each method will either return the message as a org.apache.cxf.Message object or it will return null if the message does not exist.
Example 55.2. Getting messages from a message exchange
Message getInMessage();Message getInFaultMessage();Message getOutMessage();Message getOutFaultMessage();Example 55.3. Checking the direction of a message chain
public static boolean isOutbound()
{
Exchange exchange = message.getExchange();
return message != null
&& exchange != null
&& (message == exchange.getOutMessage()
|| message == exchange.getOutFaultMessage());
}Example
Example 55.4. Example message processing method
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
public class StreamInterceptor extends AbstractPhaseInterceptor<Message>
{
...
public void handleMessage(Message message)
{
boolean isOutbound = false;
isOutbound = message == message.getExchange().getOutMessage()
|| message == message.getExchange().getOutFaultMessage();
if (!isOutbound)
{
try
{
InputStream is = message.getContent(InputStream.class);
GZIPInputStream zipInput = new GZIPInputStream(is);
message.setContent(InputStream.class, zipInput);
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
else
{
// zip the outbound message
}
}
...
}
Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.