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.auth.AuthScope;
24 import org.apache.http.auth.UsernamePasswordCredentials;
25 import org.apache.http.client.CredentialsProvider;
26 import org.apache.http.client.methods.CloseableHttpResponse;
27 import org.apache.http.client.methods.HttpDelete;
28 import org.apache.http.client.methods.HttpGet;
29 import org.apache.http.client.methods.HttpPost;
30 import org.apache.http.entity.StringEntity;
31 import org.apache.http.impl.client.BasicCredentialsProvider;
32 import org.apache.http.impl.client.CloseableHttpClient;
33 import org.apache.http.impl.client.HttpClientBuilder;
34 import org.apache.http.impl.client.HttpClients;
35 import org.apache.http.util.EntityUtils;
37 import org.apache.commons.codec.binary.Base64;
40 * HttpClient post request
42 public class HttpClientUtil {
44 private static Log log = LogFactory.getLog(HttpClientUtil.class);
46 public static String doPost(String url,String json,String charset){
47 CloseableHttpClient httpClient = null;
48 HttpPost httpPost = null;
51 httpClient = HttpClientFactory.getSSLClientFactory();
52 httpPost = new HttpPost(url);
54 StringEntity s = new StringEntity(json);
55 s.setContentEncoding("UTF-8");
56 s.setContentType("application/json"); // set contentType
57 httpPost.setEntity(s);
59 CloseableHttpResponse response = httpClient.execute(httpPost);
62 HttpEntity resEntity = response.getEntity();
63 if(resEntity != null){
64 result = EntityUtils.toString(resEntity,charset);
67 } catch (Exception e) {
68 log.error("httpClient.execute(httpPost) is fail",e);
75 log.error("doPost is fail ",e);
77 if(httpClient != null){
80 } catch (IOException e) {
88 public static String doDelete(String url ,String charset){
89 CloseableHttpClient httpClient = null;
90 HttpDelete httpDelete = null;
93 httpClient = HttpClientFactory.getSSLClientFactory();
94 httpDelete = new HttpDelete(url);
96 CloseableHttpResponse response = httpClient.execute(httpDelete);
100 HttpEntity resEntity = response.getEntity();
101 if(resEntity != null){
102 result = EntityUtils.toString(resEntity,charset);
105 } catch (Exception e) {
108 if(response != null){
113 log.error("doDelete is fail ",e);
115 if(httpClient != null){
118 } catch (IOException e) {
125 public static String doGet(String url, String charset){
126 CloseableHttpClient httpClient = null;
127 HttpGet httpGet = null;
128 String result = null;
130 httpClient = HttpClients.createDefault();
131 httpGet = new HttpGet(url);
132 httpGet.setHeader("Content-Type", "application/json");
133 httpGet.setHeader("Accept", "application/json");
134 httpGet.setHeader("X-TransactionId", "111");
135 httpGet.setHeader("X-FromAppId", "ems-driver");
136 Base64 token = new Base64();
137 String authenticationEncoding = new String(token.encode(("AAI:AAI").getBytes()));
139 httpGet.setHeader("Authorization", "Basic " + authenticationEncoding);
140 CloseableHttpResponse response = httpClient.execute(httpGet);
141 log.info("1 doGet sucess url ="+url);
143 if(response != null){
144 HttpEntity resEntity = response.getEntity();
145 if(resEntity != null){
146 result = EntityUtils.toString(resEntity,charset);
149 } catch (Exception e) {
152 if(response != null){
157 log.error("doGet is fail ",e);
159 if(httpClient != null){
162 } catch (IOException e) {