class CmdCaller {

	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<Object, Object> kv : headerProp.entrySet()) {
					if ("NONCE".equalsIgnoreCase(String.valueOf(kv.getKey()))) {
						//设置nonce flag
						if("true".equalsIgnoreCase(String.valueOf(kv.getValue()))) {
							System.out.println("---- set nonce as true ");
							builder.nonce(true);
						}
					}
					System.out.println("---- put http header " + (String) kv.getKey() + ":" + (String) kv.getValue());
					builder.putHeaderParamsMap((String) kv.getKey(), (String) kv.getValue());
				}
			}
			if (dataProp != null) {
				for (Entry<Object, Object> kv : dataProp.entrySet()) {
					System.out.println("---- put data body " + (String) kv.getKey() + ":" + (String) kv.getValue());
					builder.putParamsMap((String) kv.getKey(), (String) kv.getValue());
				}
			}
			StringBuffer resHttpHeaders = new StringBuffer();
			ret = HttpCaller.invoke(builder.build(), resHttpHeaders);

			if (curlOnly) {
				System.out.println("---- curlString = " + ret);
			} else {
				System.out.println("---- response http headers = " + resHttpHeaders.toString());
				System.out.println("---- retStr = " + ret);

				//call multi-times for stress or flow-ctrl testing
				int times = Integer.getInteger("test.stress.times",0);
				for(int i=2; i<=times; i++) {
					ret = HttpCaller.invoke(builder.build(), resHttpHeaders);
					System.out.println("---- retStr [#"+i+"] = " + ret);
				}
			}
			
			System.out.println();
			//System.exit(0);
		} catch (HttpCallerException e) {
			System.out.println("---- sdk invoke error:" + e.getMessage());
		}
	}

}