54d2c9fbbf17a5771774e26d766e023d33ae1e15
[integration.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.integration
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 java.io.IOException;
24 import java.io.UnsupportedEncodingException;
25 import org.apache.http.HttpResponse;
26 import org.apache.http.client.HttpClient;
27 import org.apache.http.client.config.RequestConfig;
28 import org.apache.http.client.methods.HttpPost;
29 import org.apache.http.entity.StringEntity;
30 import org.apache.http.impl.client.HttpClientBuilder;
31 import org.apache.logging.log4j.LogManager;
32 import org.apache.logging.log4j.Logger;
33
34 public class HttpClientProvider {
35
36     private static final Logger logger = LogManager.getLogger(HttpClientProvider.class);
37     private static final String CONTENT_TYPE = "Content-Type";
38     private static final String APPLICATION_JSON = "application/json";
39
40     private HttpClient client;
41     private String url;
42
43     public HttpClientProvider(String url) {
44
45         RequestConfig config = RequestConfig.custom()
46             .setConnectTimeout(1000)
47             .setConnectionRequestTimeout(1000)
48             .setSocketTimeout(1000)
49             .build();
50
51         this.client = HttpClientBuilder
52             .create()
53             .setDefaultRequestConfig(config)
54             .build();
55
56         this.url = url;
57     }
58
59     public void sendMsg(String content) {
60         try {
61             HttpPost request = createRequest(content);
62             HttpResponse response = client.execute(request);
63             logger.info("MESSAGE SENT, VES RESPONSE CODE: {}", response.getStatusLine());
64         } catch (IOException e) {
65             logger.info("ERROR SENDING MESSAGE TO VES: {}", e.getMessage());
66         }
67     }
68
69     private HttpPost createRequest(String content) throws UnsupportedEncodingException {
70         StringEntity stringEntity = new StringEntity(content);
71         HttpPost request = new HttpPost(url);
72         request.addHeader(CONTENT_TYPE, APPLICATION_JSON);
73         request.setEntity(stringEntity);
74         return request;
75     }
76 }