2 * Copyright 2017 BOCO Corporation. CMCC Technologies Co., Ltd
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.vfc.nfvo.emsdriver.northbound.client;
18 import org.apache.commons.codec.binary.Base64;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.apache.http.HttpEntity;
22 import org.apache.http.client.methods.CloseableHttpResponse;
23 import org.apache.http.client.methods.HttpDelete;
24 import org.apache.http.client.methods.HttpGet;
25 import org.apache.http.client.methods.HttpPost;
26 import org.apache.http.entity.StringEntity;
27 import org.apache.http.impl.client.CloseableHttpClient;
28 import org.apache.http.impl.client.HttpClients;
29 import org.apache.http.util.EntityUtils;
33 * HttpClient post request
35 public class HttpClientUtil {
37 private static final Logger log = LoggerFactory.getLogger(HttpClientUtil.class);
39 public static String doPost(String url, String json, String charset) {
42 CloseableHttpClient httpClient = HttpClientFactory.getSSLClientFactory()){
43 HttpPost httpPost = new HttpPost(url);
45 StringEntity s = new StringEntity(json);
46 s.setContentEncoding("UTF-8");
47 s.setContentType("application/json"); // set contentType
48 httpPost.setEntity(s);
50 try(CloseableHttpResponse response = httpClient.execute(httpPost)){
51 if (null != response) {
52 HttpEntity resEntity = response.getEntity();
53 if (null != resEntity) {
54 result = EntityUtils.toString(resEntity, charset);
57 } catch (Exception e) {
58 log.error("httpClient.execute(httpPost) is fail", e);
60 } catch (Exception e) {
61 log.error("doPost is fail ", e);
66 public static String doDelete(String url, String charset) {
69 CloseableHttpClient httpClient = HttpClientFactory.getSSLClientFactory()){
70 HttpDelete httpDelete = new HttpDelete(url);
72 try(CloseableHttpResponse response = httpClient.execute(httpDelete)){
73 if (null != response) {
74 HttpEntity resEntity = response.getEntity();
75 if (null != resEntity) {
76 result = EntityUtils.toString(resEntity, charset);
79 } catch (Exception e) {
80 log.error("doDelete Exception: ", e);
82 } catch (Exception e) {
83 log.error("doDelete is fail ", e);
88 public static String doGet(String url, String charset) {
91 CloseableHttpClient httpClient = HttpClients.createDefault()){
92 HttpGet httpGet = new HttpGet(url);
93 httpGet.setHeader("Content-Type", "application/json");
94 httpGet.setHeader("Accept", "application/json");
95 httpGet.setHeader("X-TransactionId", "111");
96 httpGet.setHeader("X-FromAppId", "ems-driver");
97 Base64 token = new Base64();
98 String authenticationEncoding = new String(token.encode(("AAI:AAI").getBytes()));
100 httpGet.setHeader("Authorization", "Basic " + authenticationEncoding);
101 log.info("1 doGet sucess url =" + url);
102 try (CloseableHttpResponse response = httpClient.execute(httpGet)){
103 if (null != response) {
104 HttpEntity resEntity = response.getEntity();
105 if (null != resEntity) {
106 result = EntityUtils.toString(resEntity, charset);
109 } catch (Exception e) {
110 log.error("doGet Exception: ", e);
112 } catch (Exception e) {
113 log.error("doGet is fail ", e);