summaryrefslogtreecommitdiffstats
path: root/wfenginemgrservice/src/main/java/org/onap/workflow/common
diff options
context:
space:
mode:
authorZhaoxing <meng.zhaoxing1@zte.com.cn>2017-08-08 14:02:53 +0800
committerZhaoxing <meng.zhaoxing1@zte.com.cn>2017-08-08 14:02:53 +0800
commitc0604184b2aa8cff924ca783ec6b36f1f5988775 (patch)
tree1c70ffe1ad90c915b3382a37eb2fa80901519c7b /wfenginemgrservice/src/main/java/org/onap/workflow/common
parent706fc9fed496972968fde136c3e4e10f9578a5b3 (diff)
init code
Change-Id: Icd0948118397b256da70dfbcbbec5520dc5eafd4 Signed-off-by: Zhaoxing <meng.zhaoxing1@zte.com.cn>
Diffstat (limited to 'wfenginemgrservice/src/main/java/org/onap/workflow/common')
-rw-r--r--wfenginemgrservice/src/main/java/org/onap/workflow/common/Config.java31
-rw-r--r--wfenginemgrservice/src/main/java/org/onap/workflow/common/EnumModuleUrl.java33
-rw-r--r--wfenginemgrservice/src/main/java/org/onap/workflow/common/EnumStatus.java21
-rw-r--r--wfenginemgrservice/src/main/java/org/onap/workflow/common/RestClient.java310
-rw-r--r--wfenginemgrservice/src/main/java/org/onap/workflow/common/RestClientUtils.java42
-rw-r--r--wfenginemgrservice/src/main/java/org/onap/workflow/common/RestResponse.java37
-rw-r--r--wfenginemgrservice/src/main/java/org/onap/workflow/common/ToolUtil.java67
-rw-r--r--wfenginemgrservice/src/main/java/org/onap/workflow/common/package-info.java16
8 files changed, 557 insertions, 0 deletions
diff --git a/wfenginemgrservice/src/main/java/org/onap/workflow/common/Config.java b/wfenginemgrservice/src/main/java/org/onap/workflow/common/Config.java
new file mode 100644
index 0000000..49dc227
--- /dev/null
+++ b/wfenginemgrservice/src/main/java/org/onap/workflow/common/Config.java
@@ -0,0 +1,31 @@
+/**
+ * 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 org.onap.workflow.WorkflowAppConfig;
+
+public class Config {
+ private static WorkflowAppConfig workflowAppConfig;
+
+ public static WorkflowAppConfig getWorkflowAppConfig() {
+ return workflowAppConfig;
+ }
+
+ public static void setWorkflowAppConfig(WorkflowAppConfig config) {
+ workflowAppConfig = config;
+ }
+} \ No newline at end of file
diff --git a/wfenginemgrservice/src/main/java/org/onap/workflow/common/EnumModuleUrl.java b/wfenginemgrservice/src/main/java/org/onap/workflow/common/EnumModuleUrl.java
new file mode 100644
index 0000000..00f10d8
--- /dev/null
+++ b/wfenginemgrservice/src/main/java/org/onap/workflow/common/EnumModuleUrl.java
@@ -0,0 +1,33 @@
+/**
+ * Copyright 2017 [ZTE] and others.
+ *
+ * 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;
+
+public enum EnumModuleUrl {
+ ACTIVITI {
+ @Override
+ public String getApiRootDomain() {
+ return "/activiti-rest/service";
+ }
+ };
+
+ public abstract String getApiRootDomain();
+
+ public static String getBaseUrl(EnumModuleUrl enumUrl) {
+ String msbHost = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();
+ return "http://" + msbHost + ":" + "80" + enumUrl.getApiRootDomain();
+ }
+
+}
diff --git a/wfenginemgrservice/src/main/java/org/onap/workflow/common/EnumStatus.java b/wfenginemgrservice/src/main/java/org/onap/workflow/common/EnumStatus.java
new file mode 100644
index 0000000..e2d1c5c
--- /dev/null
+++ b/wfenginemgrservice/src/main/java/org/onap/workflow/common/EnumStatus.java
@@ -0,0 +1,21 @@
+/**
+ * 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;
+
+public enum EnumStatus {
+ SUCCESS, FAIL
+}
diff --git a/wfenginemgrservice/src/main/java/org/onap/workflow/common/RestClient.java b/wfenginemgrservice/src/main/java/org/onap/workflow/common/RestClient.java
new file mode 100644
index 0000000..f037757
--- /dev/null
+++ b/wfenginemgrservice/src/main/java/org/onap/workflow/common/RestClient.java
@@ -0,0 +1,310 @@
+/**
+ * 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);
+
+ 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) {
+ 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;
+ }
+
+ private 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();
+ }
+ 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);
+ 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();
+ }
+ 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);
+ 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);
+ }
+
+}
diff --git a/wfenginemgrservice/src/main/java/org/onap/workflow/common/RestClientUtils.java b/wfenginemgrservice/src/main/java/org/onap/workflow/common/RestClientUtils.java
new file mode 100644
index 0000000..d90077c
--- /dev/null
+++ b/wfenginemgrservice/src/main/java/org/onap/workflow/common/RestClientUtils.java
@@ -0,0 +1,42 @@
+/**
+ * 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.InputStream;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.mime.MultipartEntityBuilder;
+
+/**
+ *
+ * @author 10090474
+ *
+ */
+public class RestClientUtils {
+ /**
+ *
+ * @param ins
+ * @param fileName
+ * @return
+ */
+ public static HttpEntity buildMultipartRequest(InputStream ins, String fileName) {
+ MultipartEntityBuilder builder = MultipartEntityBuilder.create();
+ builder.seContentType(ContentType.MULTIPART_FORM_DATA);
+ builder.addBinaryBody("file", ins, ContentType.APPLICATION_OCTET_STREAM, fileName);
+ return builder.build();
+ }
+}
diff --git a/wfenginemgrservice/src/main/java/org/onap/workflow/common/RestResponse.java b/wfenginemgrservice/src/main/java/org/onap/workflow/common/RestResponse.java
new file mode 100644
index 0000000..21467bf
--- /dev/null
+++ b/wfenginemgrservice/src/main/java/org/onap/workflow/common/RestResponse.java
@@ -0,0 +1,37 @@
+/**
+ * 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 lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class RestResponse {
+ private int statusCode;
+ private String result;
+
+ @Override
+ public String toString() {
+ return "RestResponse [statusCode=" + statusCode + ", result=" + result + "]";
+ }
+
+}
diff --git a/wfenginemgrservice/src/main/java/org/onap/workflow/common/ToolUtil.java b/wfenginemgrservice/src/main/java/org/onap/workflow/common/ToolUtil.java
new file mode 100644
index 0000000..c148eb3
--- /dev/null
+++ b/wfenginemgrservice/src/main/java/org/onap/workflow/common/ToolUtil.java
@@ -0,0 +1,67 @@
+/**
+ * Copyright (C) 2017 ZTE, Inc. and others. All rights reserved. (ZTE)
+ *
+ * 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 java.io.InputStream;
+import java.nio.charset.Charset;
+
+import org.apache.commons.codec.binary.Base64;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+
+public class ToolUtil {
+ private static final Logger logger = LoggerFactory.getLogger(ToolUtil.class);
+
+ /**
+ * 构造Basic Auth认证头信息
+ *
+ * @return
+ */
+ public static String getHeader() {
+ String auth = "kermit" + ":" +"kermit";
+ byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
+ String authHeader = "Basic " + new String(encodedAuth);
+ return authHeader;
+ }
+
+ public static boolean isEmptyString(String val) {
+ return ((val == null || "".equals(val)) ? true : false);
+ }
+
+ public static boolean isTrimedEmptyString(String val) {
+ return ((val == null || "".equals(val.trim())) ? true : false);
+ }
+
+ public static boolean isTrimedEmptyArray(String[] val) {
+ return ((val == null || val.length == 0) ? true : false);
+ }
+
+ /**
+ * @param ins
+ */
+ public static void closeInputStream(InputStream ins) {
+ if (ins != null) {
+ try {
+ ins.close();
+ } catch (IOException e) {
+ logger.info("Close InputStream failed.", e);
+ }
+ }
+ }
+}
diff --git a/wfenginemgrservice/src/main/java/org/onap/workflow/common/package-info.java b/wfenginemgrservice/src/main/java/org/onap/workflow/common/package-info.java
new file mode 100644
index 0000000..e47fcb8
--- /dev/null
+++ b/wfenginemgrservice/src/main/java/org/onap/workflow/common/package-info.java
@@ -0,0 +1,16 @@
+/**
+ * Copyright 2017 [ZTE] and others.
+ *
+ * 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; \ No newline at end of file