package com.yh.saas.plugin.servicer.ws.client;

import ch.qos.logback.classic.Logger;
import com.alibaba.fastjson.JSON;
import org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
import org.apache.wss4j.dom.WSConstants;
import org.apache.wss4j.dom.handler.WSHandlerConstants;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

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

@SuppressWarnings("deprecation")
public class SupplyWebServiceClient {
    private static final Logger LOG = (Logger) LoggerFactory.getLogger(SupplyWebServiceClient.class);

    @Value("${purchase.webservice.address:}")
    public static String webServiceUrl;

    /**
     * 连接超时10分钟 单位毫秒
     **/
    public static final long CONTIMEOUT = 600000;
    /**
     * 接收超时10分钟 单位毫秒
     **/
    public static final long RECTIMEOUT = 600000;

    private SupplyWebServiceClient() {

    }

    public static void main(String[] args) throws Exception {
        SupplyWebServiceClient.getData(
                webServiceUrl,
                "isConnect",
                "");
    }

    public static Object[] getData(String url, String serviceName, Object... params) throws Exception {
        Client client = null;
        Object[] results = null;
        try {

            JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
            client = factory.createClient(url);
            HTTPConduit http = (HTTPConduit) client.getConduit();
            HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
            httpClientPolicy.setConnectionTimeout(CONTIMEOUT);
            httpClientPolicy.setAllowChunking(false);
            httpClientPolicy.setReceiveTimeout(RECTIMEOUT);
            http.setClient(httpClientPolicy);
            // 添加cxf安全验证拦截器，必须
            client.getInInterceptors().add(new LoggingInInterceptor());
            client.getOutInterceptors().add(new LoggingOutInterceptor());
            client.getOutInterceptors().add(new SAAJOutInterceptor());
            client.getOutInterceptors().add(new WSS4JOutInterceptor(getOutProps()));
            results = client.invoke(serviceName, params);
            System.out.printf("result:" + JSON.toJSONString(results));
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        } finally {
            if (client != null) {
                client.close();
            }
        }
        return results;
    }

    private static Map<String, Object> getOutProps() {
        Map<String, Object> outProps = new HashMap<>();
        outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
        outProps.put(WSHandlerConstants.USER, "cxfClient");
        outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_DIGEST);
        outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, SupplyWsClientAuthHandler.class.getName());
        return outProps;
    }

}