package com.dic.bss.jike.filter.del;
import java.io.IOException;
import java.net.ConnectException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.StackObjectPoolFactory;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;
public class HSNHttpHelper {
private static final Log log = LogFactory.getLog(HSNHttpHelper.class);
private static ObjectPool httpClientPool = null;
private static long httpClientPoolMaxCount = 0;
static {
httpClientPoolMaxCount = 10;// EsbConfig.getInt("HTTP_CLIENT_POOL_MAX_COUNT");
// httpClientPool = new StackObjectPoolFactory(new
// HttpClientPoolableObjectFactory(),
// EsbConfig.getInt("HTTP_CLIENT_POOL_MAX_COUNT"),EsbConfig.getInt("HTTP_CLIENT_POOL_MIN_COUNT")).createPool();
httpClientPool = new StackObjectPoolFactory(new HttpClientPoolableObjectFactory(), 10, 2).createPool();
}
// 以http的POST方式发送请求.假设返回数据均为文本。暂不考虑返回非文本数据。
public static HttpRetBean doUrlPostStr(URI uri, Header[] headers, byte[] reqBody, int timeOut, boolean isReadResponse) throws ConnectException,
IOException, URISyntaxException {
return doUrlReqStr(new HttpPost(uri), headers, reqBody, timeOut, isReadResponse);
}
public static HttpRetBean doUrlPostStr(URI uri, String pathInfo, Header[] headers, byte[] reqBody, int timeOut, String ContentType, String reqCharset,
String resCharset, boolean isReadResponse) throws ConnectException, IOException, URISyntaxException {
return doUrlReqStr(new HttpPost(new URI(uri.getScheme(), uri.getAuthority(), uri.getHost(), uri.getPort(), uri.getPath() + pathInfo, uri.getQuery(),
uri.getFragment())), headers, reqBody, timeOut, isReadResponse);
}
private static HttpRetBean doUrlReqStr(HttpUriRequest request, Header[] headers, byte[] reqBody, int timeOut, boolean isReadResponse)
throws ConnectException, IOException, URISyntaxException {
request.setHeaders(headers);
request.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, new Integer(timeOut));
if (reqBody != null && request instanceof HttpEntityEnclosingRequestBase) {
ByteArrayEntity entity = new ByteArrayEntity(reqBody);
entity.setChunked(false);
((HttpEntityEnclosingRequestBase) request).setEntity(entity);
}
HttpClient httpClient = null;
try {
httpClient = (HttpClient) httpClientPool.borrowObject();
HttpResponse res = httpClient.execute(request);
byte[] retData = null;
if (isReadResponse) {
retData = EntityUtils.toByteArray(res.getEntity());// autoClose
} else {
res.getEntity().getContent().close();
}
return new HttpRetBean(res.getStatusLine().getStatusCode(), res.getAllHeaders(), retData);
} catch (ConnectException e) {
request.abort();
throw e;
} catch (IOException e) {
request.abort();
throw e;
} catch (URISyntaxException e) {
request.abort();
throw e;
} catch (Exception e) {
try {
throw new Exception(request.getURI().toString() + ":" + e.getMessage());
} catch (Exception e2) {
}
} finally {
if (httpClient != null) {
try {
httpClientPool.returnObject(httpClient);
} catch (Exception e) {
log.warn("HttpClient对象归还失败", e);
}
}
}
return null;
}
// 发送http请求.假设返回数据均为文本。暂不考虑返回非文本数据。
public static HttpRetBean doUrlReqStr(URI uri, String httpMethod, String queryStr, Header[] headers, byte[] reqBody, int timeOut, boolean isReadResponse)
throws ConnectException, IOException, URISyntaxException {
if (httpMethod.equals(HttpPost.METHOD_NAME)) {
return doUrlPostStr(new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), queryStr, uri.getFragment()), headers,
reqBody, timeOut, isReadResponse);
} else if (httpMethod.equals(HttpGet.METHOD_NAME)) {
return doUrlReqStr(new HttpGet(uri), headers, reqBody, timeOut, isReadResponse);
} else if (httpMethod.equals(HttpDelete.METHOD_NAME)) {
return doUrlReqStr(new HttpDelete(uri), headers, reqBody, timeOut, isReadResponse);
} else if (httpMethod.equals(HttpPut.METHOD_NAME)) {
return doUrlReqStr(new HttpPut(uri), headers, reqBody, timeOut, isReadResponse);
} else {
throw new IOException("http请求方法错误:" + httpMethod);
}
}
public static HttpRetBean doUrlReqStr(URI uri, String httpMethod, String pathInfo, String queryStr, Header[] headers, byte[] reqBody, int timeOut,
boolean isReadResponse) throws ConnectException, IOException, URISyntaxException {
if (httpMethod.equals(HttpPost.METHOD_NAME)) {
return doUrlPostStr(
new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath() + pathInfo, queryStr, uri.getFragment()), headers,
reqBody, timeOut, isReadResponse);
} else if (httpMethod.equals(HttpGet.METHOD_NAME)) {
return doUrlReqStr(
new HttpGet(
new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath() + pathInfo, queryStr, uri.getFragment())),
headers, reqBody, timeOut, isReadResponse);
} else if (httpMethod.equals(HttpDelete.METHOD_NAME)) {
return doUrlReqStr(
new HttpDelete(new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath() + pathInfo, queryStr,
uri.getFragment())), headers, reqBody, timeOut, isReadResponse);
} else if (httpMethod.equals(HttpPut.METHOD_NAME)) {
return doUrlReqStr(
new HttpPut(
new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath() + pathInfo, queryStr, uri.getFragment())),
headers, reqBody, timeOut, isReadResponse);
} else {
throw new IOException("http请求方法错误:" + httpMethod);
}
}
// yangxiao 获取对象池numActive
public static long getHttpClientPoolNumActive() {
return httpClientPool.getNumActive();
}
// yangxiao 获取对象池NumIdle
public static long getHttpClientPoolMaxNum() {
return httpClientPoolMaxCount;
}
}
|