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.net.MalformedURLException;
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.SslSupportLevel;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import org.slf4j.Marker;
43 import org.slf4j.MarkerFactory;
45 public class HttpClientAdapterImpl implements HttpClientAdapter {
47 public static final int CONNECTION_TIMEOUT = 1000;
48 private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientAdapterImpl.class);
49 private static final String CONTENT_TYPE = "Content-Type";
50 private static final String APPLICATION_JSON = "application/json";
51 private static final RequestConfig CONFIG = RequestConfig.custom()
52 .setConnectTimeout(CONNECTION_TIMEOUT)
53 .setConnectionRequestTimeout(CONNECTION_TIMEOUT)
54 .setSocketTimeout(CONNECTION_TIMEOUT)
56 private static final Marker INVOKE = MarkerFactory.getMarker("INVOKE");
57 private HttpClient client;
58 private final String targetUrl;
60 public HttpClientAdapterImpl(String targetUrl) throws MalformedURLException {
61 this.client = SslSupportLevel.getSupportLevelBasedOnProtocol(targetUrl).getClient(CONFIG);
62 this.targetUrl = targetUrl;
65 HttpClientAdapterImpl(HttpClient client, String targetUrl) {
67 this.targetUrl = targetUrl;
71 public void send(String content) {
73 HttpPost request = createRequest(content);
74 HttpResponse response = client.execute(request);
76 //response has to be fully consumed otherwise apache won't release connection
77 EntityUtils.consumeQuietly(response.getEntity());
78 LOGGER.info(INVOKE, "Message sent, ves response code: {}", response.getStatusLine());
79 } catch (IOException e) {
80 LOGGER.warn("Error sending message to ves: " + e.getMessage(), e.getCause());
84 private HttpPost createRequest(String content) throws UnsupportedEncodingException {
85 HttpPost request = new HttpPost(this.targetUrl);
86 StringEntity stringEntity = new StringEntity(content);
87 request.addHeader(CONTENT_TYPE, APPLICATION_JSON);
88 request.addHeader(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID));
89 request.addHeader(X_INVOCATION_ID, UUID.randomUUID().toString());
90 request.setEntity(stringEntity);