
/*
 *  Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
*/
package proxy.ws;

import java.net.URL;

import java.security.cert.X509Certificate;

import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;


// This source file is generated by Oracle tools.
// Contents may be subject to change.
// For reporting problems, use the following:
// Generated by Oracle JDeveloper 11g Release 2 11.1.2.1.0.6081

public class HelloWorldPortClient {
    public static HostnameVerifier ACCEPT_ALL_HOSTS = new HostnameVerifier() {

        public boolean verify(String s, SSLSession sslSession) {
            System.out.println("Host name verifier verified:" + s);
            return true;
        }
    };
    
   
    public static TrustManager[] TRUST_MANAGERS_TRUSTING_ALL = new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {

                return new X509Certificate[0];
            }

            public void checkClientTrusted(X509Certificate[] certs, String authType) {

                // Accept all
            }

            public void checkServerTrusted(X509Certificate[] certs, String authType) {

                // Accept all
            }
        } };

    public static void setSSLWithoutKeystore() throws Exception {


     
      

        TrustManager[] trustManagers = TRUST_MANAGERS_TRUSTING_ALL;


        SSLContext sc = SSLContext.getInstance("SSL");

        sc.init(null, trustManagers, new java.security.SecureRandom());

        setSunSSLContextAndDefaultHostVerifier(sc, true);
    }

   
   


    public static void setSunSSLContextAndDefaultHostVerifier(SSLContext sslContext,
                                                              boolean acceptAllHost) throws Exception {
        //        SSLContext sslContext = getSSLContext();
        //        sslContext.init(new KeyManager[0], getLenientTrustManagerArray(), new java.security.SecureRandom());
        javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
        if (acceptAllHost) {
            javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(ACCEPT_ALL_HOSTS);
        }

    }

    public static void main(String[] args) throws Exception {
       
        
        if (args == null || args.length == 0) {
            args = new String[5];
            args[0] = "wsdl";
            args[1] = "address";
            args[2] = "user";
            args[3] = "pwd";
            args[4] = "name";
        }
        if (args == null || args.length < 5) {
            throw new Exception("Pass 5 arguments. wsdl, address, user, pwd and your name");
        }
       
        QName serviceName = new QName("http://wssunt/", "HelloWorldService");

        HelloWorldService helloWorldService = new HelloWorldService(new URL(args[0]), serviceName);
        proxy.ws.HelloWorld helloWorld = null; 
        if (args[1].toLowerCase().startsWith("https://")) {
            setSSLWithoutKeystore();
            helloWorld = helloWorldService.getHelloWorldPort(new weblogic.wsee.jws.jaxws.owsm.SecurityPolicyFeature("oracle/wss_username_token_over_ssl_client_policy"));
            
        } else {
            helloWorld = helloWorldService.getHelloWorldPort(new weblogic.wsee.jws.jaxws.owsm.SecurityPolicyFeature("oracle/wss_username_token_client_policy"));
        }
       
            

        Map<String, Object> ctxt = ((BindingProvider)helloWorld).getRequestContext();
        ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, args[1]);
        ctxt.put(BindingProvider.USERNAME_PROPERTY, args[2]);
        ctxt.put(BindingProvider.PASSWORD_PROPERTY, args[3]);
        System.out.println("Testing inbound to the Cloud");
        System.out.println(helloWorld.hello(args[4]));
        System.out.println("Testing inbound -> that does outbound.");
        System.out.println(helloWorld.callHello(args[1],args[0],args[4],args[2],args[3]));

    }
}
