diff options
author | Eylon Malin <eylon.malin@intl.att.com> | 2019-09-01 15:02:06 +0300 |
---|---|---|
committer | Eylon Malin <eylon.malin@intl.att.com> | 2019-09-01 15:02:06 +0300 |
commit | e6c30425575cd76a3955b03ab389150ed74fbb1d (patch) | |
tree | e1a668efc3bbd6b5da72ebd8b9825d5e1a96d74c /vid-app-common/src/main/java | |
parent | 406cc2fe614d089c2f7834f9a22d6dfa47b4fa16 (diff) |
handle non OK response from SDC while getting model
Issue-ID: VID-378
Signed-off-by: Eylon Malin <eylon.malin@intl.att.com>
Change-Id: Idc6e587abb24fbec65ed159db7008e50abee2581
Diffstat (limited to 'vid-app-common/src/main/java')
3 files changed, 47 insertions, 16 deletions
diff --git a/vid-app-common/src/main/java/org/onap/vid/asdc/rest/SdcRestClient.java b/vid-app-common/src/main/java/org/onap/vid/asdc/rest/SdcRestClient.java index decf446d6..1de9715ee 100644 --- a/vid-app-common/src/main/java/org/onap/vid/asdc/rest/SdcRestClient.java +++ b/vid-app-common/src/main/java/org/onap/vid/asdc/rest/SdcRestClient.java @@ -28,6 +28,7 @@ import static org.onap.vid.asdc.AsdcClient.URIS.TOSCA_MODEL_URL_TEMPLATE; import static org.onap.vid.client.SyncRestClientInterface.HEADERS.AUTHORIZATION; import static org.onap.vid.client.SyncRestClientInterface.HEADERS.CONTENT_TYPE; import static org.onap.vid.client.SyncRestClientInterface.HEADERS.X_ECOMP_INSTANCE_ID; +import static org.onap.vid.client.UnirestPatchKt.extractRawAsString; import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY; import static org.onap.vid.utils.Logging.logRequest; @@ -43,6 +44,8 @@ import java.nio.file.StandardCopyOption; import java.util.Collections; import java.util.Map; import java.util.UUID; +import javax.ws.rs.ProcessingException; +import javax.ws.rs.client.ResponseProcessingException; import org.onap.portalsdk.core.util.SystemProperties; import org.onap.vid.aai.ExceptionWithRequestInfo; import org.onap.vid.aai.HttpResponseWithRequestInfo; @@ -87,13 +90,35 @@ public class SdcRestClient implements AsdcClient { @Override public Path getServiceToscaModel(UUID uuid) throws AsdcCatalogException { - InputStream inputStream = Try - .of(() -> getServiceInputStream(uuid, false)) - .getOrElseThrow(AsdcCatalogException::new) - .getResponse() - .getBody(); - - return createTmpFile(inputStream); + try { + HttpResponseWithRequestInfo<InputStream> responseWithRequestInfo = getServiceInputStream(uuid, false); + + if (responseWithRequestInfo.getResponse().getStatus()>399) { + Logging.logResponse(LOGGER, HttpMethod.GET, + responseWithRequestInfo.getRequestUrl(), responseWithRequestInfo.getResponse()); + + String body = extractRawAsString(responseWithRequestInfo.getResponse()); + throw new AsdcCatalogException(String.format("Http bad status code: %s, body: %s", + responseWithRequestInfo.getResponse().getStatus(), + body)); + } + + final InputStream csarInputStream = responseWithRequestInfo.getResponse().getBody(); + Path toscaFilePath = createTmpFile(csarInputStream); + LOGGER.debug("Received {} {} . Tosca file was saved at: {}", + responseWithRequestInfo.getRequestHttpMethod().name(), + responseWithRequestInfo.getRequestUrl(), + toscaFilePath.toAbsolutePath()); + return toscaFilePath; + } catch (ResponseProcessingException e) { + //Couldn't convert response to Java type + throw new AsdcCatalogException("ASDC response could not be processed", e); + } catch (ProcessingException e) { + //IO problems during request + throw new AsdcCatalogException("Failed to get a response from ASDC service. Cause: " + e.getMessage(), e); + } catch (RuntimeException e) { + throw new AsdcCatalogException(e); + } } @Override diff --git a/vid-app-common/src/main/java/org/onap/vid/client/UnirestPatch.kt b/vid-app-common/src/main/java/org/onap/vid/client/UnirestPatch.kt index 750646621..5730c11f1 100644 --- a/vid-app-common/src/main/java/org/onap/vid/client/UnirestPatch.kt +++ b/vid-app-common/src/main/java/org/onap/vid/client/UnirestPatch.kt @@ -22,9 +22,11 @@ package org.onap.vid.client import io.joshworks.restclient.http.Headers import io.joshworks.restclient.http.HttpResponse +import org.apache.commons.io.IOUtils import org.apache.http.HttpVersion import org.apache.http.message.BasicHttpResponse import java.io.InputStream +import java.nio.charset.StandardCharsets /// Patch NPE in joshworks's Unirest HttpResponse::getBody when getRawBody is null fun <T> patched(httpResponse: HttpResponse<T>) = @@ -35,6 +37,17 @@ private fun <T> willGetBodyTriggerNPE(httpResponse: HttpResponse<T>) = private val dummyHttpResponse = BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "ok") +fun extractRawAsString(response: HttpResponse<*>?): String { + try { + if (response == null || response.rawBody==null) return "" + response.rawBody.reset() + return IOUtils.toString(response.rawBody, StandardCharsets.UTF_8.name()) + } catch (e: Exception) { + //Nothing to do here + } + + return "" +} /** * This class inherits HttpResponse to have compatible interface, * but implementation is done through delegation to another diff --git a/vid-app-common/src/main/java/org/onap/vid/model/probes/HttpRequestMetadata.java b/vid-app-common/src/main/java/org/onap/vid/model/probes/HttpRequestMetadata.java index 984c0d766..0e7e914f1 100644 --- a/vid-app-common/src/main/java/org/onap/vid/model/probes/HttpRequestMetadata.java +++ b/vid-app-common/src/main/java/org/onap/vid/model/probes/HttpRequestMetadata.java @@ -22,10 +22,9 @@ package org.onap.vid.model.probes; import static org.apache.commons.lang3.ObjectUtils.defaultIfNull; +import static org.onap.vid.client.UnirestPatchKt.extractRawAsString; import com.google.common.base.MoreObjects; -import java.nio.charset.StandardCharsets; -import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.onap.vid.aai.ExceptionWithRequestInfo; import org.onap.vid.aai.HttpResponseWithRequestInfo; @@ -89,16 +88,10 @@ public class HttpRequestMetadata extends StatusMetadata { this.url = response.getRequestUrl(); this.httpCode = response.getResponse().getStatus(); if (readRawData) { - try { - response.getResponse().getRawBody().reset(); - this.rawData = IOUtils.toString(response.getResponse().getRawBody(), StandardCharsets.UTF_8.name()); - } catch (Exception e) { - //Nothing to do here - } + this.rawData = extractRawAsString(response.getResponse()); } } - public HttpMethod getHttpMethod() { return httpMethod; } |