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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.oom.certservice.cmpv2client.impl;
23 import java.io.ByteArrayOutputStream;
24 import java.io.IOException;
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.oom.certservice.cmpv2client.exceptions.CmpClientException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 class Cmpv2HttpClient {
37 private static final Logger LOG = LoggerFactory.getLogger(Cmpv2HttpClient.class);
39 private static final String CONTENT_TYPE = "Content-type";
40 private static final String CMP_REQUEST_MIMETYPE = "application/pkixcmp";
41 private final CloseableHttpClient httpClient;
44 * constructor for Cmpv2HttpClient
46 * @param httpClient CloseableHttpClient used for sending/recieve request.
48 Cmpv2HttpClient(CloseableHttpClient httpClient) {
49 this.httpClient = httpClient;
53 * Send Post Request to Server
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
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();
68 postRequest.setEntity(new ByteArrayEntity(requestBytes));
69 postRequest.setHeader(CONTENT_TYPE, CMP_REQUEST_MIMETYPE);
71 try (CloseableHttpResponse response = httpClient.execute(postRequest)) {
72 response.getEntity().writeTo(byteArrOutputStream);
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;