From: Ruchira Agarwal Date: Mon, 6 Aug 2018 15:10:35 +0000 (+0000) Subject: fix http error handling X-Git-Tag: 0.3.0~25 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=d92da1cb1ce4bc974ca6814593e81f924b76f21f;p=ccsdk%2Fsli%2Fadaptors.git 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 --- 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);