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 private 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 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
86 request.addHeader(Constants.AUTHORIZATION, ToolUtil.getHeader());
\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
96 } catch (IOException e) {
\r
97 logger.warn("Close httpclient failed.", e);
\r
99 closeHttpClient(httpclient);
\r
104 public static boolean closeHttpClient(CloseableHttpClient httpclient) {
\r
105 if (httpclient != null) {
\r
107 httpclient.close();
\r
109 } catch (IOException e) {
\r
110 logger.warn("Close httpclient failed.", e);
\r
117 public static HttpRequest getRequest(HttpMethod method, String url, HttpEntity body) {
\r
118 HttpRequest request = null;
\r
121 request = new HttpGet(url);
\r
124 request = new HttpPost(url);
\r
125 ((HttpPost) request).setEntity(body);
\r
128 request = new HttpPut(url);
\r
129 ((HttpPut) request).setEntity(body);
\r
132 request = new HttpDelete(url);
\r
145 * @param requestBody
\r
147 * @throws ClientProtocolException
\r
148 * @throws IOException
\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
165 * @throws ClientProtocolException
\r
166 * @throws IOException
\r
168 public static RestResponse executeHttpDeleteDeploy(HttpMethod method, String ip, Integer port,
\r
169 String url) throws IOException {
\r
171 return new RestResponse();
\r
174 ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();
\r
176 if (port == null) {
\r
177 port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();
\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
201 // result.setResult(EntityUtils.toString(httpResonse.getEntity()));
\r
202 } catch (Exception e) {
\r
203 logger.error("executeHttpDeleteDeploy ",e);
\r
216 * @throws ClientProtocolException
\r
217 * @throws IOException
\r
219 public static RestResponse executeHttpStartIntance(HttpMethod method, String ip, Integer port,
\r
220 String url, ActivitiStartProcessRequest object) throws IOException {
\r
222 return new RestResponse();
\r
224 String returnValue = "";
\r
225 RestResponse result = new RestResponse();
\r
226 ResponseHandler<String> responseHandler = new BasicResponseHandler();
\r
228 CloseableHttpClient httpClient = HttpClients.createDefault()){
\r
230 ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();
\r
232 if (port == null) {
\r
233 port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();
\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
251 } catch (Exception e) {
\r
252 logger.error("executeHttpStartIntance :",e);
\r
264 * @param requestBody
\r
266 * @throws ClientProtocolException
\r
267 * @throws IOException
\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
279 * @param requestBody
\r
281 * @throws ClientProtocolException
\r
282 * @throws IOException
\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
289 public static boolean getIsTest(){
\r
293 public static void setIsTest(boolean arg){
\r