package ora.jwsnut.chapter6.client;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import javax.xml.namespace.QName;
import javax.xml.rpc.JAXRPCException;
import javax.xml.rpc.handler.GenericHandler;
import javax.xml.rpc.handler.HandlerInfo;
import javax.xml.rpc.handler.MessageContext;
import javax.xml.rpc.handler.soap.SOAPMessageContext;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
/**
* SOAP message handler used on the client side
* of the handler book service.
*/
public class ClientHandler extends GenericHandler {
// Namespace for types used by this handler
private static final String NS_URI = "urn:jwsnut.chapter6.handlerbookservice/wsdl/HandlerBookQuery";
// Namespace prefix used by this handler
private static final String NS_PREFIX = "tns";
// Name of the authentication header as a QName
private static final QName authHeader = new QName(NS_URI, "auth");
// Name of the date/time header as a QName
private static final QName timeHeader = new QName(NS_URI, "time");
// SOAPFactory used to create Names
private static SOAPFactory factory;
// Name of the authentication header as a Name
private static Name authHeaderName;
// Name of the date/time header as a Name
private static Name timeHeaderName;
// The headers that this handler is associated with
private static QName[] headers;
// Debug flag
private boolean debug;
// The user name and password to be used
private String userName;
private String password;
// Performs initialization
public void init(HandlerInfo info) {
// Create Names
try {
factory = SOAPFactory.newInstance();
authHeaderName = factory.createName("auth", NS_PREFIX, NS_URI);
timeHeaderName = factory.createName("time", NS_PREFIX, NS_URI);
headers = new QName[] { authHeader, timeHeader };
} catch (SOAPException ex) {
throw new JAXRPCException("Init failure", ex);
}
// Get the user name and password to use
userName = System.getProperty("HandlerBooks.user");
password = System.getProperty("HandlerBooks.password");
// Extract the debug setting from the configuration
Map config = info.getHandlerConfig();
String value = (String)config.get("debug");
debug = value == null ? false : Boolean.valueOf(value).booleanValue();
if (debug) {
System.out.println("Client-side handler initialized");
System.out.println("User: [" + userName + "], password: [" + password + "]");
}
}
// Called when the handler is no longer required.
public void destroy() {
if (debug) {
System.out.println("Client-side handler destroyed");
}
}
// Gets the headers that this handler can deal with
public QName[] getHeaders() {
return headers;
}
// Called to process a request message
public boolean handleRequest(MessageContext ctx) {
try {
if (debug) {
System.out.println("----------------------\nhandleRequest called");
}
// Add authentication info to the outgoing message,
// adding a SOAPHeader part if it is not already there
if (userName != null && password != null) {
SOAPMessage message = ((SOAPMessageContext)ctx).getMessage();
SOAPHeader header = message.getSOAPPart().getEnvelope().getHeader();
if (header == null) {
header = message.getSOAPPart().getEnvelope().addHeader();
}
SOAPElement element = header.addChildElement(authHeaderName);
element.addChildElement("UserName").addTextNode(userName);
element.addChildElement("Password").addTextNode(password);
if (debug) {
System.out.println("OUTGOING MESSAGE: ");
try {
message.writeTo(System.out);
} catch (IOException ex) {
}
System.out.println();
}
}
} catch (SOAPException ex) {
throw new JAXRPCException("Error in handleRequest", ex);
}
return true;
}
// Called to process a response message
public boolean handleResponse(MessageContext ctx) {
try {
if (debug) {
System.out.println("handleResponse called");
}
SOAPMessage message = ((SOAPMessageContext)ctx).getMessage();
SOAPHeader header = message.getSOAPPart().getEnvelope().getHeader();
if (header != null) {
// Locate the "time" header
Iterator iter = header.getChildElements(timeHeaderName);
if (iter.hasNext()) {
SOAPElement element = (SOAPElement)iter.next();
String value = element.getValue();
// Just print the timer
System.out.println("Request processed at: " + value);
// Remove this header
element.detachNode();
}
// Remove any other "time" headers.
while (iter.hasNext()) {
((SOAPElement)iter.next()).detachNode();
}
}
} catch (SOAPException ex) {
throw new JAXRPCException("Error in handleRequest", ex);
}
return true;
}
} |