/**

 * Created by IntelliJ IDEA.
 * User: sgreene
 * Date: May 22, 2003
 * Time: 8:47:38 PM
 * Modified by : Chandar
 *

 * File  : NewAccountHandler.java
 *
 * Overview :
 *     This file defines the handler class for oracleBank  Web Service.
 *  The class implementsGenericHandler class and overrides handleRequest
 *  method to check the initial balance while creating a new account.
 */


package oracle.demo.handlerchain.service;

//XML parser related imports
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;

// JAX-RPC related imports

import javax.xml.rpc.handler.GenericHandler;
import javax.xml.rpc.handler.MessageContext;
import javax.xml.rpc.handler.soap.SOAPMessageContext;
import javax.xml.rpc.soap.SOAPFaultException;
import javax.xml.rpc.JAXRPCException;
import javax.xml.namespace.QName;
import javax.xml.soap.*;
import java.util.Iterator;



public class NewAccountHandler extends GenericHandler{

    // define the fault code
     private javax.xml.namespace.QName faultCode = new javax.xml.namespace.QName(
            javax.xml.rpc.NamespaceConstants.NSURI_SOAP_ENVELOPE,
            "Server",
            javax.xml.rpc.NamespaceConstants.NSPREFIX_SOAP_ENVELOPE);




    public QName[] getHeaders() {
        return new QName[0];
    }

   /*
    * This method overrides the hadleRequest method defined by the GenericHandler
    * class. The method extract SOAP message from the request  and verifies the
    * initial balance if the request is for new account creation.
    */
    public boolean handleRequest(MessageContext ctx){


        // get the SOAPMessageContext
        SOAPMessageContext sctx = (SOAPMessageContext)ctx;

        // retrieve SOAP message from the context
        SOAPMessage sm = sctx.getMessage();

       // variable for soap body
        SOAPBody sb = null;
        try{

            // get soap body from soap message

            sb = sm.getSOAPBody();

            // get all child nodes of soap body element
            NodeList nl = sb.getChildNodes();

            // iterate through child elements
            for(int i = 0; i < nl.getLength(); i++){
                // get the node
                Node node = nl.item(i);

                // get the soap element
                if(node instanceof SOAPElement){
                    SOAPElement se = (SOAPElement)node;


                   // get the name of operation being performed
                   String operationName = se.getLocalName();

                   // if operation is to create new account
                    if(operationName.equals("createAccount")){
                       // call method to check initial balance
                       checkInitialBalance(sctx,se);
                    }
                }
            }
        }
        // catch any exceptions
        catch(SOAPException ex){

            System.out.println(ex.getMessage());
        }
        return true;
    }

  /*
  *This method check the initial balance for the new account creation request
  */
    private void checkInitialBalance(SOAPMessageContext sctx,SOAPElement se) {
        SOAPFactory sf = null;
        Detail det = null;
        SOAPMessage sm = null;
        MessageFactory mf = null;
        try{
            // create instance of SOAP factory

            sf= SOAPFactory.newInstance();
            det = sf.createDetail();

            // create instance of Message Factory
            mf = MessageFactory.newInstance();
            // create message
            sm = mf.createMessage();
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
        // get all child nodes of input SOAP element
        NodeList nl = se.getChildNodes();

        float amt = 0f;
        // iteratre through each element

        for(int i = 0; i < nl.getLength();i++){
            // get a node
            Node node = nl.item(i);
            if(node instanceof SOAPElement){
                // get SOAP element
                SOAPElement s = (SOAPElement)node;
                if(s.getLocalName().equals("float_2")){
                    // get the values of amount
                    amt = new Float(s.getValue().trim()).floatValue();
                }
            }
        }
        if(amt <= 0f){
                throw new JAXRPCException(new SOAPFaultException(faultCode,
                   "Insufficient funds for new account.  You must start an account"+
                   "with more than $0",  "",det));


        }
    }

  /*
    * This method overrides the handleFault method of GenericHandler
    */
    public boolean handleFault(MessageContext ctx){
        System.out.println("handling fault");
        return false;
    }

  /*
    * This method overrides the handleResponse method of GenericHandler
    */
    public boolean handleResponse(MessageContext ctx){
        System.out.println("handling response");
        return true;
    }



}

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy