Update ves-agent dependency
[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 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;
30
31
32 /*
33  * HttpClient post request
34  */
35 public class HttpClientUtil {
36
37     private static final Logger log = LoggerFactory.getLogger(HttpClientUtil.class);
38
39     public static String doPost(String url, String json, String charset) {
40         String result = null;
41         try( 
42             CloseableHttpClient httpClient = HttpClientFactory.getSSLClientFactory()){
43             HttpPost httpPost = new HttpPost(url);
44             if (null != json) {
45                 StringEntity s = new StringEntity(json);
46                 s.setContentEncoding("UTF-8");
47                 s.setContentType("application/json"); // set contentType
48                 httpPost.setEntity(s);
49             }
50             try(CloseableHttpResponse response = httpClient.execute(httpPost)){
51                 if (null != response) {
52                     HttpEntity resEntity = response.getEntity();
53                     if (null != resEntity) {
54                         result = EntityUtils.toString(resEntity, charset);
55                     }
56                 }
57             } catch (Exception e) {
58                 log.error("httpClient.execute(httpPost) is fail", e);
59             }
60         } catch (Exception e) {
61             log.error("doPost is fail ", e);
62         } 
63         return result;
64     }
65
66     public static String doDelete(String url, String charset) {
67         String result = null;
68         try (
69             CloseableHttpClient httpClient = HttpClientFactory.getSSLClientFactory()){
70             HttpDelete httpDelete = new HttpDelete(url);
71
72             try(CloseableHttpResponse response = httpClient.execute(httpDelete)){
73                 if (null != response) {
74                     HttpEntity resEntity = response.getEntity();
75                     if (null != resEntity) {
76                         result = EntityUtils.toString(resEntity, charset);
77                     }
78                 }
79             } catch (Exception e) {
80                 log.error("doDelete Exception: ", e);
81             } 
82         } catch (Exception e) {
83             log.error("doDelete is fail ", e);
84         } 
85         return result;
86     }
87
88     public static String doGet(String url, String charset) {
89         String result = null;
90         try (
91             CloseableHttpClient httpClient = HttpClients.createDefault()){
92             HttpGet httpGet = new HttpGet(url);
93             httpGet.setHeader("Content-Type", "application/json");
94             httpGet.setHeader("Accept", "application/json");
95             httpGet.setHeader("X-TransactionId", "111");
96             httpGet.setHeader("X-FromAppId", "ems-driver");
97             Base64 token = new Base64();
98             String authenticationEncoding = new String(token.encode(("AAI:AAI").getBytes()));
99
100             httpGet.setHeader("Authorization", "Basic " + authenticationEncoding);
101             log.info("1 doGet sucess url =" + url);
102             try (CloseableHttpResponse response = httpClient.execute(httpGet)){
103                 if (null != response) {
104                     HttpEntity resEntity = response.getEntity();
105                     if (null != resEntity) {
106                         result = EntityUtils.toString(resEntity, charset);
107                     }
108                 }
109             } catch (Exception e) {
110                 log.error("doGet Exception: ", e);
111             } 
112         } catch (Exception e) {
113             log.error("doGet is fail ", e);
114         } 
115         return result;
116     }
117 }