/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edefterwsconsole2;

import com.cs.csap.service.EDefterWS;
import com.cs.csap.service.EDefterWS_Service;
import java.util.ArrayList;
import java.util.List;
import javax.xml.ws.Binding;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.Handler;

/**
 *
 * @author osman.cuhadar
 */
public class Login {
    
    public Login() {}
    
    EDefterWS port;

    public Login(String usrname, String pass) {

        
        try {
            //URL url = new URL("https://edeftertest.efinans.com.tr/edefter/ws/EDefterWS?wsdl");

            EDefterWS_Service service = new EDefterWS_Service();
            this.port = service.getEDefterWSPort();
            this.port = setAuthToPort(port, usrname, pass);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static EDefterWS setAuthToPort(EDefterWS port, String username, String password) {
        BindingProvider bp = (BindingProvider) port;
        Binding binding = bp.getBinding();
        List<Handler> handlerChain = new ArrayList<>();
        WebServiceSecurityHeaderHandler wsSecurity = new WebServiceSecurityHeaderHandler(username, password);
        handlerChain.add(wsSecurity);
        binding.setHandlerChain(handlerChain);

        return port;
    }

    
    
}
