com.alibaba.csb.sdk.CmdCaller Maven / Gradle / Ivy
package com.alibaba.csb.sdk;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map.Entry;
import java.util.Properties;
/**
* 使用java命令行的方式调用CSB开放出来的Http服务,这种方式通常用来快速测试服务和参数是否正确
*
* java -jar httpclient.jar 会打印具体的操作用法
*
* @author Alibaba Middleware CSB Team
* @author liaotian.wq
*
* @since 2016
*
* @deprecated, use the new CmdHttpCaller cmd line.
*
*/
public class CmdCaller {
private static final String SDK_VERSION = "1.1.4";
private static void usage() {
System.out
.println("[Deprecated!] Usage: java [-Dhfile=headers.prop] [-Ddfile=d.txt] -jar Http-client.jar method url apiName version [ak sk]");
System.out.println(" method = get : call url with GET method ");
System.out.println(" method = post : call url with POST method ");
System.out.println(" method = cget : return curl request string with GET method, ");
System.out.println(" method = cpost : return curl request string with POST method ");
System.out.println(" system property 'hfile' is an optional,to set a file which defines http headers, its content format:");
System.out.println(" header1=value1");
System.out.println(" header2=value2");
System.out.println(" system property 'dfile' is an optional,to set a file which defines body data, its content format:");
System.out.println(" data1=value1");
System.out.println(" data2=value2");
System.out.println(" ");
System.out.println(" print current SDK version: java -jar Http-client.jar -v ");
System.out.println("----------------------------");
System.out.println("Please use this NEW command line:");
CmdHttpCaller.usage(null);
}
public static void main(String args[]) {
try {
if (args.length > 0 && "-v".equalsIgnoreCase(args[0])) {
System.out.println("HttpCaller SDK version:" + SDK_VERSION);
try {
System.out.println(CommUtil.geCurrenttVersionFile());
} catch (IOException e) {
//e.printStackTrace();
}
return;
}
if (args.length != 4 && args.length != 6) {
usage();
return;
}
String method = args[0];
if (!"get".equalsIgnoreCase(method) && !"post".equalsIgnoreCase(method)
&& !"cget".equalsIgnoreCase(method) && !"cpost".equalsIgnoreCase(method)) {
usage();
return;
}
Properties headerProp = readPropFile("hfile");
Properties dataProp = readPropFile("dfile");
String lurl = args[1];
String apiName = args[2];
String version = args[3];
if ("null".equalsIgnoreCase(args[3]))
version = null;
String ak = null;
String sk = null;
System.out.println("---- restful request url:" + lurl);
System.out.println("---- apiName:" + apiName);
System.out.println("---- version:" + version);
System.out.println("---- method:" + method);
if (args.length == 6) {
ak = args[4];
sk = args[5];
if ("".equals(ak))
ak = null;
System.out.println("---- ak:" + ak);
System.out.println("---- sk:" + sk);
}
String ret = null;
boolean curlOnly = false;
if (method.toLowerCase().startsWith("c")) {
curlOnly = true;
HttpCaller.setCurlResponse(true);
}
HttpParameters.Builder builder = HttpParameters.newBuilder();
builder.api(apiName).version(version).method(method).requestURL(lurl).accessKey(ak).secretKey(sk);
if (headerProp != null) {
for (Entry