in odps-console-public/src/main/java/com/aliyun/openservices/odps/console/pub/HttpSubmitCommand.java [125:207]
public void run() throws OdpsException, ODPSConsoleException {
RestClient client;
if (!StringUtils.isNullOrEmpty(token)) {
client = new RestClient(new DefaultTransport());
client.setEndpoint(getContext().getEndpoint());
client.setAccount(new BearerTokenAccount(token));
} else {
client = getCurrentOdps().getRestClient();
}
InputStream content = null;
long contentLength = 0;
Map<String, String> headers = null;
if (getHeaderFileName() != null) {
headers = new HashMap<String, String>();
File file = new File(getHeaderFileName());
if (!file.exists()) {
throw new ODPSConsoleException("file not exist.");
}
FileInputStream fileInput = null;
try {
fileInput = new FileInputStream(file);
Properties properties = new ExtProperties();
properties.load(fileInput);
Enumeration enuKeys = properties.keys();
while(enuKeys.hasMoreElements()) {
String key = (String)enuKeys.nextElement();
String value = properties.getProperty(key);
if (!StringUtils.isNullOrEmpty(key) && !StringUtils.isNullOrEmpty(value)) {
headers.put(key, value);
}
}
} catch (IOException e) {
throw new ODPSConsoleException("can not read file.");
} finally {
IOUtils.closeQuietly(fileInput);
}
}
if (getFileName() != null) {
File file = new File(getFileName());
if (!file.exists()) {
throw new ODPSConsoleException("file not exist.");
}
contentLength = file.length();
try {
content = new FileInputStream(file);
// System.out.println(FileUtil.readFromStream(content));
} catch (Exception e) {
throw new ODPSConsoleException("can not read file.");
}
}
try {
Response response =
client.request(url, method.toString(), parameters, headers, content, contentLength);
// c out response
try {
Map<String, String> resHeaders = response.getHeaders();
for (String key : resHeaders.keySet()) {
String value = resHeaders.get(key);
System.out.println(key + ": " + value);
}
// 添加报文的换行
System.out.println("");
System.out.println(new String(response.getBody(), "utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} finally {
IOUtils.closeQuietly(content);
}
}