import java.io.*;
import java.net.*;
import java.util.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

/**
 * A SOAP-based "stub" for a CurrencyCoverter
 *
 * Note: This version does not use the fault feature of SOAP
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class CurrencyConverterStub implements CurrencyConverter
{

    /**
     * Convert between currencies (required by CurrencyConverter)
     *
     * @param amount  The amount of "from" currency to convert
     * @param from    The "from" currency (e.g. USD)
     * @param to      The "to" currency (e.g., GBP)
     * @return        The equivalent amount of "to" currency (or -1.0)
     */
    public double convert(double amount, String from, String to)
    {
       ByteArrayInputStream          bis;          
       CurrencyConverterSOAPHandler  handler;
       double                        value;
       HttpInputStream               in;
       HttpOutputStream              out;          
       HttpRequest                   request;
       HttpResponse                  response;
       InputSource                   inputSource;
       SAXParser                     parser;
       SAXParserFactory              factory;
       Socket                        socket;
       String                        requestString, responseString;
       String                        soapResponse;
       URI                           u;
	
       value = 0.0;
	
       requestString = "<?xml version=\"1.0\"?>"+
          "<env:Envelope>"+
          "<env:Body>"+
          "<CurrencyConverter>"+
          "<convert>"+
          "<amount>"+amount+"</amount>"+
          "<fromCurrency>"+from+"</fromCurrency>"+
          "<toCurrency >"+to+"</toCurrency>"+
          "</convert>"+
          "</CurrencyConverter>"+
          "</env:Body>"+
          "</env:Envelope>";

       try 
       {
          // Create the HttpRequest
          u = new URI("http://localhost:8080/CurrencyConverter.soap");
          request = new HttpRequest();
          request.setVersion("1.0");
          request.setMethod("POST");
          request.setURI(u);          
          request.setContent(requestString.getBytes());
          
          // Make the connection
          socket = new Socket("localhost", 8080);
          in  = new HttpInputStream(socket.getInputStream());
          out = new HttpOutputStream(socket.getOutputStream());
          
          // Send the request
          request.write(out);
          

          // Construct and read the response (which is a SOAP response)
          response = new HttpResponse();          
          response.read(in);
          response.readContent(in);

          // Create a ByteArrayInputStream from the SOAP request
          // (for the SAX Parser)
          responseString = new String(response.getContent());
          soapResponse = URLDecoder.decode(responseString, "UTF-8");
          bis = new ByteArrayInputStream(soapResponse.getBytes());
          
          // Parse the response
          inputSource = new InputSource(bis);
          factory     = SAXParserFactory.newInstance();
          parser      = factory.newSAXParser();
          handler     = new CurrencyConverterSOAPHandler("convert");
          parser.parse(inputSource, handler);

          // Get the return value
          value = handler.getAmount();
       } 
       catch (Exception e) 
       {
          value = -1.0;
       }
       return value;
    }

}
