/*
 * @author : Chandar
 * @version 1.0
 *
 * Development Environment : Oracle9i JDeveloper
 *

 * Name of the File        : FlightBookingService.java
 *
 * Creation / Modification History
 *
 *    Chandar          25-Jul-2003        Created
 *
 *  Overview :

 *  This class is Web Service implementation class. It defines methods exposed by
 *  the Web Service and also defines methods to process the SOAP headers sent by
 *  the client with each Web Service request.
 */

package oracle.otnsamples.soapheader.webservice;



// SOAP Headers related imports
import oracle.j2ee.ws.HeaderCallback;
import org.apache.soap.Header;

// JNDI related imports
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;


// XML Parser imports
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

// util imports
import java.util.Vector;

// The class must implement HeaderCallback interface in order to process the SOAP
// headers

public class FlightBookingService implements HeaderCallback
{
  String cardNumber;     // credit card number
  String expiryDate;     // expiry date
  String ffpNo;          // frequent flying plan number
  String fromCurrency;   // getExchangeRate -from country
  String toCurrency;     // getExchangeRate -to country

  /**
   * Empty Constructor
   */

  public FlightBookingService()
  {}

  /**
   * This method processes the SOAP headers.
   * @param Header - containing header information
   * @exception    - if processing of headers fail
   */
  public void processHeaders(Header header)
         throws java.io.IOException, oracle.xml.parser.v2.XSLException
  {
     // Get all object containing header entries

     Vector entries = header.getHeaderEntries();

     // process each header. Header will be a valid XML element
     for (int a=0; a < entries.size(); a++ ){

       // get the header element
       Element e = (Element) entries.elementAt(a);

       // get the tag name of the element
       String tagName = e.getTagName();


       if(tagName.equals("ExchangeRate"))

       {
         // get the nodes with name as "FromCurrency"
         NodeList list = e.getElementsByTagName("FromCurrency");

         // get the first node from node list
         Node fCurrency = list.item(0);

         // store the value of node into class variable
         this.fromCurrency = fCurrency.getFirstChild().getNodeValue();

         // get the nodes with name as "ToCurrency"
         list = e.getElementsByTagName("ToCurrency");

         // get the first node from node list

         Node tCurrency = list.item(0);

         // store the value of node into class variable
         this.toCurrency = tCurrency.getFirstChild().getNodeValue();
       }

       // get the nodes with name as "CreditCard"
       else if(tagName.equals("CreditCard")){

       // get the first clild of input element
       NodeList list = e.getElementsByTagName("CardNumber");

       // get the first node from node list
       Node card = list.item(0);
       cardNumber = card.getFirstChild().getNodeValue();


       // get the nodes with name as "ExpiryDate"
       list = e.getElementsByTagName("ExpiryDate");

       // get the first node from node list
       Node expDate = list.item(0);

       // store the value of node into class variable
       expiryDate = expDate.getFirstChild().getNodeValue();
     }

     // get the nodes with name as "ffpNumber"
     else if(tagName.equals("ffpNumber")){

       // get the first child of element
         Node enode = e.getFirstChild();


       //get the value of  element
         ffpNo=  enode.getNodeValue();
     }
   System.out.println("Processed the Header information");
  }
  }

  /**
   * This method inserts booking details into the database
   *
   * @param BookingDetails : Value object containing booking details
   * @param double         : Total amount of ticktes
   * @exception Exception  : if booking fails
   */
  public Integer bookTicket(BookingDetails details,double amount) throws Exception
  {

    // variable for booking id to be returned to client
    Integer bookingID=null;
    try{

    // instantiate the BookingDataManager object
     BookingDataManager  manager = new BookingDataManager (cardNumber,expiryDate,ffpNo);

     // book the ticket
     int returnValue = manager.bookTicket(details,amount);
     bookingID = new Integer(returnValue);
    }
     //catch any exceptions
     catch(Exception e)
     {
       e.printStackTrace();
       // throw the exception message to client
       throw new Exception(e.getMessage());
     }



     // return booking id
     return bookingID;
  }

 /**
   * This method call the Currency Exchange Rate web service from xmethods.net
   * and gets the exchange rate between two countries. The names of countries
   * are retrieved from the SOAP header passed from the client.
   *
   */
  public Float getExchangeRate()
  {
    // variable to store the exchange rate
    Float rate=null ;
    try{
      // instantiate the proxy for the ExchangeRate web service
      CurrencyExchangeServiceStub stub = new CurrencyExchangeServiceStub();


      // get the exchange rate
      rate = stub.getRate(this.fromCurrency, this.toCurrency);
   }
   // catch any exceptions
   catch(Exception e)
   {
     e.printStackTrace();
   }

    // return the exchange rate
    return rate;
  }
}
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