diff options
author | Ruchira Agarwal <ra1926@att.com> | 2018-08-06 15:10:35 +0000 |
---|---|---|
committer | Ruchira Agarwal <ra1926@att.com> | 2018-08-06 15:10:35 +0000 |
commit | d92da1cb1ce4bc974ca6814593e81f924b76f21f (patch) | |
tree | 629bb3577f3d8692480b0ca79b7008af975d6a17 | |
parent | 5d0c8984dc39d465ef6c3ed776213010849d34a5 (diff) |
fix http error handling
Enhance exception handling by providing alternate message when getResponseMessage() call fails.
Change-Id: I70123b1cb295f6fe6aeb0afbdd6714e1adddaa01
Issue-ID: CCSDK-426
Signed-off-by: Ruchira Agarwal <ra1926@att.com>
-rwxr-xr-x | aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIClientRESTExecutor.java | 57 |
1 files changed, 52 insertions, 5 deletions
diff --git a/aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIClientRESTExecutor.java b/aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIClientRESTExecutor.java index aa134cb4..6eb53746 100755 --- a/aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIClientRESTExecutor.java +++ b/aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIClientRESTExecutor.java @@ -49,6 +49,8 @@ import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocketFactory; import javax.ws.rs.HttpMethod; +import javax.ws.rs.core.Response.Status; +import javax.ws.rs.core.Response; import org.apache.commons.codec.binary.Base64; import org.onap.ccsdk.sli.adaptors.aai.AAIService.TransactionIdTracker; @@ -248,13 +250,22 @@ public class AAIClientRESTExecutor implements AAIExecutorInterface { } // Check for errors - String responseMessage = con.getResponseMessage(); int responseCode = con.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { inputStream = con.getInputStream(); } else { inputStream = con.getErrorStream(); } + String responseMessage = null; + try { + responseMessage = con.getResponseMessage(); + } catch(Exception exc) { + Status status = Response.Status.fromStatusCode(responseCode) ; + if(status != null && status.getReasonPhrase() != null) + responseMessage = status.getReasonPhrase(); + else + responseMessage = "NOT PROVIDED"; + } // Process the response LOG.debug("HttpURLConnection result:" + responseCode + " : " + responseMessage); @@ -382,13 +393,22 @@ public class AAIClientRESTExecutor implements AAIExecutorInterface { osw.flush(); // Check for errors - String responseMessage = con.getResponseMessage(); int responseCode = con.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_NO_CONTENT) { inputStream = con.getInputStream(); } else { inputStream = con.getErrorStream(); } + String responseMessage = null; + try { + responseMessage = con.getResponseMessage(); + } catch(Exception exc) { + Status status = Response.Status.fromStatusCode(responseCode) ; + if(status != null && status.getReasonPhrase() != null) + responseMessage = status.getReasonPhrase(); + else + responseMessage = "NOT PROVIDED"; + } LOG.debug("HttpURLConnection result:" + responseCode + " : " + responseMessage); logMetricResponse(responseCode, responseMessage); @@ -456,13 +476,22 @@ public class AAIClientRESTExecutor implements AAIExecutorInterface { conn.setDoOutput(true); // Check for errors - String responseMessage = conn.getResponseMessage(); int responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_NO_CONTENT) { inputStream = conn.getInputStream(); } else { inputStream = conn.getErrorStream(); } + String responseMessage = null; + try { + responseMessage = conn.getResponseMessage(); + } catch(Exception exc) { + Status status = Response.Status.fromStatusCode(responseCode) ; + if(status != null && status.getReasonPhrase() != null) + responseMessage = status.getReasonPhrase(); + else + responseMessage = "NOT PROVIDED"; + } // Process the response LOG.debug("HttpURLConnection result:" + responseCode + " : " + responseMessage); @@ -530,13 +559,22 @@ public class AAIClientRESTExecutor implements AAIExecutorInterface { logMetricRequest("GET "+requestUrl.getPath(), "", requestUrl.getPath()); // Check for errors - String responseMessage = con.getResponseMessage(); int responseCode = con.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { inputStream = con.getInputStream(); } else { inputStream = con.getErrorStream(); } + String responseMessage = null; + try { + responseMessage = con.getResponseMessage(); + } catch(Exception exc) { + Status status = Response.Status.fromStatusCode(responseCode) ; + if(status != null && status.getReasonPhrase() != null) + responseMessage = status.getReasonPhrase(); + else + responseMessage = "NOT PROVIDED"; + } logMetricResponse(responseCode, responseMessage); ObjectMapper mapper = AAIService.getObjectMapper(); @@ -596,13 +634,22 @@ public class AAIClientRESTExecutor implements AAIExecutorInterface { osw.flush(); // Check for errors - String responseMessage = con.getResponseMessage(); int responseCode = con.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_NO_CONTENT) { inputStream = con.getInputStream(); } else { inputStream = con.getErrorStream(); } + String responseMessage = null; + try { + responseMessage = con.getResponseMessage(); + } catch(Exception exc) { + Status status = Response.Status.fromStatusCode(responseCode) ; + if(status != null && status.getReasonPhrase() != null) + responseMessage = status.getReasonPhrase(); + else + responseMessage = "NOT PROVIDED"; + } LOG.info("HttpURLConnection result: " + responseCode + " : " + responseMessage); logMetricResponse(responseCode, responseMessage); |