All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.csb.ws.sdk.CmdWsCaller Maven / Gradle / Ivy

package com.alibaba.csb.ws.sdk;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.xml.soap.SOAPMessage;
import javax.xml.ws.Dispatch;

import com.alibaba.csb.sdk.CommUtil;
import com.alibaba.csb.sdk.Version;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.io.FileUtils;

public class CmdWsCaller {
	private static final String SDK_VERSION = "1.1.4.0";

	private static void usage(Options opt) {
		HelpFormatter formatter = new HelpFormatter();
		formatter.printHelp("java -jar wsclient.jar [options...]", opt);
		System.out.println("\ncurrent SDK version:" + SDK_VERSION+"\n----");
		Version.version();
	}

	private static boolean isEmpty1(String str, String msg) {
		if (isEmpty(str)) {
			System.out.println("请输入参数 "+msg);
			return true;
		}
		return false;
	}

	private static boolean isEmpty(String str) {
		return str == null || str.isEmpty();
	}

	private static void print(boolean debug, String format, Object args) {
		if (!debug) {
			return;
		}
		System.out.println(String.format(format, args));
	}

	/**
	 * 使用Dispatch方式发送soap请求调用WS
	 * 
	 * @param params
	 * @param ns
	 * @param sname
	 * @param pname
	 * @param isSoap12
	 * @param wa
	 * @param ea
	 * @param reqSoap
	 * @param requestHeaders
	 * @throws Exception
	 */
	private static void invokeWithDispath(WSParams params, String ns, String sname,
			String pname, boolean isSoap12, String wa, String ea, String reqSoap, Map requestHeaders) throws Exception {
		Dispatch dispatch = WSInvoker.createDispatch(params, ns, sname, pname, isSoap12, wa, ea);
		SOAPMessage request = WSInvoker.createSOAPMessage(isSoap12, reqSoap);
		WSInvoker.setHttpHeaders(dispatch, requestHeaders);
		SOAPMessage reply = dispatch.invoke(request);

		String response = DumpSoapUtil.dumpSoapMessage(reply);

		if (response != null)
			System.out.println("\n-- 调用返回:\n" + response);
		else
			System.out.println("\n-- 调用返回为空");
		
		//call multi-times for stress or flow-ctrl testing
		int times = Integer.getInteger("test.stress.times",0);
		for(int i=2; i<=times; i++) {
			reply = dispatch.invoke(request);;
			System.out.println("---- [#"+i+"] times call it for stress testing");
			if (reply != null)
				System.out.println("\n-- 调用返回:\n" + DumpSoapUtil.dumpSoapMessage(reply));
			else
				System.out.println("\n-- 调用返回为空");
		}
	}


	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("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版本");

		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 wa = commandline.getOptionValue("wa");
			String ea = commandline.getOptionValue("ea");
			String ns = commandline.getOptionValue("ns");
			String sname = commandline.getOptionValue("sname");
			String pname = commandline.getOptionValue("pname");
			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");

			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("isSoap12=" + isSoap12);
				System.out.println("nonce=" + nonce);
				System.out.println("wa=" + wa);
				System.out.println("ea=" + ea);
				System.out.println("ns=" + ns);
				System.out.println("sname=" + sname);
				System.out.println("pname=" + pname);
				System.out.println("headers=" + 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, "-wa wsdl地址")) {
				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 (isEmpty(rf) && isEmpty(rd)) {
				System.out.println("-rf 或 -rd 参数没有定义");
				usage(opt);
				return;
			}

			String kv[];
			Map httpHeaders = new HashMap();
			if(headers != null) {
				for (String header : headers) {
					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]);
				}
			}

			String reqData = (isEmpty(rd)) ? FileUtils.readFileToString(new File(rf)) : rd;
			print(isDebug, "-- 请求报文: \n%s\n", reqData);
			if (isEmpty(reqData)) {
				print(true, "-- 操作失败:文件%s请求报文为空", rf);
				return;
			}
			WSParams params = WSParams.create().accessKey(ak).secretKey(sk).fingerPrinter(fingerStr)
					.api(api).version(version).nonce(nonce).timestamp(!skipTimestamp).debug(isDebug);
			invokeWithDispath(params, ns, sname, pname, isSoap12, wa, ea, reqData, httpHeaders);
		} catch (Exception e) {
			System.out.println("-- 操作失败:" + e.getMessage());
			if (isDebug)
				e.printStackTrace(System.out);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy