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 java.io.IOException;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.apache.http.HttpEntity;
23 import org.apache.http.client.methods.CloseableHttpResponse;
24 import org.apache.http.client.methods.HttpDelete;
25 import org.apache.http.client.methods.HttpGet;
26 import org.apache.http.client.methods.HttpPost;
27 import org.apache.http.entity.StringEntity;
28 import org.apache.http.impl.client.CloseableHttpClient;
29 import org.apache.http.util.EntityUtils;
32 * HttpClient post request
34 public class HttpClientUtil {
36 private static Log log = LogFactory.getLog(HttpClientUtil.class);
38 public static String doPost(String url,String json,String charset){
39 CloseableHttpClient httpClient = null;
40 HttpPost httpPost = null;
43 httpClient = HttpClientFactory.getSSLClientFactory();
44 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 CloseableHttpResponse response = httpClient.execute(httpPost);
54 HttpEntity resEntity = response.getEntity();
55 if(resEntity != null){
56 result = EntityUtils.toString(resEntity,charset);
59 } catch (Exception e) {
60 log.error("httpClient.execute(httpPost) is fail",e);
67 log.error("doPost is fail ",e);
69 if(httpClient != null){
72 } catch (IOException e) {
80 public static String doDelete(String url ,String charset){
81 CloseableHttpClient httpClient = null;
82 HttpDelete httpDelete = null;
85 httpClient = HttpClientFactory.getSSLClientFactory();
86 httpDelete = new HttpDelete(url);
88 CloseableHttpResponse response = httpClient.execute(httpDelete);
92 HttpEntity resEntity = response.getEntity();
93 if(resEntity != null){
94 result = EntityUtils.toString(resEntity,charset);
97 } catch (Exception e) {
100 if(response != null){
105 log.error("doDelete is fail ",e);
107 if(httpClient != null){
110 } catch (IOException e) {
117 public static String doGet(String url, String charset){
118 CloseableHttpClient httpClient = null;
119 HttpGet httpGet = null;
120 String result = null;
122 httpClient = HttpClientFactory.getSSLClientFactory();
123 httpGet = new HttpGet(url);
125 CloseableHttpResponse response = httpClient.execute(httpGet);
128 if(response != null){
129 HttpEntity resEntity = response.getEntity();
130 if(resEntity != null){
131 result = EntityUtils.toString(resEntity,charset);
134 } catch (Exception e) {
137 if(response != null){
142 log.error("doGet is fail ",e);
144 if(httpClient != null){
147 } catch (IOException e) {