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

import java.net.URL;

import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jws.WebService;

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

import proxy.ws.HelloWorldService;

import weblogic.wsee.jws.jaxws.owsm.SecurityPolicy;


@WebService

@SecurityPolicy(uri = "oracle/wss_username_token_over_ssl_service_policy")
public class HelloWorld {
    public HelloWorld() {
        super();
    }
    final static Logger logger = Logger.getLogger(HelloWorld.class.getName());

    public String hello(String name) {
        return "Hi:" + name;
    }

    public String callHello(String address, String wsdl, String name, String user, String pwd) {
        return callUtilHello(address, wsdl, name, user, pwd, null);
    }

    public String callHelloWithCSFKey(String address, String wsdl, String name, String csfkey) {
        return callUtilHello(address, wsdl, name, null, null, csfkey);
    }

    public static String callUtilHello(String address, String wsdl, String name, String user, String pwd,
                                       String csfkey) {
        String context = "Failure Context:";
        try {
            if (address == null) {
                throw new Exception("address is null;");
            }
            QName serviceName = new QName("http://wssunt/", "HelloWorldService");
            if (address.toLowerCase().startsWith("http://")) {
                serviceName = new QName("http://wssunt/", "HelloWorld_HttpService");
            }

            HelloWorldService helloWorldService = new HelloWorldService(new URL(wsdl), serviceName);
            proxy.ws.HelloWorld helloWorld = null;
            if (address.toLowerCase().startsWith("http://")) {
                context += "Calling over unsecured http\n";
                helloWorld =
                    helloWorldService.getPort(new QName("http://wssunt/", "HelloWorld_HttpPort"),
                                              proxy.ws.HelloWorld.class,
                                              new weblogic.wsee.jws.jaxws.owsm.SecurityPolicyFeature("oracle/wss_username_token_client_policy"));
                

            } else {
                context += "Calling over secured http\n";
                helloWorld =
                    helloWorldService.getHelloWorldPort(new weblogic.wsee.jws.jaxws.owsm.SecurityPolicyFeature("oracle/wss_username_token_over_ssl_client_policy"));
            }


            Map<String, Object> ctxt = ((BindingProvider)helloWorld).getRequestContext();
            ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, address);
            if (csfkey != null) {
                ctxt.put("csf-key", csfkey);
                context += "->using  csf-key\n";
            } else {
                ctxt.put(BindingProvider.USERNAME_PROPERTY, user);
                ctxt.put(BindingProvider.PASSWORD_PROPERTY, pwd);
                context += "-> using user and pwd\n";
            }
            context += "-> with " + name +"\n";
            return helloWorld.hello(name);


        } catch (Throwable e) {
            logger.setLevel(Level.FINER);
            logger.throwing("HelloWorld", "callHello", e);
            throw new RuntimeException(context, e);
        }
    }
}
