Merge "update the MR image with the new certificate"
[integration.git] / test / mocks / pnfsimulator / src / main / java / org / onap / pnfsimulator / simulator / client / HttpClientAdapterImpl.java
1 package org.onap.pnfsimulator.simulator.client;
2
3 import static org.onap.pnfsimulator.logging.MDCVariables.REQUEST_ID;
4 import static org.onap.pnfsimulator.logging.MDCVariables.X_INVOCATION_ID;
5 import static org.onap.pnfsimulator.logging.MDCVariables.X_ONAP_REQUEST_ID;
6
7 import java.io.IOException;
8 import java.io.UnsupportedEncodingException;
9 import java.util.UUID;
10 import org.apache.http.HttpResponse;
11 import org.apache.http.client.HttpClient;
12 import org.apache.http.client.config.RequestConfig;
13 import org.apache.http.client.methods.HttpPost;
14 import org.apache.http.entity.StringEntity;
15 import org.apache.http.impl.client.HttpClientBuilder;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18 import org.slf4j.MDC;
19 import org.slf4j.Marker;
20 import org.slf4j.MarkerFactory;
21
22 public class HttpClientAdapterImpl implements HttpClientAdapter {
23
24     private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientAdapterImpl.class);
25     private static final String CONTENT_TYPE = "Content-Type";
26     private static final String APPLICATION_JSON = "application/json";
27     private final Marker INVOKE = MarkerFactory.getMarker("INVOKE");
28     private static final RequestConfig CONFIG = RequestConfig.custom()
29         .setConnectTimeout(1000)
30         .setConnectionRequestTimeout(1000)
31         .setSocketTimeout(1000)
32         .build();
33
34     private HttpClient client;
35
36     public HttpClientAdapterImpl(HttpClient client) {
37         this.client = client;
38     }
39
40     public HttpClientAdapterImpl() {
41         this.client = HttpClientBuilder
42             .create()
43             .setDefaultRequestConfig(CONFIG)
44             .build();
45     }
46
47     @Override
48     public void send(String content, String url) {
49         try {
50             HttpPost request = createRequest(content, url);
51             HttpResponse response = client.execute(request);
52             LOGGER.info(INVOKE, "Message sent, ves response code: {}", response.getStatusLine());
53         } catch (IOException e) {
54             LOGGER.warn("Error sending message to ves: {}", e.getMessage());
55         }
56     }
57
58     private HttpPost createRequest(String content, String url) throws UnsupportedEncodingException {
59         HttpPost request = new HttpPost(url);
60         StringEntity stringEntity = new StringEntity(content);
61         request.addHeader(CONTENT_TYPE, APPLICATION_JSON);
62         request.addHeader(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID));
63         request.addHeader(X_INVOCATION_ID, UUID.randomUUID().toString());
64         request.setEntity(stringEntity);
65         return request;
66     }
67 }