Fixed merge of development / 12.8.1-5 versions of HttpClient.java

Former-commit-id: 3f88fa9078 [formerly 6dae0398d3 [formerly 34a733bb4701c6f083595be42e5b15cd4ba868bd]]
Former-commit-id: 6dae0398d3
Former-commit-id: 6615c96f33
This commit is contained in:
Steve Harris 2012-08-07 10:04:39 -05:00
parent 1cd1cb250f
commit f19c9fa55d

View file

@ -38,6 +38,7 @@ import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor; import org.apache.http.HttpResponseInterceptor;
import org.apache.http.client.entity.GzipDecompressingEntity; import org.apache.http.client.entity.GzipDecompressingEntity;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ConnectionPoolTimeoutException; import org.apache.http.conn.ConnectionPoolTimeoutException;
import org.apache.http.entity.AbstractHttpEntity; import org.apache.http.entity.AbstractHttpEntity;
import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.ByteArrayEntity;
@ -77,6 +78,17 @@ import com.raytheon.uf.common.util.ByteArrayOutputStreamPool.ByteArrayOutputStre
*/ */
public class HttpClient { public class HttpClient {
public static class HttpClientResponse {
public final int code;
public final byte[] data;
private HttpClientResponse(int code, byte[] data) {
this.code = code;
this.data = data != null ? data : new byte[0];
}
}
private static final int SUCCESS_CODE = 200; private static final int SUCCESS_CODE = 200;
private final org.apache.http.client.HttpClient client; private final org.apache.http.client.HttpClient client;
@ -91,12 +103,12 @@ public class HttpClient {
*/ */
private int retryCount = 1; private int retryCount = 1;
private static IUFStatusHandler statusHandler = UFStatus.getHandler( private static final IUFStatusHandler statusHandler = UFStatus.getHandler(
HttpClient.class, "DEFAULT"); HttpClient.class, "DEFAULT");
private ThreadSafeClientConnManager connManager = null; private ThreadSafeClientConnManager connManager = null;
private NetworkStatistics stats = NetworkStatistics.getInstance(); private NetworkStatistics stats = new NetworkStatistics();
private boolean gzipRequests = false; private boolean gzipRequests = false;
@ -240,7 +252,7 @@ public class HttpClient {
* @throws IOException * @throws IOException
* @throws CommunicationException * @throws CommunicationException
*/ */
private HttpResponse postRequest(HttpPost put) throws IOException, private HttpResponse postRequest(HttpUriRequest put) throws IOException,
CommunicationException { CommunicationException {
HttpResponse resp = client.execute(put); HttpResponse resp = client.execute(put);
int code = resp.getStatusLine().getStatusCode(); int code = resp.getStatusLine().getStatusCode();
@ -265,9 +277,10 @@ public class HttpClient {
* the request to post * the request to post
* @param handlerCallback * @param handlerCallback
* the handler to handle the response stream * the handler to handle the response stream
* @return the http status code
* @throws CommunicationException * @throws CommunicationException
*/ */
private void process(HttpPost put, IStreamHandler handlerCallback) private int process(HttpUriRequest put, IStreamHandler handlerCallback)
throws CommunicationException { throws CommunicationException {
int tries = 0; int tries = 0;
boolean retry = true; boolean retry = true;
@ -327,6 +340,7 @@ public class HttpClient {
// should only be able to get here if we didn't encounter the // should only be able to get here if we didn't encounter the
// exceptions above on the most recent try // exceptions above on the most recent try
processResponse(resp, handlerCallback); processResponse(resp, handlerCallback);
return resp.getStatusLine().getStatusCode();
} finally { } finally {
if (ongoing != null) { if (ongoing != null) {
ongoing.decrementAndGet(); ongoing.decrementAndGet();
@ -449,6 +463,22 @@ public class HttpClient {
handlerCallback); handlerCallback);
} }
/**
* Executes an HttpUriRequest and returns a response with the byte[] and
* http status code.
*
* @param request
* the request to execute
* @return the result and status code
* @throws CommunicationException
*/
public HttpClientResponse executeRequest(HttpUriRequest request)
throws CommunicationException {
DefaultInternalStreamHandler streamHandler = new DefaultInternalStreamHandler();
int statusCode = process(request, streamHandler);
return new HttpClientResponse(statusCode, streamHandler.byteResult);
}
/** /**
* Post a string to an endpoint and stream the result back. * Post a string to an endpoint and stream the result back.
* *
@ -524,6 +554,15 @@ public class HttpClient {
this.retryCount = retryCount; this.retryCount = retryCount;
} }
/**
* Gets the network statistics for http traffic.
*
* @return
*/
public NetworkStatistics getStats() {
return stats;
}
/** /**
* Provides a safe interface callback for implementing stream behavior with * Provides a safe interface callback for implementing stream behavior with
* http * http