package hello;

import java.net.URL;
import java.util.Vector;
import org.apache.soap.SOAPException;
import org.apache.soap.Constants;
import org.apache.soap.Fault;
import org.apache.soap.rpc.Call;
import org.apache.soap.rpc.Parameter;
import org.apache.soap.rpc.Response;
import org.apache.soap.encoding.SOAPMappingRegistry;

public class Client
{
   public static void main(String[] args) throws Exception
   {
      if(args.length == 0)
      {
         System.err.println("Usage: java hello.Client [SOAP-router-URL] ");
         System.exit (1);
      }

      try
      {
         URL url = null;
         String name = null;
         if(args.length == 2)
         {
            url = new URL(args[0]);
            name = args[1];
         }
         else
         {
            url = new URL("http://localhost:8080/soap/servlet/rpcrouter");
            name = args[0];
         }

         // Build the call.
         Call call = new Call();
         SOAPMappingRegistry smr= new SOAPMappingRegistry();
         call.setSOAPMappingRegistry(smr);
         call.setTargetObjectURI("urn:Hello");
         call.setMethodName("sayHelloTo");
         call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
         Vector params = new Vector();
         params.addElement(new Parameter("name", String.class, name, null));
         call.setParams(params);
         System.out.println("I am after set params");

         // Invoke the call.


            Response resp = null;
            System.out.println("I am in the try loop before invoke");
            resp = call.invoke(url, "");

            System.out.println("I am in the try loop after invoke");
            // Check the response.
            if( !resp.generatedFault() )
            {
               Parameter ret = resp.getReturnValue();
               Object value = ret.getValue();
               System.out.println(value);
            }
            else
            {
               Fault fault = resp.getFault();
               System.err.println("Generated fault: ");
               System.out.println ("  Fault Code   = " + fault.getFaultCode());
               System.out.println ("  Fault String = " + fault.getFaultString());
            }
        
            /*catch( SOAPException e )
            {
                System.err.println("Caught SOAPException (" + e.getFaultCode() + "): " + e.getMessage());
                System.exit(-1);
            }

            catch(Exception e)
            {
                e.printStackTrace();
            }*/
           

            Vector v = resp.getparams();
            Iterator iter = v.iterator();

            while(iter.hasNext() == true) {
                System.out.println(iter.next());
            }
         }    //  closing try

         catch( SOAPException e )
         {
            System.err.println("Caught SOAPException (" + e.getFaultCode() + "): " + e.getMessage());
            System.exit(-1);
         }

         catch(Exception e)
         {
         e.printStackTrace();
         }
     }    //  closing main
 }  //  closing class

