package com.adullact.iparapheur.wsclient;

import java.net.URL;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import org.adullact.spring_ws.iparapheur._1.InterfaceParapheur;
import org.adullact.spring_ws.iparapheur._1.InterfaceParapheurService;

/**
 * Sample client connecting to iparapheur web services.
 * 
 * @author Vivien Barousse
 */
public class Main {

    /**
     * Program entry point.
     * @param args Command line arguments
     * @throws Exception if something goes wrong
     */
    public static void main(String[] args) throws Exception {
        // Trust store used to validate server certificate
        System.setProperty("javax.net.ssl.trustStore", "jetty-server.jks");

        // Key store used to provide a certificate to the server
        System.setProperty("javax.net.ssl.keyStoreType", "PKCS12");
        System.setProperty("javax.net.ssl.keyStore", "certificat.p12");
        System.setProperty("javax.net.ssl.keyStorePassword", "toto");

        // Accept any SSL hostname, even if not found in trust store
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

            public boolean verify(String string, SSLSession ssls) {
                return true;
            }
        });

        // Retrieve InterfaceParapheur service, used to create SOAP requests
        InterfaceParapheur service = new InterfaceParapheurService(new URL("file:./iparapheur.wsdl"),
                new QName("http://www.adullact.org/spring-ws/iparapheur/1.0", "InterfaceParapheurService")).getInterfaceParapheurSoap11();

        // Provides server entry point, and user authentication
        ((BindingProvider) service).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "https://localhost:9191/parapheur/ws-iparapheur");
        ((BindingProvider) service).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "admin");
        ((BindingProvider) service).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "admin");

        // Invokes echo service using SOAP
        String response = service.echo("Hello");
        System.out.println(response);
    }
}
