import java.io.*;
import java.net.*;
import java.util.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;


/**
 * An SOAP servlet for converting currencies.
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class CurrencyConverterSOAPServlet extends AbstractHttpServlet
{
    /**
     * Default Constructor
     */
    public CurrencyConverterSOAPServlet()
    {
       super();
    }

    /**
     * Explicit Value Constructor
     *
     * @param in   The HttpInputStream to read from
     * @param out  The HttpOutputStream to write to
     */
    public CurrencyConverterSOAPServlet(HttpInputStream  in, 
                                        HttpOutputStream out)
    {
       super(in, out);
    }
    


    /**
     * Handle a SOAP request for a currency converter
     *
     * Note: This method only handles SOAP requests for
     *       a particular service.  It is for illustrative
     *       purposes only.
     *
     * @param request   Contents of the request
     * @param response  Used to generate the response
     */
    protected void doPost(HttpRequest request, 
			  HttpResponse response)
    {
       byte[]                        content;
       ByteArrayInputStream          bis;        
       CurrencyConverterSOAPHandler  handler;
       CurrencyConverter             cc;
       double                        amount, rate, value;
       HttpInputStream               is;
       HttpOutputStream              os;
       InputSource                   inputSource;
       int                           length;
       SAXParser                     parser;
       SAXParserFactory              factory;
       String                        contentString, from, soapResponse;
       String                        soapRequest, to;



       try 
       {
          // Get the Content-Length of the SOAP request
          length = request.getContentLength();	    

          // Read the SOAP request
          request.readContent(in);
          contentString = new String(request.getContent());
          soapRequest = URLDecoder.decode(contentString, "UTF-8");

          // Create a ByteArrayInputStream from the SOAP request
          // (for the SAX Parser)
          bis = new ByteArrayInputStream(soapRequest.getBytes());

          // Parse the request
          inputSource = new InputSource(bis);
          factory     = SAXParserFactory.newInstance();
          parser      = factory.newSAXParser();
          handler     = new CurrencyConverterSOAPHandler("convert");
          parser.parse(inputSource, handler);
          amount      = handler.getAmount();
          from        = handler.getFromCurrency();
          to          = handler.getToCurrency();

          // Perform the conversion
          cc = new CurrencyConverterImpl();
          value  = cc.convert(amount, from, to);

          // Create the SOAP response
          // (Note: This version does not use the fault
          //         feature of SOAP but could)
          soapResponse = "<?xml version=\"1.0\"?>"+
             "<env:Envelope>"+
             "<env:Body>"+
             "<CurrencyConverterResponse>"+
             "<convert>"+
             "<amount>"+value+"</amount>"+
             "</convert>"+
             "</CurrencyConverterResponse>"+
             "</env:Body>"+
              "</env:Envelope>";

          // Setup the HTTP response
          response.setContentLength(soapResponse.length());
          response.setStatus(HttpResponse.SC_OK);
          response.setContentType("text/xml");

          // Put the content in the response
          response.setContent(soapResponse.getBytes());
                
          // Transmit the response
          response.write(out);
       } 
       catch (Exception e) 
       {
          response.sendError(HttpResponse.SC_NOT_FOUND, out);
       } 
    }



}
