FileDocCategorySizeDatePackage
ServiceHandler.javaAPI DocExample6498Thu Oct 24 12:39:52 BST 2002ora.jwsnut.chapter6.handlerbookservice

ServiceHandler

public class ServiceHandler extends javax.xml.rpc.handler.GenericHandler
SOAP message handler used on the server 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 final SimpleDateFormat
format
private static SOAPFactory
factory
private static Name
authHeaderName
private static Name
timeHeaderName
private static QName[]
headers
private boolean
debug
Constructors Summary
Methods Summary
public voiddestroy()

        if (debug) {
            System.out.println("Server-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("handleRequest called");
            }

            String userName = null;
            String password = null;
            SOAPMessage message = ((SOAPMessageContext)ctx).getMessage();
            SOAPHeader header = message.getSOAPPart().getEnvelope().getHeader();
            if (header != null) {
                
                // Locate the "auth" header
                Iterator iter = header.getChildElements(authHeaderName);
                if (iter.hasNext()) {
                    SOAPElement element = (SOAPElement)iter.next();
                    Iterator children = element.getChildElements();
                    while (children.hasNext()) {
                        SOAPElement childElement = (SOAPElement)children.next();
                        String localPart = childElement.getElementName().getLocalName();
                        if (localPart.equals("UserName") && userName == null) {
                            userName = childElement.getValue();
                        } else if (localPart.equals("Password") && password == null) {
                            password = childElement.getValue();
                        }                        
                    }                 
                    
                    // Remove this header
                    element.detachNode();
                }                
                
                // Remove any other "auth" headers.
                while (iter.hasNext()) {
                    ((SOAPElement)iter.next()).detachNode();                    
                }
                
                // Install the user name and password in the MessageContext.
                // This installs null if either attribute was missing.
                ctx.setProperty(HandlerBookServiceConstants.USERNAME_PROPERTY, userName);
                ctx.setProperty(HandlerBookServiceConstants.PASSWORD_PROPERTY, password);
                
                if (debug) {
                    System.out.println("Got auth: user: [" + userName + "], password: [" + password + "]");
                }
            }            
        } 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) {
                header = message.getSOAPPart().getEnvelope().addHeader();
            }
            SOAPElement element = header.addChildElement(timeHeaderName);
            String text = format.format(new Date());
            element.addTextNode(text);   
            
            if (debug) {
                System.out.println("Added time header, value " + text);
            }
        } catch (SOAPException ex) {
            throw new JAXRPCException("Error in handleRequest", ex);
        }           
        return true;
    
public voidinit(javax.xml.rpc.handler.HandlerInfo info)

    
    // Performs initialization
        

        // 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();
        
        // 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);
        }
            
        if (debug) {
            System.out.println("Server-side handler initialized");
        }