30f881bbc0d70085c2fec9fe2f415f6b20d0d349
[oom/platform/cert-service.git] / certServiceClient / src / main / java / org / onap / aaf / certservice / client / httpclient / HttpClient.java
1 /*
2  * ============LICENSE_START=======================================================
3  * aaf-certservice-client
4  * ================================================================================
5  * Copyright (C) 2020 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aaf.certservice.client.httpclient;
22
23 import com.google.gson.Gson;
24 import org.apache.http.HttpEntity;
25 import org.apache.http.HttpResponse;
26 import org.apache.http.HttpStatus;
27 import org.apache.http.client.methods.HttpGet;
28 import org.apache.http.impl.client.CloseableHttpClient;
29 import org.apache.http.util.EntityUtils;
30 import org.onap.aaf.certservice.client.httpclient.exception.CertServiceApiResponseException;
31 import org.onap.aaf.certservice.client.httpclient.exception.HttpClientException;
32 import org.onap.aaf.certservice.client.httpclient.model.CertServiceResponse;
33 import org.onap.aaf.certservice.client.httpclient.model.ErrorCertServiceResponse;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import java.io.IOException;
38
39 public class HttpClient {
40
41     private static final String CSR_HEADER_NAME = "CSR";
42     private static final String PK_HEADER_NAME = "PK";
43     private static final String CHARSET_UTF_8 = "UTF-8";
44
45     private final Logger LOGGER = LoggerFactory.getLogger(HttpClient.class);
46
47     private final Gson gson = new Gson();
48     private final CloseableHttpClientProvider httpClientProvider;
49     private final String certServiceAddress;
50
51     public HttpClient(CloseableHttpClientProvider httpClientProvider, String certServiceAddress) {
52         this.httpClientProvider = httpClientProvider;
53         this.certServiceAddress = certServiceAddress;
54     }
55
56     public CertServiceResponse retrieveCertServiceData(String caName, String csr, String encodedPk)
57             throws CertServiceApiResponseException, HttpClientException {
58
59         try (CloseableHttpClient httpClient = httpClientProvider.getClient()) {
60             LOGGER.info("Sending request to API. Url: {}{} ", certServiceAddress, caName);
61             HttpResponse httpResponse = httpClient.execute(createHttpRequest(caName, csr, encodedPk));
62             LOGGER.info("Received response from API");
63             return extractCertServiceResponse(httpResponse);
64
65         } catch (IOException e) {
66             LOGGER.error("Failed execute request to API for URL: {}{} . Exception message: {}",
67                     certServiceAddress, caName, e.getMessage());
68             throw new HttpClientException(e);
69         }
70     }
71
72     private int getStatusCode(HttpResponse httpResponse) {
73         return httpResponse.getStatusLine().getStatusCode();
74     }
75
76     private CertServiceResponse extractCertServiceResponse(HttpResponse httpResponse)
77             throws CertServiceApiResponseException, HttpClientException {
78         int httpResponseCode = getStatusCode(httpResponse);
79         if (HttpStatus.SC_OK != httpResponseCode) {
80             LOGGER.error("Error on API response. Response Code: {}", httpResponseCode);
81             throw generateApiResponseException(httpResponse);
82         }
83         String jsonResponse = getStringResponse(httpResponse.getEntity());
84         return gson.fromJson(jsonResponse, CertServiceResponse.class);
85     }
86
87     private String getStringResponse(HttpEntity httpEntity) throws HttpClientException {
88         try {
89             return EntityUtils.toString(httpEntity, CHARSET_UTF_8);
90         } catch (IOException e) {
91             LOGGER.error("Cannot parse response to string", e);
92             throw new HttpClientException(e);
93         }
94     }
95
96     private HttpGet createHttpRequest(String caName, String csr, String pk) {
97         String url = certServiceAddress + caName;
98         HttpGet httpGet = new HttpGet(url);
99         httpGet.addHeader(CSR_HEADER_NAME, csr);
100         httpGet.addHeader(PK_HEADER_NAME, pk);
101         return httpGet;
102     }
103
104
105     private CertServiceApiResponseException generateApiResponseException(HttpResponse httpResponse)
106             throws HttpClientException {
107         String stringResponse = getStringResponse(httpResponse.getEntity());
108         ErrorCertServiceResponse errorCertServiceResponse =
109                 gson.fromJson(stringResponse, ErrorCertServiceResponse.class);
110
111         String messageFromApi = errorCertServiceResponse.getMessage();
112         String path = errorCertServiceResponse.getPath();
113         int httpResponseCode = getStatusCode(httpResponse);
114
115         return new CertServiceApiResponseException(certServiceAddress + path, httpResponseCode, messageFromApi);
116     }
117 }