/*************************************************************************
 * Sample SOAP Client that will call a service which adds two numbers
 * and returns the sum
**************************************************************************/

import java.io.*;
import java.net.*;
import java.util.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;


public class AddClient
{

  
  	public static int addNumbers (URL url, int num1, int num2) throws Exception 
  	{
	    // STEP 2: Create the Call object
	    Call call = new Call ();
	
	    // Set encodings. Service uses standard SOAP encoding
	    String encodingStyleURI = Constants.NS_URI_SOAP_ENC;
	    call.setEncodingStyleURI(encodingStyleURI);
	
	    // STEP 3: Set service locator parameters
	    // Set the target URI into the Call object using the setTargetObjectURI(...) method.
	    // Pass in the URN that the service used to identify itself in its deployment descriptor.

	    call.setTargetObjectURI ("urn:xmethods-add-numbers");
	    call.setMethodName ("add");
	
	    // STEP 4: Create input parameter vector
	    Vector params = new Vector ();
	    params.addElement (new Parameter("num1", Integer.class, new Integer(num1), null));
	    params.addElement (new Parameter("num2", Integer.class, new Integer(num2), null));	    
	    call.setParams (params);
	
	    //STEP 5: Invoke the service ....
	    Response resp = call.invoke (url,"");
	
	    //STEP 6:  ... and evaluate the response
	    if (resp.generatedFault ()) 
	    {
			throw new Exception();
	    } 
	    else 
	    {
	      // Call was successful. Extract response parameter and return result
	      Parameter result = resp.getReturnValue ();
	      Integer sum = (Integer) result.getValue();
	      return sum.intValue();
	    }
	 }
  }

  /* Driver to illustrate service invocation*/
  public static void main(String[] args) 
  {
    try 
    {
      URL url=new URL("http://services.xmethods.com:80/soap");
      int num1 = 5;
      int num2 = 10;
      int sum = addNumbers(url, num1, num2);
      System.out.println(sum);
    }
    catch (Exception e) {e.printStackTrace();}
  }
}