2 * Copyright 2017 ZTE Corporation.
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
16 package org.onap.workflow.common;
\r
18 import java.io.IOException;
\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
43 import com.google.gson.Gson;
\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
50 public enum HttpMethod {
\r
51 GET, POST, PUT, DELETE
\r
62 * @throws ClientProtocolException
\r
63 * @throws IOException
\r
65 public static RestResponse executeHttp(HttpMethod method, String ip, Integer port, String url,
\r
66 HttpEntity body) throws ClientProtocolException, IOException {
\r
68 return new RestResponse();
\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
77 ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();
\r
80 port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();
\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
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
95 } catch (IOException e) {
\r
96 logger.warn("Close httpclient failed.", e);
\r
98 closeHttpClient(httpclient);
\r
103 public static boolean closeHttpClient(CloseableHttpClient httpclient) {
\r
104 if (httpclient != null) {
\r
106 httpclient.close();
\r
108 } catch (IOException e) {
\r
109 logger.warn("Close httpclient failed.", e);
\r
116 public static HttpRequest getRequest(HttpMethod method, String url, HttpEntity body) {
\r
117 HttpRequest request = null;
\r
120 request = new HttpGet(url);
\r
123 request = new HttpPost(url);
\r
124 ((HttpPost) request).setEntity(body);
\r
127 request = new HttpPut(url);
\r
128 ((HttpPut) request).setEntity(body);
\r
131 request = new HttpDelete(url);
\r
144 * @param requestBody
\r
146 * @throws ClientProtocolException
\r
147 * @throws IOException
\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
164 * @throws ClientProtocolException
\r
165 * @throws IOException
\r
167 public static RestResponse executeHttpDeleteDeploy(HttpMethod method, String ip, Integer port,
\r
168 String url) throws ClientProtocolException, IOException {
\r
170 return new RestResponse();
\r
173 ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();
\r
175 if (port == null) {
\r
176 port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();
\r
178 RestResponse result = new RestResponse();
\r
179 CloseableHttpClient httpClient = HttpClients.createDefault();
\r
181 httpClient = HttpClients.createDefault();
\r
182 // "http://localhost:8080/activiti-rest/service/repository/deployments/167501"
\r
183 String deleteUrl = Constants.HTTP_HEADER + ip + Constants.COLON + port + url;
\r
184 HttpDeleteWithBody httpDeteTest = new HttpDeleteWithBody(deleteUrl);
\r
185 Gson gson = new Gson();
\r
186 RequestParameters reqPa = new RequestParameters();
\r
187 reqPa.setCasCade(true);
\r
188 String jsonStr = gson.toJson(reqPa, RequestParameters.class);
\r
189 StringEntity requestEntity = new StringEntity(jsonStr, "UTF-8");
\r
190 requestEntity.setContentEncoding("UTF-8");
\r
191 httpDeteTest.setHeader("Content-type", "application/json");
\r
192 httpDeteTest.setHeader(Constants.AUTHORIZATION, ToolUtil.getHeader());
\r
193 httpDeteTest.setEntity(new StringEntity(jsonStr));
\r
194 // returnValue = httpClient.execute(httpDeteTest, responseHandler); // 调接口获取返回值时,必须用此方法
\r
195 CloseableHttpResponse httpResonse = httpClient.execute(httpDeteTest);
\r
196 if (httpResonse != null && httpResonse.getStatusLine() != null) {
\r
197 int statusCode = httpResonse.getStatusLine().getStatusCode();
\r
198 result.setStatusCode(statusCode);
\r
200 // result.setResult(EntityUtils.toString(httpResonse.getEntity()));
\r
201 } catch (Exception e) {
\r
202 e.printStackTrace();
\r
204 closeHttpClient(httpClient);
\r
218 * @throws ClientProtocolException
\r
219 * @throws IOException
\r
221 public static RestResponse executeHttpStartIntance(HttpMethod method, String ip, Integer port,
\r
222 String url, ActivitiStartProcessRequest object) throws ClientProtocolException, IOException {
\r
224 return new RestResponse();
\r
226 String returnValue = "";
\r
227 RestResponse result = new RestResponse();
\r
228 CloseableHttpClient httpClient = HttpClients.createDefault();
\r
229 ResponseHandler<String> responseHandler = new BasicResponseHandler();
\r
231 httpClient = HttpClients.createDefault();
\r
233 ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();
\r
235 if (port == null) {
\r
236 port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();
\r
238 HttpPost httpPost = new HttpPost(Constants.HTTP_HEADER + ip + ":" + port + url);
\r
239 Gson gson = new Gson();
\r
240 String jsonStr = gson.toJson(object, ActivitiStartProcessRequest.class);
\r
241 StringEntity requestEntity = new StringEntity(jsonStr, "utf-8");
\r
242 requestEntity.setContentEncoding("UTF-8");
\r
243 httpPost.setHeader("Content-type", "application/json");
\r
244 httpPost.setHeader(Constants.AUTHORIZATION, ToolUtil.getHeader());
\r
245 httpPost.setEntity(requestEntity);
\r
246 // returnValue = httpClient.execute(httpPost, responseHandler); // 调接口获取返回值时,必须用此方法
\r
247 CloseableHttpResponse httpResonse = httpClient.execute(httpPost);
\r
248 if (httpResonse != null && httpResonse.getStatusLine() != null) {
\r
249 int statusCode = httpResonse.getStatusLine().getStatusCode();
\r
250 returnValue = EntityUtils.toString(httpResonse.getEntity(), "UTF-8");
\r
251 result.setStatusCode(statusCode);
\r
252 result.setResult(returnValue);
\r
254 } catch (Exception e) {
\r
255 e.printStackTrace();
\r
259 closeHttpClient(httpClient);
\r
270 * @param requestBody
\r
272 * @throws ClientProtocolException
\r
273 * @throws IOException
\r
275 public static RestResponse post(String ip, Integer port, String url,
\r
276 ActivitiStartProcessRequest requestBody) throws ClientProtocolException, IOException {
\r
277 return executeHttpStartIntance(HttpMethod.POST, ip, port, url, requestBody);
\r
285 * @param requestBody
\r
287 * @throws ClientProtocolException
\r
288 * @throws IOException
\r
290 public static RestResponse post(String ip, Integer port, String url)
\r
291 throws ClientProtocolException, IOException {
\r
292 return executeHttpDeleteDeploy(HttpMethod.DELETE, ip, port, url);
\r