summaryrefslogtreecommitdiffstats
path: root/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HttpUtil.java
diff options
context:
space:
mode:
Diffstat (limited to 'activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HttpUtil.java')
-rw-r--r--activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HttpUtil.java269
1 files changed, 224 insertions, 45 deletions
diff --git a/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HttpUtil.java b/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HttpUtil.java
index 75a6b2f..0acab5f 100644
--- a/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HttpUtil.java
+++ b/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HttpUtil.java
@@ -1,5 +1,5 @@
/**
- * Copyright 2016-2017 ZTE Corporation.
+ * 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.
@@ -15,9 +15,11 @@
*/
package org.onap.workflow.activitiext.restservicetask;
-import java.util.HashMap;
+import java.util.LinkedList;
import java.util.List;
-import java.util.Map;
+import java.util.Queue;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.delegate.BpmnError;
@@ -26,11 +28,18 @@ import org.activiti.engine.delegate.Expression;
import org.activiti.engine.delegate.JavaDelegate;
import org.activiti.engine.impl.context.Context;
import org.apache.commons.lang3.StringUtils;
+import org.onap.workflow.activitiext.common.ConstString;
+import org.onap.workflow.activitiext.common.Parameter;
+import org.onap.workflow.activitiext.common.RestInfo;
+import org.onap.workflow.utils.MsbUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import com.google.gson.JsonPrimitive;
/**
* rest service
@@ -42,7 +51,10 @@ public class HttpUtil implements JavaDelegate {
private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);
- private Expression uri;
+ private Expression name;
+ private Expression version;
+ private Expression url;
+ private Expression path;
private Expression method;
private Expression accept;
private Expression contentType;
@@ -54,7 +66,6 @@ public class HttpUtil implements JavaDelegate {
try {
this.executeMethod(execution);
} catch (Exception e) {
-
logger.error("Invoke rest service failed!", e);
throw new BpmnError(e.getMessage());
}
@@ -65,40 +76,38 @@ public class HttpUtil implements JavaDelegate {
*
* @param execution
*/
- public boolean executeMethod(DelegateExecution execution) throws Exception {
+ public void executeMethod(DelegateExecution execution) throws Exception {
+
+ // get rest task information
+ RestInfo restInfo = new RestInfo();
+ restInfo.setName(getValue(name, execution));
+ restInfo.setVersion(getValue(version, execution));
+ restInfo.setUrl(getValue(url, execution));
+ restInfo.setPath(getValue(path, execution));
+ restInfo.setMethod(getValue(method, execution));
+ restInfo.setAccept(getValue(accept, execution));
+ restInfo.setContentType(getValue(contentType, execution));
- String uriValue = getValue(uri, execution);
- String methodValue = getValue(method, execution);
- String acceptValue = getValue(accept, execution);
- String contentTypeValue = getValue(contentType, execution);
String parametersValue = getValue(parameters, execution);
- Map<String, String> requestBody = new HashMap<String, String>();
+ // real uri
+ compeleteUri(restInfo);
if (!StringUtils.isEmpty(parametersValue)) {
-
// Parse the parameter into Object List
List<Parameter> parameters = JSONArray.parseArray(parametersValue, Parameter.class);
for (Parameter param : parameters) {
- handleParam(execution, param, requestBody, uriValue);
+ restInfo = handleParam(execution, param, restInfo);
}
}
- String requestPayload = JSON.toJSONString(requestBody);
-
// invoke http service
- HttpResponseMessage msg = HighLevelRestApi.invoke(methodValue, uriValue, requestPayload, acceptValue,
- contentTypeValue);
+ HttpResponseMessage msg = HighLevelRestApi.invoke(restInfo);
// inject the result to variable
execution.setVariable(execution.getCurrentActivityId(), msg);
-
- logger.info("statusCode: " + msg.getStatusCode());
- logger.info("responseBody: " + msg.getResponseBody());
-
- return true;
}
/**
@@ -108,37 +117,58 @@ public class HttpUtil implements JavaDelegate {
* @param requestBody
* @param uriValue
*/
- public void handleParam(DelegateExecution execution, Parameter param, Map<String, String> requestBody,
- String uriValue) throws ActivitiException {
+ public RestInfo handleParam(DelegateExecution execution, Parameter param, RestInfo restInfo)
+ throws ActivitiException {
- if (ConstString.PARAMETER_TYPE_EXPRESSION.equals(param.getType())) {
- handleExpression(execution, param);
+ String realUri = restInfo.getRealUri();
+
+ if (ConstString.PARAMETER_VALUE_SOURCE_PLAN.equals(param.getValueSource())) {
+
+ String planValue = parsePlanExpression(param.getValue(), execution);
+ param.setValue(planValue);
+ } else if (ConstString.PARAMETER_VALUE_SOURCE_TOPOLOGY.equals(param.getValueSource())) {
+
+ String topValue = parseTopologyExpression(param.getValue(), execution);
+ param.setValue(topValue);
+ }else if(ConstString.PARAMETER_VALUE_SOURCE_VARIABLE.equals(param.getValueSource())) {
+
+ String varValue = parseVariableExpression(param.getValue(), execution);
+ param.setValue(varValue);
}
switch (param.getPosition()) {
case ConstString.PARAMETER_POSITION_PATH:
// replace the path parameter
- uriValue = uriValue.replaceAll("\\{+" + param.getName() + "+\\}", param.getValue());
+ realUri = realUri.replaceAll("\\{+" + param.getName() + "+\\}", param.getValue());
+ restInfo.setRealUri(realUri);
break;
case ConstString.PARAMETER_POSITION_QUERY:
// add the query parameter
- if (!uriValue.contains("?")) {
- uriValue = uriValue + "?" + param.getName() + "=" + param.getValue();
+ if (!realUri.contains("?")) {
+ realUri = realUri + "?" + param.getName() + "=" + param.getValue();
} else {
- uriValue = uriValue + "&" + param.getName() + "=" + param.getValue();
+ realUri = realUri + "&" + param.getName() + "=" + param.getValue();
}
+ restInfo.setRealUri(realUri);
break;
case ConstString.PARAMETER_POSITION_BODY:
// add parameter to request body
- requestBody.put(param.getName(), param.getValue());
+ String value = param.getValue();
+
+ JsonObject obj = new JsonParser().parse(value).getAsJsonObject();
+
+ JsonObject res = GsonUtil.formatJsonObject(null, obj, execution);
+
+ restInfo.setRequestBody(res.toString());
break;
default:
- throw new ActivitiException(
- "The position '" + param.getPosition() + "' is illegal, please check your input!");
+ logger.info("The position {} is illegal, please check your input!",param.getPosition());
}
+
+ return restInfo;
}
/**
@@ -150,27 +180,176 @@ public class HttpUtil implements JavaDelegate {
*/
public String getValue(Expression expression, DelegateExecution execution) {
- String value = new String();
+ String result = new String();
if (expression != null) {
- value = (String) expression.getValue(execution);
+ result = (String) expression.getValue(execution);
}
- return value;
+ return result;
}
/**
- * parse expression in the parameter
+ * parse plan expression
*
* @param execution
- * @param param
+ * @param expStr
+ * @return
*/
- public void handleExpression(DelegateExecution execution, Parameter param) {
+ public static String parsePlanExpression(String expStr, DelegateExecution execution) {
- Expression expression = Context.getProcessEngineConfiguration().getExpressionManager()
- .createExpression(param.getValue());
+ String result = "";
+ String key = "";
+ try {
- String value = String.valueOf(expression.getValue(execution));
+ Queue<String> planQueue = parseExpressionToQueue(expStr);
+ Object planObj = execution.getVariable(planQueue.poll());
+
+ if (planQueue.isEmpty()) {
+ result = String.valueOf(planObj);
+ } else if(planObj instanceof HttpResponseMessage) {
+
+ JsonObject jsonObj = new JsonParser().parse(planObj.toString()).getAsJsonObject();
+ while (!planQueue.isEmpty()) {
+
+ key = planQueue.poll();
+ Object obj = jsonObj.get(key);
+ if (obj instanceof JsonArray) {
+
+ break;
+ }else if(obj instanceof JsonObject){
+
+ jsonObj = (JsonObject) obj;
+ }else if(obj instanceof JsonPrimitive){
+
+ JsonPrimitive jsonPri = (JsonPrimitive)obj;
+ result = jsonPri.getAsString();
+ }
+ }
+ }
+ } catch (Exception e) {
+ logger.error("expression {} is illegal, parse key {} failed!", expStr, key, e);
+ throw e;
+ }
- param.setValue(value);
+ return result;
}
-} \ No newline at end of file
+
+ /**
+ * parse topology expression
+ *
+ * @param expStr
+ * @param csarId
+ * @return
+ */
+ public static String parseTopologyExpression(String expStr, DelegateExecution execution) {
+
+ String csarId = getCsarId(execution);
+ Queue<String> topQueue = parseExpressionToQueue(expStr);
+
+ // parse topology expression
+ if (topQueue.size() != 2) {
+ logger.error("topology data {} is illegal!", expStr);
+ return new String();
+ }
+
+ // obtain topology json data
+ String topJsonStr = CatalogServiceConsumer.getTopologyJson(csarId, topQueue.poll());
+ JsonObject topJsonObj = new JsonParser().parse(topJsonStr).getAsJsonObject();
+
+ // obtain topology value
+ String topValue = topJsonObj.get(topQueue.poll()).getAsString();
+
+ logger.info("topology expression {} : {}", expStr, topValue);
+
+ return topValue;
+ }
+
+ /**
+ * parse variable expression
+ *
+ * @param execution
+ * @param expStr
+ * @return
+ */
+ public static String parseVariableExpression(String expStr, DelegateExecution execution) {
+
+ String result = "";
+
+ try {
+ Expression expression = Context.getProcessEngineConfiguration().getExpressionManager()
+ .createExpression(expStr);
+
+ result = String.valueOf(expression.getValue(execution));
+ } catch (Exception e) {
+ logger.error("variable expression illegal!",e);
+ }
+
+ return result;
+ }
+
+ /**
+ * obtain csar id
+ *
+ * @param execution
+ */
+ private static String getCsarId(DelegateExecution execution) {
+
+ String csarId = "";
+ csarId = String.valueOf(execution.getVariable(ConstString.CSARID_EXPRESSION));
+
+ return csarId;
+ }
+
+ /**
+ * compelete uri
+ *
+ * @param restInfo
+ * @return
+ */
+ private static String compeleteUri(RestInfo restInfo) {
+
+ String publishUrl = "";
+ try {
+ publishUrl = new MsbUtils().getServiceAddress(restInfo.getName(), restInfo.getVersion());
+ } catch (Exception e) {
+ logger.error("get service publish address error!", e);
+ }
+ String realUri = null;
+ if (StringUtils.isNotEmpty(publishUrl)) {
+ realUri = publishUrl + restInfo.getPath();
+ } else {
+ realUri = PropertyUtil.getBasePath() + restInfo.getUrl() + restInfo.getPath();
+ }
+
+ logger.info("realUri is " + realUri);
+ restInfo.setRealUri(realUri);
+
+ return realUri;
+ }
+
+ /**
+ * prase expression as a queue
+ *
+ * @param expStr
+ * @return
+ */
+ private static Queue<String> parseExpressionToQueue(String expStr) {
+
+ Queue<String> queue = new LinkedList<String>();
+
+ if (StringUtils.isNotEmpty(expStr) && expStr.startsWith("[")) {
+
+ Pattern p = Pattern.compile("[\\[]+[^]]+[\\]]");
+ Matcher m = p.matcher(expStr);
+ String key = "";
+ while (m.find()) {
+ key = m.group();
+ key = key.substring(1, key.length() - 1);
+ queue.offer(key);
+ }
+ }
+
+ return queue;
+ }
+
+}