diff options
Diffstat (limited to 'common')
4 files changed, 38 insertions, 35 deletions
diff --git a/common/src/main/java/org/onap/so/client/ResponseExceptionMapper.java b/common/src/main/java/org/onap/so/client/ResponseExceptionMapper.java index 31cdd50aee..bcc60b6915 100644 --- a/common/src/main/java/org/onap/so/client/ResponseExceptionMapper.java +++ b/common/src/main/java/org/onap/so/client/ResponseExceptionMapper.java @@ -20,9 +20,6 @@ package org.onap.so.client; -import java.io.IOException; -import java.io.InputStream; -import java.io.StringWriter; import java.util.Optional; import javax.ws.rs.BadRequestException; @@ -33,29 +30,34 @@ import javax.ws.rs.NotAllowedException; import javax.ws.rs.NotAuthorizedException; import javax.ws.rs.NotFoundException; import javax.ws.rs.NotSupportedException; +import javax.ws.rs.ProcessingException; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Response; -import org.apache.commons.io.IOUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public abstract class ResponseExceptionMapper { - + private static final Logger logger = LoggerFactory.getLogger(ResponseExceptionMapper.class); public void map(Response response) { - - response.bufferEntity(); if (response.getStatus() >= 300) { + String body = ""; String message = "empty message"; - if (response.hasEntity()) { - StringWriter writer = new StringWriter(); - try { - IOUtils.copy((InputStream)response.getEntity(), writer, "UTF-8"); - } catch (IOException e) { - writer.append("failed to read entity stream"); - } - Optional<String> result = this.extractMessage(writer.toString()); - if (result.isPresent()) { - message = result.get(); + try { + response.bufferEntity(); + if (response.hasEntity()) { + body = response.readEntity(String.class); } + } catch (IllegalStateException e) { + body = "failed to read entity stream"; + logger.error(body, e); + } catch (ProcessingException e) { + body = "could not buffer stream"; + logger.error(body, e); + } + Optional<String> result = this.extractMessage(body); + if (result.isPresent()) { + message = result.get(); } Response.Status status = Response.Status.fromStatusCode(response.getStatus()); WebApplicationException webAppException; diff --git a/common/src/main/java/org/onap/so/logging/jaxrs/filter/JaxRsClientLogging.java b/common/src/main/java/org/onap/so/logging/jaxrs/filter/JaxRsClientLogging.java index 6c2a96c87d..436faef27f 100644 --- a/common/src/main/java/org/onap/so/logging/jaxrs/filter/JaxRsClientLogging.java +++ b/common/src/main/java/org/onap/so/logging/jaxrs/filter/JaxRsClientLogging.java @@ -121,7 +121,6 @@ public class JaxRsClientLogging implements ClientRequestFilter,ClientResponseFil statusCode=ONAPLogConstants.ResponseStatus.ERROR.toString(); } MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, String.valueOf(responseContext.getStatus())); - MDC.put(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION,getStringFromInputStream(responseContext)); MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, statusCode); logger.info(ONAPLogConstants.Markers.INVOKE_RETURN, "InvokeReturn"); clearClientMDCs(); @@ -142,21 +141,4 @@ public class JaxRsClientLogging implements ClientRequestFilter,ClientResponseFil MDC.remove(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP); } - private static String getStringFromInputStream(ClientResponseContext clientResponseContext) { - - InputStream is = clientResponseContext.getEntityStream(); - ByteArrayOutputStream boas = new ByteArrayOutputStream(); - - try { - IOUtils.copy(is,boas); - InputStream copiedStream = new ByteArrayInputStream(boas.toByteArray()); - clientResponseContext.setEntityStream(copiedStream); - return boas.toString(); - - } catch (IOException e) { - logger.warn("Failed to read response body", e); - } - return "Unable to read input stream"; - } - } diff --git a/common/src/test/java/org/onap/so/client/aai/AAIResourcesClientTest.java b/common/src/test/java/org/onap/so/client/aai/AAIResourcesClientTest.java index a55fbc9517..73fbff6e4f 100644 --- a/common/src/test/java/org/onap/so/client/aai/AAIResourcesClientTest.java +++ b/common/src/test/java/org/onap/so/client/aai/AAIResourcesClientTest.java @@ -35,6 +35,7 @@ import static org.junit.Assert.assertThat; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.spy; +import javax.ws.rs.BadRequestException; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -207,6 +208,23 @@ public class AAIResourcesClientTest { } @Test + public void verifyFailedCallException() { + AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test"); + wireMockRule.stubFor(get( + urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build())) + .willReturn( + aResponse() + .withHeader("Content-Type", "text/plain") + .withBodyFile("aai/error-message.json") + .withStatus(400))); + AAIResourcesClient client= aaiClient; + + thrown.expect(BadRequestException.class); + thrown.expectMessage(containsString("Invalid input performing PUT on url (msg=Precondition Required:resource-version not passed for update of url")); + AAIResultWrapper result = client.get(path); + } + + @Test public void buildRelationshipTest() { AAIResourcesClient client = aaiClient; AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test"); diff --git a/common/src/test/resources/__files/aai/error-message.json b/common/src/test/resources/__files/aai/error-message.json new file mode 100644 index 0000000000..155232294a --- /dev/null +++ b/common/src/test/resources/__files/aai/error-message.json @@ -0,0 +1 @@ +{"requestError":{"serviceException":{"messageId":"SVC3000","text":"Invalid input performing %1 on %2 (msg=%3) (ec=%4)","variables":["PUT","url","Precondition Required:resource-version not passed for update of url","ERR.5.4.6130"]}}}
\ No newline at end of file |