2 * Copyright 2016-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.HashMap;
\r
19 import java.util.List;
\r
20 import java.util.Map;
\r
22 import org.activiti.engine.ActivitiException;
\r
23 import org.activiti.engine.delegate.BpmnError;
\r
24 import org.activiti.engine.delegate.DelegateExecution;
\r
25 import org.activiti.engine.delegate.Expression;
\r
26 import org.activiti.engine.delegate.JavaDelegate;
\r
27 import org.activiti.engine.impl.context.Context;
\r
28 import org.apache.commons.lang3.StringUtils;
\r
29 import org.slf4j.Logger;
\r
30 import org.slf4j.LoggerFactory;
\r
32 import com.alibaba.fastjson.JSON;
\r
33 import com.alibaba.fastjson.JSONArray;
\r
41 public class HttpUtil implements JavaDelegate {
\r
43 private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);
\r
45 private Expression uri;
\r
46 private Expression method;
\r
47 private Expression accept;
\r
48 private Expression contentType;
\r
49 private Expression parameters;
\r
52 public void execute(DelegateExecution execution) throws ActivitiException {
\r
55 this.executeMethod(execution);
\r
56 } catch (Exception e) {
\r
58 logger.error("Invoke rest service failed!", e);
\r
59 throw new BpmnError(e.getMessage());
\r
64 * invoke rest service
\r
68 public boolean executeMethod(DelegateExecution execution) throws Exception {
\r
70 String uriValue = getValue(uri, execution);
\r
71 String methodValue = getValue(method, execution);
\r
72 String acceptValue = getValue(accept, execution);
\r
73 String contentTypeValue = getValue(contentType, execution);
\r
74 String parametersValue = getValue(parameters, execution);
\r
76 Map<String, String> requestBody = new HashMap<String, String>();
\r
78 if (!StringUtils.isEmpty(parametersValue)) {
\r
80 // Parse the parameter into Object List
\r
81 List<Parameter> parameters = JSONArray.parseArray(parametersValue, Parameter.class);
\r
83 for (Parameter param : parameters) {
\r
85 handleParam(execution, param, requestBody, uriValue);
\r
89 String requestPayload = JSON.toJSONString(requestBody);
\r
91 // invoke http service
\r
92 HttpResponseMessage msg = HighLevelRestApi.invoke(methodValue, uriValue, requestPayload, acceptValue,
\r
95 // inject the result to variable
\r
96 execution.setVariable(execution.getCurrentActivityId(), msg);
\r
98 logger.info("statusCode: " + msg.getStatusCode());
\r
99 logger.info("responseBody: " + msg.getResponseBody());
\r
108 * @param requestBody
\r
111 public void handleParam(DelegateExecution execution, Parameter param, Map<String, String> requestBody,
\r
112 String uriValue) throws ActivitiException {
\r
114 if (ConstString.PARAMETER_TYPE_EXPRESSION.equals(param.getType())) {
\r
115 handleExpression(execution, param);
\r
118 switch (param.getPosition()) {
\r
119 case ConstString.PARAMETER_POSITION_PATH:
\r
121 // replace the path parameter
\r
122 uriValue = uriValue.replaceAll("\\{+" + param.getName() + "+\\}", param.getValue());
\r
124 case ConstString.PARAMETER_POSITION_QUERY:
\r
126 // add the query parameter
\r
127 if (!uriValue.contains("?")) {
\r
128 uriValue = uriValue + "?" + param.getName() + "=" + param.getValue();
\r
130 uriValue = uriValue + "&" + param.getName() + "=" + param.getValue();
\r
133 case ConstString.PARAMETER_POSITION_BODY:
\r
135 // add parameter to request body
\r
136 requestBody.put(param.getName(), param.getValue());
\r
139 throw new ActivitiException(
\r
140 "The position '" + param.getPosition() + "' is illegal, please check your input!");
\r
145 * get value from VariableScope
\r
147 * @param expression
\r
151 public String getValue(Expression expression, DelegateExecution execution) {
\r
153 String value = new String();
\r
154 if (expression != null) {
\r
155 value = (String) expression.getValue(execution);
\r
162 * parse expression in the parameter
\r
167 public void handleExpression(DelegateExecution execution, Parameter param) {
\r
169 Expression expression = Context.getProcessEngineConfiguration().getExpressionManager()
\r
170 .createExpression(param.getValue());
\r
172 String value = String.valueOf(expression.getValue(execution));
\r
174 param.setValue(value);
\r