Critical Bug fixes wfenginemgrservice
[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.ResponseHandler;\r
25 import org.apache.http.client.methods.CloseableHttpResponse;\r
26 import org.apache.http.client.methods.HttpDelete;\r
27 import org.apache.http.client.methods.HttpGet;\r
28 import org.apache.http.client.methods.HttpPost;\r
29 import org.apache.http.client.methods.HttpPut;\r
30 import org.apache.http.entity.StringEntity;\r
31 import org.apache.http.impl.client.BasicResponseHandler;\r
32 import org.apache.http.impl.client.CloseableHttpClient;\r
33 import org.apache.http.impl.client.HttpClients;\r
34 import org.apache.http.util.EntityUtils;\r
35 import org.onap.workflow.externalservice.entity.activitientitiy.ActivitiStartProcessRequest;\r
36 import org.onap.workflow.tools.Constants;\r
37 import org.onap.workflow.tools.HttpDeleteWithBody;\r
38 import org.onap.workflow.tools.RequestParameters;\r
39 import org.slf4j.Logger;\r
40 import org.slf4j.LoggerFactory;\r
41 \r
42 import com.google.gson.Gson;\r
43 \r
44 public class RestClient {\r
45   private static final String HTTP = "http";\r
46   private static final Logger logger = LoggerFactory.getLogger(RestClient.class);\r
47 \r
48   //PublicExposureUsedInTest\r
49   public static boolean isTest = false;\r
50 \r
51   public enum HttpMethod {\r
52     GET, POST, PUT, DELETE\r
53   }\r
54 \r
55   /**\r
56    * \r
57    * @param method\r
58    * @param ip\r
59    * @param port\r
60    * @param url\r
61    * @param body\r
62    * @return\r
63    * @throws ClientProtocolException\r
64    * @throws IOException\r
65    */\r
66   public static RestResponse executeHttp(HttpMethod method, String ip, Integer port, String url,\r
67       HttpEntity body) throws IOException {\r
68     if (isTest) {\r
69       return new RestResponse();\r
70     }\r
71 \r
72     logger.info("deployfile method send");\r
73     CloseableHttpClient httpclient = HttpClients.createDefault();\r
74     HttpResponse httpResponse = null;\r
75     RestResponse result = new RestResponse();\r
76     try {\r
77       if (ip == null) {\r
78         ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();\r
79       }\r
80       if (port == null) {\r
81         port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();\r
82       }\r
83       HttpHost target = new HttpHost(ip, port, HTTP);\r
84       HttpRequest request = getRequest(method, url, body);\r
85       logger.info("deployfile method send ip" + ip);\r
86       if(null != request)\r
87         request.addHeader(Constants.AUTHORIZATION, ToolUtil.getHeader());\r
88 \r
89       httpResponse = httpclient.execute(target, request);\r
90       HttpEntity entity = httpResponse.getEntity();\r
91       logger.info("deployfile method send");\r
92       if (entity != null && httpResponse.getStatusLine() != null) {\r
93         result.setStatusCode(httpResponse.getStatusLine().getStatusCode());\r
94         logger.info("reply status code deploy" + httpResponse.getStatusLine().getStatusCode());\r
95         result.setResult(EntityUtils.toString(entity));\r
96       }\r
97     } catch (IOException e) {\r
98       logger.warn("Close httpclient failed.", e);\r
99     } finally {\r
100       closeHttpClient(httpclient);\r
101     }\r
102     return result;\r
103   }\r
104 \r
105   public static boolean closeHttpClient(CloseableHttpClient httpclient) {\r
106     if (httpclient != null) {\r
107       try {\r
108         httpclient.close();\r
109         return true;\r
110       } catch (IOException e) {\r
111         logger.warn("Close httpclient failed.", e);\r
112         return false;\r
113       }\r
114     }\r
115     return true;\r
116   }\r
117 \r
118   public static HttpRequest getRequest(HttpMethod method, String url, HttpEntity body) {\r
119     HttpRequest request = null;\r
120     switch (method) {\r
121       case GET:\r
122         request = new HttpGet(url);\r
123         break;\r
124       case POST:\r
125         request = new HttpPost(url);\r
126         ((HttpPost) request).setEntity(body);\r
127         break;\r
128       case PUT:\r
129         request = new HttpPut(url);\r
130         ((HttpPut) request).setEntity(body);\r
131         break;\r
132       case DELETE:\r
133         request = new HttpDelete(url);\r
134         break;\r
135       default:\r
136         break;\r
137     }\r
138     return request;\r
139   }\r
140 \r
141   /**\r
142    * \r
143    * @param ip\r
144    * @param port\r
145    * @param url\r
146    * @param requestBody\r
147    * @return\r
148    * @throws ClientProtocolException\r
149    * @throws IOException\r
150    */\r
151   public static RestResponse post(String ip, int port, String url, HttpEntity requestBody)\r
152       throws IOException {\r
153     return executeHttp(HttpMethod.POST, ip, port, url, requestBody);\r
154   }\r
155 \r
156 \r
157 \r
158   /**\r
159    * \r
160    * @param method\r
161    * @param ip\r
162    * @param port\r
163    * @param url\r
164    * @param body\r
165    * @return\r
166    * @throws ClientProtocolException\r
167    * @throws IOException\r
168    */\r
169   public static RestResponse executeHttpDeleteDeploy(HttpMethod method, String ip, Integer port,\r
170       String url) throws IOException {\r
171     if (isTest) {\r
172       return new RestResponse();\r
173     }\r
174     if (ip == null) {\r
175       ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();\r
176     }\r
177     if (port == null) {\r
178       port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();\r
179     }\r
180     RestResponse result = new RestResponse();\r
181     try(CloseableHttpClient 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         logger.error("executeHttpDeleteDeploy ",e);\r
205     } \r
206     return result;\r
207   }\r
208 \r
209   /**\r
210    * \r
211    * @param method\r
212    * @param ip\r
213    * @param port\r
214    * @param url\r
215    * @param body\r
216    * @return\r
217    * @throws ClientProtocolException\r
218    * @throws IOException\r
219    */\r
220   public static RestResponse executeHttpStartIntance(HttpMethod method, String ip, Integer port,\r
221       String url, ActivitiStartProcessRequest object) throws IOException {\r
222     if (isTest) {\r
223       return new RestResponse();\r
224     }\r
225     String returnValue = "";\r
226     RestResponse result = new RestResponse();\r
227     ResponseHandler<String> responseHandler = new BasicResponseHandler();\r
228     try (\r
229       CloseableHttpClient httpClient = HttpClients.createDefault()){\r
230       if (ip == null) {\r
231         ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();\r
232       }\r
233       if (port == null) {\r
234         port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();\r
235       }\r
236       HttpPost httpPost = new HttpPost(Constants.HTTP_HEADER + ip + ":" + port + url);\r
237       Gson gson = new Gson();\r
238       String jsonStr = gson.toJson(object, ActivitiStartProcessRequest.class);\r
239       StringEntity requestEntity = new StringEntity(jsonStr, "utf-8");\r
240       requestEntity.setContentEncoding("UTF-8");\r
241       httpPost.setHeader("Content-type", "application/json");\r
242       httpPost.setHeader(Constants.AUTHORIZATION, ToolUtil.getHeader());\r
243       httpPost.setEntity(requestEntity);\r
244 //      returnValue = httpClient.execute(httpPost, responseHandler); // 调接口获取返回值时,必须用此方法\r
245       CloseableHttpResponse httpResonse = httpClient.execute(httpPost);\r
246       if (httpResonse != null && httpResonse.getStatusLine() != null) {\r
247         int statusCode = httpResonse.getStatusLine().getStatusCode();\r
248         returnValue = EntityUtils.toString(httpResonse.getEntity(), "UTF-8");\r
249         result.setStatusCode(statusCode);\r
250         result.setResult(returnValue);\r
251       }\r
252     } catch (Exception e) {\r
253         logger.error("executeHttpStartIntance :",e);\r
254     }\r
255     \r
256 \r
257     return result;\r
258   }\r
259 \r
260   /**\r
261    * \r
262    * @param ip\r
263    * @param port\r
264    * @param url\r
265    * @param requestBody\r
266    * @return\r
267    * @throws ClientProtocolException\r
268    * @throws IOException\r
269    */\r
270   public static RestResponse post(String ip, Integer port, String url,\r
271       ActivitiStartProcessRequest requestBody) throws IOException {\r
272     return executeHttpStartIntance(HttpMethod.POST, ip, port, url, requestBody);\r
273   }\r
274 \r
275   /**\r
276    * \r
277    * @param ip\r
278    * @param port\r
279    * @param url\r
280    * @param requestBody\r
281    * @return\r
282    * @throws ClientProtocolException\r
283    * @throws IOException\r
284    */\r
285   public static RestResponse post(String ip, Integer port, String url)\r
286       throws IOException {\r
287     return executeHttpDeleteDeploy(HttpMethod.DELETE, ip, port, url);\r
288   }\r
289 \r
290 }\r