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.activitiext.restservicetask;
\r
18 import java.util.LinkedList;
\r
19 import java.util.List;
\r
20 import java.util.Queue;
\r
21 import java.util.regex.Matcher;
\r
22 import java.util.regex.Pattern;
\r
24 import org.activiti.engine.ActivitiException;
\r
25 import org.activiti.engine.delegate.BpmnError;
\r
26 import org.activiti.engine.delegate.DelegateExecution;
\r
27 import org.activiti.engine.delegate.Expression;
\r
28 import org.activiti.engine.delegate.JavaDelegate;
\r
29 import org.activiti.engine.impl.context.Context;
\r
30 import org.apache.commons.lang3.StringUtils;
\r
31 import org.onap.workflow.activitiext.common.ConstString;
\r
32 import org.onap.workflow.activitiext.common.Parameter;
\r
33 import org.onap.workflow.activitiext.common.RestInfo;
\r
34 import org.onap.workflow.utils.MsbUtils;
\r
35 import org.slf4j.Logger;
\r
36 import org.slf4j.LoggerFactory;
\r
38 import com.alibaba.fastjson.JSONArray;
\r
39 import com.google.gson.JsonArray;
\r
40 import com.google.gson.JsonObject;
\r
41 import com.google.gson.JsonParser;
\r
42 import com.google.gson.JsonPrimitive;
\r
50 public class HttpUtil implements JavaDelegate {
\r
52 private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);
\r
54 private Expression name;
\r
55 private Expression version;
\r
56 private Expression url;
\r
57 private Expression path;
\r
58 private Expression method;
\r
59 private Expression accept;
\r
60 private Expression contentType;
\r
61 private Expression parameters;
\r
64 public void execute(DelegateExecution execution) throws ActivitiException {
\r
67 this.executeMethod(execution);
\r
68 } catch (Exception e) {
\r
69 logger.error("Invoke rest service failed!", e);
\r
70 throw new BpmnError(e.getMessage());
\r
75 * invoke rest service
\r
79 public void executeMethod(DelegateExecution execution) throws Exception {
\r
81 // get rest task information
\r
82 RestInfo restInfo = new RestInfo();
\r
83 restInfo.setName(getValue(name, execution));
\r
84 restInfo.setVersion(getValue(version, execution));
\r
85 restInfo.setUrl(getValue(url, execution));
\r
86 restInfo.setPath(getValue(path, execution));
\r
87 restInfo.setMethod(getValue(method, execution));
\r
88 restInfo.setAccept(getValue(accept, execution));
\r
89 restInfo.setContentType(getValue(contentType, execution));
\r
91 String parametersValue = getValue(parameters, execution);
\r
94 compeleteUri(restInfo);
\r
96 if (!StringUtils.isEmpty(parametersValue)) {
\r
97 // Parse the parameter into Object List
\r
98 List<Parameter> parameters = JSONArray.parseArray(parametersValue, Parameter.class);
\r
100 for (Parameter param : parameters) {
\r
102 restInfo = handleParam(execution, param, restInfo);
\r
106 // invoke http service
\r
107 HttpResponseMessage msg = HighLevelRestApi.invoke(restInfo);
\r
109 // inject the result to variable
\r
110 execution.setVariable(execution.getCurrentActivityId(), msg);
\r
117 * @param requestBody
\r
120 public RestInfo handleParam(DelegateExecution execution, Parameter param, RestInfo restInfo)
\r
121 throws ActivitiException {
\r
123 String realUri = restInfo.getRealUri();
\r
125 if (ConstString.PARAMETER_VALUE_SOURCE_PLAN.equals(param.getValueSource())) {
\r
127 String planValue = parsePlanExpression(param.getValue(), execution);
\r
128 param.setValue(planValue);
\r
129 } else if (ConstString.PARAMETER_VALUE_SOURCE_TOPOLOGY.equals(param.getValueSource())) {
\r
131 String topValue = parseTopologyExpression(param.getValue(), execution);
\r
132 param.setValue(topValue);
\r
133 }else if(ConstString.PARAMETER_VALUE_SOURCE_VARIABLE.equals(param.getValueSource())) {
\r
135 String varValue = parseVariableExpression(param.getValue(), execution);
\r
136 param.setValue(varValue);
\r
139 switch (param.getPosition()) {
\r
140 case ConstString.PARAMETER_POSITION_PATH:
\r
142 // replace the path parameter
\r
143 realUri = realUri.replaceAll("\\{+" + param.getName() + "+\\}", param.getValue());
\r
144 restInfo.setRealUri(realUri);
\r
146 case ConstString.PARAMETER_POSITION_QUERY:
\r
148 // add the query parameter
\r
149 if (!realUri.contains("?")) {
\r
150 realUri = realUri + "?" + param.getName() + "=" + param.getValue();
\r
152 realUri = realUri + "&" + param.getName() + "=" + param.getValue();
\r
154 restInfo.setRealUri(realUri);
\r
156 case ConstString.PARAMETER_POSITION_BODY:
\r
158 // add parameter to request body
\r
159 String value = param.getValue();
\r
161 JsonObject obj = new JsonParser().parse(value).getAsJsonObject();
\r
163 JsonObject res = GsonUtil.formatJsonObject(null, obj, execution);
\r
165 restInfo.setRequestBody(res.toString());
\r
168 logger.info("The position {} is illegal, please check your input!",param.getPosition());
\r
175 * get value from VariableScope
\r
177 * @param expression
\r
181 public String getValue(Expression expression, DelegateExecution execution) {
\r
183 String result = new String();
\r
184 if (expression != null) {
\r
185 result = (String) expression.getValue(execution);
\r
192 * parse plan expression
\r
198 public static String parsePlanExpression(String expStr, DelegateExecution execution) {
\r
200 String result = "";
\r
204 Queue<String> planQueue = parseExpressionToQueue(expStr);
\r
205 Object planObj = execution.getVariable(planQueue.poll());
\r
207 if (planQueue.isEmpty()) {
\r
208 result = String.valueOf(planObj);
\r
209 } else if(planObj instanceof HttpResponseMessage) {
\r
211 JsonObject jsonObj = new JsonParser().parse(planObj.toString()).getAsJsonObject();
\r
212 while (!planQueue.isEmpty()) {
\r
214 key = planQueue.poll();
\r
215 Object obj = jsonObj.get(key);
\r
216 if (obj instanceof JsonArray) {
\r
219 }else if(obj instanceof JsonObject){
\r
221 jsonObj = (JsonObject) obj;
\r
222 }else if(obj instanceof JsonPrimitive){
\r
224 JsonPrimitive jsonPri = (JsonPrimitive)obj;
\r
225 result = jsonPri.getAsString();
\r
229 } catch (Exception e) {
\r
230 logger.error("expression {} is illegal, parse key {} failed!", expStr, key, e);
\r
238 * parse topology expression
\r
244 public static String parseTopologyExpression(String expStr, DelegateExecution execution) {
\r
246 String csarId = getCsarId(execution);
\r
247 Queue<String> topQueue = parseExpressionToQueue(expStr);
\r
249 // parse topology expression
\r
250 if (topQueue.size() != 2) {
\r
251 logger.error("topology data {} is illegal!", expStr);
\r
252 return new String();
\r
255 // obtain topology json data
\r
256 String topJsonStr = CatalogServiceConsumer.getTopologyJson(csarId, topQueue.poll());
\r
257 JsonObject topJsonObj = new JsonParser().parse(topJsonStr).getAsJsonObject();
\r
259 // obtain topology value
\r
260 String topValue = topJsonObj.get(topQueue.poll()).getAsString();
\r
262 logger.info("topology expression {} : {}", expStr, topValue);
\r
268 * parse variable expression
\r
274 public static String parseVariableExpression(String expStr, DelegateExecution execution) {
\r
276 String result = "";
\r
279 Expression expression = Context.getProcessEngineConfiguration().getExpressionManager()
\r
280 .createExpression(expStr);
\r
282 result = String.valueOf(expression.getValue(execution));
\r
283 } catch (Exception e) {
\r
284 logger.error("variable expression illegal!",e);
\r
295 private static String getCsarId(DelegateExecution execution) {
\r
297 String csarId = "";
\r
298 csarId = String.valueOf(execution.getVariable(ConstString.CSARID_EXPRESSION));
\r
309 private static String compeleteUri(RestInfo restInfo) {
\r
311 String publishUrl = "";
\r
313 publishUrl = new MsbUtils().getServiceAddress(restInfo.getName(), restInfo.getVersion());
\r
314 } catch (Exception e) {
\r
315 logger.error("get service publish address error!", e);
\r
317 String realUri = null;
\r
318 if (StringUtils.isNotEmpty(publishUrl)) {
\r
319 realUri = publishUrl + restInfo.getPath();
\r
321 realUri = PropertyUtil.getBasePath() + restInfo.getUrl() + restInfo.getPath();
\r
324 logger.info("realUri is " + realUri);
\r
325 restInfo.setRealUri(realUri);
\r
331 * prase expression as a queue
\r
336 private static Queue<String> parseExpressionToQueue(String expStr) {
\r
338 Queue<String> queue = new LinkedList<String>();
\r
340 if (StringUtils.isNotEmpty(expStr) && expStr.startsWith("[")) {
\r
342 Pattern p = Pattern.compile("[\\[]+[^]]+[\\]]");
\r
343 Matcher m = p.matcher(expStr);
\r
347 key = key.substring(1, key.length() - 1);
\r