/*******************************************************************************
 *                                                                              
 *  Copyright FUJITSU LIMITED 2016                                           
 *                                                                                                                                 
 *  Creation Date: 18.11.2015                                                      
 *                                                                              
 *******************************************************************************/

package org.oscm.test.ws;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;
import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.HandlerResolver;
import javax.xml.ws.handler.PortInfo;

import org.oscm.apiversioning.handler.ClientVersionHandler;

import com.sun.xml.wss.XWSSConstants;

/**
 * @author stavreva
 * 
 */
public class WebServiceProxy {

    public static <T> T get(String baseUrl, String version, String auth,
            String namespace, Class<T> remoteInterface, String userName,
            String password) throws Exception {
        return get(baseUrl, version, version, auth, namespace, remoteInterface,
                userName, password);
    }

    public static <T> T get(String baseUrl, String versionWSDL,
            final String versionHeader, String auth, String namespace,
            Class<T> remoteInterface, String userName, String password)
            throws Exception {
        String wsdlUrl = baseUrl + "/oscm/" + versionWSDL + "/"
                + remoteInterface.getSimpleName() + "/" + auth + "?wsdl";
        URL url = new URL(wsdlUrl);
        QName qName = new QName(namespace, remoteInterface.getSimpleName());
        Service service = Service.create(url, qName);
        if ("v1.7".equals(versionHeader) || "v1.8".equals(versionHeader)) {
            service.setHandlerResolver(new HandlerResolver() {
                @SuppressWarnings("rawtypes")
                @Override
                public List<Handler> getHandlerChain(PortInfo portInfo) {
                    List<Handler> handlerList = new ArrayList<Handler>();
                    handlerList.add(new VersionHandlerCtmg(versionHeader));
                    return handlerList;
                }
            });
        } else {
            ClientVersionHandler versionHandler = new ClientVersionHandler(
                    versionHeader);
            service = versionHandler.addVersionInformationToClient(service);
        }
        T port = service.getPort(remoteInterface);
        BindingProvider bindingProvider = (BindingProvider) port;
        Map<String, Object> clientRequestContext = bindingProvider
                .getRequestContext();
        if ("STS".equals(auth)) {
            clientRequestContext.put(XWSSConstants.USERNAME_PROPERTY, userName);
            clientRequestContext.put(XWSSConstants.PASSWORD_PROPERTY, password);
        } else {
            clientRequestContext.put(BindingProvider.USERNAME_PROPERTY,
                    userName);
            clientRequestContext.put(BindingProvider.PASSWORD_PROPERTY,
                    password);
            clientRequestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
                    baseUrl + "/" + remoteInterface.getSimpleName() + "/"
                            + auth);
        }
        return port;
    }
}

@@ -1473,32 +1473,38 @@
 (String baseUrl,
+ final
  String versionW
@@ -1520,22 +1520,16 @@
         
-final 
 String v
@@ -2014,22 +2014,20 @@
 (version
-Header
+WSDL
 ) %7C%7C %22v1
@@ -2044,22 +2044,20 @@
 (version
-Header
+WSDL
 )) %7B%0A   
@@ -2402,22 +2402,20 @@
 (version
-Header
+WSDL
 ));%0A    

Correct version value - Suzana