5dcef70aae5e7924c9fac4bddffffb45bc7e8809
[vfc/nfvo/wfengine.git] / wfenginemgrservice / src / main / java / org / onap / workflow / common / RestClient.java
1 /**\r
2  * Copyright 2017 ZTE Corporation.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 package org.onap.workflow.common;\r
17 \r
18 import java.io.IOException;\r
19 \r
20 import org.apache.http.HttpEntity;\r
21 import org.apache.http.HttpHost;\r
22 import org.apache.http.HttpRequest;\r
23 import org.apache.http.HttpResponse;\r
24 import org.apache.http.client.ClientProtocolException;\r
25 import org.apache.http.client.ResponseHandler;\r
26 import org.apache.http.client.methods.CloseableHttpResponse;\r
27 import org.apache.http.client.methods.HttpDelete;\r
28 import org.apache.http.client.methods.HttpGet;\r
29 import org.apache.http.client.methods.HttpPost;\r
30 import org.apache.http.client.methods.HttpPut;\r
31 import org.apache.http.entity.StringEntity;\r
32 import org.apache.http.impl.client.BasicResponseHandler;\r
33 import org.apache.http.impl.client.CloseableHttpClient;\r
34 import org.apache.http.impl.client.HttpClients;\r
35 import org.apache.http.util.EntityUtils;\r
36 import org.onap.workflow.externalservice.entity.activitientitiy.ActivitiStartProcessRequest;\r
37 import org.onap.workflow.tools.Constants;\r
38 import org.onap.workflow.tools.HttpDeleteWithBody;\r
39 import org.onap.workflow.tools.RequestParameters;\r
40 import org.slf4j.Logger;\r
41 import org.slf4j.LoggerFactory;\r
42 \r
43 import com.google.gson.Gson;\r
44 \r
45 public class RestClient {\r
46   private static final String HTTP = "http";\r
47   private static final Logger logger = LoggerFactory.getLogger(RestClient.class);\r
48   public static boolean isTest = false;\r
49 \r
50   public enum HttpMethod {\r
51     GET, POST, PUT, DELETE\r
52   }\r
53 \r
54   /**\r
55    * \r
56    * @param method\r
57    * @param ip\r
58    * @param port\r
59    * @param url\r
60    * @param body\r
61    * @return\r
62    * @throws ClientProtocolException\r
63    * @throws IOException\r
64    */\r
65   public static RestResponse executeHttp(HttpMethod method, String ip, Integer port, String url,\r
66       HttpEntity body) throws ClientProtocolException, IOException {\r
67     if (isTest) {\r
68       return new RestResponse();\r
69     }\r
70 \r
71     logger.info("deployfile method send");\r
72     CloseableHttpClient httpclient = HttpClients.createDefault();\r
73     HttpResponse httpResponse = null;\r
74     RestResponse result = new RestResponse();\r
75     try {\r
76       if (ip == null) {\r
77         ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();\r
78       }\r
79       if (port == null) {\r
80         port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();\r
81       }\r
82       HttpHost target = new HttpHost(ip, port, HTTP);\r
83       HttpRequest request = getRequest(method, url, body);\r
84       logger.info("deployfile method send ip" + ip);\r
85       request.addHeader(Constants.AUTHORIZATION, ToolUtil.getHeader());\r
86 \r
87       httpResponse = httpclient.execute(target, request);\r
88       HttpEntity entity = httpResponse.getEntity();\r
89       logger.info("deployfile method send");\r
90       if (entity != null && httpResponse.getStatusLine() != null) {\r
91         result.setStatusCode(httpResponse.getStatusLine().getStatusCode());\r
92         logger.info("reply status code deploy" + httpResponse.getStatusLine().getStatusCode());\r
93         result.setResult(EntityUtils.toString(entity));\r
94       }\r
95     } catch (IOException e) {\r
96       logger.warn("Close httpclient failed.", e);\r
97     } finally {\r
98       closeHttpClient(httpclient);\r
99     }\r
100     return result;\r
101   }\r
102 \r
103   public static boolean closeHttpClient(CloseableHttpClient httpclient) {\r
104     if (httpclient != null) {\r
105       try {\r
106         httpclient.close();\r
107         return true;\r
108       } catch (IOException e) {\r
109         logger.warn("Close httpclient failed.", e);\r
110         return false;\r
111       }\r
112     }\r
113     return true;\r
114   }\r
115 \r
116   public static HttpRequest getRequest(HttpMethod method, String url, HttpEntity body) {\r
117     HttpRequest request = null;\r
118     switch (method) {\r
119       case GET:\r
120         request = new HttpGet(url);\r
121         break;\r
122       case POST:\r
123         request = new HttpPost(url);\r
124         ((HttpPost) request).setEntity(body);\r
125         break;\r
126       case PUT:\r
127         request = new HttpPut(url);\r
128         ((HttpPut) request).setEntity(body);\r
129         break;\r
130       case DELETE:\r
131         request = new HttpDelete(url);\r
132         break;\r
133       default:\r
134         break;\r
135     }\r
136     return request;\r
137   }\r
138 \r
139   /**\r
140    * \r
141    * @param ip\r
142    * @param port\r
143    * @param url\r
144    * @param requestBody\r
145    * @return\r
146    * @throws ClientProtocolException\r
147    * @throws IOException\r
148    */\r
149   public static RestResponse post(String ip, int port, String url, HttpEntity requestBody)\r
150       throws ClientProtocolException, IOException {\r
151     return executeHttp(HttpMethod.POST, ip, port, url, requestBody);\r
152   }\r
153 \r
154 \r
155 \r
156   /**\r
157    * \r
158    * @param method\r
159    * @param ip\r
160    * @param port\r
161    * @param url\r
162    * @param body\r
163    * @return\r
164    * @throws ClientProtocolException\r
165    * @throws IOException\r
166    */\r
167   public static RestResponse executeHttpDeleteDeploy(HttpMethod method, String ip, Integer port,\r
168       String url) throws ClientProtocolException, IOException {\r
169     if (isTest) {\r
170       return new RestResponse();\r
171     }\r
172     if (ip == null) {\r
173       ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();\r
174     }\r
175     if (port == null) {\r
176       port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();\r
177     }\r
178     RestResponse result = new RestResponse();\r
179     CloseableHttpClient httpClient = HttpClients.createDefault();\r
180     try {\r
181       httpClient = HttpClients.createDefault();\r
182       // "http://localhost:8080/activiti-rest/service/repository/deployments/167501"\r
183     /*  String deleteUrl =\r
184           Constants.HTTP_HEADER + ip + Constants.COLON + port + url + "?cascade=true";*/\r
185       String deleteUrl = Constants.HTTP_HEADER + ip + Constants.COLON + port + url + "?cascade=true";\r
186       HttpDeleteWithBody httpDeteTest = new HttpDeleteWithBody(deleteUrl);\r
187       Gson gson = new Gson();\r
188       RequestParameters reqPa = new RequestParameters();\r
189       reqPa.setCasCade(true);\r
190       String jsonStr = gson.toJson(reqPa, RequestParameters.class);\r
191       StringEntity requestEntity = new StringEntity(jsonStr, "UTF-8");\r
192       requestEntity.setContentEncoding("UTF-8");\r
193       httpDeteTest.setHeader("Content-type", "application/json");\r
194       httpDeteTest.setHeader(Constants.AUTHORIZATION, ToolUtil.getHeader());\r
195       httpDeteTest.setEntity(new StringEntity(jsonStr));\r
196       // returnValue = httpClient.execute(httpDeteTest, responseHandler); // 调接口获取返回值时,必须用此方法\r
197       CloseableHttpResponse httpResonse = httpClient.execute(httpDeteTest);\r
198       if (httpResonse != null && httpResonse.getStatusLine() != null) {\r
199         int statusCode = httpResonse.getStatusLine().getStatusCode();\r
200         result.setStatusCode(statusCode);\r
201       }\r
202       // result.setResult(EntityUtils.toString(httpResonse.getEntity()));\r
203     } catch (Exception e) {\r
204       e.printStackTrace();\r
205     } finally {\r
206       closeHttpClient(httpClient);\r
207     }\r
208 \r
209     return result;\r
210   }\r
211 \r
212   /**\r
213    * \r
214    * @param method\r
215    * @param ip\r
216    * @param port\r
217    * @param url\r
218    * @param body\r
219    * @return\r
220    * @throws ClientProtocolException\r
221    * @throws IOException\r
222    */\r
223   public static RestResponse executeHttpStartIntance(HttpMethod method, String ip, Integer port,\r
224       String url, ActivitiStartProcessRequest object) throws ClientProtocolException, IOException {\r
225     if (isTest) {\r
226       return new RestResponse();\r
227     }\r
228     String returnValue = "";\r
229     RestResponse result = new RestResponse();\r
230     CloseableHttpClient httpClient = HttpClients.createDefault();\r
231     ResponseHandler<String> responseHandler = new BasicResponseHandler();\r
232     try {\r
233       httpClient = HttpClients.createDefault();\r
234       if (ip == null) {\r
235         ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();\r
236       }\r
237       if (port == null) {\r
238         port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();\r
239       }\r
240       HttpPost httpPost = new HttpPost(Constants.HTTP_HEADER + ip + ":" + port + url);\r
241       Gson gson = new Gson();\r
242       String jsonStr = gson.toJson(object, ActivitiStartProcessRequest.class);\r
243       StringEntity requestEntity = new StringEntity(jsonStr, "utf-8");\r
244       requestEntity.setContentEncoding("UTF-8");\r
245       httpPost.setHeader("Content-type", "application/json");\r
246       httpPost.setHeader(Constants.AUTHORIZATION, ToolUtil.getHeader());\r
247       httpPost.setEntity(requestEntity);\r
248 //      returnValue = httpClient.execute(httpPost, responseHandler); // 调接口获取返回值时,必须用此方法\r
249       CloseableHttpResponse httpResonse = httpClient.execute(httpPost);\r
250       if (httpResonse != null && httpResonse.getStatusLine() != null) {\r
251         int statusCode = httpResonse.getStatusLine().getStatusCode();\r
252         returnValue = EntityUtils.toString(httpResonse.getEntity(), "UTF-8");\r
253         result.setStatusCode(statusCode);\r
254         result.setResult(returnValue);\r
255       }\r
256     } catch (Exception e) {\r
257       e.printStackTrace();\r
258     }\r
259 \r
260     finally {\r
261       closeHttpClient(httpClient);\r
262     }\r
263 \r
264     return result;\r
265   }\r
266 \r
267   /**\r
268    * \r
269    * @param ip\r
270    * @param port\r
271    * @param url\r
272    * @param requestBody\r
273    * @return\r
274    * @throws ClientProtocolException\r
275    * @throws IOException\r
276    */\r
277   public static RestResponse post(String ip, Integer port, String url,\r
278       ActivitiStartProcessRequest requestBody) throws ClientProtocolException, IOException {\r
279     return executeHttpStartIntance(HttpMethod.POST, ip, port, url, requestBody);\r
280   }\r
281 \r
282   /**\r
283    * \r
284    * @param ip\r
285    * @param port\r
286    * @param url\r
287    * @param requestBody\r
288    * @return\r
289    * @throws ClientProtocolException\r
290    * @throws IOException\r
291    */\r
292   public static RestResponse post(String ip, Integer port, String url)\r
293       throws ClientProtocolException, IOException {\r
294     return executeHttpDeleteDeploy(HttpMethod.DELETE, ip, port, url);\r
295   }\r
296 \r
297 }\r