68c743d220bd0e2b1427f2400f72a87f6fd91383
[oom/platform/cert-service.git] / certService / src / main / java / org / onap / aaf / certservice / cmpv2client / impl / Cmpv2HttpClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aaf.certservice.cmpv2client.impl;
22
23 import java.io.ByteArrayOutputStream;
24 import java.io.IOException;
25
26 import org.apache.http.client.methods.CloseableHttpResponse;
27 import org.apache.http.client.methods.HttpPost;
28 import org.apache.http.entity.ByteArrayEntity;
29 import org.apache.http.impl.client.CloseableHttpClient;
30 import org.bouncycastle.asn1.cmp.PKIMessage;
31 import org.onap.aaf.certservice.cmpv2client.exceptions.CmpClientException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 class Cmpv2HttpClient {
36
37     private static final Logger LOG = LoggerFactory.getLogger(Cmpv2HttpClient.class);
38
39     private static final String CONTENT_TYPE = "Content-type";
40     private static final String CMP_REQUEST_MIMETYPE = "application/pkixcmp";
41     private final CloseableHttpClient httpClient;
42
43     /**
44      * constructor for Cmpv2HttpClient
45      *
46      * @param httpClient CloseableHttpClient used for sending/recieve request.
47      */
48     Cmpv2HttpClient(CloseableHttpClient httpClient) {
49         this.httpClient = httpClient;
50     }
51
52     /**
53      * Send Post Request to Server
54      *
55      * @param pkiMessage PKIMessage to send to server
56      * @param urlString  url for the server we're sending request
57      * @param caName     name of CA server
58      * @return PKIMessage received from CMPServer
59      * @throws CmpClientException thrown if problems with connecting or parsing response to server
60      */
61     public byte[] postRequest(
62             final PKIMessage pkiMessage, final String urlString, final String caName)
63             throws CmpClientException {
64         try (ByteArrayOutputStream byteArrOutputStream = new ByteArrayOutputStream()) {
65             final HttpPost postRequest = new HttpPost(urlString);
66             final byte[] requestBytes = pkiMessage.getEncoded();
67
68             postRequest.setEntity(new ByteArrayEntity(requestBytes));
69             postRequest.setHeader(CONTENT_TYPE, CMP_REQUEST_MIMETYPE);
70
71             try (CloseableHttpResponse response = httpClient.execute(postRequest)) {
72                 response.getEntity().writeTo(byteArrOutputStream);
73             }
74             return byteArrOutputStream.toByteArray();
75         } catch (IOException ioe) {
76             CmpClientException cmpClientException =
77                     new CmpClientException(
78                             String.format("IOException error while trying to connect CA %s", caName), ioe);
79             LOG.error("IOException error {}, while trying to connect CA {}", ioe.getMessage(), caName);
80             throw cmpClientException;
81         }
82     }
83 }