FileDocCategorySizeDatePackage
ClientHandler.javaAPI DocExample6185Wed Oct 23 14:29:32 BST 2002ora.jwsnut.chapter6.client

ClientHandler

public class ClientHandler extends javax.xml.rpc.handler.GenericHandler
SOAP message handler used on the client side of the handler book service.

Fields Summary
private static final String
NS_URI
private static final String
NS_PREFIX
private static final QName
authHeader
private static final QName
timeHeader
private static SOAPFactory
factory
private static Name
authHeaderName
private static Name
timeHeaderName
private static QName[]
headers
private boolean
debug
private String
userName
private String
password
Constructors Summary
Methods Summary
public voiddestroy()

        if (debug) {
            System.out.println("Client-side handler destroyed");
        }
    
public javax.xml.namespace.QName[]getHeaders()

        return headers;
    
public booleanhandleRequest(javax.xml.rpc.handler.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;
    
public booleanhandleResponse(javax.xml.rpc.handler.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;
    
public voidinit(javax.xml.rpc.handler.HandlerInfo info)

       
    // Performs initialization
        
        
        // 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 + "]");
        }