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.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
42 import com.google.gson.Gson;
\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
48 //PublicExposureUsedInTest
\r
49 public static boolean isTest = false;
\r
51 public enum HttpMethod {
\r
52 GET, POST, PUT, DELETE
\r
63 * @throws ClientProtocolException
\r
64 * @throws IOException
\r
66 public static RestResponse executeHttp(HttpMethod method, String ip, Integer port, String url,
\r
67 HttpEntity body) throws IOException {
\r
69 return new RestResponse();
\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
78 ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();
\r
81 port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();
\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
87 request.addHeader(Constants.AUTHORIZATION, ToolUtil.getHeader());
\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
97 } catch (IOException e) {
\r
98 logger.warn("Close httpclient failed.", e);
\r
100 closeHttpClient(httpclient);
\r
105 public static boolean closeHttpClient(CloseableHttpClient httpclient) {
\r
106 if (httpclient != null) {
\r
108 httpclient.close();
\r
110 } catch (IOException e) {
\r
111 logger.warn("Close httpclient failed.", e);
\r
118 public static HttpRequest getRequest(HttpMethod method, String url, HttpEntity body) {
\r
119 HttpRequest request = null;
\r
122 request = new HttpGet(url);
\r
125 request = new HttpPost(url);
\r
126 ((HttpPost) request).setEntity(body);
\r
129 request = new HttpPut(url);
\r
130 ((HttpPut) request).setEntity(body);
\r
133 request = new HttpDelete(url);
\r
146 * @param requestBody
\r
148 * @throws ClientProtocolException
\r
149 * @throws IOException
\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
166 * @throws ClientProtocolException
\r
167 * @throws IOException
\r
169 public static RestResponse executeHttpDeleteDeploy(HttpMethod method, String ip, Integer port,
\r
170 String url) throws IOException {
\r
172 return new RestResponse();
\r
175 ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();
\r
177 if (port == null) {
\r
178 port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();
\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
202 // result.setResult(EntityUtils.toString(httpResonse.getEntity()));
\r
203 } catch (Exception e) {
\r
204 logger.error("executeHttpDeleteDeploy ",e);
\r
217 * @throws ClientProtocolException
\r
218 * @throws IOException
\r
220 public static RestResponse executeHttpStartIntance(HttpMethod method, String ip, Integer port,
\r
221 String url, ActivitiStartProcessRequest object) throws IOException {
\r
223 return new RestResponse();
\r
225 String returnValue = "";
\r
226 RestResponse result = new RestResponse();
\r
227 ResponseHandler<String> responseHandler = new BasicResponseHandler();
\r
229 CloseableHttpClient httpClient = HttpClients.createDefault()){
\r
231 ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();
\r
233 if (port == null) {
\r
234 port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();
\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
252 } catch (Exception e) {
\r
253 logger.error("executeHttpStartIntance :",e);
\r
265 * @param requestBody
\r
267 * @throws ClientProtocolException
\r
268 * @throws IOException
\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
280 * @param requestBody
\r
282 * @throws ClientProtocolException
\r
283 * @throws IOException
\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