Sonar:Access management wfenginemgr
[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   private 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 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       if(null != request)\r
86         request.addHeader(Constants.AUTHORIZATION, ToolUtil.getHeader());\r
87 \r
88       httpResponse = httpclient.execute(target, request);\r
89       HttpEntity entity = httpResponse.getEntity();\r
90       logger.info("deployfile method send");\r
91       if (entity != null && httpResponse.getStatusLine() != null) {\r
92         result.setStatusCode(httpResponse.getStatusLine().getStatusCode());\r
93         logger.info("reply status code deploy" + httpResponse.getStatusLine().getStatusCode());\r
94         result.setResult(EntityUtils.toString(entity));\r
95       }\r
96     } catch (IOException e) {\r
97       logger.warn("Close httpclient failed.", e);\r
98     } finally {\r
99       closeHttpClient(httpclient);\r
100     }\r
101     return result;\r
102   }\r
103 \r
104   public static boolean closeHttpClient(CloseableHttpClient httpclient) {\r
105     if (httpclient != null) {\r
106       try {\r
107         httpclient.close();\r
108         return true;\r
109       } catch (IOException e) {\r
110         logger.warn("Close httpclient failed.", e);\r
111         return false;\r
112       }\r
113     }\r
114     return true;\r
115   }\r
116 \r
117   public static HttpRequest getRequest(HttpMethod method, String url, HttpEntity body) {\r
118     HttpRequest request = null;\r
119     switch (method) {\r
120       case GET:\r
121         request = new HttpGet(url);\r
122         break;\r
123       case POST:\r
124         request = new HttpPost(url);\r
125         ((HttpPost) request).setEntity(body);\r
126         break;\r
127       case PUT:\r
128         request = new HttpPut(url);\r
129         ((HttpPut) request).setEntity(body);\r
130         break;\r
131       case DELETE:\r
132         request = new HttpDelete(url);\r
133         break;\r
134       default:\r
135         break;\r
136     }\r
137     return request;\r
138   }\r
139 \r
140   /**\r
141    * \r
142    * @param ip\r
143    * @param port\r
144    * @param url\r
145    * @param requestBody\r
146    * @return\r
147    * @throws ClientProtocolException\r
148    * @throws IOException\r
149    */\r
150   public static RestResponse post(String ip, int port, String url, HttpEntity requestBody)\r
151       throws IOException {\r
152     return executeHttp(HttpMethod.POST, ip, port, url, requestBody);\r
153   }\r
154 \r
155 \r
156 \r
157   /**\r
158    * \r
159    * @param method\r
160    * @param ip\r
161    * @param port\r
162    * @param url\r
163    * @param body\r
164    * @return\r
165    * @throws ClientProtocolException\r
166    * @throws IOException\r
167    */\r
168   public static RestResponse executeHttpDeleteDeploy(HttpMethod method, String ip, Integer port,\r
169       String url) throws IOException {\r
170     if (isTest) {\r
171       return new RestResponse();\r
172     }\r
173     if (ip == null) {\r
174       ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();\r
175     }\r
176     if (port == null) {\r
177       port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();\r
178     }\r
179     RestResponse result = new RestResponse();\r
180     try(CloseableHttpClient httpClient = HttpClients.createDefault()){\r
181       // "http://localhost:8080/activiti-rest/service/repository/deployments/167501"\r
182     /*  String deleteUrl =\r
183           Constants.HTTP_HEADER + ip + Constants.COLON + port + url + "?cascade=true";*/\r
184       String deleteUrl = Constants.HTTP_HEADER + ip + Constants.COLON + port + url + "?cascade=true";\r
185       HttpDeleteWithBody httpDeteTest = new HttpDeleteWithBody(deleteUrl);\r
186       Gson gson = new Gson();\r
187       RequestParameters reqPa = new RequestParameters();\r
188       reqPa.setCasCade(true);\r
189       String jsonStr = gson.toJson(reqPa, RequestParameters.class);\r
190       StringEntity requestEntity = new StringEntity(jsonStr, "UTF-8");\r
191       requestEntity.setContentEncoding("UTF-8");\r
192       httpDeteTest.setHeader("Content-type", "application/json");\r
193       httpDeteTest.setHeader(Constants.AUTHORIZATION, ToolUtil.getHeader());\r
194       httpDeteTest.setEntity(new StringEntity(jsonStr));\r
195       // returnValue = httpClient.execute(httpDeteTest, responseHandler); // 调接口获取返回值时,必须用此方法\r
196       CloseableHttpResponse httpResonse = httpClient.execute(httpDeteTest);\r
197       if (httpResonse != null && httpResonse.getStatusLine() != null) {\r
198         int statusCode = httpResonse.getStatusLine().getStatusCode();\r
199         result.setStatusCode(statusCode);\r
200       }\r
201       // result.setResult(EntityUtils.toString(httpResonse.getEntity()));\r
202     } catch (Exception e) {\r
203         logger.error("executeHttpDeleteDeploy ",e);\r
204     } \r
205     return result;\r
206   }\r
207 \r
208   /**\r
209    * \r
210    * @param method\r
211    * @param ip\r
212    * @param port\r
213    * @param url\r
214    * @param body\r
215    * @return\r
216    * @throws ClientProtocolException\r
217    * @throws IOException\r
218    */\r
219   public static RestResponse executeHttpStartIntance(HttpMethod method, String ip, Integer port,\r
220       String url, ActivitiStartProcessRequest object) throws IOException {\r
221     if (isTest) {\r
222       return new RestResponse();\r
223     }\r
224     String returnValue = "";\r
225     RestResponse result = new RestResponse();\r
226     ResponseHandler<String> responseHandler = new BasicResponseHandler();\r
227     try (\r
228       CloseableHttpClient httpClient = HttpClients.createDefault()){\r
229       if (ip == null) {\r
230         ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();\r
231       }\r
232       if (port == null) {\r
233         port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();\r
234       }\r
235       HttpPost httpPost = new HttpPost(Constants.HTTP_HEADER + ip + ":" + port + url);\r
236       Gson gson = new Gson();\r
237       String jsonStr = gson.toJson(object, ActivitiStartProcessRequest.class);\r
238       StringEntity requestEntity = new StringEntity(jsonStr, "utf-8");\r
239       requestEntity.setContentEncoding("UTF-8");\r
240       httpPost.setHeader("Content-type", "application/json");\r
241       httpPost.setHeader(Constants.AUTHORIZATION, ToolUtil.getHeader());\r
242       httpPost.setEntity(requestEntity);\r
243 //      returnValue = httpClient.execute(httpPost, responseHandler); // 调接口获取返回值时,必须用此方法\r
244       CloseableHttpResponse httpResonse = httpClient.execute(httpPost);\r
245       if (httpResonse != null && httpResonse.getStatusLine() != null) {\r
246         int statusCode = httpResonse.getStatusLine().getStatusCode();\r
247         returnValue = EntityUtils.toString(httpResonse.getEntity(), "UTF-8");\r
248         result.setStatusCode(statusCode);\r
249         result.setResult(returnValue);\r
250       }\r
251     } catch (Exception e) {\r
252         logger.error("executeHttpStartIntance :",e);\r
253     }\r
254     \r
255 \r
256     return result;\r
257   }\r
258 \r
259   /**\r
260    * \r
261    * @param ip\r
262    * @param port\r
263    * @param url\r
264    * @param requestBody\r
265    * @return\r
266    * @throws ClientProtocolException\r
267    * @throws IOException\r
268    */\r
269   public static RestResponse post(String ip, Integer port, String url,\r
270       ActivitiStartProcessRequest requestBody) throws IOException {\r
271     return executeHttpStartIntance(HttpMethod.POST, ip, port, url, requestBody);\r
272   }\r
273 \r
274   /**\r
275    * \r
276    * @param ip\r
277    * @param port\r
278    * @param url\r
279    * @param requestBody\r
280    * @return\r
281    * @throws ClientProtocolException\r
282    * @throws IOException\r
283    */\r
284   public static RestResponse post(String ip, Integer port, String url)\r
285       throws IOException {\r
286     return executeHttpDeleteDeploy(HttpMethod.DELETE, ip, port, url);\r
287   }\r
288 \r
289   public static boolean getIsTest(){\r
290         return isTest;\r
291   }\r
292 \r
293   public static void setIsTest(boolean arg){\r
294         isTest = arg;\r
295   }\r
296 \r
297 }\r