75a6b2f398a37051519eb997cd53993ce2409208
[vfc/nfvo/wfengine.git] / activiti-extension / src / main / java / org / onap / workflow / activitiext / restservicetask / HttpUtil.java
1 /**\r
2  * Copyright 2016-2017 ZTE Corporation.\r
3  *\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
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
15  */\r
16 package org.onap.workflow.activitiext.restservicetask;\r
17 \r
18 import java.util.HashMap;\r
19 import java.util.List;\r
20 import java.util.Map;\r
21 \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
31 \r
32 import com.alibaba.fastjson.JSON;\r
33 import com.alibaba.fastjson.JSONArray;\r
34 \r
35 /**\r
36  * rest service\r
37  * \r
38  * @author 10222158\r
39  *\r
40  */\r
41 public class HttpUtil implements JavaDelegate {\r
42 \r
43         private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);\r
44 \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
50 \r
51         @Override\r
52         public void execute(DelegateExecution execution) throws ActivitiException {\r
53 \r
54                 try {\r
55                         this.executeMethod(execution);\r
56                 } catch (Exception e) {\r
57 \r
58                         logger.error("Invoke rest service failed!", e);\r
59                         throw new BpmnError(e.getMessage());\r
60                 }\r
61         }\r
62 \r
63         /**\r
64          * invoke rest service\r
65          * \r
66          * @param execution\r
67          */\r
68         public boolean executeMethod(DelegateExecution execution) throws Exception {\r
69 \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
75 \r
76                 Map<String, String> requestBody = new HashMap<String, String>();\r
77 \r
78                 if (!StringUtils.isEmpty(parametersValue)) {\r
79 \r
80                         // Parse the parameter into Object List\r
81                         List<Parameter> parameters = JSONArray.parseArray(parametersValue, Parameter.class);\r
82 \r
83                         for (Parameter param : parameters) {\r
84 \r
85                                 handleParam(execution, param, requestBody, uriValue);\r
86                         }\r
87                 }\r
88 \r
89                 String requestPayload = JSON.toJSONString(requestBody);\r
90 \r
91                 // invoke http service\r
92                 HttpResponseMessage msg = HighLevelRestApi.invoke(methodValue, uriValue, requestPayload, acceptValue,\r
93                                 contentTypeValue);\r
94 \r
95                 // inject the result to variable\r
96                 execution.setVariable(execution.getCurrentActivityId(), msg);\r
97 \r
98                 logger.info("statusCode: " + msg.getStatusCode());\r
99                 logger.info("responseBody: " + msg.getResponseBody());\r
100                 \r
101                 return true;\r
102         }\r
103 \r
104         /**\r
105          * \r
106          * @param execution\r
107          * @param param\r
108          * @param requestBody\r
109          * @param uriValue\r
110          */\r
111         public void handleParam(DelegateExecution execution, Parameter param, Map<String, String> requestBody,\r
112                         String uriValue) throws ActivitiException {\r
113 \r
114                 if (ConstString.PARAMETER_TYPE_EXPRESSION.equals(param.getType())) {\r
115                         handleExpression(execution, param);\r
116                 }\r
117 \r
118                 switch (param.getPosition()) {\r
119                 case ConstString.PARAMETER_POSITION_PATH:\r
120 \r
121                         // replace the path parameter\r
122                         uriValue = uriValue.replaceAll("\\{+" + param.getName() + "+\\}", param.getValue());\r
123                         break;\r
124                 case ConstString.PARAMETER_POSITION_QUERY:\r
125 \r
126                         // add the query parameter\r
127                         if (!uriValue.contains("?")) {\r
128                                 uriValue = uriValue + "?" + param.getName() + "=" + param.getValue();\r
129                         } else {\r
130                                 uriValue = uriValue + "&" + param.getName() + "=" + param.getValue();\r
131                         }\r
132                         break;\r
133                 case ConstString.PARAMETER_POSITION_BODY:\r
134 \r
135                         // add parameter to request body\r
136                         requestBody.put(param.getName(), param.getValue());\r
137                         break;\r
138                 default:\r
139                         throw new ActivitiException(\r
140                                         "The position '" + param.getPosition() + "' is illegal, please check your input!");\r
141                 }\r
142         }\r
143 \r
144         /**\r
145          * get value from VariableScope\r
146          * \r
147          * @param expression\r
148          * @param execution\r
149          * @return\r
150          */\r
151         public String getValue(Expression expression, DelegateExecution execution) {\r
152 \r
153                 String value = new String();\r
154                 if (expression != null) {\r
155                         value = (String) expression.getValue(execution);\r
156                 }\r
157 \r
158                 return value;\r
159         }\r
160 \r
161         /**\r
162          * parse expression in the parameter\r
163          * \r
164          * @param execution\r
165          * @param param\r
166          */\r
167         public void handleExpression(DelegateExecution execution, Parameter param) {\r
168 \r
169                 Expression expression = Context.getProcessEngineConfiguration().getExpressionManager()\r
170                                 .createExpression(param.getValue());\r
171 \r
172                 String value = String.valueOf(expression.getValue(execution));\r
173 \r
174                 param.setValue(value);\r
175         }\r
176 }