2 * ============LICENSE_START=======================================================
3 * PNF-REGISTRATION-HANDLER
4 * ================================================================================
5 * Copyright (C) 2018 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.pnfsimulator.simulator.client;
23 import static org.onap.pnfsimulator.logging.MDCVariables.REQUEST_ID;
24 import static org.onap.pnfsimulator.logging.MDCVariables.X_INVOCATION_ID;
25 import static org.onap.pnfsimulator.logging.MDCVariables.X_ONAP_REQUEST_ID;
27 import java.io.IOException;
28 import java.io.UnsupportedEncodingException;
29 import java.security.GeneralSecurityException;
30 import java.util.UUID;
32 import org.apache.http.HttpResponse;
33 import org.apache.http.client.HttpClient;
34 import org.apache.http.client.config.RequestConfig;
35 import org.apache.http.client.methods.HttpPost;
36 import org.apache.http.entity.StringEntity;
37 import org.apache.http.util.EntityUtils;
38 import org.onap.pnfsimulator.simulator.client.utils.ssl.SSLAuthenticationHelper;
39 import org.onap.pnfsimulator.simulator.client.utils.ssl.SslSupportLevel;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import org.slf4j.Marker;
44 import org.slf4j.MarkerFactory;
46 public class HttpClientAdapterImpl implements HttpClientAdapter {
48 private static final int CONNECTION_TIMEOUT = 1000;
49 private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientAdapterImpl.class);
50 private static final String CONTENT_TYPE = "Content-Type";
51 private static final String APPLICATION_JSON = "application/json";
52 private static final RequestConfig CONFIG = RequestConfig.custom()
53 .setConnectTimeout(CONNECTION_TIMEOUT)
54 .setConnectionRequestTimeout(CONNECTION_TIMEOUT)
55 .setSocketTimeout(CONNECTION_TIMEOUT)
57 private static final Marker INVOKE = MarkerFactory.getMarker("INVOKE");
58 private SslSupportLevel sslSupportLevel;
59 private HttpClient client;
60 private final String targetUrl;
62 public HttpClientAdapterImpl(String targetUrl, SSLAuthenticationHelper sslAuthenticationHelper)
63 throws IOException, GeneralSecurityException {
64 this.sslSupportLevel = sslAuthenticationHelper.isClientCertificateEnabled() ?
65 SslSupportLevel.CLIENT_CERT_AUTH : SslSupportLevel.getSupportLevelBasedOnProtocol(targetUrl);
66 this.client = sslSupportLevel.getClient(CONFIG, sslAuthenticationHelper);
67 this.targetUrl = targetUrl;
70 HttpClientAdapterImpl(HttpClient client, String targetUrl) {
72 this.targetUrl = targetUrl;
76 public void send(String content) {
78 HttpPost request = createRequest(content);
79 HttpResponse response = client.execute(request);
81 //response has to be fully consumed otherwise apache won't release connection
82 EntityUtils.consumeQuietly(response.getEntity());
83 LOGGER.info(INVOKE, "Message sent, ves response code: {}", response.getStatusLine());
84 } catch (IOException e) {
85 LOGGER.warn("Error sending message to ves: " + e.getMessage(), e.getCause());
89 public SslSupportLevel getSslSupportLevel(){
90 return sslSupportLevel;
93 private HttpPost createRequest(String content) throws UnsupportedEncodingException {
94 HttpPost request = new HttpPost(this.targetUrl);
95 StringEntity stringEntity = new StringEntity(content);
96 request.addHeader(CONTENT_TYPE, APPLICATION_JSON);
97 request.addHeader(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID));
98 request.addHeader(X_INVOCATION_ID, UUID.randomUUID().toString());
99 request.setEntity(stringEntity);