-/**
- * Copyright 2017 ZTE Corporation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.onap.workflow.common;
-
-import java.io.IOException;
-
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpHost;
-import org.apache.http.HttpRequest;
-import org.apache.http.HttpResponse;
-import org.apache.http.client.ClientProtocolException;
-import org.apache.http.client.ResponseHandler;
-import org.apache.http.client.methods.CloseableHttpResponse;
-import org.apache.http.client.methods.HttpDelete;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.client.methods.HttpPut;
-import org.apache.http.entity.StringEntity;
-import org.apache.http.impl.client.BasicResponseHandler;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.impl.client.HttpClients;
-import org.apache.http.util.EntityUtils;
-import org.onap.workflow.externalservice.entity.activitientitiy.ActivitiStartProcessRequest;
-import org.onap.workflow.tools.Constants;
-import org.onap.workflow.tools.HttpDeleteWithBody;
-import org.onap.workflow.tools.RequestParameters;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.gson.Gson;
-
-public class RestClient {
- private static final String HTTP = "http";
- private static final Logger logger = LoggerFactory.getLogger(RestClient.class);
-
- public enum HttpMethod {
- GET, POST, PUT, DELETE
- }
-
- /**
- *
- * @param method
- * @param ip
- * @param port
- * @param url
- * @param body
- * @return
- * @throws ClientProtocolException
- * @throws IOException
- */
- public static RestResponse executeHttp(HttpMethod method, String ip, Integer port, String url,
- HttpEntity body) throws ClientProtocolException, IOException {
- logger.info("deployfile method send");
- CloseableHttpClient httpclient = HttpClients.createDefault();
- HttpResponse httpResponse = null;
- RestResponse result = new RestResponse();
- try {
- if (ip == null) {
- ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();
- }
- if (port == null) {
- port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();
- }
- HttpHost target = new HttpHost(ip, port, HTTP);
- HttpRequest request = getRequest(method, url, body);
- logger.info("deployfile method send ip" + ip);
- request.addHeader(Constants.AUTHORIZATION, ToolUtil.getHeader());
-
- httpResponse = httpclient.execute(target, request);
- HttpEntity entity = httpResponse.getEntity();
- logger.info("deployfile method send");
- if (entity != null && httpResponse.getStatusLine()!=null) {
- result.setStatusCode(httpResponse.getStatusLine().getStatusCode());
- logger.info("reply status code deploy"+httpResponse.getStatusLine().getStatusCode());
- result.setResult(EntityUtils.toString(entity));
- }
- } catch (IOException e) {
- logger.warn("Close httpclient failed.", e);
- } finally {
- if (httpclient != null) {
- try {
- httpclient.close();
- } catch (IOException e) {
- logger.warn("Close httpclient failed.", e);
- }
- }
- }
- return result;
- }
-
- public static HttpRequest getRequest(HttpMethod method, String url, HttpEntity body) {
- HttpRequest request = null;
- switch (method) {
- case GET:
- request = new HttpGet(url);
- break;
- case POST:
- request = new HttpPost(url);
- ((HttpPost) request).setEntity(body);
- break;
- case PUT:
- request = new HttpPut(url);
- ((HttpPut) request).setEntity(body);
- break;
- case DELETE:
- request = new HttpDelete(url);
- break;
- default:
- break;
- }
- return request;
- }
-
- /**
- *
- * @param ip
- * @param port
- * @param url
- * @return
- * @throws ClientProtocolException
- * @throws IOException
- */
- public static String get(String ip, int port, String url)
- throws ClientProtocolException, IOException {
- return executeHttp(HttpMethod.GET, ip, port, url, null).getResult();
- }
-
- /**
- *
- * @param ip
- * @param port
- * @param url
- * @return
- * @throws ClientProtocolException
- * @throws IOException
- */
- public static RestResponse delete(String ip, int port, String url)
- throws ClientProtocolException, IOException {
- return executeHttp(HttpMethod.GET, ip, port, url, null);
- }
-
- /**
- *
- * @param ip
- * @param port
- * @param url
- * @param requestBody
- * @return
- * @throws ClientProtocolException
- * @throws IOException
- */
- public static RestResponse post(String ip, int port, String url, HttpEntity requestBody)
- throws ClientProtocolException, IOException {
- return executeHttp(HttpMethod.POST, ip, port, url, requestBody);
- }
-
-
-
- /**
- *
- * @param method
- * @param ip
- * @param port
- * @param url
- * @param body
- * @return
- * @throws ClientProtocolException
- * @throws IOException
- */
- public static RestResponse executeHttpDeleteDeploy(HttpMethod method, String ip, Integer port,
- String url) throws ClientProtocolException, IOException {
- if (ip == null) {
- ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();
- }
- if(port==null) {
- port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();
- }
- RestResponse result = new RestResponse();
- CloseableHttpClient httpClient = HttpClients.createDefault();
- try {
- httpClient = HttpClients.createDefault();
- // "http://localhost:8080/activiti-rest/service/repository/deployments/167501"
- String deleteUrl = Constants.HTTP_HEADER + ip + Constants.COLON + port + url;
- HttpDeleteWithBody httpDeteTest = new HttpDeleteWithBody(deleteUrl);
- Gson gson = new Gson();
- RequestParameters reqPa = new RequestParameters();
- reqPa.setCasCade(true);
- String jsonStr = gson.toJson(reqPa, RequestParameters.class);
- StringEntity requestEntity = new StringEntity(jsonStr, "UTF-8");
- requestEntity.setContentEncoding("UTF-8");
- httpDeteTest.setHeader("Content-type", "application/json");
- httpDeteTest.setHeader(Constants.AUTHORIZATION, ToolUtil.getHeader());
- httpDeteTest.setEntity(new StringEntity(jsonStr));
- // returnValue = httpClient.execute(httpDeteTest, responseHandler); // 调接口获取返回值时,必须用此方法
- CloseableHttpResponse httpResonse = httpClient.execute(httpDeteTest);
- if(httpResonse!=null && httpResonse.getStatusLine()!=null) {
- int statusCode = httpResonse.getStatusLine().getStatusCode();
- result.setStatusCode(statusCode);
- }
- // result.setResult(EntityUtils.toString(httpResonse.getEntity()));
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- finally {
- try {
- httpClient.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- return result;
- }
-
- /**
- *
- * @param method
- * @param ip
- * @param port
- * @param url
- * @param body
- * @return
- * @throws ClientProtocolException
- * @throws IOException
- */
- public static RestResponse executeHttpStartIntance(HttpMethod method, String ip, Integer port,
- String url, ActivitiStartProcessRequest object) throws ClientProtocolException, IOException {
- String returnValue = "";
- RestResponse result = new RestResponse();
- CloseableHttpClient httpClient = HttpClients.createDefault();
- ResponseHandler<String> responseHandler = new BasicResponseHandler();
- try {
- httpClient = HttpClients.createDefault();
- if (ip == null) {
- ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();
- }
- if(ip==null) {
- port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();
- }
- HttpPost httpPost = new HttpPost(Constants.HTTP_HEADER + ip + ":" + port + url);
- Gson gson = new Gson();
- String jsonStr = gson.toJson(object, ActivitiStartProcessRequest.class);
- StringEntity requestEntity = new StringEntity(jsonStr, "utf-8");
- requestEntity.setContentEncoding("UTF-8");
- httpPost.setHeader("Content-type", "application/json");
- httpPost.setHeader(Constants.AUTHORIZATION, ToolUtil.getHeader());
- httpPost.setEntity(requestEntity);
- returnValue = httpClient.execute(httpPost, responseHandler); // 调接口获取返回值时,必须用此方法
- CloseableHttpResponse httpResonse = httpClient.execute(httpPost);
- if(httpResonse!=null && httpResonse.getStatusLine()!=null) {
- int statusCode = httpResonse.getStatusLine().getStatusCode();
- returnValue = EntityUtils.toString(httpResonse.getEntity(), "UTF-8");
- result.setStatusCode(statusCode);
- result.setResult(returnValue);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- finally {
- try {
- httpClient.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- return result;
- }
-
- /**
- *
- * @param ip
- * @param port
- * @param url
- * @param requestBody
- * @return
- * @throws ClientProtocolException
- * @throws IOException
- */
- public static RestResponse post(String ip, Integer port, String url,
- ActivitiStartProcessRequest requestBody) throws ClientProtocolException, IOException {
- return executeHttpStartIntance(HttpMethod.POST, ip, port, url, requestBody);
- }
-
- /**
- *
- * @param ip
- * @param port
- * @param url
- * @param requestBody
- * @return
- * @throws ClientProtocolException
- * @throws IOException
- */
- public static RestResponse post(String ip, Integer port, String url)
- throws ClientProtocolException, IOException {
- return executeHttpDeleteDeploy(HttpMethod.DELETE, ip, port, url);
- }
-
-}
+/**\r
+ * Copyright 2017 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.onap.workflow.common;\r
+\r
+import java.io.IOException;\r
+\r
+import org.apache.http.HttpEntity;\r
+import org.apache.http.HttpHost;\r
+import org.apache.http.HttpRequest;\r
+import org.apache.http.HttpResponse;\r
+import org.apache.http.client.ClientProtocolException;\r
+import org.apache.http.client.ResponseHandler;\r
+import org.apache.http.client.methods.CloseableHttpResponse;\r
+import org.apache.http.client.methods.HttpDelete;\r
+import org.apache.http.client.methods.HttpGet;\r
+import org.apache.http.client.methods.HttpPost;\r
+import org.apache.http.client.methods.HttpPut;\r
+import org.apache.http.entity.StringEntity;\r
+import org.apache.http.impl.client.BasicResponseHandler;\r
+import org.apache.http.impl.client.CloseableHttpClient;\r
+import org.apache.http.impl.client.HttpClients;\r
+import org.apache.http.util.EntityUtils;\r
+import org.onap.workflow.externalservice.entity.activitientitiy.ActivitiStartProcessRequest;\r
+import org.onap.workflow.tools.Constants;\r
+import org.onap.workflow.tools.HttpDeleteWithBody;\r
+import org.onap.workflow.tools.RequestParameters;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+import com.google.gson.Gson;\r
+\r
+public class RestClient {\r
+ private static final String HTTP = "http";\r
+ private static final Logger logger = LoggerFactory.getLogger(RestClient.class);\r
+ public static boolean isTest = false;\r
+\r
+ public enum HttpMethod {\r
+ GET, POST, PUT, DELETE\r
+ }\r
+\r
+ /**\r
+ * \r
+ * @param method\r
+ * @param ip\r
+ * @param port\r
+ * @param url\r
+ * @param body\r
+ * @return\r
+ * @throws ClientProtocolException\r
+ * @throws IOException\r
+ */\r
+ public static RestResponse executeHttp(HttpMethod method, String ip, Integer port, String url,\r
+ HttpEntity body) throws ClientProtocolException, IOException {\r
+ if (isTest) {\r
+ return new RestResponse();\r
+ }\r
+\r
+ logger.info("deployfile method send");\r
+ CloseableHttpClient httpclient = HttpClients.createDefault();\r
+ HttpResponse httpResponse = null;\r
+ RestResponse result = new RestResponse();\r
+ try {\r
+ if (ip == null) {\r
+ ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();\r
+ }\r
+ if (port == null) {\r
+ port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();\r
+ }\r
+ HttpHost target = new HttpHost(ip, port, HTTP);\r
+ HttpRequest request = getRequest(method, url, body);\r
+ logger.info("deployfile method send ip" + ip);\r
+ request.addHeader(Constants.AUTHORIZATION, ToolUtil.getHeader());\r
+\r
+ httpResponse = httpclient.execute(target, request);\r
+ HttpEntity entity = httpResponse.getEntity();\r
+ logger.info("deployfile method send");\r
+ if (entity != null && httpResponse.getStatusLine() != null) {\r
+ result.setStatusCode(httpResponse.getStatusLine().getStatusCode());\r
+ logger.info("reply status code deploy" + httpResponse.getStatusLine().getStatusCode());\r
+ result.setResult(EntityUtils.toString(entity));\r
+ }\r
+ } catch (IOException e) {\r
+ logger.warn("Close httpclient failed.", e);\r
+ } finally {\r
+ closeHttpClient(httpclient);\r
+ }\r
+ return result;\r
+ }\r
+\r
+ public static boolean closeHttpClient(CloseableHttpClient httpclient) {\r
+ if (httpclient != null) {\r
+ try {\r
+ httpclient.close();\r
+ return true;\r
+ } catch (IOException e) {\r
+ logger.warn("Close httpclient failed.", e);\r
+ return false;\r
+ }\r
+ }\r
+ return true;\r
+ }\r
+\r
+ public static HttpRequest getRequest(HttpMethod method, String url, HttpEntity body) {\r
+ HttpRequest request = null;\r
+ switch (method) {\r
+ case GET:\r
+ request = new HttpGet(url);\r
+ break;\r
+ case POST:\r
+ request = new HttpPost(url);\r
+ ((HttpPost) request).setEntity(body);\r
+ break;\r
+ case PUT:\r
+ request = new HttpPut(url);\r
+ ((HttpPut) request).setEntity(body);\r
+ break;\r
+ case DELETE:\r
+ request = new HttpDelete(url);\r
+ break;\r
+ default:\r
+ break;\r
+ }\r
+ return request;\r
+ }\r
+\r
+ /**\r
+ * \r
+ * @param ip\r
+ * @param port\r
+ * @param url\r
+ * @param requestBody\r
+ * @return\r
+ * @throws ClientProtocolException\r
+ * @throws IOException\r
+ */\r
+ public static RestResponse post(String ip, int port, String url, HttpEntity requestBody)\r
+ throws ClientProtocolException, IOException {\r
+ return executeHttp(HttpMethod.POST, ip, port, url, requestBody);\r
+ }\r
+\r
+\r
+\r
+ /**\r
+ * \r
+ * @param method\r
+ * @param ip\r
+ * @param port\r
+ * @param url\r
+ * @param body\r
+ * @return\r
+ * @throws ClientProtocolException\r
+ * @throws IOException\r
+ */\r
+ public static RestResponse executeHttpDeleteDeploy(HttpMethod method, String ip, Integer port,\r
+ String url) throws ClientProtocolException, IOException {\r
+ if (isTest) {\r
+ return new RestResponse();\r
+ }\r
+ if (ip == null) {\r
+ ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();\r
+ }\r
+ if (port == null) {\r
+ port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();\r
+ }\r
+ RestResponse result = new RestResponse();\r
+ CloseableHttpClient httpClient = HttpClients.createDefault();\r
+ try {\r
+ httpClient = HttpClients.createDefault();\r
+ // "http://localhost:8080/activiti-rest/service/repository/deployments/167501"\r
+ /* String deleteUrl =\r
+ Constants.HTTP_HEADER + ip + Constants.COLON + port + url + "?cascade=true";*/\r
+ String deleteUrl = Constants.HTTP_HEADER + ip + Constants.COLON + port + url + "?cascade=true";\r
+ HttpDeleteWithBody httpDeteTest = new HttpDeleteWithBody(deleteUrl);\r
+ Gson gson = new Gson();\r
+ RequestParameters reqPa = new RequestParameters();\r
+ reqPa.setCasCade(true);\r
+ String jsonStr = gson.toJson(reqPa, RequestParameters.class);\r
+ StringEntity requestEntity = new StringEntity(jsonStr, "UTF-8");\r
+ requestEntity.setContentEncoding("UTF-8");\r
+ httpDeteTest.setHeader("Content-type", "application/json");\r
+ httpDeteTest.setHeader(Constants.AUTHORIZATION, ToolUtil.getHeader());\r
+ httpDeteTest.setEntity(new StringEntity(jsonStr));\r
+ // returnValue = httpClient.execute(httpDeteTest, responseHandler); // 调接口获取返回值时,必须用此方法\r
+ CloseableHttpResponse httpResonse = httpClient.execute(httpDeteTest);\r
+ if (httpResonse != null && httpResonse.getStatusLine() != null) {\r
+ int statusCode = httpResonse.getStatusLine().getStatusCode();\r
+ result.setStatusCode(statusCode);\r
+ }\r
+ // result.setResult(EntityUtils.toString(httpResonse.getEntity()));\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ } finally {\r
+ closeHttpClient(httpClient);\r
+ }\r
+\r
+ return result;\r
+ }\r
+\r
+ /**\r
+ * \r
+ * @param method\r
+ * @param ip\r
+ * @param port\r
+ * @param url\r
+ * @param body\r
+ * @return\r
+ * @throws ClientProtocolException\r
+ * @throws IOException\r
+ */\r
+ public static RestResponse executeHttpStartIntance(HttpMethod method, String ip, Integer port,\r
+ String url, ActivitiStartProcessRequest object) throws ClientProtocolException, IOException {\r
+ if (isTest) {\r
+ return new RestResponse();\r
+ }\r
+ String returnValue = "";\r
+ RestResponse result = new RestResponse();\r
+ CloseableHttpClient httpClient = HttpClients.createDefault();\r
+ ResponseHandler<String> responseHandler = new BasicResponseHandler();\r
+ try {\r
+ httpClient = HttpClients.createDefault();\r
+ if (ip == null) {\r
+ ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();\r
+ }\r
+ if (port == null) {\r
+ port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();\r
+ }\r
+ HttpPost httpPost = new HttpPost(Constants.HTTP_HEADER + ip + ":" + port + url);\r
+ Gson gson = new Gson();\r
+ String jsonStr = gson.toJson(object, ActivitiStartProcessRequest.class);\r
+ StringEntity requestEntity = new StringEntity(jsonStr, "utf-8");\r
+ requestEntity.setContentEncoding("UTF-8");\r
+ httpPost.setHeader("Content-type", "application/json");\r
+ httpPost.setHeader(Constants.AUTHORIZATION, ToolUtil.getHeader());\r
+ httpPost.setEntity(requestEntity);\r
+// returnValue = httpClient.execute(httpPost, responseHandler); // 调接口获取返回值时,必须用此方法\r
+ CloseableHttpResponse httpResonse = httpClient.execute(httpPost);\r
+ if (httpResonse != null && httpResonse.getStatusLine() != null) {\r
+ int statusCode = httpResonse.getStatusLine().getStatusCode();\r
+ returnValue = EntityUtils.toString(httpResonse.getEntity(), "UTF-8");\r
+ result.setStatusCode(statusCode);\r
+ result.setResult(returnValue);\r
+ }\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+\r
+ finally {\r
+ closeHttpClient(httpClient);\r
+ }\r
+\r
+ return result;\r
+ }\r
+\r
+ /**\r
+ * \r
+ * @param ip\r
+ * @param port\r
+ * @param url\r
+ * @param requestBody\r
+ * @return\r
+ * @throws ClientProtocolException\r
+ * @throws IOException\r
+ */\r
+ public static RestResponse post(String ip, Integer port, String url,\r
+ ActivitiStartProcessRequest requestBody) throws ClientProtocolException, IOException {\r
+ return executeHttpStartIntance(HttpMethod.POST, ip, port, url, requestBody);\r
+ }\r
+\r
+ /**\r
+ * \r
+ * @param ip\r
+ * @param port\r
+ * @param url\r
+ * @param requestBody\r
+ * @return\r
+ * @throws ClientProtocolException\r
+ * @throws IOException\r
+ */\r
+ public static RestResponse post(String ip, Integer port, String url)\r
+ throws ClientProtocolException, IOException {\r
+ return executeHttpDeleteDeploy(HttpMethod.DELETE, ip, port, url);\r
+ }\r
+\r
+}\r