2 * ============LICENSE_START=======================================================
3 * PNF-REGISTRATION-HANDLER
4 * ================================================================================
5 * Copyright (C) 2018 NOKIA Intellectual Property. 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.util.UUID;
30 import org.apache.http.HttpResponse;
31 import org.apache.http.client.HttpClient;
32 import org.apache.http.client.config.RequestConfig;
33 import org.apache.http.client.methods.HttpPost;
34 import org.apache.http.entity.StringEntity;
35 import org.apache.http.impl.client.HttpClientBuilder;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import org.slf4j.Marker;
40 import org.slf4j.MarkerFactory;
42 public class HttpClientAdapterImpl implements HttpClientAdapter {
44 private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientAdapterImpl.class);
45 private static final String CONTENT_TYPE = "Content-Type";
46 private static final String APPLICATION_JSON = "application/json";
47 private final Marker INVOKE = MarkerFactory.getMarker("INVOKE");
48 private static final RequestConfig CONFIG = RequestConfig.custom()
49 .setConnectTimeout(1000)
50 .setConnectionRequestTimeout(1000)
51 .setSocketTimeout(1000)
54 private HttpClient client;
56 public HttpClientAdapterImpl() {
57 this.client = HttpClientBuilder
59 .setDefaultRequestConfig(CONFIG)
64 public void send(String content, String url) {
66 HttpPost request = createRequest(content, url);
67 HttpResponse response = client.execute(request);
68 LOGGER.info(INVOKE, "Message sent, ves response code: {}", response.getStatusLine());
69 } catch (IOException e) {
70 LOGGER.warn("Error sending message to ves: {}", e.getMessage());
74 HttpClientAdapterImpl(HttpClient client) {
78 private HttpPost createRequest(String content, String url) throws UnsupportedEncodingException {
79 HttpPost request = new HttpPost(url);
80 StringEntity stringEntity = new StringEntity(content);
81 request.addHeader(CONTENT_TYPE, APPLICATION_JSON);
82 request.addHeader(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID));
83 request.addHeader(X_INVOCATION_ID, UUID.randomUUID().toString());
84 request.setEntity(stringEntity);