com.taobao.drc.clusterclient.httpclient.HttpClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of consumer-core Show documentation
Show all versions of consumer-core Show documentation
The java consumer core component for Data Transmission Service
package com.taobao.drc.clusterclient.httpclient;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class HttpClient {
private static final String DEFAULT_API_VERSION = "1.0";
private static final String KEY_API_VERSION = "X-API-Version";
private static final Logger logger = LoggerFactory.getLogger(HttpClient.class);
private final static String DefaultEncoding = "UTF-8";
private final static int DefaultConnectionTimeout = 60 * 1000;
private final static int DefaultSocketTimeout = 60 * 1000;
private final static int DefaultMaxPerRoute = 500;
private final static RequestConfig defaultRequestConfig = RequestConfig.custom()
.setConnectTimeout(DefaultConnectionTimeout)
.setSocketTimeout(DefaultSocketTimeout)
.setConnectionRequestTimeout(DefaultConnectionTimeout)
.build();
private final static org.apache.http.client.HttpClient client;
static {
PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager();
manager.setMaxTotal(DefaultMaxPerRoute);
manager.setDefaultMaxPerRoute(DefaultMaxPerRoute);
client = HttpClients.custom()
.setRetryHandler(new DefaultHttpRequestRetryHandler(3, true))
.setConnectionManager(manager)
.build();
}
public HttpResponse get(final String path) throws IOException {
HttpGet method = new HttpGet(path);
method.setConfig(defaultRequestConfig);
try{
org.apache.http.HttpResponse response = client.execute(method);
HttpResponse resp = new HttpResponse(response);
if (resp.getCode() != 200) {
throw new IllegalStateException("Register to " + path + " failed: " + resp);
}
return resp;
}finally {
if(method != null){
method.releaseConnection();
}
}
}
public HttpResponse post(String url, HttpEntity entity, String apiVersion, RequestConfig requestConfig) throws IOException {
HttpPost method = new HttpPost(url);
method.addHeader(KEY_API_VERSION, apiVersion);
method.setEntity(entity);
method.setConfig(requestConfig);
try
{
HttpResponse resp = new HttpResponse(client.execute(method));
logger.info("Post to [" + url + "] with [" + EntityUtils.toString(entity) + "] rsp: [" + resp + "]");
if (resp.getCode() != 200) {
throw new IllegalStateException("Post to [" + url + "] with [" + EntityUtils.toString(entity) + "] failed: [" + resp + "]");
}
return resp;
}catch(IOException e){
logger.warn(e.toString());
throw e;
}finally{
if( null != method) {
method.releaseConnection();
}
}
}
public HttpResponse post(final String url, final Map posts)
throws IllegalStateException, IOException {
List parameters = new ArrayList();
for (Entry pair : posts.entrySet()) {
parameters.add(new BasicNameValuePair(pair.getKey(), pair.getValue()));
}
return post(url, new UrlEncodedFormEntity(parameters, DefaultEncoding), DEFAULT_API_VERSION, defaultRequestConfig);
}
public HttpResponse post(final String url, final List parameters) throws IOException {
return post(url, new UrlEncodedFormEntity(parameters, DefaultEncoding), DEFAULT_API_VERSION, defaultRequestConfig);
}
public R post(String url, String data, Class responseClass, String apiVersion, RequestConfig requestConfig) throws IOException {
HttpResponse response = post(url, new StringEntity(data, ContentType.APPLICATION_JSON), apiVersion, requestConfig);
return JSON.parseObject(response.getString(), responseClass);
}
}