Handlers are interceptors that can be easily plugged into the Java API for XML-Based Web Services (JAX-WS) 2.0 runtime environment to do additional processing of inbound and outbound messages.
JAX-WS defines two types of handlers:
In this step, you will write a SOAP protocol handler and a logical handler for the use with JAX-WS services. You will describe the handlers in a custom bindings file using the JAX-WS Bindings Editor .
You will perform following tasks in this step:
SOAPLoggingHandler
This creates the SOAPLoggingHandler class that implements methods from the interface.
MessageContext.MESSAGE_OUTBOUND_PROPERTY
from
SOAPMessageContext. The value of the
MESSAGE_OUTBOUND_PROPERTY
indicates the message direction. A value of
true means that the message is outbound. A value of
false indicates an inbound message. Based on the property value,
logToSystemOut()
shall print either "Outbound message" or "Inbound message." Also, retrieve the
SOAPMessage and print it as shown below.
private void logToSystemOut(SOAPMessageContext context) {
Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outboundProperty.booleanValue()) {
System.out.println("Outbound message:");
} else {
System.out.println("Inbound message:");
}
SOAPMessage message = context.getMessage();
try {
message.writeTo(System.out);
System.out.println("");
} catch (Exception e) {
System.out.println("Exception in handler: " + e);
}
}
Edit the handleMessage() and handleFault() methods to log the messages by calling logToSystemOut() method.
@Override
public boolean handleFault(SOAPMessageContext context) {
System.out.println("\n==== SOAPLoggingHandler#handleFault() ====");
logToSystemOut(context);
System.out.println("==========================================");
return true;
}
@Override
public boolean handleMessage(SOAPMessageContext context) {
System.out.println("\n==== SOAPLoggingHandler#handleMessage() ====");
logToSystemOut(context);
System.out.println("============================================");
return true;
}
In this step, you will implement a ProductLogicalHandler, which handles inbound messages and modifies the message payload; i.e. convert the value of the product name node to uppercase.
Perform the following steps in handleMessage() to modify the message payload; i.e. convert the value of product name node to uppercase.
MessageContext.MESSAGE_OUTBOUND_PROPERTY
from
LogicalMessageContext. The value of
MESSAGE_OUTBOUND_PROPERTY
indicates the message direction. A value of
true means that the message is outbound. A value of
false indicates an inbound message. Ignore further processing, if the message is outbound.
@Override
public boolean handleMessage(LogicalMessageContext context) {
Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
// ignore OUTBOUND requests
if (outboundProperty.booleanValue()) {
return true;
}
// only process INBOUND requests
LogicalMessage lm = context.getMessage();
Source payload = lm.getPayload();
if (payload instanceof DOMSource) {
DOMSource source = ((DOMSource) payload);
Node rootNode = source.getNode();
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodes;
try {
nodes = (NodeList) xpath.evaluate("//*", rootNode, XPathConstants.NODESET);
for (int i = 0; i > nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node.getNodeName().equals("ns3:name")) {
String value = node.getFirstChild().getNodeValue();
node.getFirstChild().setNodeValue(value.toUpperCase());
}
}
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
return true;
}
In this step, you will describe the handlers in custom bindings file using the JAX-WS Bindings Editor .
Since, you have modified the proudctBindings.xml file with additional JAX-WS handlers, you are required to re-generate the Web Service client artifacts.