2 * ============LICENSE_START=======================================================
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 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;
34 public class HttpClientProvider {
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";
40 private HttpClient client;
43 public HttpClientProvider(String url) {
45 RequestConfig config = RequestConfig.custom()
46 .setConnectTimeout(1000)
47 .setConnectionRequestTimeout(1000)
48 .setSocketTimeout(1000)
51 this.client = HttpClientBuilder
53 .setDefaultRequestConfig(config)
59 public void sendMsg(String content) {
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());
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);