class CmdWsCaller {

    public static void main(String[] args) {
        Options opt = new Options();
        opt.addOption("ak", true, "accessKey, 可选");
        opt.addOption("sk", true, "secretKey, 可选");
        opt.addOption("api", true, "服务名, 可选");
        opt.addOption("version", true, "服务版本, 可选");
		opt.addOption("wa", true, "wsdl地址，e.g: http://broker-ip:9081/api/version/method?wsdl");
        opt.addOption("ea", true, "endpoint地址，e.g: http://broker-ip:9081/api/version/method");
        opt.addOption("ns", true, "在wsdl中定义的服务的target namespace");
        opt.addOption("sname", "serviceName", true, "在wsdl中定义的服务名");
        opt.addOption("pname", "portName", true, "在wsdl中定义的端口名");
        opt.addOption("action", "SOAPAction", true, "SOAPAction值");
        opt.addOption("soap12", false, "-soap12 为soap12调用, 不定义为soap11");
        opt.addOption("nonce", false, "-nonce 是否做nonce防重放处理，不定义为不做nonce重放处理");
        opt.addOption("skipTimestamp", false, "-skipTimestamp 不设置时间戳，默认是设置");
        opt.addOption("fingerStr", true, "签名指纹串, 可选");
        opt.addOption("H", true, "http header, 格式: -H \"key:value\"");
        opt.addOption("h", "help", false, "打印帮助信息");
        opt.addOption("d", "debug", false, "打印调试信息");
        opt.addOption("rf", true, "soap请求文件，文件里存储soap请求的Message格式内容");
        opt.addOption("rd", true, "soap请求内容(Message)，如果设置该选项时，-rf选项被忽略");
        opt.addOption("sdkv", "sdk-version", false, "sdk版本");
        opt.addOption("bizIdKey", true, "业务id Key");
        opt.addOption("bizId", true, "业务id");

        CommandLineParser parser = new DefaultParser();

        Boolean isDebug = false;
        try {
            CommandLine commandline = parser.parse(opt, args);
            if (commandline.hasOption("h") || commandline.getOptions().length == 0) {
                usage(opt);
                return;
            }

            String ak = commandline.getOptionValue("ak");
            String sk = commandline.getOptionValue("sk");
            String api = commandline.getOptionValue("api");
            String version = commandline.getOptionValue("version");
            Boolean sdkv = commandline.hasOption("sdkv");
            String ea = commandline.getOptionValue("ea");
            String ns = commandline.getOptionValue("ns");
            String sname = commandline.getOptionValue("sname");
            String pname = commandline.getOptionValue("pname");
            String action = commandline.getOptionValue("action");
            String rf = commandline.getOptionValue("rf");
            String rd = commandline.getOptionValue("rd");
            String fingerStr = commandline.getOptionValue("fingerStr");
            String[] headers = commandline.getOptionValues("H");
            boolean isSoap12 = commandline.hasOption("soap12");
            boolean nonce = commandline.hasOption("nonce");
            boolean skipTimestamp = commandline.hasOption("skipTimestamp");
            isDebug = commandline.hasOption("d");
            String bizIdKey = commandline.getOptionValue("bizIdKey");
            String bizId = commandline.getOptionValue("bizId");

            if (sdkv) {
                Version.version();
                return;
            }

            if (isDebug) {
                // printParams();
                System.out.println("ak=" + ak);
                System.out.println("sk=" + sk);
                System.out.println("api=" + api);
                System.out.println("version=" + version);
                System.out.println("bizIdKey=" + bizIdKey);
                System.out.println("bizId=" + bizId);
                System.out.println("isSoap12=" + isSoap12);
                System.out.println("nonce=" + nonce);
                System.out.println("ea=" + ea);
                System.out.println("ns=" + ns);
                System.out.println("sname=" + sname);
                System.out.println("pname=" + pname);
                System.out.println("action=" + action);
                System.out.println("headers=" + Arrays.toString(headers));
                System.out.println("rd=" + rd);
                if (isEmpty(rd)) {
                    System.out.println("rf=" + rf);
                }
                System.out.println("nonce=" + nonce);
                System.out.println("skipTimestamp=" + skipTimestamp);
                System.out.println("fingerStr=" + fingerStr);
            }

            if (isEmpty1(ea, "-ea endpoint地址")) {
                usage(opt);
                return;
            }

            if (isEmpty1(ea, "-ns namespace")) {
                usage(opt);
                return;
            }

            if (isEmpty1(ea, "-sname serviceName")) {
                usage(opt);
                return;
            }

            if (isEmpty1(pname, "-pname portName")) {
                usage(opt);
                return;
            }

            if (isEmpty1(pname, "-action SOAPAction")) {
                usage(opt);
                return;
            }

            if (isEmpty(rf) && isEmpty(rd)) {
                System.out.println("-rf 或 -rd 参数没有定义");
                usage(opt);
                return;
            }

            String reqData = (isEmpty(rd)) ? FileUtils.readFileToString(new File(rf)) : rd;
            print(isDebug, "-- 请求报文: \n%s\n", reqData);
            if (isEmpty(reqData)) {
                print(true, "-- 操作失败：文件%s请求报文为空", rf);
                return;
            }
            if (bizIdKey != null && !bizIdKey.trim().equals("")) {
                WSClientSDK.bizIdKey(bizIdKey);
            }
            WSParams params = WSParams.create().accessKey(ak).secretKey(sk).fingerPrinter(fingerStr)
                    .api(api).bizId(bizId).version(version).nonce(nonce).timestamp(!skipTimestamp).debug(isDebug);

            Map<String, String> httpHeaders = new HashMap<String, String>();

            if (headers != null) {
                for (String header : headers) {
                    String[] kv = header.split(":", 2);
                    if (kv == null || kv.length != 2) {
                        System.out.println("错误的HTTP头定义 正确格式: -H \"key:value\" !!" + header);
                        return;
                    }
                    httpHeaders.put(kv[0], kv[1]);
                }
            }

            invokeWithDispath(params, ns, sname, pname, action, isSoap12, ea, reqData, httpHeaders);
        } catch (Exception e) {
            System.out.println("-- 操作失败：" + e.getMessage());
            if (isDebug)
                e.printStackTrace(System.out);
        }
    }

}