//------------------------------------------------------------------------------
// <copyright file="SOAUtil.java" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <summary>
//      SOA test utility tools
// </summary>
//------------------------------------------------------------------------------

import java.util.HashMap;
import java.util.Map;

import javax.xml.ws.Dispatch;

import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.handler.WSHandlerConstants;

import com.microsoft.hpc.scheduler.session.SessionStartInfo;
import com.microsoft.hpc.scheduler.session.HpcJava;
import com.microsoft.hpc.scheduler.session.SessionBase;
import com.microsoft.hpc.sessionlauncher.ISessionLauncher;
import com.microsoft.hpc.sessionlauncher.SessionLauncher;


public class SOAUtil
{
    public static int CheckSessionStartInfo(SessionStartInfo info)
    {
        TestUtil.IntoAPI();
        int nerrs = 0;
        System.out.println("Todo CheckSessionStartInfo() !!!!!!!!!!!!!!");
        return nerrs;
    }
    
    protected static ISessionLauncher GetSessionLauncher(String headnode) throws Exception
    {
        String sessionLauncherAddr = "https://" + headnode + "/SessionLauncher?wsdl";
        SessionLauncher soaService = new SessionLauncher();
        ISessionLauncher sessionLauncher = soaService.getBasicHttpEndpointForJava();
        java.util.Map<String, Object> requestContext = ((javax.xml.ws.BindingProvider)sessionLauncher).getRequestContext();
        requestContext.put(Dispatch.ENDPOINT_ADDRESS_PROPERTY, sessionLauncherAddr);
        SetHttps(sessionLauncher);          
        return sessionLauncher;
    }

    protected static void SetHttps(ISessionLauncher serviceObject) throws Exception
    {   
        org.apache.cxf.endpoint.Client client = org.apache.cxf.frontend.ClientProxy.getClient(serviceObject);
        org.apache.cxf.endpoint.Endpoint cxfEndpoint = client.getEndpoint();
    
        Map<String,Object> outProps = new HashMap<String,Object>();
    
        outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
        outProps.put(WSHandlerConstants.USER, BasicTests.username);
        outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
        outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, com.microsoft.hpc.scheduler.session.SessionBase.PWHandlerClass.class.getName()); 
        WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
        cxfEndpoint.getOutInterceptors().add(wssOut);       
    }
}
