//package com.carolinacustom;

/* Decompiled by Mocha from UPS_Shipping_Calculator.class */
/* Originally compiled from UPS_Shipping_Calculator.java */

import com.allaire.cfx.*;
import java.io.*;
import java.net.Socket;
import javax.net.SocketFactory;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

public class UPS_Shipping_Calculator
{
public static final String UPS_NEXT_DAY_AIR            = "01";
public static final String UPS_2ND_DAY_AIR             = "02";
public static final String UPS_GROUND                  = "03";
public static final String UPS_WORLDWIDE_EXPRESS       = "07";
public static final String UPS_WORLDWIDE_EXPEDITED     = "08";
public static final String UPS_STANDARD                = "11";
public static final String UPS_3_DAY_SELECT            = "12";
public static final String UPS_NEXT_DAY_AIR_SAVER      = "13";
public static final String UPS_NEXT_DAY_EARLY_AM       = "14";
public static final String UPS_WORLDWIDE_EXPRESS_PLUS  = "54";
public static final String UPS_2ND_DAY_AIR_AM          = "59";
public static final String UPS_EXPRESS_SAVER           = "65";
    
    public String server;
    public int port;
    public String protocol;

    private String UPS_request;
    private String UPS_response;

    private int num_rated_shipments;
    String[] service_code;
    float[] shipping_cost; 
    
//-----------------------------------------------------------------------------------
public UPS_Shipping_Calculator()
{
server="www.ups.com";
port=443;
protocol="HTTPS";
}

//-----------------------------------------------------------------------------------
public UPS_Shipping_Calculator(String new_server, int new_port, String new_protocol)
{
server=new_server;
port=new_port;
protocol=new_protocol;
}

//-----------------------------------------------------------------------------------
public void set_server(String new_server)
{
server=new_server;
}

//-----------------------------------------------------------------------------------
public void set_port(int new_port)
{
port=new_port;
}

//-----------------------------------------------------------------------------------
public void set_protocol(String new_protocol)
{
protocol=new_protocol;
}

//-----------------------------------------------------------------------------------
    public void communicate_request_to_UPS()
        throws Exception
    {
  System.out.println(server);

        if (protocol.equals("HTTP"))
            doHTTPSocket();
        else if (protocol.equals("Other"))
            doOtherSocket();
        else
        {
            if (!protocol.equals("HTTPS"))
                throw new Exception("Invalid value for PROTOCOL Attribute.  Only HTTP, HTTPS and Other are valid values.");
            doHTTPSSocket();
        }
    }

//-----------------------------------------------------------------------------------
    void doHTTPSocket()
        throws Exception
    {
        BufferedReader bufferedReader;
        UPS_response = "";
        Socket socket = new Socket(server, port);
        PrintWriter printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
        printWriter.println(UPS_request);
        printWriter.println();
        printWriter.flush();
        String string;
        for (bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); (string = bufferedReader.readLine()) != null; )
            UPS_response = UPS_response + string + "\r\n";
        bufferedReader.close();
        printWriter.close();
        socket.close();
    }

//-----------------------------------------------------------------------------------
    void doOtherSocket()
        throws Exception
    {
        BufferedReader bufferedReader;
        UPS_response = "";
        Socket socket = new Socket(server, port);
        PrintWriter printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
        printWriter.println(UPS_request);
        printWriter.println();
        printWriter.flush();
        String string;
        for (bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); (string = bufferedReader.readLine()) != null; )
            UPS_response = UPS_response + string + "\r\n";
        bufferedReader.close();
        printWriter.close();
        socket.close();
    }

//-----------------------------------------------------------------------------------
    void doHTTPSSocket()
        throws Exception
    {
        BufferedReader bufferedReader;
        UPS_response = "";
        SSLSocketFactory sSLSocketFactory = (SSLSocketFactory)SSLSocketFactory.getDefault();
        SSLSocket sSLSocket = (SSLSocket)sSLSocketFactory.createSocket(server, port);
        sSLSocket.startHandshake();
        PrintWriter printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sSLSocket.getOutputStream())));
        printWriter.println(UPS_request);
        printWriter.println();
        printWriter.flush();
        String string;
        for (bufferedReader = new BufferedReader(new InputStreamReader(sSLSocket.getInputStream())); (string = bufferedReader.readLine()) != null; )
            UPS_response = UPS_response + string + "\r\n";
        bufferedReader.close();
        printWriter.close();
        sSLSocket.close();
    }

//-----------------------------------------------------------------------------------
public void set_UPS_request(String shipto_zipcode, String shipto_country, String cart_weight)
{
String crln = new String("\r\n");
String double_quote = new String("\"");
String http_header;
String xml_request;

xml_request =
   "<?xml version=" + double_quote + "1.0" + double_quote + "?>" + crln +
   "<AccessRequest xml:lang=" + double_quote + "en-US" + double_quote + ">" + crln +
   "   <AccessLicenseNumber>4B8431F95EDBD4B0</AccessLicenseNumber>" + crln +
   "   <UserId>scdemarc</UserId>" + crln +
   "   <Password>d0gEYE$!</Password>" + crln +
   "</AccessRequest>" + crln +
   "<?xml version=" + double_quote + "1.0" + double_quote + "?>" + crln +
   "<RatingServiceSelectionRequest xml:lang=" + double_quote + "en-US" + double_quote + ">" + crln +
   "      <Request>" + crln +
   "         <TransactionReference>" + crln +
   "         <CustomerContext>Rating and Service</CustomerContext>" + crln +
   "         <XpciVersion>1.0001</XpciVersion>" + crln +
   "         </TransactionReference>" + crln +
   "         <RequestAction>Rate</RequestAction>" + crln +
   "         <RequestOption>shop</RequestOption>" + crln +
   "      </Request>" + crln +
   "      <PickupType>" + crln +
   "         <Code>01</Code>" + crln +
   "      </PickupType>" + crln +
   "      <Shipment>" + crln +
   "         <Shipper>" + crln +
   "            <Address>" + crln +
   "               <PostalCode>28328</PostalCode>" + crln +
   "               <CountryCode>US</CountryCode>" + crln +
   "            </Address>" + crln +
   "         </Shipper>" + crln +
   "         <ShipTo>" + crln +
   "            <Address>" + crln +
   "               <PostalCode>" + shipto_zipcode + "</PostalCode>" + crln +
   "               <CountryCode>" + shipto_country + "</CountryCode>" + crln +
   "            </Address>" + crln +
   "         </ShipTo>" + crln +
   "         <Package>" + crln +
   "         <PackagingType>" + crln +
   "            <Code>02</Code>" + crln +
   "         </PackagingType>" + crln +
   "         <PackageWeight>" + crln +
   "            <Weight>" + cart_weight + "</Weight>" + crln +
   "    <UnitOfMeasurement>" + crln +
   "               <Code>LBS</Code>" + crln +
   "            </UnitOfMeasurement>" + crln +
   "         </PackageWeight>" + crln +
   "      </Package>" + crln +
   "      </Shipment>" + crln +
   "   </RatingServiceSelectionRequest>" + crln;

http_header = "POST /ups.app/xml/Rate HTTP/1.0" + crln +
                     "Content-Length: " + xml_request.length() + crln +
                     "Content-type: application/x-www-form-urlencoded" + crln + crln;
      
UPS_request = http_header + xml_request;
}

//-----------------------------------------------------------------------------------
public String get_UPS_request()
{
return(UPS_request); 
}

//-----------------------------------------------------------------------------------
public String get_UPS_response()
{
return(UPS_response); 
}

//-----------------------------------------------------------------------------------
public void extract_rated_shipments_from_UPS_response()
{
int result, search_index=0;
int n, begin_index, end_index;

num_rated_shipments=0;
result = UPS_response.indexOf("<RatedShipment>", search_index);
while(result != -1)
   {
   num_rated_shipments++;
   search_index = result + 1;
   result = UPS_response.indexOf("<RatedShipment>", search_index);
   }

service_code  = new String[num_rated_shipments];
shipping_cost = new float[num_rated_shipments];

search_index=0;
for (n=0; n<num_rated_shipments; n++)
   {
   search_index = UPS_response.indexOf("<RatedShipment>", search_index);
   begin_index = UPS_response.indexOf("<Code>", search_index) + 6;
   end_index = UPS_response.indexOf("</Code>", search_index);
   service_code[n] = UPS_response.substring(begin_index, end_index);
   
   search_index = UPS_response.indexOf("<TotalCharges>", search_index);
   begin_index = UPS_response.indexOf("<MonetaryValue>", search_index) + 15;
   end_index = UPS_response.indexOf("</MonetaryValue>", search_index);
   shipping_cost[n] = (Float.valueOf(UPS_response.substring(begin_index, end_index))).floatValue();
   }
}

//-----------------------------------------------------------------------------------
public int get_num_rated_shipments()
{
return(num_rated_shipments);
}

//-----------------------------------------------------------------------------------
public String get_service_code(int n)
{
return(service_code[n]);
}

//-----------------------------------------------------------------------------------
public float get_shipping_cost(int n)
{
return(shipping_cost[n]);
}

//-----------------------------------------------------------------------------------
public float get_shipping_cost(String desired_service_code)
{
int n;

for (n=0; n<num_rated_shipments; n++)
   if (service_code[n].compareTo(desired_service_code)==0)
      return(shipping_cost[n]);
return(-1);
}

//-----------------------------------------------------------------------------------
public void calculate_shipping_options(String shipto_zipcode, String shipto_country, String cart_weight) throws Exception
{
this.set_UPS_request(shipto_zipcode, shipto_country, cart_weight);
this.communicate_request_to_UPS();
}


//===================================================================================
public static void main(String[] args) throws Exception
{
int n;
UPS_Shipping_Calculator x = new UPS_Shipping_Calculator();

//x.set_server("www.ups.com");
//x.set_port(443);
//x.set_protocol("HTTPS");

//x.set_UPS_request("27513", "US", "34.56");
//x.communicate_request_to_UPS();
x.calculate_shipping_options("27513", "US", "34.56");
x.get_UPS_response();

System.out.println("");
System.out.println("server: " + x.server);
System.out.println("");
System.out.println("port: " + x.port);
System.out.println("");
System.out.println("protocol: " + x.protocol);
System.out.println("");
System.out.println(x.get_UPS_request());
System.out.println("length: " + (x.get_UPS_request()).length());
System.out.println("");
System.out.println(x.get_UPS_response());
System.out.println("length: " + (x.get_UPS_response()).length());

x.extract_rated_shipments_from_UPS_response();
System.out.println("number_rated_shipments: " + x.get_num_rated_shipments());

for (n=0; n<x.get_num_rated_shipments(); n++)
  System.out.println(x.get_service_code(n) + "  " + x.get_shipping_cost(n));

System.out.println("UPS_GROUND        " + x.get_shipping_cost(UPS_Shipping_Calculator.UPS_GROUND));
System.out.println("UPS_3_DAY_SELECT  " + x.get_shipping_cost(UPS_Shipping_Calculator.UPS_3_DAY_SELECT));
System.out.println("UPS_2ND_DAY_AIR   " + x.get_shipping_cost(UPS_Shipping_Calculator.UPS_2ND_DAY_AIR));
System.out.println("UPS_NEXT_DAY_AIR  " + x.get_shipping_cost(UPS_Shipping_Calculator.UPS_NEXT_DAY_AIR));
}

}
