2 * Copyright 2017 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.workflow.common;
18 import java.io.IOException;
20 import org.apache.http.HttpEntity;
21 import org.apache.http.HttpHost;
22 import org.apache.http.HttpRequest;
23 import org.apache.http.HttpResponse;
24 import org.apache.http.client.ClientProtocolException;
25 import org.apache.http.client.ResponseHandler;
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.client.methods.HttpPut;
31 import org.apache.http.entity.StringEntity;
32 import org.apache.http.impl.client.BasicResponseHandler;
33 import org.apache.http.impl.client.CloseableHttpClient;
34 import org.apache.http.impl.client.HttpClients;
35 import org.apache.http.util.EntityUtils;
36 import org.onap.workflow.externalservice.entity.activitientitiy.ActivitiStartProcessRequest;
37 import org.onap.workflow.tools.Constants;
38 import org.onap.workflow.tools.HttpDeleteWithBody;
39 import org.onap.workflow.tools.RequestParameters;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import com.google.gson.Gson;
45 public class RestClient {
46 private static final String HTTP = "http";
47 private static final Logger logger = LoggerFactory.getLogger(RestClient.class);
50 GET, POST, PUT, DELETE
61 * @throws ClientProtocolException
64 public static RestResponse executeHttp(HttpMethod method, String ip, Integer port, String url,
65 HttpEntity body) throws ClientProtocolException, IOException {
66 logger.info("deployfile method send");
67 CloseableHttpClient httpclient = HttpClients.createDefault();
68 HttpResponse httpResponse = null;
69 RestResponse result = new RestResponse();
72 ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();
75 port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();
77 HttpHost target = new HttpHost(ip, port, HTTP);
78 HttpRequest request = getRequest(method, url, body);
79 logger.info("deployfile method send ip"+ip);
80 request.addHeader(Constants.AUTHORIZATION, ToolUtil.getHeader());
82 httpResponse = httpclient.execute(target, request);
83 HttpEntity entity = httpResponse.getEntity();
84 logger.info("deployfile method send");
86 result.setStatusCode(httpResponse.getStatusLine().getStatusCode());
87 logger.info("reply status code deploy"+httpResponse.getStatusLine().getStatusCode());
88 result.setResult(EntityUtils.toString(entity));
90 } catch (IOException e) {
91 logger.warn("Close httpclient failed.", e);
93 if (httpclient != null) {
96 } catch (IOException e) {
97 logger.warn("Close httpclient failed.", e);
104 private static HttpRequest getRequest(HttpMethod method, String url, HttpEntity body) {
105 HttpRequest request = null;
108 request = new HttpGet(url);
111 request = new HttpPost(url);
112 ((HttpPost) request).setEntity(body);
115 request = new HttpPut(url);
116 ((HttpPut) request).setEntity(body);
119 request = new HttpDelete(url);
133 * @throws ClientProtocolException
134 * @throws IOException
136 public static String get(String ip, int port, String url)
137 throws ClientProtocolException, IOException {
138 return executeHttp(HttpMethod.GET, ip, port, url, null).getResult();
147 * @throws ClientProtocolException
148 * @throws IOException
150 public static RestResponse delete(String ip, int port, String url)
151 throws ClientProtocolException, IOException {
152 return executeHttp(HttpMethod.GET, ip, port, url, null);
162 * @throws ClientProtocolException
163 * @throws IOException
165 public static RestResponse post(String ip, int port, String url, HttpEntity requestBody)
166 throws ClientProtocolException, IOException {
167 return executeHttp(HttpMethod.POST, ip, port, url, requestBody);
180 * @throws ClientProtocolException
181 * @throws IOException
183 public static RestResponse executeHttpDeleteDeploy(HttpMethod method, String ip, Integer port,
184 String url) throws ClientProtocolException, IOException {
186 ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();
188 port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();
189 RestResponse result = new RestResponse();
190 CloseableHttpClient httpClient = HttpClients.createDefault();
192 httpClient = HttpClients.createDefault();
193 // "http://localhost:8080/activiti-rest/service/repository/deployments/167501"
194 String deleteUrl = Constants.HTTP_HEADER + ip + Constants.COLON + port + url;
195 HttpDeleteWithBody httpDeteTest = new HttpDeleteWithBody(deleteUrl);
196 Gson gson = new Gson();
197 RequestParameters reqPa = new RequestParameters();
198 reqPa.setCasCade(true);
199 String jsonStr = gson.toJson(reqPa, RequestParameters.class);
200 StringEntity requestEntity = new StringEntity(jsonStr, "UTF-8");
201 requestEntity.setContentEncoding("UTF-8");
202 httpDeteTest.setHeader("Content-type", "application/json");
203 httpDeteTest.setHeader(Constants.AUTHORIZATION, ToolUtil.getHeader());
204 httpDeteTest.setEntity(new StringEntity(jsonStr));
205 // returnValue = httpClient.execute(httpDeteTest, responseHandler); // 调接口获取返回值时,必须用此方法
206 CloseableHttpResponse httpResonse = httpClient.execute(httpDeteTest);
207 int statusCode = httpResonse.getStatusLine().getStatusCode();
208 result.setStatusCode(statusCode);
209 // result.setResult(EntityUtils.toString(httpResonse.getEntity()));
210 } catch (Exception e) {
217 } catch (IOException e) {
218 // TODO Auto-generated catch block
234 * @throws ClientProtocolException
235 * @throws IOException
237 public static RestResponse executeHttpStartIntance(HttpMethod method, String ip, Integer port,
238 String url, ActivitiStartProcessRequest object) throws ClientProtocolException, IOException {
239 String returnValue = "";
240 RestResponse result = new RestResponse();
241 CloseableHttpClient httpClient = HttpClients.createDefault();
242 ResponseHandler<String> responseHandler = new BasicResponseHandler();
244 httpClient = HttpClients.createDefault();
246 ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();
248 port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();
250 HttpPost httpPost = new HttpPost(Constants.HTTP_HEADER + ip + ":" + port + url);
251 Gson gson = new Gson();
252 String jsonStr = gson.toJson(object, ActivitiStartProcessRequest.class);
253 StringEntity requestEntity = new StringEntity(jsonStr, "utf-8");
254 requestEntity.setContentEncoding("UTF-8");
255 httpPost.setHeader("Content-type", "application/json");
256 httpPost.setHeader(Constants.AUTHORIZATION, ToolUtil.getHeader());
257 httpPost.setEntity(requestEntity);
258 returnValue = httpClient.execute(httpPost, responseHandler); // 调接口获取返回值时,必须用此方法
259 CloseableHttpResponse httpResonse = httpClient.execute(httpPost);
260 int statusCode = httpResonse.getStatusLine().getStatusCode();
261 returnValue = EntityUtils.toString(httpResonse.getEntity(),"UTF-8");
262 result.setStatusCode(statusCode);
263 result.setResult(returnValue);
264 } catch (Exception e) {
271 } catch (IOException e) {
272 // TODO Auto-generated catch block
287 * @throws ClientProtocolException
288 * @throws IOException
290 public static RestResponse post(String ip, Integer port, String url,
291 ActivitiStartProcessRequest requestBody) throws ClientProtocolException, IOException {
292 return executeHttpStartIntance(HttpMethod.POST, ip, port, url, requestBody);
302 * @throws ClientProtocolException
303 * @throws IOException
305 public static RestResponse post(String ip, Integer port, String url)
306 throws ClientProtocolException, IOException {
307 return executeHttpDeleteDeploy(HttpMethod.DELETE, ip, port, url);