From 83bbca087a66d5ea4cff1f5d066f7761948a2cb3 Mon Sep 17 00:00:00 2001 From: eschcam Date: Fri, 18 Nov 2022 12:02:27 +0000 Subject: Added null check in ExternalTestingManagerImpl Added missing null checks in ExternalTestingManagerImpl. Issue-ID: SDC-4269 Change-Id: Ic66f30ab7d78014ad7311ce53c03498168909a61 Signed-off-by: eschcam --- .../impl/ExternalTestingManagerImpl.java | 42 +++++++++++++--------- 1 file changed, 26 insertions(+), 16 deletions(-) (limited to 'openecomp-be/lib') diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/ExternalTestingManagerImpl.java b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/ExternalTestingManagerImpl.java index f115a98e5b..b6cc045a3e 100644 --- a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/ExternalTestingManagerImpl.java +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/ExternalTestingManagerImpl.java @@ -612,12 +612,15 @@ public class ExternalTestingManagerImpl implements ExternalTestingManager { } else { logger.debug("GET request to {} for {}", url, responseType.getType().getTypeName()); } + SimpleClientHttpRequestFactory rf = (SimpleClientHttpRequestFactory) restTemplate.getRequestFactory(); + ResponseEntity re; + if (rf != null) { rf.setReadTimeout(10000); rf.setConnectTimeout(10000); } - ResponseEntity re; + try { if (request != null) { re = restTemplate.exchange(url, HttpMethod.POST, request, responseType); @@ -626,22 +629,28 @@ public class ExternalTestingManagerImpl implements ExternalTestingManager { } } catch (HttpStatusCodeException ex) { // make my own exception out of this. + HttpHeaders httpHeaders = ex.getResponseHeaders(); logger.warn("Unexpected HTTP Status from endpoint {}", ex.getRawStatusCode()); - if ((ex.getResponseHeaders().getContentType() != null) && ( - (ex.getResponseHeaders().getContentType().isCompatibleWith(MediaType.APPLICATION_JSON)) - || (ex.getResponseHeaders().getContentType() - .isCompatibleWith(MediaType.parseMediaType("application/problem+json"))))) { - String s = ex.getResponseBodyAsString(); - logger.warn("endpoint body content is {}", s); - try { - JsonObject o = new GsonBuilder().create().fromJson(s, JsonObject.class); - throw buildTestingException(ex.getRawStatusCode(), o); - } catch (JsonParseException e) { - logger.warn("unexpected JSON response", e); - throw new ExternalTestingException(ENDPOINT_ERROR_CODE, ex.getStatusCode().value(), - ex.getResponseBodyAsString(), ex); - } - } else { + + if (httpHeaders == null) { + throw new ExternalTestingException(ENDPOINT_ERROR_CODE, 500, "Failed to get HTTP Response Headers", ex); + } + + MediaType responseHeadersContentType = httpHeaders.getContentType(); + + if (responseHeadersContentType == null || (!responseHeadersContentType.isCompatibleWith(MediaType.APPLICATION_JSON) + && !responseHeadersContentType.isCompatibleWith(MediaType.parseMediaType("application/problem+json")))) { + throw new ExternalTestingException(ENDPOINT_ERROR_CODE, ex.getStatusCode().value(), ex.getResponseBodyAsString(), ex); + } + + String s = ex.getResponseBodyAsString(); + logger.warn("endpoint body content is {}", s); + + try { + JsonObject o = new GsonBuilder().create().fromJson(s, JsonObject.class); + throw buildTestingException(ex.getRawStatusCode(), o); + } catch (JsonParseException e) { + logger.warn("unexpected JSON response", e); throw new ExternalTestingException(ENDPOINT_ERROR_CODE, ex.getStatusCode().value(), ex.getResponseBodyAsString(), ex); } @@ -650,6 +659,7 @@ public class ExternalTestingManagerImpl implements ExternalTestingManager { } catch (Exception ex) { throw new ExternalTestingException(ENDPOINT_ERROR_CODE, 500, "Generic Exception " + ex.getMessage(), ex); } + if (re != null) { logger.debug("http status of {} from external testing entity {}", re.getStatusCodeValue(), url); return re.getBody(); -- cgit 1.2.3-korg