All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.alibaba.ocean.rawsdk.client.http.platform.HttpURLConnectionClient Maven / Gradle / Ivy
/**
*
*/
package com.alibaba.ocean.rawsdk.client.http.platform;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import com.alibaba.ocean.rawsdk.client.Response;
import com.alibaba.ocean.rawsdk.client.http.AbstractHttpClient;
import com.alibaba.ocean.rawsdk.client.http.HttpResponseBuilder;
import com.alibaba.ocean.rawsdk.client.http.HttpSupport;
import com.alibaba.ocean.rawsdk.client.http.InvokeContext;
import com.alibaba.ocean.rawsdk.client.policy.ClientPolicy;
import com.alibaba.ocean.rawsdk.client.serialize.DeSerializerListener;
import com.alibaba.ocean.rawsdk.client.serialize.SerializerProvider;
/**
* @author hongbang.hb
*
*/
public class HttpURLConnectionClient extends AbstractHttpClient {
public HttpURLConnectionClient(SerializerProvider serializerProvider, HttpResponseBuilder responseBuilder) {
super(serializerProvider, responseBuilder);
}
protected Response doMultipartPost(URL url, Map parameters, InvokeContext invokeContext,
ClientPolicy clientPolicy, Collection deSerializerListners) throws IOException {
HttpURLConnection conn = null;
Response response = null;
String BOUNDARY = "---------------------------123821742118716";
OutputStream out = null;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
Map clientHttpHeader = HttpSupport.buildHttpHeader(invokeContext, clientPolicy);
for (Map.Entry entry : clientHttpHeader.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
out = new DataOutputStream(conn.getOutputStream());
StringBuffer strBuf = new StringBuffer();
Iterator> iter = parameters.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = iter.next();
String inputName = (String) entry.getKey();
Object inputValue = entry.getValue();
if (inputValue == null) {
continue;
}
if (inputValue.getClass().isAssignableFrom(byte[].class)
|| inputValue.getClass().isAssignableFrom(Byte[].class)) {
strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"" + "\"\r\n");
strBuf.append("Content-Type:" + "application/x-www-form-urlencoded" + "\r\n\r\n");
out.write(strBuf.toString().getBytes());
if (inputValue instanceof byte[]) {
byte[] inputBytes = (byte[]) inputValue;
out.write(inputBytes);
}
if (inputValue instanceof Byte[]) {
Byte[] inputBytes = (Byte[]) inputValue;
for (Byte b : inputBytes) {
out.write(b.byteValue());
}
}
} else {
strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n");
strBuf.append(inputValue);
out.write(strBuf.toString().getBytes());
}
}
int httpCode = conn.getResponseCode();
String contentEncoding = conn.getContentEncoding();
String contentType = conn.getContentType();
InputStream inputStream = null;
if (httpCode >= 200 && httpCode <= 299) {
inputStream = conn.getInputStream();
} else {
inputStream = conn.getErrorStream();
}
response = this.getResponseBuilder().buildResponse(inputStream, httpCode, contentEncoding, contentType,
invokeContext, deSerializerListners);
} catch (IOException e) {
throw e;
} finally {
if (out != null) {
out.close();
}
if (conn != null) {
conn.disconnect();
}
}
return response;
}
protected Response doPost(URL url, Map parameters, InvokeContext invokeContext,
ClientPolicy clientPolicy, Collection deSerializerListners) throws IOException {
HttpURLConnection conn = null;
OutputStream out = null;
Response response = null;
try {
try {
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
Map clientHttpHeader = HttpSupport.buildHttpHeader(invokeContext, clientPolicy);
for (Map.Entry entry : clientHttpHeader.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}
} catch (IOException e) {
throw e;
}
try {
String query = HttpSupport.buildQuery(parameters);
byte[] content = {};
if (query != null) {
content = query.getBytes(invokeContext.getPolicy().getContentCharset());
}
out = conn.getOutputStream();
out.write(content);
int httpCode = conn.getResponseCode();
String contentEncoding = conn.getContentEncoding();
String contentType = conn.getContentType();
InputStream inputStream = null;
if (httpCode >= 200 && httpCode <= 299) {
inputStream = conn.getInputStream();
} else {
inputStream = conn.getErrorStream();
}
response = this.getResponseBuilder().buildResponse(inputStream, httpCode, contentEncoding, contentType,
invokeContext, deSerializerListners);
} catch (IOException e) {
throw e;
}
} finally {
if (out != null) {
out.close();
}
if (conn != null) {
conn.disconnect();
}
}
return response;
}
protected Response doGet(URL url, Map parameters, InvokeContext invokeContext,
ClientPolicy clientPolicy, Collection deSerializerListners) throws IOException {
HttpURLConnection conn = null;
OutputStream out = null;
Response response = null;
try {
try {
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setDoOutput(true);
Map clientHttpHeader = HttpSupport.buildHttpHeader(invokeContext, clientPolicy);
for (Map.Entry entry : clientHttpHeader.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}
} catch (IOException e) {
throw e;
}
try {
int httpCode = conn.getResponseCode();
String contentEncoding = conn.getContentEncoding();
String contentType = conn.getContentType();
InputStream inputStream = null;
if (httpCode >= 200 && httpCode <= 299) {
inputStream = conn.getInputStream();
} else {
inputStream = conn.getErrorStream();
}
response = this.getResponseBuilder().buildResponse(inputStream, httpCode, contentEncoding, contentType,
invokeContext, deSerializerListners);
} catch (IOException e) {
throw e;
}
} finally {
if (out != null) {
out.close();
}
if (conn != null) {
conn.disconnect();
}
}
return response;
}
}