InvokeProcessor.java 19.1 KB
Newer Older
R
roo00 已提交
1 2 3
package com.x.processplatform.service.processing.processor.invoke;

import java.util.ArrayList;
R
roo00 已提交
4
import java.util.HashMap;
R
roo00 已提交
5
import java.util.LinkedHashMap;
R
roo00 已提交
6
import java.util.List;
R
roo00 已提交
7 8 9
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
R
roo00 已提交
10

R
roo00 已提交
11 12 13
import javax.script.CompiledScript;
import javax.script.ScriptContext;

R
roo00 已提交
14
import org.apache.commons.lang3.BooleanUtils;
R
roo00 已提交
15
import org.apache.commons.lang3.StringUtils;
R
roo00 已提交
16

R
Ray 已提交
17
import com.google.gson.reflect.TypeToken;
R
roo00 已提交
18
import com.x.base.core.container.EntityManagerContainer;
R
roo00 已提交
19 20 21
import com.x.base.core.project.Application;
import com.x.base.core.project.connection.ActionResponse;
import com.x.base.core.project.connection.CipherConnectionAction;
Z
zhourui 已提交
22
import com.x.base.core.project.connection.ConnectionAction;
R
roo00 已提交
23
import com.x.base.core.project.exception.RunningException;
O
o2null 已提交
24
import com.x.base.core.project.http.ActionResult.Type;
R
roo00 已提交
25 26
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
R
Ray 已提交
27 28
import com.x.base.core.project.scripting.JsonScriptingExecutor;
import com.x.base.core.project.scripting.ScriptingFactory;
R
roo00 已提交
29
import com.x.base.core.project.tools.StringTools;
R
roo00 已提交
30 31 32
import com.x.processplatform.core.entity.content.Work;
import com.x.processplatform.core.entity.element.Invoke;
import com.x.processplatform.core.entity.element.Route;
Z
zhourui 已提交
33
import com.x.processplatform.core.entity.log.Signal;
R
roo00 已提交
34
import com.x.processplatform.service.processing.Business;
R
roo00 已提交
35
import com.x.processplatform.service.processing.ThisApplication;
R
roo00 已提交
36
import com.x.processplatform.service.processing.WrapScriptObject;
R
roo00 已提交
37 38 39 40
import com.x.processplatform.service.processing.processor.AeiObjects;

public class InvokeProcessor extends AbstractInvokeProcessor {

R
Ray 已提交
41
	private static final Logger LOGGER = LoggerFactory.getLogger(InvokeProcessor.class);
R
roo00 已提交
42 43 44 45 46 47 48

	public InvokeProcessor(EntityManagerContainer entityManagerContainer) throws Exception {
		super(entityManagerContainer);
	}

	@Override
	protected Work arriving(AeiObjects aeiObjects, Invoke invoke) throws Exception {
Z
zhourui 已提交
49
		// 发送ProcessingSignal
Z
zhourui 已提交
50
		aeiObjects.getProcessingAttributes().push(Signal.invokeArrive(aeiObjects.getWork().getActivityToken(), invoke));
R
roo00 已提交
51 52 53 54 55
		return aeiObjects.getWork();
	}

	@Override
	protected List<Work> executing(AeiObjects aeiObjects, Invoke invoke) throws Exception {
Z
zhourui 已提交
56
		// 发送ProcessingSignal
Z
zhourui 已提交
57 58
		aeiObjects.getProcessingAttributes()
				.push(Signal.invokeExecute(aeiObjects.getWork().getActivityToken(), invoke));
R
roo00 已提交
59
		List<Work> results = new ArrayList<>();
60
		boolean passThrough = false;
R
roo00 已提交
61 62
		switch (invoke.getInvokeMode()) {
		case jaxws:
63 64
			// 可以根据返回脚本判断时候流转
			passThrough = this.jaxws(aeiObjects, invoke);
R
roo00 已提交
65 66
			break;
		case jaxrs:
67 68
			// 可以根据返回脚本判断时候流转
			passThrough = this.jaxrs(aeiObjects, invoke);
R
roo00 已提交
69 70 71
			break;
		default:
			break;
R
roo00 已提交
72
		}
73 74
		if (passThrough) {
			results.add(aeiObjects.getWork());
R
Ray 已提交
75 76 77
		} else {
			LOGGER.info("work title:{}, id:{} invoke return false, stay in the current activity.",
					() -> aeiObjects.getWork().getTitle(), () -> aeiObjects.getWork().getId());
78
		}
R
roo00 已提交
79 80 81 82 83
		return results;
	}

	@Override
	protected List<Route> inquiring(AeiObjects aeiObjects, Invoke invoke) throws Exception {
Z
zhourui 已提交
84
		// 发送ProcessingSignal
Z
zhourui 已提交
85 86
		aeiObjects.getProcessingAttributes()
				.push(Signal.invokeInquire(aeiObjects.getWork().getActivityToken(), invoke));
R
roo00 已提交
87 88 89 90 91
		List<Route> results = new ArrayList<>();
		results.add(aeiObjects.getRoutes().get(0));
		return results;
	}

92
	private boolean jaxws(AeiObjects aeiObjects, Invoke invoke) throws Exception {
R
roo00 已提交
93
		if (BooleanUtils.isTrue(invoke.getInternal())) {
94
			return this.jaxwsInternal(aeiObjects, invoke);
R
roo00 已提交
95
		} else {
96
			return this.jaxwsExternal(aeiObjects, invoke);
R
roo00 已提交
97 98 99
		}
	}

R
Ray 已提交
100
	private boolean jaxwsInternal(AeiObjects aeiObjects, Invoke invoke) {
101
		return true;
R
roo00 已提交
102 103
	}

104
	private boolean jaxwsExternal(AeiObjects aeiObjects, Invoke invoke) throws Exception {
R
roo00 已提交
105 106 107 108 109
		Object[] parameters = this.jaxwsEvalParameters(aeiObjects, invoke);
		JaxwsObject jaxwsObject = new JaxwsObject();
		jaxwsObject.setAddress(invoke.getJaxwsAddress());
		jaxwsObject.setMethod(invoke.getJaxwsMethod());
		jaxwsObject.setParameters(parameters);
110
		boolean passThrough = false;
R
roo00 已提交
111 112
		if (BooleanUtils.isTrue(invoke.getAsync())) {
			ThisApplication.syncJaxwsInvokeQueue.send(jaxwsObject);
113
			passThrough = true;
R
roo00 已提交
114 115
		} else {
			InvokeExecutor executor = new InvokeExecutor();
116
			Object[] response = executor.execute(jaxwsObject);
R
roo00 已提交
117 118
			if ((StringUtils.isNotEmpty(invoke.getJaxwsResponseScript()))
					|| (StringUtils.isNotEmpty(invoke.getJaxwsResponseScriptText()))) {
R
roo00 已提交
119 120 121 122
				ScriptContext scriptContext = aeiObjects.scriptContext();
				CompiledScript cs = aeiObjects.business().element().getCompiledScript(
						aeiObjects.getWork().getApplication(), aeiObjects.getActivity(),
						Business.EVENT_INVOKEJAXWSRESPONSE);
R
Ray 已提交
123
				scriptContext.getBindings(ScriptContext.ENGINE_SCOPE).put(ScriptingFactory.BINDING_NAME_JAXWSRESPONSE,
O
o2null 已提交
124
						response);
R
Ray 已提交
125
				passThrough = JsonScriptingExecutor.evalBoolean(cs, scriptContext, Boolean.TRUE);
126 127
			} else {
				passThrough = true;
R
roo00 已提交
128 129
			}
		}
130
		return passThrough;
R
roo00 已提交
131 132 133
	}

	private Object[] jaxwsEvalParameters(AeiObjects aeiObjects, Invoke invoke) throws Exception {
R
fix  
Ray 已提交
134
		List<Object> parameters = new ArrayList<>();
R
roo00 已提交
135 136
		if ((StringUtils.isNotEmpty(invoke.getJaxwsParameterScript()))
				|| (StringUtils.isNotEmpty(invoke.getJaxwsParameterScriptText()))) {
R
roo00 已提交
137 138 139
			ScriptContext scriptContext = aeiObjects.scriptContext();
			CompiledScript cs = aeiObjects.business().element().getCompiledScript(aeiObjects.getWork().getApplication(),
					aeiObjects.getActivity(), Business.EVENT_INVOKEJAXWSPARAMETER);
R
Ray 已提交
140
			scriptContext.getBindings(ScriptContext.ENGINE_SCOPE).put(ScriptingFactory.BINDING_NAME_JAXWSPARAMETERS,
O
o2null 已提交
141
					parameters);
R
Ray 已提交
142 143 144 145 146 147 148
			JsonScriptingExecutor.jsonArray(cs, scriptContext, o -> {
				if (o.size() > 0) {
					parameters.clear();
					parameters.addAll(gson.fromJson(o, new TypeToken<List<Object>>() {
					}.getType()));
				}
			});
R
roo00 已提交
149 150 151 152
		}
		return parameters.toArray();
	}

153
	private boolean jaxrs(AeiObjects aeiObjects, Invoke invoke) throws Exception {
R
roo00 已提交
154
		if (BooleanUtils.isTrue(invoke.getInternal())) {
155
			return this.jaxrsInternal(aeiObjects, invoke);
R
roo00 已提交
156
		} else {
157
			return this.jaxrsExternal(aeiObjects, invoke);
R
roo00 已提交
158 159 160
		}
	}

161
	private boolean jaxrsInternal(AeiObjects aeiObjects, Invoke invoke) throws Exception {
R
roo00 已提交
162
		ActionResponse resp = null;
O
o2null 已提交
163
		switch (StringUtils.upperCase(invoke.getJaxrsMethod())) {
Z
zhourui 已提交
164
		case ConnectionAction.METHOD_POST:
165
			resp = jaxrsInternalPost(aeiObjects, invoke);
R
roo00 已提交
166
			break;
Z
zhourui 已提交
167
		case ConnectionAction.METHOD_PUT:
168
			resp = jaxrsInternalPut(aeiObjects, invoke);
R
roo00 已提交
169
			break;
Z
zhourui 已提交
170
		case ConnectionAction.METHOD_GET:
171
			resp = jaxrsInternalGet(aeiObjects, invoke);
R
roo00 已提交
172
			break;
Z
zhourui 已提交
173
		case ConnectionAction.METHOD_DELETE:
174
			resp = jaxrsInternalDelete(aeiObjects, invoke);
R
roo00 已提交
175 176 177 178 179 180 181 182 183 184
			break;
		case "head":
			break;
		case "options":
			break;
		case "patch":
			break;
		case "trace":
			break;
		default:
O
o2null 已提交
185 186 187 188 189
			throw new ExceptionUnknownHttpMethod(invoke.getJaxrsMethod());
		}
		// 同步执行状态下进行调用判断
		if ((!BooleanUtils.isTrue(invoke.getAsync()))
				&& ((null == resp) || (!Objects.equals(Type.success, resp.getType())))) {
190
			throw new RunningException("invoke not success, work:{}.", aeiObjects.getWork().getId());
R
roo00 已提交
191
		}
192
		boolean passThrough = false;
R
roo00 已提交
193 194
		if (!BooleanUtils.isTrue(invoke.getAsync())) {
			WrapScriptObject jaxrsResponse = new WrapScriptObject();
O
o2null 已提交
195
			if (null != resp) {
R
Ray 已提交
196
				jaxrsResponse.type(resp.getType().toString());
O
o2null 已提交
197 198
				jaxrsResponse.set(gson.toJson(resp.getData()));
			}
R
roo00 已提交
199 200
			if ((StringUtils.isNotEmpty(invoke.getJaxrsResponseScript()))
					|| (StringUtils.isNotEmpty(invoke.getJaxrsResponseScriptText()))) {
R
roo00 已提交
201 202 203 204
				ScriptContext scriptContext = aeiObjects.scriptContext();
				CompiledScript cs = aeiObjects.business().element().getCompiledScript(
						aeiObjects.getWork().getApplication(), aeiObjects.getActivity(),
						Business.EVENT_INVOKEJAXRSRESPONSE);
R
Ray 已提交
205
				scriptContext.getBindings(ScriptContext.ENGINE_SCOPE).put(ScriptingFactory.BINDING_NAME_JAXRSRESPONSE,
O
o2null 已提交
206
						jaxrsResponse);
R
Ray 已提交
207
				passThrough = JsonScriptingExecutor.evalBoolean(cs, scriptContext, Boolean.TRUE);
208 209
			} else {
				passThrough = true;
R
roo00 已提交
210
			}
211 212
		} else {
			passThrough = true;
R
roo00 已提交
213
		}
214
		return passThrough;
R
roo00 已提交
215 216
	}

217 218
	private ActionResponse jaxrsInternalDelete(AeiObjects aeiObjects, Invoke invoke) throws Exception {
		String uri = this.jaxrsUrl(aeiObjects, invoke);
O
o2null 已提交
219 220
		if (BooleanUtils.isTrue(invoke.getAsync())) {
			JaxrsObject jaxrsObject = new JaxrsObject();
221 222
			Application application = ThisApplication.context().applications()
					.randomWithWeight(invoke.getInternalProject());
O
o2null 已提交
223 224 225
			jaxrsObject
					.setAddress(StringTools.JoinUrl(application.getUrlJaxrsRoot() + CipherConnectionAction.trim(uri)));
			jaxrsObject.setInternal(invoke.getInternal());
Z
zhourui 已提交
226
			jaxrsObject.setMethod(ConnectionAction.METHOD_DELETE);
O
o2null 已提交
227 228 229
			jaxrsObject.setContentType(invoke.getJaxrsContentType());
			ThisApplication.syncJaxrsInvokeQueue.send(jaxrsObject);
		} else {
230
			return ThisApplication.context().applications().deleteQuery(invoke.getInternalProject(), uri);
O
o2null 已提交
231 232 233 234
		}
		return null;
	}

235 236
	private ActionResponse jaxrsInternalGet(AeiObjects aeiObjects, Invoke invoke) throws Exception {
		String uri = this.jaxrsUrl(aeiObjects, invoke);
O
o2null 已提交
237 238
		if (BooleanUtils.isTrue(invoke.getAsync())) {
			JaxrsObject jaxrsObject = new JaxrsObject();
239 240
			Application application = ThisApplication.context().applications()
					.randomWithWeight(invoke.getInternalProject());
O
o2null 已提交
241 242 243
			jaxrsObject
					.setAddress(StringTools.JoinUrl(application.getUrlJaxrsRoot() + CipherConnectionAction.trim(uri)));
			jaxrsObject.setInternal(invoke.getInternal());
Z
zhourui 已提交
244
			jaxrsObject.setMethod(ConnectionAction.METHOD_GET);
O
o2null 已提交
245 246
			jaxrsObject.setContentType(invoke.getJaxrsContentType());
			ThisApplication.syncJaxrsInvokeQueue.send(jaxrsObject);
247
			return null;
O
o2null 已提交
248
		} else {
249
			return ThisApplication.context().applications().getQuery(invoke.getInternalProject(), uri);
O
o2null 已提交
250 251 252
		}
	}

253
	private ActionResponse jaxrsInternalPut(AeiObjects aeiObjects, Invoke invoke) throws Exception {
O
o2null 已提交
254
		String body = this.jaxrsEvalBody(aeiObjects, invoke);
255
		String uri = this.jaxrsUrl(aeiObjects, invoke);
O
o2null 已提交
256 257
		if (BooleanUtils.isTrue(invoke.getAsync())) {
			JaxrsObject jaxrsObject = new JaxrsObject();
258 259
			Application application = ThisApplication.context().applications()
					.randomWithWeight(invoke.getInternalProject());
O
o2null 已提交
260 261 262 263
			jaxrsObject
					.setAddress(StringTools.JoinUrl(application.getUrlJaxrsRoot() + CipherConnectionAction.trim(uri)));
			jaxrsObject.setBody(body);
			jaxrsObject.setInternal(invoke.getInternal());
Z
zhourui 已提交
264
			jaxrsObject.setMethod(ConnectionAction.METHOD_PUT);
O
o2null 已提交
265 266
			jaxrsObject.setContentType(invoke.getJaxrsContentType());
			ThisApplication.syncJaxrsInvokeQueue.send(jaxrsObject);
267
			return null;
O
o2null 已提交
268
		} else {
269
			return ThisApplication.context().applications().putQuery(invoke.getInternalProject(), uri, body);
O
o2null 已提交
270 271 272
		}
	}

273
	private ActionResponse jaxrsInternalPost(AeiObjects aeiObjects, Invoke invoke) throws Exception {
O
o2null 已提交
274
		String body = this.jaxrsEvalBody(aeiObjects, invoke);
275
		String uri = this.jaxrsUrl(aeiObjects, invoke);
O
o2null 已提交
276 277
		if (BooleanUtils.isTrue(invoke.getAsync())) {
			JaxrsObject jaxrsObject = new JaxrsObject();
278 279
			Application application = ThisApplication.context().applications()
					.randomWithWeight(invoke.getInternalProject());
O
o2null 已提交
280 281 282 283
			jaxrsObject
					.setAddress(StringTools.JoinUrl(application.getUrlJaxrsRoot() + CipherConnectionAction.trim(uri)));
			jaxrsObject.setBody(body);
			jaxrsObject.setInternal(invoke.getInternal());
Z
zhourui 已提交
284
			jaxrsObject.setMethod(ConnectionAction.METHOD_POST);
O
o2null 已提交
285 286
			jaxrsObject.setContentType(invoke.getJaxrsContentType());
			ThisApplication.syncJaxrsInvokeQueue.send(jaxrsObject);
287
			return null;
O
o2null 已提交
288
		} else {
289
			return ThisApplication.context().applications().postQuery(invoke.getInternalProject(), uri, body);
O
o2null 已提交
290 291 292
		}
	}

293
	private boolean jaxrsExternal(AeiObjects aeiObjects, Invoke invoke) throws Exception {
R
roo00 已提交
294
		String result = "";
O
o2null 已提交
295
		String uri = this.jaxrsUrl(aeiObjects, invoke);
R
roo00 已提交
296
		JaxrsObject jaxrsObject = new JaxrsObject();
R
Ray 已提交
297
		jaxrsObject.setHead(this.jaxrsEvalHeader(aeiObjects, invoke));
O
o2null 已提交
298
		switch (StringUtils.upperCase(invoke.getJaxrsMethod())) {
Z
zhourui 已提交
299
		case ConnectionAction.METHOD_POST:
O
o2null 已提交
300
			result = jaxrsExternalPost(aeiObjects, invoke, uri, jaxrsObject);
R
roo00 已提交
301
			break;
Z
zhourui 已提交
302
		case ConnectionAction.METHOD_PUT:
O
o2null 已提交
303
			result = jaxrsExternalPut(aeiObjects, invoke, uri, jaxrsObject);
R
roo00 已提交
304
			break;
Z
zhourui 已提交
305
		case ConnectionAction.METHOD_GET:
O
o2null 已提交
306
			result = jaxrsExternalGet(aeiObjects, invoke, uri, jaxrsObject);
R
roo00 已提交
307
			break;
Z
zhourui 已提交
308
		case ConnectionAction.METHOD_DELETE:
O
o2null 已提交
309
			result = jaxrsExternalDelete(aeiObjects, invoke, uri, jaxrsObject);
R
roo00 已提交
310 311 312 313 314 315 316 317 318 319
			break;
		case "head":
			break;
		case "options":
			break;
		case "patch":
			break;
		case "trace":
			break;
		default:
O
o2null 已提交
320 321 322 323 324
			throw new ExceptionUnknownHttpMethod(invoke.getJaxrsMethod());
		}
		// 同步执行状态下进行调用判断
		if ((!BooleanUtils.isTrue(invoke.getAsync())) && (null == result)) {
			throw new RunningException("invoke address:{} not success, work:{}.", uri, aeiObjects.getWork().getId());
R
roo00 已提交
325
		}
326
		boolean passThrough = false;
R
roo00 已提交
327 328
		if (!BooleanUtils.isTrue(invoke.getAsync())) {
			WrapScriptObject jaxrsResponse = new WrapScriptObject();
R
Ray 已提交
329 330 331 332 333
			if (null == result) {
				jaxrsResponse.type(Type.connectFatal.toString());
			} else {
				jaxrsResponse.type(Type.success.toString());
			}
R
roo00 已提交
334 335 336
			jaxrsResponse.set(result);
			if ((StringUtils.isNotEmpty(invoke.getJaxrsResponseScript()))
					|| (StringUtils.isNotEmpty(invoke.getJaxrsResponseScriptText()))) {
R
roo00 已提交
337 338 339 340
				ScriptContext scriptContext = aeiObjects.scriptContext();
				CompiledScript cs = aeiObjects.business().element().getCompiledScript(
						aeiObjects.getWork().getApplication(), aeiObjects.getActivity(),
						Business.EVENT_INVOKEJAXRSRESPONSE);
R
Ray 已提交
341
				scriptContext.getBindings(ScriptContext.ENGINE_SCOPE).put(ScriptingFactory.BINDING_NAME_JAXRSRESPONSE,
O
o2null 已提交
342
						jaxrsResponse);
R
Ray 已提交
343
				passThrough = JsonScriptingExecutor.evalBoolean(cs, scriptContext, Boolean.TRUE);
344 345
			} else {
				passThrough = true;
R
roo00 已提交
346
			}
347 348
		} else {
			passThrough = true;
R
roo00 已提交
349
		}
350
		return passThrough;
R
roo00 已提交
351 352
	}

O
o2null 已提交
353 354
	private String jaxrsExternalDelete(AeiObjects aeiObjects, Invoke invoke, String address, JaxrsObject jaxrsObject)
			throws Exception {
Z
zhourui 已提交
355
		jaxrsObject.setMethod(ConnectionAction.METHOD_DELETE);
O
o2null 已提交
356 357 358 359 360
		jaxrsObject.setInternal(false);
		jaxrsObject.setAddress(address);
		jaxrsObject.setContentType(invoke.getJaxrsContentType());
		if (BooleanUtils.isTrue(invoke.getAsync())) {
			ThisApplication.syncJaxrsInvokeQueue.send(jaxrsObject);
361
			return null;
O
o2null 已提交
362 363 364 365 366 367 368 369
		} else {
			InvokeExecutor executor = new InvokeExecutor();
			return executor.execute(jaxrsObject);
		}
	}

	private String jaxrsExternalGet(AeiObjects aeiObjects, Invoke invoke, String address, JaxrsObject jaxrsObject)
			throws Exception {
Z
zhourui 已提交
370
		jaxrsObject.setMethod(ConnectionAction.METHOD_GET);
O
o2null 已提交
371 372 373 374 375
		jaxrsObject.setInternal(false);
		jaxrsObject.setAddress(address);
		jaxrsObject.setContentType(invoke.getJaxrsContentType());
		if (BooleanUtils.isTrue(invoke.getAsync())) {
			ThisApplication.syncJaxrsInvokeQueue.send(jaxrsObject);
376
			return null;
O
o2null 已提交
377 378 379 380 381 382 383 384 385
		} else {
			InvokeExecutor executor = new InvokeExecutor();
			return executor.execute(jaxrsObject);
		}
	}

	private String jaxrsExternalPut(AeiObjects aeiObjects, Invoke invoke, String address, JaxrsObject jaxrsObject)
			throws Exception {
		String body = this.jaxrsEvalBody(aeiObjects, invoke);
Z
zhourui 已提交
386
		jaxrsObject.setMethod(ConnectionAction.METHOD_PUT);
O
o2null 已提交
387 388 389 390 391 392
		jaxrsObject.setInternal(false);
		jaxrsObject.setAddress(address);
		jaxrsObject.setBody(body);
		jaxrsObject.setContentType(invoke.getJaxrsContentType());
		if (BooleanUtils.isTrue(invoke.getAsync())) {
			ThisApplication.syncJaxrsInvokeQueue.send(jaxrsObject);
393
			return null;
O
o2null 已提交
394 395 396 397 398 399 400 401 402
		} else {
			InvokeExecutor executor = new InvokeExecutor();
			return executor.execute(jaxrsObject);
		}
	}

	private String jaxrsExternalPost(AeiObjects aeiObjects, Invoke invoke, String address, JaxrsObject jaxrsObject)
			throws Exception {
		String body = this.jaxrsEvalBody(aeiObjects, invoke);
Z
zhourui 已提交
403
		jaxrsObject.setMethod(ConnectionAction.METHOD_POST);
O
o2null 已提交
404 405 406 407 408 409
		jaxrsObject.setInternal(false);
		jaxrsObject.setAddress(address);
		jaxrsObject.setBody(body);
		jaxrsObject.setContentType(invoke.getJaxrsContentType());
		if (BooleanUtils.isTrue(invoke.getAsync())) {
			ThisApplication.syncJaxrsInvokeQueue.send(jaxrsObject);
410
			return null;
O
o2null 已提交
411 412 413 414 415 416
		} else {
			InvokeExecutor executor = new InvokeExecutor();
			return executor.execute(jaxrsObject);
		}
	}

R
roo00 已提交
417 418 419 420 421
	private String jaxrsUrl(AeiObjects aeiObjects, Invoke invoke) throws Exception {
		String url = invoke.getJaxrsAddress();
		Map<String, String> parameters = new HashMap<>();
		if ((StringUtils.isNotEmpty(invoke.getJaxrsParameterScript()))
				|| (StringUtils.isNotEmpty(invoke.getJaxrsParameterScriptText()))) {
R
roo00 已提交
422 423 424
			ScriptContext scriptContext = aeiObjects.scriptContext();
			CompiledScript cs = aeiObjects.business().element().getCompiledScript(aeiObjects.getWork().getApplication(),
					aeiObjects.getActivity(), Business.EVENT_INVOKEJAXRSPARAMETER);
R
Ray 已提交
425
			scriptContext.getBindings(ScriptContext.ENGINE_SCOPE).put(ScriptingFactory.BINDING_NAME_JAXRSPARAMETERS,
O
o2null 已提交
426
					parameters);
R
Ray 已提交
427 428 429 430 431 432 433
			// map有可能返回null
			Map<String, String> map = JsonScriptingExecutor.eval(cs, scriptContext,
					new TypeToken<Map<String, String>>() {
					}.getType());
			if (null != map) {
				parameters.putAll(map);
			}
R
roo00 已提交
434 435 436 437 438 439 440 441 442 443 444
		}
		for (Entry<String, String> entry : parameters.entrySet()) {
			url = StringUtils.replace(url, "{" + entry.getKey() + "}", entry.getValue());
		}
		return url;
	}

	private String jaxrsEvalBody(AeiObjects aeiObjects, Invoke invoke) throws Exception {
		JaxrsBody jaxrsBody = new JaxrsBody();
		if ((StringUtils.isNotEmpty(invoke.getJaxrsBodyScript()))
				|| (StringUtils.isNotEmpty(invoke.getJaxrsBodyScriptText()))) {
R
roo00 已提交
445
			ScriptContext scriptContext = aeiObjects.scriptContext();
O
o2null 已提交
446
			CompiledScript cs = aeiObjects.business().element().getCompiledScript(aeiObjects.getApplication().getId(),
R
roo00 已提交
447
					aeiObjects.getActivity(), Business.EVENT_INVOKEJAXRSBODY);
R
Ray 已提交
448 449
			scriptContext.getBindings(ScriptContext.ENGINE_SCOPE).put(ScriptingFactory.BINDING_NAME_JAXRSBODY,
					jaxrsBody);
R
Ray 已提交
450 451 452 453 454
			JsonScriptingExecutor.jsonElement(cs, scriptContext, o -> {
				if (!o.isJsonNull()) {
					jaxrsBody.set(gson.toJson(o));
				}
			});
R
roo00 已提交
455 456 457 458
		}
		return jaxrsBody.get();
	}

R
Ray 已提交
459
	private Map<String, String> jaxrsEvalHeader(AeiObjects aeiObjects, Invoke invoke) throws Exception {
R
roo00 已提交
460 461 462
		Map<String, String> map = new LinkedHashMap<>();
		if ((StringUtils.isNotEmpty(invoke.getJaxrsHeadScript()))
				|| (StringUtils.isNotEmpty(invoke.getJaxrsHeadScriptText()))) {
R
roo00 已提交
463 464 465
			ScriptContext scriptContext = aeiObjects.scriptContext();
			CompiledScript cs = aeiObjects.business().element().getCompiledScript(aeiObjects.getWork().getApplication(),
					aeiObjects.getActivity(), Business.EVENT_INVOKEJAXRSHEAD);
R
Ray 已提交
466
			scriptContext.getBindings(ScriptContext.ENGINE_SCOPE).put(ScriptingFactory.BINDING_NAME_JAXRSHEADERS, map);
R
Ray 已提交
467 468
			map.putAll(JsonScriptingExecutor.eval(cs, scriptContext, new TypeToken<Map<String, String>>() {
			}.getType()));
R
roo00 已提交
469 470 471 472
		}
		return map;
	}

R
roo00 已提交
473 474 475 476 477 478 479 480
	public class JaxrsBody {

		private String value;

		private String get() {
			return Objects.toString(value, "");
		}

O
o2null 已提交
481
		public void set(String value) {
R
roo00 已提交
482 483 484 485
			this.value = value;
		}

	}
R
roo00 已提交
486 487 488

	@Override
	protected void arrivingCommitted(AeiObjects aeiObjects, Invoke invoke) throws Exception {
O
o2null 已提交
489
		// nothing
R
roo00 已提交
490 491 492
	}

	@Override
493
	protected void executingCommitted(AeiObjects aeiObjects, Invoke invoke, List<Work> works) throws Exception {
O
o2null 已提交
494
		// nothing
R
roo00 已提交
495 496 497 498
	}

	@Override
	protected void inquiringCommitted(AeiObjects aeiObjects, Invoke invoke) throws Exception {
O
o2null 已提交
499
		// nothing
R
roo00 已提交
500 501 502
	}

}