Modify emsdriver Code
[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             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()));
138
139             httpGet.setHeader("Authorization", "Basic " + authenticationEncoding);
140             CloseableHttpResponse response = httpClient.execute(httpGet);
141             log.info("1 doGet sucess url ="+url);
142             try {
143                                 if(response != null){
144                                     HttpEntity resEntity = response.getEntity();
145                                     if(resEntity != null){
146                                         result = EntityUtils.toString(resEntity,charset);
147                                     }
148                                 }
149                         } catch (Exception e) {
150                                 log.error("",e);
151                         }finally{
152                                 if(response != null){
153                                         response.close();
154                                 }
155                         }
156         }catch(Exception e){
157                 log.error("doGet is fail ",e);
158         }finally{
159                 if(httpClient != null){
160                         try {
161                                         httpClient.close();
162                                 } catch (IOException e) {
163                                 }
164                         }
165         }
166         return result;
167     }
168 }