Fix for bug PNF Simulator
[integration.git] / test / mocks / pnfsimulator / src / main / java / org / onap / pnfsimulator / simulator / client / HttpClientAdapterImpl.java
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.pnfsimulator.simulator.client;
22
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;
26
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.apache.http.util.EntityUtils;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.slf4j.MDC;
40 import org.slf4j.Marker;
41 import org.slf4j.MarkerFactory;
42
43 public class HttpClientAdapterImpl implements HttpClientAdapter {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientAdapterImpl.class);
46     private static final String CONTENT_TYPE = "Content-Type";
47     private static final String APPLICATION_JSON = "application/json";
48     private final Marker INVOKE = MarkerFactory.getMarker("INVOKE");
49     private static final RequestConfig CONFIG = RequestConfig.custom()
50         .setConnectTimeout(1000)
51         .setConnectionRequestTimeout(1000)
52         .setSocketTimeout(1000)
53         .build();
54
55     private HttpClient client;
56
57     public HttpClientAdapterImpl() {
58         this.client = HttpClientBuilder
59             .create()
60             .setDefaultRequestConfig(CONFIG)
61             .build();
62     }
63
64     @Override
65     public void send(String content, String url) {
66         try {
67             HttpPost request = createRequest(content, url);
68             HttpResponse response = client.execute(request);
69             EntityUtils.consumeQuietly(response.getEntity());
70             LOGGER.info(INVOKE, "Message sent, ves response code: {}", response.getStatusLine());
71         } catch (IOException e) {
72             LOGGER.warn("Error sending message to ves: {}", e.getMessage());
73         }
74     }
75
76     HttpClientAdapterImpl(HttpClient client) {
77         this.client = client;
78     }
79
80     private HttpPost createRequest(String content, String url) throws UnsupportedEncodingException {
81         HttpPost request = new HttpPost(url);
82         StringEntity stringEntity = new StringEntity(content);
83         request.addHeader(CONTENT_TYPE, APPLICATION_JSON);
84         request.addHeader(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID));
85         request.addHeader(X_INVOCATION_ID, UUID.randomUUID().toString());
86         request.setEntity(stringEntity);
87         return request;
88     }
89 }