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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aaf.certservice.client.httpclient;
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;
37 import java.io.IOException;
39 public class HttpClient {
41 private static final Logger LOGGER = LoggerFactory.getLogger(HttpClient.class);
42 private static final String CSR_HEADER_NAME = "CSR";
43 private static final String PK_HEADER_NAME = "PK";
44 private static final String CHARSET_UTF_8 = "UTF-8";
46 private final Gson gson = new Gson();
47 private final CloseableHttpsClientProvider httpClientProvider;
48 private final String certServiceAddress;
50 public HttpClient(CloseableHttpsClientProvider httpClientProvider, String certServiceAddress) {
51 this.httpClientProvider = httpClientProvider;
52 this.certServiceAddress = certServiceAddress;
55 public CertServiceResponse retrieveCertServiceData(String caName, String csr, String encodedPk)
56 throws CertServiceApiResponseException, HttpClientException {
58 try (CloseableHttpClient httpClient = httpClientProvider.getClient()) {
59 LOGGER.info("Attempt to send request to API, on url: {}{} ", certServiceAddress, caName);
60 HttpResponse httpResponse = httpClient.execute(createHttpRequest(caName, csr, encodedPk));
61 LOGGER.info("Received response from API");
62 return extractCertServiceResponse(httpResponse);
64 } catch (IOException e) {
65 LOGGER.error("Failed execute request to API for URL: {}{} , exception message: {}",
66 certServiceAddress, caName, e.getMessage());
67 throw new HttpClientException(e);
71 private HttpGet createHttpRequest(String caName, String csr, String pk) {
72 String url = certServiceAddress + caName;
73 HttpGet httpGet = new HttpGet(url);
74 httpGet.addHeader(CSR_HEADER_NAME, csr);
75 httpGet.addHeader(PK_HEADER_NAME, pk);
79 private CertServiceResponse extractCertServiceResponse(HttpResponse httpResponse)
80 throws CertServiceApiResponseException, HttpClientException {
81 int httpResponseCode = getStatusCode(httpResponse);
82 if (HttpStatus.SC_OK != httpResponseCode) {
83 LOGGER.error("Error on API response. Response Code: {}", httpResponseCode);
84 throw generateApiResponseException(httpResponse);
86 String jsonResponse = getStringResponse(httpResponse.getEntity());
87 return gson.fromJson(jsonResponse, CertServiceResponse.class);
90 private CertServiceApiResponseException generateApiResponseException(HttpResponse httpResponse)
91 throws HttpClientException {
92 String stringResponse = getStringResponse(httpResponse.getEntity());
93 ErrorCertServiceResponse errorCertServiceResponse =
94 gson.fromJson(stringResponse, ErrorCertServiceResponse.class);
96 return new CertServiceApiResponseException(getStatusCode(httpResponse), errorCertServiceResponse.getMessage());
99 private int getStatusCode(HttpResponse httpResponse) {
100 return httpResponse.getStatusLine().getStatusCode();
103 private String getStringResponse(HttpEntity httpEntity) throws HttpClientException {
105 return EntityUtils.toString(httpEntity, CHARSET_UTF_8);
106 } catch (IOException e) {
107 LOGGER.error("Cannot parse response to string, exception message: {}", e.getMessage());
108 throw new HttpClientException(e);