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