From 0e71c58d17748db0b96faa757d3cfc779e5260a0 Mon Sep 17 00:00:00 2001 From: Steve Harris Date: Tue, 7 Aug 2012 15:29:25 -0500 Subject: [PATCH] Fixed merge (take 2) of development / 12.8.1-5 versions of HttpClient.java Former-commit-id: c72218985be462d4f7fd92975f34ab8d0abbfc6f [formerly e2267aa18630835d664cfc227910c7f001a878a3] [formerly 926ff5dc5a7f85c5732e9ffd306f8b23eaf244eb] [formerly c72218985be462d4f7fd92975f34ab8d0abbfc6f [formerly e2267aa18630835d664cfc227910c7f001a878a3] [formerly 926ff5dc5a7f85c5732e9ffd306f8b23eaf244eb] [formerly 9e2e0fa166866a7a08f1d9415369bc51ee9a869a [formerly 926ff5dc5a7f85c5732e9ffd306f8b23eaf244eb [formerly 5717f9bf344b6d78ca30f1387679e535bac5a13c]]]] Former-commit-id: 9e2e0fa166866a7a08f1d9415369bc51ee9a869a Former-commit-id: 9688e06a3a56e42777ee94284dc71500ab54462c [formerly d42beafc734356134e7267131244ca066849eb6a] [formerly 447b1173369877f42f724fff7bac204016c3883e [formerly 15753699bd9770f168b90518d3e03199430b4b7d]] Former-commit-id: d7ed4a42f3312d5ff6c151f837117009fbb2640a [formerly 3eefb26e941045d0d98268565fc1799b53b4d80a] Former-commit-id: 05ef298baf04f31c137879f584cff0022d29d83f --- .../raytheon/uf/common/comm/HttpClient.java | 43 +++++++++++-------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/edexOsgi/com.raytheon.uf.common.comm/src/com/raytheon/uf/common/comm/HttpClient.java b/edexOsgi/com.raytheon.uf.common.comm/src/com/raytheon/uf/common/comm/HttpClient.java index 5d28a06c29..48c4276c67 100644 --- a/edexOsgi/com.raytheon.uf.common.comm/src/com/raytheon/uf/common/comm/HttpClient.java +++ b/edexOsgi/com.raytheon.uf.common.comm/src/com/raytheon/uf/common/comm/HttpClient.java @@ -255,12 +255,7 @@ public class HttpClient { private HttpResponse postRequest(HttpUriRequest put) throws IOException, CommunicationException { HttpResponse resp = client.execute(put); - int code = resp.getStatusLine().getStatusCode(); - if (code != SUCCESS_CODE) { - throw new CommunicationException( - "Error reading server response. Got error message: " - + EntityUtils.toString(resp.getEntity())); - } else if (previousConnectionFailed) { + if (previousConnectionFailed) { previousConnectionFailed = false; statusHandler.handle(Priority.INFO, "Connection with server reestablished."); @@ -280,8 +275,8 @@ public class HttpClient { * @return the http status code * @throws CommunicationException */ - private int process(HttpUriRequest put, IStreamHandler handlerCallback) - throws CommunicationException { + private HttpClientResponse process(HttpUriRequest put, + IStreamHandler handlerCallback) throws CommunicationException { int tries = 0; boolean retry = true; HttpResponse resp = null; @@ -340,7 +335,12 @@ public class HttpClient { // should only be able to get here if we didn't encounter the // exceptions above on the most recent try processResponse(resp, handlerCallback); - return resp.getStatusLine().getStatusCode(); + byte[] byteResult = null; + if (handlerCallback instanceof DefaultInternalStreamHandler) { + byteResult = ((DefaultInternalStreamHandler) handlerCallback).byteResult; + } + return new HttpClientResponse(resp.getStatusLine().getStatusCode(), + byteResult); } finally { if (ongoing != null) { ongoing.decrementAndGet(); @@ -387,10 +387,6 @@ public class HttpClient { "Error reading InputStream, assuming closed", e); } } - } else { - // this should be impossible to reach - throw new CommunicationException( - "Error ocurred while contacting server, did not get a reponse or an exception"); } } @@ -406,8 +402,9 @@ public class HttpClient { private byte[] executePostMethod(HttpPost put) throws CommunicationException { DefaultInternalStreamHandler handlerCallback = new DefaultInternalStreamHandler(); - this.process(put, handlerCallback); - return handlerCallback.byteResult; + HttpClientResponse resp = this.process(put, handlerCallback); + checkStatusCode(resp); + return resp.data; } /** @@ -475,8 +472,7 @@ public class HttpClient { public HttpClientResponse executeRequest(HttpUriRequest request) throws CommunicationException { DefaultInternalStreamHandler streamHandler = new DefaultInternalStreamHandler(); - int statusCode = process(request, streamHandler); - return new HttpClientResponse(statusCode, streamHandler.byteResult); + return process(request, streamHandler); } /** @@ -500,6 +496,16 @@ public class HttpClient { postStreamingEntity(address, new StringEntity(message), handlerCallback); } + private void checkStatusCode(HttpClientResponse response) + throws CommunicationException { + if (response.code != SUCCESS_CODE) { + throw new CommunicationException( + "Error reading server response. Got error message: " + + response.data != null ? new String(response.data) + : null); + } + } + /** * Posts an entity to the address and stream the result back. * @@ -514,7 +520,8 @@ public class HttpClient { private void postStreamingEntity(String address, AbstractHttpEntity entity, IStreamHandler handlerCallback) throws CommunicationException { HttpPost put = new HttpPost(address); - process(put, handlerCallback); + HttpClientResponse resp = process(put, handlerCallback); + checkStatusCode(resp); } public void setMaxConnectionsPerHost(int maxConnections) {