mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-19 08:28:46 +00:00
Added new HTTP pool manager and http handling.
This commit is contained in:
@@ -4,28 +4,15 @@ import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.http.HttpClientConnection;
|
||||
import org.apache.http.HttpException;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
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.client.protocol.HttpClientContext;
|
||||
import org.apache.http.conn.ConnectionPoolTimeoutException;
|
||||
import org.apache.http.conn.ConnectionRequest;
|
||||
import org.apache.http.conn.HttpClientConnectionManager;
|
||||
import org.apache.http.conn.routing.HttpRoute;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.http.protocol.HttpRequestExecutor;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -34,29 +21,9 @@ import com.bwssystems.HABridge.api.NameValue;
|
||||
|
||||
public class HTTPHandler {
|
||||
private static final Logger log = LoggerFactory.getLogger(HTTPHandler.class);
|
||||
// private CloseableHttpClient httpClient;
|
||||
// private RequestConfig globalConfig;
|
||||
private HttpClientContext context;
|
||||
private PoolingHttpClientConnectionManager connMgr;
|
||||
private HttpRoute route;
|
||||
private HttpClientConnection conn;
|
||||
private HttpHost theHost;
|
||||
|
||||
public HTTPHandler() {
|
||||
context = HttpClientContext.create();
|
||||
connMgr = new PoolingHttpClientConnectionManager();
|
||||
// Increase max total connection to 200
|
||||
connMgr.setMaxTotal(200);
|
||||
// Increase default max connection per route to 20
|
||||
connMgr.setDefaultMaxPerRoute(20);
|
||||
// Increase max connections for localhost:80 to 50
|
||||
HttpHost localhost = new HttpHost("locahost", 80);
|
||||
connMgr.setMaxPerRoute(new HttpRoute(localhost), 50);
|
||||
route = null;
|
||||
conn = null;
|
||||
theHost = null;
|
||||
// globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();
|
||||
// httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig).build();
|
||||
super();
|
||||
}
|
||||
|
||||
|
||||
@@ -68,9 +35,8 @@ public class HTTPHandler {
|
||||
String theContent = null;
|
||||
URI theURI = null;
|
||||
ContentType parsedContentType = null;
|
||||
ConnectionRequest connRequest = null;
|
||||
StringEntity requestBody = null;
|
||||
boolean badResponse = false;
|
||||
|
||||
if (contentType != null && !contentType.trim().isEmpty()) {
|
||||
parsedContentType = ContentType.parse(contentType);
|
||||
if (body != null && body.length() > 0)
|
||||
@@ -82,44 +48,7 @@ public class HTTPHandler {
|
||||
log.warn("Error creating URI http request: " + url + " with message: " + e1.getMessage());
|
||||
return null;
|
||||
}
|
||||
if(route == null) {
|
||||
theHost = new HttpHost(theURI.getHost(), theURI.getPort());
|
||||
route = new HttpRoute(theHost);
|
||||
}
|
||||
if(conn == null) {
|
||||
// Request new connection. This can be a long process
|
||||
connRequest = connMgr.requestConnection(route, null);
|
||||
// Wait for connection up to 10 sec
|
||||
try {
|
||||
conn = connRequest.get(10, TimeUnit.SECONDS);
|
||||
} catch (ConnectionPoolTimeoutException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
// If not open
|
||||
if (!conn.isOpen()) {
|
||||
// establish connection based on its route info
|
||||
try {
|
||||
connMgr.connect(conn, route, 1000, context);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
// and mark it as route complete
|
||||
try {
|
||||
connMgr.routeComplete(conn, route, context);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (httpVerb == null || httpVerb.trim().isEmpty() || HttpGet.METHOD_NAME.equalsIgnoreCase(httpVerb)) {
|
||||
request = new HttpGet(theURI);
|
||||
@@ -146,28 +75,12 @@ public class HTTPHandler {
|
||||
request.setHeader(headers[i].getName(), headers[i].getValue());
|
||||
}
|
||||
}
|
||||
HttpResponse response = null;
|
||||
HttpRequestExecutor exeRequest = new HttpRequestExecutor();
|
||||
context.setTargetHost(theHost);
|
||||
CloseableHttpResponse response = null;
|
||||
for (int retryCount = 0; retryCount < 2; retryCount++) {
|
||||
try {
|
||||
response = exeRequest.execute(request, conn, context);
|
||||
response = HttpClientPool.getClient().execute(request);
|
||||
log.debug((httpVerb == null ? "GET" : httpVerb) + " execute (" + retryCount + ") on URL responded: "
|
||||
+ response.getStatusLine().getStatusCode());
|
||||
if (response != null && response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300) {
|
||||
log.debug("Successfull response - The http response is <<<" + theContent + ">>>");
|
||||
retryCount = 2;
|
||||
} else if (response != null) {
|
||||
log.warn("HTTP response code was not an expected successful response of between 200 - 299, the code was: "
|
||||
+ response.getStatusLine());
|
||||
if (response.getStatusLine().getStatusCode() == 504) {
|
||||
log.warn("HTTP response code was 504, retrying...");
|
||||
} else
|
||||
retryCount = 2;
|
||||
|
||||
badResponse = true;
|
||||
}
|
||||
|
||||
if (response != null && response.getEntity() != null) {
|
||||
try {
|
||||
|
||||
@@ -180,18 +93,28 @@ public class HTTPHandler {
|
||||
// inputstream
|
||||
// ignore
|
||||
// content
|
||||
if(!badResponse)
|
||||
theContent = null;
|
||||
} catch (Exception e) {
|
||||
log.debug("Error ocurred in handling response entity after successful call, still responding success. "
|
||||
+ e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
if (response != null && response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300) {
|
||||
log.debug("Successfull response - The http response is <<<" + theContent + ">>>");
|
||||
retryCount = 2;
|
||||
} else if (response != null) {
|
||||
log.warn("HTTP response code was not an expected successful response of between 200 - 299, the code was: "
|
||||
+ response.getStatusLine());
|
||||
if (response.getStatusLine().getStatusCode() == 504) {
|
||||
log.warn("HTTP response code was 504, retrying...");
|
||||
} else
|
||||
retryCount = 2;
|
||||
|
||||
theContent = null;
|
||||
}
|
||||
|
||||
} catch (ClientProtocolException e) {
|
||||
log.warn("Client Protocol Exception received, retyring....");
|
||||
} catch (HttpException e) {
|
||||
log.warn("Error calling out to HA gateway: HttpException in log", e);
|
||||
} catch (IOException e) {
|
||||
}catch (IOException e) {
|
||||
log.warn("Error calling out to HA gateway: IOException in log: " + e.getMessage());
|
||||
retryCount = 2;
|
||||
}
|
||||
@@ -205,30 +128,8 @@ public class HTTPHandler {
|
||||
}
|
||||
}
|
||||
}
|
||||
connMgr.releaseConnection(conn, null, 1, TimeUnit.SECONDS);
|
||||
return theContent;
|
||||
}
|
||||
|
||||
// public HttpClient getHttpClient() {
|
||||
// return httpClient;
|
||||
// }
|
||||
|
||||
|
||||
// public CloseableHttpClient getHttpClient() {
|
||||
// return httpClient;
|
||||
// }
|
||||
|
||||
|
||||
public void closeHandler() {
|
||||
try {
|
||||
// httpClient.close();
|
||||
if(conn != null)
|
||||
conn.close();
|
||||
connMgr.closeExpiredConnections();
|
||||
connMgr.shutdown();
|
||||
} catch (IOException e) {
|
||||
// noop
|
||||
}
|
||||
// httpClient = null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user