f9484f826da93b5dbf535e320fc5c6dea1e6fad6
[vfc/nfvo/driver/ems.git] / ems / boco / src / main / java / org / onap / vfc / nfvo / emsdriver / northbound / client / HttpClientUtil.java
1 /**
2  * Copyright 2017 BOCO Corporation.  CMCC Technologies Co., Ltd
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.vfc.nfvo.emsdriver.northbound.client;
17
18 import java.io.IOException;
19
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;
36
37 import org.apache.commons.codec.binary.Base64;
38
39 /*
40  * HttpClient post request
41  */
42 public class HttpClientUtil {
43         
44         private static Log log = LogFactory.getLog(HttpClientUtil.class);
45         
46     public static String doPost(String url,String json,String charset){
47         CloseableHttpClient httpClient = null;
48         HttpPost httpPost = null;
49         String result = null;
50         try{
51             httpClient = HttpClientFactory.getSSLClientFactory();
52             httpPost = new HttpPost(url);
53             if (null != json) {
54                                 StringEntity s = new StringEntity(json);
55                                 s.setContentEncoding("UTF-8");
56                                 s.setContentType("application/json"); // set contentType
57                                 httpPost.setEntity(s);
58                         }
59             CloseableHttpResponse response = httpClient.execute(httpPost);
60             try {
61                                 if(response != null){
62                                     HttpEntity resEntity = response.getEntity();
63                                     if(resEntity != null){
64                                         result = EntityUtils.toString(resEntity,charset);
65                                     }
66                                 }
67                         } catch (Exception e) {
68                                 log.error("httpClient.execute(httpPost) is fail",e);
69                         }finally{
70                                 if(response != null){
71                                         response.close();
72                                 }
73                         }
74         }catch(Exception e){
75                 log.error("doPost is fail ",e);
76         }finally{
77                 if(httpClient != null){
78                         try {
79                                         httpClient.close();
80                                 } catch (IOException e) {
81                                 }
82                 }
83                 
84                 }
85         return result;
86     }
87     
88     public static String doDelete(String url ,String charset){
89         CloseableHttpClient httpClient = null;
90         HttpDelete httpDelete = null;
91         String result = null;
92         try{
93             httpClient = HttpClientFactory.getSSLClientFactory();
94             httpDelete = new HttpDelete(url);
95             
96             CloseableHttpResponse response = httpClient.execute(httpDelete);
97             
98             try {
99                                 if(response != null){
100                                     HttpEntity resEntity = response.getEntity();
101                                     if(resEntity != null){
102                                         result = EntityUtils.toString(resEntity,charset);
103                                     }
104                                 }
105                         } catch (Exception e) {
106                                 log.error("",e);
107                         }finally{
108                                 if(response != null){
109                                         response.close();
110                                 }
111                         }
112         }catch(Exception e){
113                 log.error("doDelete is fail ",e);
114         }finally{
115                 if(httpClient != null){
116                         try {
117                                         httpClient.close();
118                                 } catch (IOException e) {
119                                 }
120                         }
121         }
122         return result;
123     }
124     
125     public static String doGet(String url, String charset){
126         CloseableHttpClient httpClient = null;
127         HttpGet httpGet = null;
128         String result = null;
129         try{
130             httpClient = HttpClients.createDefault();
131             httpGet = new HttpGet(url);
132             Base64 token = new Base64();
133             String authenticationEncoding = new String(token.encode(("AAI:AAI").getBytes()));
134
135             httpGet.setHeader("Authorization", "Basic " + authenticationEncoding);
136             CloseableHttpResponse response = httpClient.execute(httpGet);
137             log.info("doGet sucess url ="+url);
138             try {
139                                 if(response != null){
140                                     HttpEntity resEntity = response.getEntity();
141                                     if(resEntity != null){
142                                         result = EntityUtils.toString(resEntity,charset);
143                                     }
144                                 }
145                         } catch (Exception e) {
146                                 log.error("",e);
147                         }finally{
148                                 if(response != null){
149                                         response.close();
150                                 }
151                         }
152         }catch(Exception e){
153                 log.error("doGet is fail ",e);
154         }finally{
155                 if(httpClient != null){
156                         try {
157                                         httpClient.close();
158                                 } catch (IOException e) {
159                                 }
160                         }
161         }
162         return result;
163     }
164     
165     public static CloseableHttpClient createCloseableHttpClientWithBasicAuth(){  
166           // 创建HttpClientBuilder  
167         HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();  
168         // 设置BasicAuth  
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);  
178         // HttpClient  
179         CloseableHttpClient closeableHttpClient = httpClientBuilder.build(); 
180         
181         return closeableHttpClient;
182     }  
183     
184 }