From 8b74643e0738bd0266e510a422bf71cc67604f37 Mon Sep 17 00:00:00 2001 From: Tomasz Wrobel Date: Wed, 18 Mar 2020 14:35:36 +0100 Subject: Improve exceptions logging in certservice client Issue-ID: AAF-1107 Signed-off-by: Tomasz Wrobel Change-Id: I94a6477a7b1f086b698be4f0388e7a3ded0cd0f3 --- .../aaf/certservice/client/AppExitHandler.java | 12 +++--- .../aaf/certservice/client/CertServiceClient.java | 11 +++-- .../onap/aaf/certservice/client/api/ExitCode.java | 41 ------------------ .../aaf/certservice/client/api/ExitStatus.java | 48 ++++++++++++++++++++++ .../certservice/client/api/ExitableException.java | 2 +- .../client/certification/CsrFactory.java | 2 - .../client/certification/KeyPairFactory.java | 2 +- .../certification/PrivateKeyToPemEncoder.java | 2 +- .../conversion/PKCS12FilesCreator.java | 2 +- .../conversion/PemToPKCS12Converter.java | 4 +- .../exception/CsrGenerationException.java | 8 ++-- .../exception/KeyPairGenerationException.java | 8 ++-- .../exception/PemToPKCS12ConverterException.java | 8 ++-- .../exception/PkEncodingException.java | 8 ++-- .../exception/ClientConfigurationException.java | 8 ++-- .../exception/CsrConfigurationException.java | 8 ++-- .../certservice/client/httpclient/HttpClient.java | 45 +++++++++----------- .../exception/CertServiceApiResponseException.java | 13 +++--- .../httpclient/exception/HttpClientException.java | 10 ++--- .../httpclient/model/ErrorCertServiceResponse.java | 13 ++---- 20 files changed, 128 insertions(+), 127 deletions(-) delete mode 100644 certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitCode.java create mode 100644 certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitStatus.java (limited to 'certServiceClient/src/main/java') diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/AppExitHandler.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/AppExitHandler.java index e29142e8..caeca37e 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/AppExitHandler.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/AppExitHandler.java @@ -18,14 +18,16 @@ */ package org.onap.aaf.certservice.client; +import org.onap.aaf.certservice.client.api.ExitStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class AppExitHandler { - public static final Logger LOGGER = LoggerFactory.getLogger(AppExitHandler.class); + private static final Logger LOGGER = LoggerFactory.getLogger(AppExitHandler.class); - public void exit(int exitCode) { - LOGGER.debug("Application exits with following exit code: {}", exitCode); - System.exit(exitCode); + public void exit(ExitStatus exitStatus) { + LOGGER.info(String.format("Application exits with following exit code: %s and message: %s", + exitStatus.getExitCodeValue(), exitStatus.getMessage())); + System.exit(exitStatus.getExitCodeValue()); } -} \ No newline at end of file +} diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/CertServiceClient.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/CertServiceClient.java index d3d7f26d..0916bb8a 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/CertServiceClient.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/CertServiceClient.java @@ -36,13 +36,17 @@ import org.onap.aaf.certservice.client.configuration.model.CsrConfiguration; import org.onap.aaf.certservice.client.httpclient.CloseableHttpClientProvider; import org.onap.aaf.certservice.client.httpclient.HttpClient; import org.onap.aaf.certservice.client.httpclient.model.CertServiceResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -import static org.onap.aaf.certservice.client.api.ExitCode.SUCCESS_EXIT_CODE; +import static org.onap.aaf.certservice.client.api.ExitStatus.SUCCESS; import static org.onap.aaf.certservice.client.certification.EncryptionAlgorithmConstants.KEY_SIZE; import static org.onap.aaf.certservice.client.certification.EncryptionAlgorithmConstants.RSA_ENCRYPTION_ALGORITHM; public class CertServiceClient { + private static final Logger LOGGER = LoggerFactory.getLogger(CertServiceClient.class); + private AppExitHandler appExitHandler; public CertServiceClient(AppExitHandler appExitHandler) { @@ -74,8 +78,9 @@ public class CertServiceClient { filesCreator.createKeystore(certServiceData.getCertificateChain(), keyPair.getPrivate()); filesCreator.createTruststore(certServiceData.getTrustedCertificates()); } catch (ExitableException e) { - appExitHandler.exit(e.applicationExitCode()); + LOGGER.error("Cert Service Client fail in execution: ", e); + appExitHandler.exit(e.applicationExitStatus()); } - appExitHandler.exit(SUCCESS_EXIT_CODE.getValue()); + appExitHandler.exit(SUCCESS); } } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitCode.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitCode.java deleted file mode 100644 index 670cbe90..00000000 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitCode.java +++ /dev/null @@ -1,41 +0,0 @@ -/*============LICENSE_START======================================================= - * aaf-certservice-client - * ================================================================================ - * Copyright (C) 2020 Nokia. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ -package org.onap.aaf.certservice.client.api; - -public enum ExitCode { - SUCCESS_EXIT_CODE(0), - CLIENT_CONFIGURATION_EXCEPTION(1), - CSR_CONFIGURATION_EXCEPTION(2), - KEY_PAIR_GENERATION_EXCEPTION(3), - CSR_GENERATION_EXCEPTION(4), - CERT_SERVICE_API_CONNECTION_EXCEPTION(5), - HTTP_CLIENT_EXCEPTION(6), - PKCS12_CONVERSION_EXCEPTION(7), - PK_TO_PEM_ENCODING_EXCEPTION(8); - - private final int value; - - ExitCode(int value) { - this.value = value; - } - - public int getValue() { - return value; - } -} diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitStatus.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitStatus.java new file mode 100644 index 00000000..c474fd03 --- /dev/null +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitStatus.java @@ -0,0 +1,48 @@ +/*============LICENSE_START======================================================= + * aaf-certservice-client + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.aaf.certservice.client.api; + +public enum ExitStatus { + + SUCCESS(0, "Success"), + CLIENT_CONFIGURATION_EXCEPTION(1,"Invalid client configuration"), + CSR_CONFIGURATION_EXCEPTION(2,"Invalid CSR configuration"), + KEY_PAIR_GENERATION_EXCEPTION(3,"Fail in key pair generation"), + CSR_GENERATION_EXCEPTION(4,"Fail in CSR generation"), + CERT_SERVICE_API_CONNECTION_EXCEPTION(5,"CertService HTTP unsuccessful response"), + HTTP_CLIENT_EXCEPTION(6,"Internal HTTP Client connection problem"), + PKCS12_CONVERSION_EXCEPTION(7,"Fail in PKCS12 conversion"), + PK_TO_PEM_ENCODING_EXCEPTION(8,"Fail in Private Key to PEM Encoding"); + + private final int value; + private final String message; + + ExitStatus(int value, String message) { + this.value = value; + this.message = message; + } + + public int getExitCodeValue() { + return value; + } + + public String getMessage(){ + return message; + } +} diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitableException.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitableException.java index f9166c96..fc1bc760 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitableException.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitableException.java @@ -26,5 +26,5 @@ public abstract class ExitableException extends Exception { public ExitableException(String message) { super(message); } - public abstract int applicationExitCode(); + public abstract ExitStatus applicationExitStatus(); } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/CsrFactory.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/CsrFactory.java index 83fa6d44..b536127c 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/CsrFactory.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/CsrFactory.java @@ -40,9 +40,7 @@ import org.slf4j.LoggerFactory; import javax.security.auth.x500.X500Principal; import java.io.IOException; import java.io.StringWriter; -import java.nio.charset.StandardCharsets; import java.security.KeyPair; -import java.util.Base64; import java.util.Optional; import static org.onap.aaf.certservice.client.certification.EncryptionAlgorithmConstants.COMMON_NAME; diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/KeyPairFactory.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/KeyPairFactory.java index 64136863..5d56f08c 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/KeyPairFactory.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/KeyPairFactory.java @@ -41,7 +41,7 @@ public class KeyPairFactory { try { return createKeyPairGenerator().generateKeyPair(); } catch (NoSuchAlgorithmException e) { - LOGGER.error("Generation of KeyPair failed, exception message: " + e.getMessage()); + LOGGER.error("Generation of KeyPair failed, exception message: {}" , e.getMessage()); throw new KeyPairGenerationException(e); } } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/PrivateKeyToPemEncoder.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/PrivateKeyToPemEncoder.java index 77995958..4e88a805 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/PrivateKeyToPemEncoder.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/PrivateKeyToPemEncoder.java @@ -43,7 +43,7 @@ public class PrivateKeyToPemEncoder { try (JcaPEMWriter pemWriter = new JcaPEMWriter(stringWriter)) { pemWriter.writeObject(new PemObject(PEM_OBJECT_TYPE, pk.getEncoded())); } catch (IOException e) { - LOGGER.error("Exception occurred during encoding PrivateKey to PEM", e); + LOGGER.error("Encode of private key to PEM failed. Exception message: {}", e.getMessage()); throw new PkEncodingException(e); } return stringWriter.toString(); diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12FilesCreator.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12FilesCreator.java index d8c41bfd..8e6fb89c 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12FilesCreator.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12FilesCreator.java @@ -66,7 +66,7 @@ class PKCS12FilesCreator { try (FileOutputStream fos = new FileOutputStream(path)) { fos.write(data); } catch (IOException e) { - LOGGER.error("PKCS12 files creation failed", e); + LOGGER.error("PKCS12 files creation failed, exception message: {}", e.getMessage()); throw new PemToPKCS12ConverterException(e); } } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PemToPKCS12Converter.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PemToPKCS12Converter.java index eab9bf7c..8c794e72 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PemToPKCS12Converter.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PemToPKCS12Converter.java @@ -79,7 +79,7 @@ class PemToPKCS12Converter { ks.store(bos, password.toCharArray()); return bos.toByteArray(); } catch (IOException | CertificateException | NoSuchAlgorithmException | KeyStoreException e) { - LOGGER.error("Pem to PKCS12 converter failed", e); + LOGGER.error("Pem to PKCS12 converter failed, exception message: {}", e.getMessage()); throw new PemToPKCS12ConverterException(e); } } @@ -126,7 +126,7 @@ class PemToPKCS12Converter { .setProvider(new BouncyCastleProvider()) .getCertificate(certHolder); } catch (IOException | CertificateException e) { - LOGGER.error("Certificates conversion failed", e); + LOGGER.error("Certificates conversion failed, exception message: {}", e.getMessage()); throw new PemToPKCS12ConverterException(e); } } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/CsrGenerationException.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/CsrGenerationException.java index c1d4afd2..8c93a227 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/CsrGenerationException.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/CsrGenerationException.java @@ -19,17 +19,17 @@ package org.onap.aaf.certservice.client.certification.exception; -import org.onap.aaf.certservice.client.api.ExitCode; +import org.onap.aaf.certservice.client.api.ExitStatus; import org.onap.aaf.certservice.client.api.ExitableException; public class CsrGenerationException extends ExitableException { - private static final ExitCode EXIT_CODE = ExitCode.CSR_GENERATION_EXCEPTION; + private static final ExitStatus EXIT_STATUS = ExitStatus.CSR_GENERATION_EXCEPTION; public CsrGenerationException(Throwable e) { super(e); } - public int applicationExitCode() { - return EXIT_CODE.getValue(); + public ExitStatus applicationExitStatus() { + return EXIT_STATUS; } } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/KeyPairGenerationException.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/KeyPairGenerationException.java index 6af69889..d03c819d 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/KeyPairGenerationException.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/KeyPairGenerationException.java @@ -18,17 +18,17 @@ */ package org.onap.aaf.certservice.client.certification.exception; -import org.onap.aaf.certservice.client.api.ExitCode; +import org.onap.aaf.certservice.client.api.ExitStatus; import org.onap.aaf.certservice.client.api.ExitableException; public class KeyPairGenerationException extends ExitableException { - private static final ExitCode EXIT_CODE = ExitCode.KEY_PAIR_GENERATION_EXCEPTION; + private static final ExitStatus EXIT_STATUS = ExitStatus.KEY_PAIR_GENERATION_EXCEPTION; public KeyPairGenerationException(Throwable e) { super(e); } - public int applicationExitCode() { - return EXIT_CODE.getValue(); + public ExitStatus applicationExitStatus() { + return EXIT_STATUS; } } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/PemToPKCS12ConverterException.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/PemToPKCS12ConverterException.java index 87020d6f..b98f4ace 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/PemToPKCS12ConverterException.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/PemToPKCS12ConverterException.java @@ -19,11 +19,11 @@ package org.onap.aaf.certservice.client.certification.exception; -import org.onap.aaf.certservice.client.api.ExitCode; +import org.onap.aaf.certservice.client.api.ExitStatus; import org.onap.aaf.certservice.client.api.ExitableException; public class PemToPKCS12ConverterException extends ExitableException { - private static final ExitCode EXIT_CODE = ExitCode.PKCS12_CONVERSION_EXCEPTION; + private static final ExitStatus EXIT_STATUS = ExitStatus.PKCS12_CONVERSION_EXCEPTION; public PemToPKCS12ConverterException(Throwable e) { super(e); @@ -33,7 +33,7 @@ public class PemToPKCS12ConverterException extends ExitableException { } @Override - public int applicationExitCode() { - return EXIT_CODE.getValue(); + public ExitStatus applicationExitStatus() { + return EXIT_STATUS; } } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/PkEncodingException.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/PkEncodingException.java index 596a6a44..20d623aa 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/PkEncodingException.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/PkEncodingException.java @@ -19,17 +19,17 @@ package org.onap.aaf.certservice.client.certification.exception; -import org.onap.aaf.certservice.client.api.ExitCode; +import org.onap.aaf.certservice.client.api.ExitStatus; import org.onap.aaf.certservice.client.api.ExitableException; public class PkEncodingException extends ExitableException { - private static final ExitCode EXIT_CODE = ExitCode.PK_TO_PEM_ENCODING_EXCEPTION; + private static final ExitStatus EXIT_STATUS = ExitStatus.PK_TO_PEM_ENCODING_EXCEPTION; public PkEncodingException(Throwable e) { super(e); } - public int applicationExitCode() { - return EXIT_CODE.getValue(); + public ExitStatus applicationExitStatus() { + return EXIT_STATUS; } } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/exception/ClientConfigurationException.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/exception/ClientConfigurationException.java index 5d968f43..f9360d73 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/exception/ClientConfigurationException.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/exception/ClientConfigurationException.java @@ -19,17 +19,17 @@ */ package org.onap.aaf.certservice.client.configuration.exception; -import org.onap.aaf.certservice.client.api.ExitCode; +import org.onap.aaf.certservice.client.api.ExitStatus; import org.onap.aaf.certservice.client.api.ExitableException; public class ClientConfigurationException extends ExitableException { - private static final ExitCode EXIT_CODE = ExitCode.CLIENT_CONFIGURATION_EXCEPTION; + private static final ExitStatus EXIT_STATUS = ExitStatus.CLIENT_CONFIGURATION_EXCEPTION; public ClientConfigurationException(String message) { super(message); } - public int applicationExitCode() { - return EXIT_CODE.getValue(); + public ExitStatus applicationExitStatus() { + return EXIT_STATUS; } } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/exception/CsrConfigurationException.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/exception/CsrConfigurationException.java index f9373117..a7cf4f7a 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/exception/CsrConfigurationException.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/exception/CsrConfigurationException.java @@ -19,17 +19,17 @@ */ package org.onap.aaf.certservice.client.configuration.exception; -import org.onap.aaf.certservice.client.api.ExitCode; +import org.onap.aaf.certservice.client.api.ExitStatus; import org.onap.aaf.certservice.client.api.ExitableException; public class CsrConfigurationException extends ExitableException { - private static final ExitCode EXIT_CODE = ExitCode.CSR_CONFIGURATION_EXCEPTION; + private static final ExitStatus EXIT_STATUS = ExitStatus.CSR_CONFIGURATION_EXCEPTION; public CsrConfigurationException(String message) { super(message); } - public int applicationExitCode() { - return EXIT_CODE.getValue(); + public ExitStatus applicationExitStatus() { + return EXIT_STATUS; } } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/httpclient/HttpClient.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/httpclient/HttpClient.java index 30f881bb..0cb88051 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/httpclient/HttpClient.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/httpclient/HttpClient.java @@ -63,14 +63,18 @@ public class HttpClient { return extractCertServiceResponse(httpResponse); } catch (IOException e) { - LOGGER.error("Failed execute request to API for URL: {}{} . Exception message: {}", + LOGGER.error("Failed execute request to API for URL: {}{} , exception message: {}", certServiceAddress, caName, e.getMessage()); throw new HttpClientException(e); } } - private int getStatusCode(HttpResponse httpResponse) { - return httpResponse.getStatusLine().getStatusCode(); + private HttpGet createHttpRequest(String caName, String csr, String pk) { + String url = certServiceAddress + caName; + HttpGet httpGet = new HttpGet(url); + httpGet.addHeader(CSR_HEADER_NAME, csr); + httpGet.addHeader(PK_HEADER_NAME, pk); + return httpGet; } private CertServiceResponse extractCertServiceResponse(HttpResponse httpResponse) @@ -84,34 +88,25 @@ public class HttpClient { return gson.fromJson(jsonResponse, CertServiceResponse.class); } - private String getStringResponse(HttpEntity httpEntity) throws HttpClientException { - try { - return EntityUtils.toString(httpEntity, CHARSET_UTF_8); - } catch (IOException e) { - LOGGER.error("Cannot parse response to string", e); - throw new HttpClientException(e); - } - } - - private HttpGet createHttpRequest(String caName, String csr, String pk) { - String url = certServiceAddress + caName; - HttpGet httpGet = new HttpGet(url); - httpGet.addHeader(CSR_HEADER_NAME, csr); - httpGet.addHeader(PK_HEADER_NAME, pk); - return httpGet; - } - - private CertServiceApiResponseException generateApiResponseException(HttpResponse httpResponse) throws HttpClientException { String stringResponse = getStringResponse(httpResponse.getEntity()); ErrorCertServiceResponse errorCertServiceResponse = gson.fromJson(stringResponse, ErrorCertServiceResponse.class); - String messageFromApi = errorCertServiceResponse.getMessage(); - String path = errorCertServiceResponse.getPath(); - int httpResponseCode = getStatusCode(httpResponse); + return new CertServiceApiResponseException(getStatusCode(httpResponse), errorCertServiceResponse.getMessage()); + } + + private int getStatusCode(HttpResponse httpResponse) { + return httpResponse.getStatusLine().getStatusCode(); + } - return new CertServiceApiResponseException(certServiceAddress + path, httpResponseCode, messageFromApi); + private String getStringResponse(HttpEntity httpEntity) throws HttpClientException { + try { + return EntityUtils.toString(httpEntity, CHARSET_UTF_8); + } catch (IOException e) { + LOGGER.error("Cannot parse response to string, exception message: {}", e.getMessage()); + throw new HttpClientException(e); + } } } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/httpclient/exception/CertServiceApiResponseException.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/httpclient/exception/CertServiceApiResponseException.java index 7286318f..151c2a1c 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/httpclient/exception/CertServiceApiResponseException.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/httpclient/exception/CertServiceApiResponseException.java @@ -20,23 +20,22 @@ package org.onap.aaf.certservice.client.httpclient.exception; -import org.onap.aaf.certservice.client.api.ExitCode; +import org.onap.aaf.certservice.client.api.ExitStatus; import org.onap.aaf.certservice.client.api.ExitableException; public class CertServiceApiResponseException extends ExitableException { - private static final ExitCode EXIT_CODE = ExitCode.CERT_SERVICE_API_CONNECTION_EXCEPTION; + private static final ExitStatus EXIT_STATUS = ExitStatus.CERT_SERVICE_API_CONNECTION_EXCEPTION; - public CertServiceApiResponseException(String url, int responseCode, String messageFromAPI) { + public CertServiceApiResponseException(int responseCode, String messageFromAPI) { - super(String.format("Request failed for URL '%s'. Response code: %d . Message from API: %s", - url, + super(String.format("CertService HTTP unsuccessful response. Response code: %d . Message from Service: %s", responseCode, messageFromAPI)); } @Override - public int applicationExitCode() { - return EXIT_CODE.getValue(); + public ExitStatus applicationExitStatus() { + return EXIT_STATUS; } } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/httpclient/exception/HttpClientException.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/httpclient/exception/HttpClientException.java index 28f83073..697d152a 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/httpclient/exception/HttpClientException.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/httpclient/exception/HttpClientException.java @@ -20,18 +20,18 @@ package org.onap.aaf.certservice.client.httpclient.exception; -import org.onap.aaf.certservice.client.api.ExitCode; +import org.onap.aaf.certservice.client.api.ExitStatus; import org.onap.aaf.certservice.client.api.ExitableException; public class HttpClientException extends ExitableException { - private static final ExitCode EXIT_CODE = ExitCode.HTTP_CLIENT_EXCEPTION; + private static final ExitStatus EXIT_STATUS = ExitStatus.HTTP_CLIENT_EXCEPTION; - public HttpClientException(Throwable e) { + public HttpClientException(Throwable e){ super(e); } @Override - public int applicationExitCode() { - return EXIT_CODE.getValue(); + public ExitStatus applicationExitStatus() { + return EXIT_STATUS; } } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/httpclient/model/ErrorCertServiceResponse.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/httpclient/model/ErrorCertServiceResponse.java index 6fe99ce5..4a9efcb4 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/httpclient/model/ErrorCertServiceResponse.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/httpclient/model/ErrorCertServiceResponse.java @@ -22,19 +22,14 @@ package org.onap.aaf.certservice.client.httpclient.model; public class ErrorCertServiceResponse { - private final String message; - private final String path; + private final String errorMessage; - public ErrorCertServiceResponse(String message, String path) { - this.message = message; - this.path = path; + public ErrorCertServiceResponse(String errorMessage) { + this.errorMessage = errorMessage; } public String getMessage() { - return message; + return errorMessage; } - public String getPath() { - return path; - } } -- cgit 1.2.3-korg