Normal message processing is handled by the handleMessage()
method.
The handleMessage()
method receives a LogicalMessageContext
object that provides access to the message body and any properties stored in the message context.
The handleMessage()
method returns either true
or
false
depending on how message processing is to continue. It can also throw an exception.
The LogicalMessageContext
object passed into logical message handlers allows access
to the message body using the context's getMessage()
method. The
getMessage()
method, shown in Example 21.2,
returns the message payload as a LogicalMessage
object.
Example 21.2. Method for Getting the Message Payload in a Logical Handler
LogicalMessage getMessage();
Once you have the LogicalMessage
object, you can use it to manipulate the message body.
The LogicalMessage
interface, shown in Example 21.3,
has getters and setters for working with the actual message body.
Example 21.3. Logical Message Holder
LogicalMessage {Source getPayload();
Object getPayload(JAXBContext context);
void setPayload(Object payload,
JAXBContext context);void setPayload(Source payload);
}
![]() | Important |
---|---|
The contents of the message payload are determined by the type of binding in use. The SOAP binding only allows access to the SOAP body of the message. The XML binding allows access to the entire message body. |
One pair of getters and setters of the logical message work with the message payload as a
javax.xml.transform.dom.DOMSource
object.
The getPayload()
method that has no parameters returns the message payload as
a DOMSource
object. The returned object is the actual message payload. Any changes made
to the returned object change the message body immediately.
You can replace the body of the message with a DOMSource
object using the
setPayload()
method that takes the single Source
object.
The other pair of getters and setters allow you to work with the message payload as a JAXB object. They use a
JAXBContext
object to transform the message payload into JAXB objects.
To use the JAXB objects you do the following:
Get a JAXBContext
object that can manage the data types in the message body.
For information on creating a JAXBContext
object see Using A JAXBContext
Object.
Get the message body as shown in Example 21.4.
Example 21.4. Getting the Message Body as a JAXB Object
JAXBContext jaxbc = JAXBContext(myObjectFactory.class);
Object body = message.getPayload(jaxbc);
Cast the returned object to the proper type.
Manipulate the message body as needed.
Put the updated message body back into the context as shown in Example 21.5.
The logical message context passed into a logical handler is an instance of the application's message context and can access
all of the properties stored in it. Handlers have access to properties at both the APPLICATION
scope and the
HANDLER
scope.
Like the application's message context, the logical message context is a subclass of Java Map
.
To access the properties stored in the context, you use the get()
method and put()
method inherited from the Map
interface.
By default, any properties you set in the message context from inside a logical handler are assigned a scope of
HANDLER
. If you want the application code to be able to access the property you need to use the context's
setScope()
method to explicitly set the property's scope to APPLICATION.
For more information on working with properties in the message context see Understanding Contexts.
It is often important to know the direction a message is passing through the handler chain. For example, you would want to retrieve a security token from incoming requests and attach a security token to an outgoing response.
The direction of the message is stored in the message context's outbound message property. You retrieve the outbound message property from the
message context using the MessageContext.MESSAGE_OUTBOUND_PROPERTY
key as shown in Example 21.6.
Example 21.6. Getting the Message's Direction from the SOAP Message Context
Boolean outbound; outbound = (Boolean)smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
The property is stored as a Boolean
object. You can use the object's booleanValue()
method to determine the property's value. If the property is set to true
, the message is outbound. If the property is set to
false
the message is inbound.
How the handleMessage()
method completes its message processing has a direct impact on how message processing proceeds.
It can complete by doing one of the following actions:
Return true
—Returning true
signals to the FUSE Services Framework runtime that message
processing should continue normally. The next handler, if any, has its handleMessage()
invoked.
Return false
—Returning false
signals to the FUSE Services Framework runtime that normal
message processing must stop. How the runtime proceeds depends on the message exchange pattern in use for the
current message.
For request-response message exchanges the following happens:
The direction of message processing is reversed.
For example, if a request is being processed by a service provider, the message stops progressing toward the service's implementation object. Instead, it is sent back towards the binding for return to the consumer that originated the request.
Any message handlers that reside along the handler chain in the new processing direction have their handleMessage()
method
invoked in the order in which they reside in the chain.
When the message reaches the end of the handler chain it is dispatched.
For one-way message exchanges the following happens:
Message processing stops.
All previously invoked message handlers have their close()
method invoked.
The message is dispatched.
Throw a ProtocolException
exception—Throwing a
ProtocolException
exception, or a subclass of this exception, signals the FUSE Services Framework runtime
that fault message processing is beginning. How the runtime proceeds depends on the message exchange pattern in use for the
current message.
For request-response message exchanges the following happens:
If the handler has not already created a fault message, the runtime wraps the message in a fault message.
The direction of message processing is reversed.
For example, if a request is being processed by a service provider, the message stops progressing toward the service's implementation object. Instead, it is sent back towards the binding for return to the consumer that originated the request.
Any message handlers that reside along the handler chain in the new processing direction have their handleFault()
method
invoked in the order in which they reside in the chain.
When the fault message reaches the end of the handler chain it is dispatched.
For one-way message exchanges the following happens:
If the handler has not already created a fault message, the runtime wraps the message in a fault message.
Message processing stops.
All previously invoked message handlers have their close()
method invoked.
The fault message is dispatched.
Throw any other runtime exception—Throwing a runtime exception other than a
ProtocolException
exception signals the FUSE Services Framework runtime that message processing is to stop. All
previously invoked message handlers have the close()
method invoked and the exception is dispatched. If
the message is part of a request-response message exchange, the exception is dispatched so that it is returned to the consumer
that originated the request.
Example 21.7 shows an implementation of handleMessage()
message
for a logical message handler that is used by a service consumer. It processes requests before they are sent to the service
provider.
Example 21.7. Logical Message Handler Message Processing
public class SmallNumberHandler implements LogicalHandler<LogicalMessageContext> { public final boolean handleMessage(LogicalMessageContext messageContext) { try { boolean outbound = (Boolean)messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); if (outbound){ LogicalMessage msg = messageContext.getMessage();
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class); Object payload = msg.getPayload(jaxbContext);
if (payload instanceof JAXBElement) { payload = ((JAXBElement)payload).getValue(); } if (payload instanceof AddNumbers)
{ AddNumbers req = (AddNumbers)payload; int a = req.getArg0(); int b = req.getArg1(); int answer = a + b; if (answer < 20)
{ AddNumbersResponse resp = new AddNumbersResponse();
resp.setReturn(answer); msg.setPayload(new ObjectFactory().createAddNumbersResponse(resp), jaxbContext); return false;
} } else { throw new WebServiceException("Bad Request");
} } return true;
} catch (JAXBException ex)
{ throw new ProtocolException(ex); } } ... }
The code in Example 21.7 does the following:
Checks if the message is an outbound request. If the message is an outbound request, the handler does additional message processing. | |
Gets the | |
Gets the actual message payload as a JAXB object. | |
Checks to make sure the request is of the correct type. If it is, the handler continues processing the message. | |
Checks the value of the sum. If it is less than the threshold of 20 then it builds a response and returns it to the client. | |
Builds the response. | |
Returns | |
Throws a runtime exception if the message is not of the correct type. This exception is returned to the client. | |
Returns Message processing continues normally. | |
Throws a The exception is passed back to the client after it is processed by the |