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);
38 public static final String APPLICATION_JSON = "application/json";
40 public static String doPost(String url, String json, String charset) {
43 CloseableHttpClient httpClient = HttpClientFactory.getSSLClientFactory()){
44 HttpPost httpPost = new HttpPost(url);
46 StringEntity s = new StringEntity(json);
47 s.setContentEncoding("UTF-8");
48 s.setContentType(APPLICATION_JSON); // set contentType
49 httpPost.setEntity(s);
51 try(CloseableHttpResponse response = httpClient.execute(httpPost)){
52 if (null != response) {
53 HttpEntity resEntity = response.getEntity();
54 if (null != resEntity) {
55 result = EntityUtils.toString(resEntity, charset);
58 } catch (Exception e) {
59 log.error("httpClient.execute(httpPost) is fail", e);
61 } catch (Exception e) {
62 log.error("doPost is fail ", e);
67 public static String doDelete(String url, String charset) {
70 CloseableHttpClient httpClient = HttpClientFactory.getSSLClientFactory()){
71 HttpDelete httpDelete = new HttpDelete(url);
73 try(CloseableHttpResponse response = httpClient.execute(httpDelete)){
74 if (null != response) {
75 HttpEntity resEntity = response.getEntity();
76 if (null != resEntity) {
77 result = EntityUtils.toString(resEntity, charset);
80 } catch (Exception e) {
81 log.error("doDelete Exception: ", e);
83 } catch (Exception e) {
84 log.error("doDelete is fail ", e);
89 public static String doGet(String url, String charset) {
92 CloseableHttpClient httpClient = HttpClients.createDefault()){
93 HttpGet httpGet = new HttpGet(url);
94 httpGet.setHeader("Content-Type", APPLICATION_JSON);
95 httpGet.setHeader("Accept", APPLICATION_JSON);
96 httpGet.setHeader("X-TransactionId", "111");
97 httpGet.setHeader("X-FromAppId", "ems-driver");
98 Base64 token = new Base64();
99 String authenticationEncoding = new String(token.encode(("AAI:AAI").getBytes()));
101 httpGet.setHeader("Authorization", "Basic " + authenticationEncoding);
102 log.info("1 doGet sucess url =" + url);
103 try (CloseableHttpResponse response = httpClient.execute(httpGet)){
104 if (null != response) {
105 HttpEntity resEntity = response.getEntity();
106 if (null != resEntity) {
107 result = EntityUtils.toString(resEntity, charset);
110 } catch (Exception e) {
111 log.error("doGet Exception: ", e);
113 } catch (Exception e) {
114 log.error("doGet is fail ", e);