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 Base64 token = new Base64();
133 String authenticationEncoding = new String(token.encode(("AAI:AAI").getBytes()));
135 httpGet.setHeader("Authorization", "Basic " + authenticationEncoding);
136 CloseableHttpResponse response = httpClient.execute(httpGet);
137 log.info("doGet sucess url ="+url);
139 if(response != null){
140 HttpEntity resEntity = response.getEntity();
141 if(resEntity != null){
142 result = EntityUtils.toString(resEntity,charset);
145 } catch (Exception e) {
148 if(response != null){
153 log.error("doGet is fail ",e);
155 if(httpClient != null){
158 } catch (IOException e) {
165 public static CloseableHttpClient createCloseableHttpClientWithBasicAuth(){
166 // 创建HttpClientBuilder
167 HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
169 CredentialsProvider provider = new BasicCredentialsProvider();
170 // Create the authentication scope
171 AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
172 // Create credential pair,username and password
173 UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("AAI", "AAI");
174 // Inject the credentials
175 provider.setCredentials(scope, credentials);
176 // Set the default credentials provider
177 httpClientBuilder.setDefaultCredentialsProvider(provider);
179 CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
181 return closeableHttpClient;