/*
 * Created on 2005.7.14
 */

import java.io.*;
import java.net.MalformedURLException;
import java.util.Hashtable;
import java.util.Vector;

import javax.net.ssl.*;

import org.apache.xmlrpc.*;
import org.apache.xmlrpc.secure.*;

/**
 * @author andrej
 */
public class SecureXmlRpcClientDemo {
	private final static int PORT_NUM = 443;
	private final static String serverURL = "https://localhost:"+String.valueOf(PORT_NUM);
	private final static String trustStoreFilename = "trustStoreClient";
	
	public static void main(String[] args) {
        try {
            System.setProperty("javax.net.ssl.trustStore", trustStoreFilename);
            setHostNameVerifier();
            
            SecureXmlRpcClient client = new SecureXmlRpcClient(serverURL);

            // Fill in the appropriate method call that's available on your server
            String method = "sample.sumAndDifference";
            // Build our parameter list.
            Vector params = new Vector();
            params.addElement(new Integer(10));
            params.addElement(new Integer(7));

            System.out.println("Invoking remote method:");
            // Call the server, and get our result.
            Hashtable result = (Hashtable) client.execute(method, params);
     
            int sum = ((Integer) result.get("sum")).intValue();
            int difference = ((Integer) result.get("difference")).intValue();
            // Print out our result.
            System.out.println("Sum: " + Integer.toString(sum) + ", Difference: " + Integer.toString(difference));

        } 
        catch (XmlRpcException e) {
            e.printStackTrace();
        } 
        catch (MalformedURLException e) {
        	e.printStackTrace();
        } 
        catch (IOException e) {
        	e.printStackTrace();
        } 
        catch (Exception e) {
        	e.printStackTrace();
        }
	
	}
	
	private static void setHostNameVerifier() {
		HostnameVerifier hv = new HostnameVerifier() {
		    public boolean verify(String urlHostName, SSLSession session) {
		        System.out.println("Warning: URL Host: "+urlHostName+" vs. "+session.getPeerHost());
		        return true;
		    }
		};
		HttpsURLConnection.setDefaultHostnameVerifier(hv);
	}
}
