InvokeProcessor.java 25.9 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
import com.x.base.core.project.Application;
20 21 22
import com.x.base.core.project.x_program_center;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.config.Node;
R
roo00 已提交
23 24
import com.x.base.core.project.connection.ActionResponse;
import com.x.base.core.project.connection.CipherConnectionAction;
Z
zhourui 已提交
25
import com.x.base.core.project.connection.ConnectionAction;
R
roo00 已提交
26
import com.x.base.core.project.exception.RunningException;
O
o2null 已提交
27
import com.x.base.core.project.http.ActionResult.Type;
R
roo00 已提交
28 29
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
R
Ray 已提交
30 31
import com.x.base.core.project.scripting.JsonScriptingExecutor;
import com.x.base.core.project.scripting.ScriptingFactory;
R
roo00 已提交
32
import com.x.base.core.project.tools.StringTools;
R
roo00 已提交
33 34 35
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 已提交
36
import com.x.processplatform.core.entity.log.Signal;
R
roo00 已提交
37
import com.x.processplatform.service.processing.Business;
R
roo00 已提交
38
import com.x.processplatform.service.processing.ThisApplication;
R
roo00 已提交
39
import com.x.processplatform.service.processing.WrapScriptObject;
R
roo00 已提交
40 41 42 43
import com.x.processplatform.service.processing.processor.AeiObjects;

public class InvokeProcessor extends AbstractInvokeProcessor {

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
    private static final Logger LOGGER = LoggerFactory.getLogger(InvokeProcessor.class);

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

    @Override
    protected Work arriving(AeiObjects aeiObjects, Invoke invoke) throws Exception {
        // 发送ProcessingSignal
        aeiObjects.getProcessingAttributes().push(Signal.invokeArrive(aeiObjects.getWork().getActivityToken(), invoke));
        return aeiObjects.getWork();
    }

    @Override
    protected List<Work> executing(AeiObjects aeiObjects, Invoke invoke) throws Exception {
        // 发送ProcessingSignal
        aeiObjects.getProcessingAttributes()
                .push(Signal.invokeExecute(aeiObjects.getWork().getActivityToken(), invoke));
        List<Work> results = new ArrayList<>();
        boolean passThrough = false;
        switch (invoke.getInvokeMode()) {
            case jaxws:
                // 可以根据返回脚本判断时候流转
                passThrough = this.jaxws(aeiObjects, invoke);
                break;
            case jaxrs:
                // 可以根据返回脚本判断时候流转
                passThrough = this.jaxrs(aeiObjects, invoke);
                break;
            default:
                break;
        }
        if (passThrough) {
            results.add(aeiObjects.getWork());
        } else {
            LOGGER.info("work title:{}, id:{} invoke return false, stay in the current activity.",
                    () -> aeiObjects.getWork().getTitle(), () -> aeiObjects.getWork().getId());
        }
        return results;
    }

    @Override
    protected List<Route> inquiring(AeiObjects aeiObjects, Invoke invoke) throws Exception {
        // 发送ProcessingSignal
        aeiObjects.getProcessingAttributes()
                .push(Signal.invokeInquire(aeiObjects.getWork().getActivityToken(), invoke));
        List<Route> results = new ArrayList<>();
        results.add(aeiObjects.getRoutes().get(0));
        return results;
    }

    private boolean jaxws(AeiObjects aeiObjects, Invoke invoke) throws Exception {
        if (BooleanUtils.isTrue(invoke.getInternal())) {
            return this.jaxwsInternal(aeiObjects, invoke);
        } else {
            return this.jaxwsExternal(aeiObjects, invoke);
        }
    }

    private boolean jaxwsInternal(AeiObjects aeiObjects, Invoke invoke) {
        return true;
    }

    private boolean jaxwsExternal(AeiObjects aeiObjects, Invoke invoke) throws Exception {
        Object[] parameters = this.jaxwsEvalParameters(aeiObjects, invoke);
        JaxwsObject jaxwsObject = new JaxwsObject();
        jaxwsObject.setAddress(invoke.getJaxwsAddress());
        jaxwsObject.setMethod(invoke.getJaxwsMethod());
        jaxwsObject.setParameters(parameters);
        boolean passThrough = false;
        if (BooleanUtils.isTrue(invoke.getAsync())) {
            ThisApplication.syncJaxwsInvokeQueue.send(jaxwsObject);
            passThrough = true;
        } else {
            InvokeExecutor executor = new InvokeExecutor();
            Object[] response = executor.execute(jaxwsObject);
            if ((StringUtils.isNotEmpty(invoke.getJaxwsResponseScript()))
                    || (StringUtils.isNotEmpty(invoke.getJaxwsResponseScriptText()))) {
                ScriptContext scriptContext = aeiObjects.scriptContext();
                CompiledScript cs = aeiObjects.business().element().getCompiledScript(
                        aeiObjects.getWork().getApplication(), aeiObjects.getActivity(),
                        Business.EVENT_INVOKEJAXWSRESPONSE);
                scriptContext.getBindings(ScriptContext.ENGINE_SCOPE).put(ScriptingFactory.BINDING_NAME_JAXWSRESPONSE,
                        response);
                passThrough = JsonScriptingExecutor.evalBoolean(cs, scriptContext, Boolean.TRUE);
            } else {
                passThrough = true;
            }
        }
        return passThrough;
    }

    private Object[] jaxwsEvalParameters(AeiObjects aeiObjects, Invoke invoke) throws Exception {
        List<Object> parameters = new ArrayList<>();
        if ((StringUtils.isNotEmpty(invoke.getJaxwsParameterScript()))
                || (StringUtils.isNotEmpty(invoke.getJaxwsParameterScriptText()))) {
            ScriptContext scriptContext = aeiObjects.scriptContext();
            CompiledScript cs = aeiObjects.business().element().getCompiledScript(aeiObjects.getWork().getApplication(),
                    aeiObjects.getActivity(), Business.EVENT_INVOKEJAXWSPARAMETER);
            scriptContext.getBindings(ScriptContext.ENGINE_SCOPE).put(ScriptingFactory.BINDING_NAME_JAXWSPARAMETERS,
                    parameters);
            JsonScriptingExecutor.jsonArray(cs, scriptContext, o -> {
                if (o.size() > 0) {
                    parameters.clear();
                    parameters.addAll(gson.fromJson(o, new TypeToken<List<Object>>() {
                    }.getType()));
                }
            });
        }
        return parameters.toArray();
    }

    private boolean jaxrs(AeiObjects aeiObjects, Invoke invoke) throws Exception {
        if (BooleanUtils.isTrue(invoke.getInternal())) {
            return this.jaxrsInternal(aeiObjects, invoke);
        } else {
            return this.jaxrsExternal(aeiObjects, invoke);
        }
    }

    private boolean jaxrsInternal(AeiObjects aeiObjects, Invoke invoke) throws Exception {
        ActionResponse resp = null;
        switch (StringUtils.upperCase(invoke.getJaxrsMethod())) {
            case ConnectionAction.METHOD_POST:
                resp = jaxrsInternalPost(aeiObjects, invoke);
                break;
            case ConnectionAction.METHOD_PUT:
                resp = jaxrsInternalPut(aeiObjects, invoke);
                break;
            case ConnectionAction.METHOD_GET:
                resp = jaxrsInternalGet(aeiObjects, invoke);
                break;
            case ConnectionAction.METHOD_DELETE:
                resp = jaxrsInternalDelete(aeiObjects, invoke);
                break;
            case "head":
                break;
            case "options":
                break;
            case "patch":
                break;
            case "trace":
                break;
            default:
                throw new ExceptionUnknownHttpMethod(invoke.getJaxrsMethod());
        }
        // 同步执行状态下进行调用判断
        if ((!BooleanUtils.isTrue(invoke.getAsync()))
                && ((null == resp) || (!Objects.equals(Type.success, resp.getType())))) {
            throw new RunningException("invoke not success, work:{}.", aeiObjects.getWork().getId());
        }
        boolean passThrough = false;
        if (!BooleanUtils.isTrue(invoke.getAsync())) {
            WrapScriptObject jaxrsResponse = new WrapScriptObject();
            if (null != resp) {
                jaxrsResponse.type(resp.getType().toString());
                jaxrsResponse.set(gson.toJson(resp.getData()));
            }
            if ((StringUtils.isNotEmpty(invoke.getJaxrsResponseScript()))
                    || (StringUtils.isNotEmpty(invoke.getJaxrsResponseScriptText()))) {
                ScriptContext scriptContext = aeiObjects.scriptContext();
                CompiledScript cs = aeiObjects.business().element().getCompiledScript(
                        aeiObjects.getWork().getApplication(), aeiObjects.getActivity(),
                        Business.EVENT_INVOKEJAXRSRESPONSE);
                scriptContext.getBindings(ScriptContext.ENGINE_SCOPE).put(ScriptingFactory.BINDING_NAME_JAXRSRESPONSE,
                        jaxrsResponse);
                passThrough = JsonScriptingExecutor.evalBoolean(cs, scriptContext, Boolean.TRUE);
            } else {
                passThrough = true;
            }
        } else {
            passThrough = true;
        }
        return passThrough;
    }

    private ActionResponse jaxrsInternalDelete(AeiObjects aeiObjects, Invoke invoke) throws Exception {
        String uri = this.jaxrsUrl(aeiObjects, invoke);
        if (BooleanUtils.isTrue(invoke.getAsync())) {
            JaxrsObject jaxrsObject = new JaxrsObject();
            if (invoke.getInternalProject().equalsIgnoreCase(x_program_center.class.getSimpleName())
                    || invoke.getInternalProject().equalsIgnoreCase(x_program_center.class.getName())) {
                final Node node = Config.nodes().get(Config.resource_node_centersPirmaryNode());
                if (null != node) {
                    String prefix = ThisApplication.context().applications().urlPrefixOfCenterServer(
                            Config.resource_node_centersPirmaryNode(),
                            node.getCenter());
                    jaxrsObject
                            .setAddress(StringTools.JoinUrl(prefix + CipherConnectionAction.trim(uri)));
                }
            } else {
                Application application = ThisApplication.context().applications()
                        .randomWithWeight(invoke.getInternalProject());
                jaxrsObject
                        .setAddress(
                                StringTools.JoinUrl(application.getUrlJaxrsRoot() + CipherConnectionAction.trim(uri)));
            }
            jaxrsObject.setInternal(invoke.getInternal());
            jaxrsObject.setMethod(ConnectionAction.METHOD_DELETE);
            jaxrsObject.setContentType(invoke.getJaxrsContentType());
            ThisApplication.syncJaxrsInvokeQueue.send(jaxrsObject);
        } else {
            return ThisApplication.context().applications().deleteQuery(invoke.getInternalProject(), uri);
        }
        return null;
    }

    private ActionResponse jaxrsInternalGet(AeiObjects aeiObjects, Invoke invoke) throws Exception {
        String uri = this.jaxrsUrl(aeiObjects, invoke);
        if (BooleanUtils.isTrue(invoke.getAsync())) {
            JaxrsObject jaxrsObject = new JaxrsObject();
            if (invoke.getInternalProject().equalsIgnoreCase(x_program_center.class.getSimpleName())
                    || invoke.getInternalProject().equalsIgnoreCase(x_program_center.class.getName())) {
                final Node node = Config.nodes().get(Config.resource_node_centersPirmaryNode());
                if (null != node) {
                    String prefix = ThisApplication.context().applications().urlPrefixOfCenterServer(
                            Config.resource_node_centersPirmaryNode(),
                            node.getCenter());
                    jaxrsObject
                            .setAddress(StringTools.JoinUrl(prefix + CipherConnectionAction.trim(uri)));
                }
            } else {
                Application application = ThisApplication.context().applications()
                        .randomWithWeight(invoke.getInternalProject());
                jaxrsObject
                        .setAddress(
                                StringTools.JoinUrl(application.getUrlJaxrsRoot() + CipherConnectionAction.trim(uri)));
            }
            jaxrsObject.setInternal(invoke.getInternal());
            jaxrsObject.setMethod(ConnectionAction.METHOD_GET);
            jaxrsObject.setContentType(invoke.getJaxrsContentType());
            ThisApplication.syncJaxrsInvokeQueue.send(jaxrsObject);
            return null;
        } else {
            return ThisApplication.context().applications().getQuery(invoke.getInternalProject(), uri);
        }
    }

    private ActionResponse jaxrsInternalPut(AeiObjects aeiObjects, Invoke invoke) throws Exception {
        String body = this.jaxrsEvalBody(aeiObjects, invoke);
        String uri = this.jaxrsUrl(aeiObjects, invoke);
        if (BooleanUtils.isTrue(invoke.getAsync())) {
            JaxrsObject jaxrsObject = new JaxrsObject();
            if (invoke.getInternalProject().equalsIgnoreCase(x_program_center.class.getSimpleName())
                    || invoke.getInternalProject().equalsIgnoreCase(x_program_center.class.getName())) {
                final Node node = Config.nodes().get(Config.resource_node_centersPirmaryNode());
                if (null != node) {
                    String prefix = ThisApplication.context().applications().urlPrefixOfCenterServer(
                            Config.resource_node_centersPirmaryNode(),
                            node.getCenter());
                    jaxrsObject
                            .setAddress(StringTools.JoinUrl(prefix + CipherConnectionAction.trim(uri)));
                }
            } else {
                Application application = ThisApplication.context().applications()
                        .randomWithWeight(invoke.getInternalProject());
                jaxrsObject
                        .setAddress(
                                StringTools.JoinUrl(application.getUrlJaxrsRoot() + CipherConnectionAction.trim(uri)));
            }
            jaxrsObject.setAddress(uri);
            jaxrsObject.setBody(body);
            jaxrsObject.setInternal(invoke.getInternal());
            jaxrsObject.setMethod(ConnectionAction.METHOD_PUT);
            jaxrsObject.setContentType(invoke.getJaxrsContentType());
            ThisApplication.syncJaxrsInvokeQueue.send(jaxrsObject);
            return null;
        } else {
            return ThisApplication.context().applications().putQuery(invoke.getInternalProject(), uri, body);
        }
    }

    private ActionResponse jaxrsInternalPost(AeiObjects aeiObjects, Invoke invoke) throws Exception {
        String body = this.jaxrsEvalBody(aeiObjects, invoke);
        String uri = this.jaxrsUrl(aeiObjects, invoke);
        if (BooleanUtils.isTrue(invoke.getAsync())) {
            JaxrsObject jaxrsObject = new JaxrsObject();
            if (invoke.getInternalProject().equalsIgnoreCase(x_program_center.class.getSimpleName())
                    || invoke.getInternalProject().equalsIgnoreCase(x_program_center.class.getName())) {
                final Node node = Config.nodes().get(Config.resource_node_centersPirmaryNode());
                if (null != node) {
                    String prefix = ThisApplication.context().applications().urlPrefixOfCenterServer(
                            Config.resource_node_centersPirmaryNode(),
                            node.getCenter());
                    jaxrsObject
                            .setAddress(StringTools.JoinUrl(prefix + CipherConnectionAction.trim(uri)));
                }
            } else {
                Application application = ThisApplication.context().applications()
                        .randomWithWeight(invoke.getInternalProject());
                jaxrsObject
                        .setAddress(
                                StringTools.JoinUrl(application.getUrlJaxrsRoot() + CipherConnectionAction.trim(uri)));
            }
            jaxrsObject.setBody(body);
            jaxrsObject.setInternal(invoke.getInternal());
            jaxrsObject.setMethod(ConnectionAction.METHOD_POST);
            jaxrsObject.setContentType(invoke.getJaxrsContentType());
            ThisApplication.syncJaxrsInvokeQueue.send(jaxrsObject);
            return null;
        } else {
            return ThisApplication.context().applications().postQuery(invoke.getInternalProject(), uri, body);
        }
    }

    private boolean jaxrsExternal(AeiObjects aeiObjects, Invoke invoke) throws Exception {
        String result = "";
        String uri = this.jaxrsUrl(aeiObjects, invoke);
        JaxrsObject jaxrsObject = new JaxrsObject();
        jaxrsObject.setHead(this.jaxrsEvalHeader(aeiObjects, invoke));
        switch (StringUtils.upperCase(invoke.getJaxrsMethod())) {
            case ConnectionAction.METHOD_POST:
                result = jaxrsExternalPost(aeiObjects, invoke, uri, jaxrsObject);
                break;
            case ConnectionAction.METHOD_PUT:
                result = jaxrsExternalPut(aeiObjects, invoke, uri, jaxrsObject);
                break;
            case ConnectionAction.METHOD_GET:
                result = jaxrsExternalGet(aeiObjects, invoke, uri, jaxrsObject);
                break;
            case ConnectionAction.METHOD_DELETE:
                result = jaxrsExternalDelete(aeiObjects, invoke, uri, jaxrsObject);
                break;
            case "head":
                break;
            case "options":
                break;
            case "patch":
                break;
            case "trace":
                break;
            default:
                throw new ExceptionUnknownHttpMethod(invoke.getJaxrsMethod());
        }
        // 同步执行状态下进行调用判断
        if ((!BooleanUtils.isTrue(invoke.getAsync())) && (null == result)) {
            throw new RunningException("invoke address:{} not success, work:{}.", uri, aeiObjects.getWork().getId());
        }
        boolean passThrough = false;
        if (!BooleanUtils.isTrue(invoke.getAsync())) {
            WrapScriptObject jaxrsResponse = new WrapScriptObject();
            if (null == result) {
                jaxrsResponse.type(Type.connectFatal.toString());
            } else {
                jaxrsResponse.type(Type.success.toString());
            }
            jaxrsResponse.set(result);
            if ((StringUtils.isNotEmpty(invoke.getJaxrsResponseScript()))
                    || (StringUtils.isNotEmpty(invoke.getJaxrsResponseScriptText()))) {
                ScriptContext scriptContext = aeiObjects.scriptContext();
                CompiledScript cs = aeiObjects.business().element().getCompiledScript(
                        aeiObjects.getWork().getApplication(), aeiObjects.getActivity(),
                        Business.EVENT_INVOKEJAXRSRESPONSE);
                scriptContext.getBindings(ScriptContext.ENGINE_SCOPE).put(ScriptingFactory.BINDING_NAME_JAXRSRESPONSE,
                        jaxrsResponse);
                passThrough = JsonScriptingExecutor.evalBoolean(cs, scriptContext, Boolean.TRUE);
            } else {
                passThrough = true;
            }
        } else {
            passThrough = true;
        }
        return passThrough;
    }

    private String jaxrsExternalDelete(AeiObjects aeiObjects, Invoke invoke, String address, JaxrsObject jaxrsObject)
            throws Exception {
        jaxrsObject.setMethod(ConnectionAction.METHOD_DELETE);
        jaxrsObject.setInternal(false);
        jaxrsObject.setAddress(address);
        jaxrsObject.setContentType(invoke.getJaxrsContentType());
        if (BooleanUtils.isTrue(invoke.getAsync())) {
            ThisApplication.syncJaxrsInvokeQueue.send(jaxrsObject);
            return null;
        } else {
            InvokeExecutor executor = new InvokeExecutor();
            return executor.execute(jaxrsObject);
        }
    }

    private String jaxrsExternalGet(AeiObjects aeiObjects, Invoke invoke, String address, JaxrsObject jaxrsObject)
            throws Exception {
        jaxrsObject.setMethod(ConnectionAction.METHOD_GET);
        jaxrsObject.setInternal(false);
        jaxrsObject.setAddress(address);
        jaxrsObject.setContentType(invoke.getJaxrsContentType());
        if (BooleanUtils.isTrue(invoke.getAsync())) {
            ThisApplication.syncJaxrsInvokeQueue.send(jaxrsObject);
            return null;
        } 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);
        jaxrsObject.setMethod(ConnectionAction.METHOD_PUT);
        jaxrsObject.setInternal(false);
        jaxrsObject.setAddress(address);
        jaxrsObject.setBody(body);
        jaxrsObject.setContentType(invoke.getJaxrsContentType());
        if (BooleanUtils.isTrue(invoke.getAsync())) {
            ThisApplication.syncJaxrsInvokeQueue.send(jaxrsObject);
            return null;
        } 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);
        jaxrsObject.setMethod(ConnectionAction.METHOD_POST);
        jaxrsObject.setInternal(false);
        jaxrsObject.setAddress(address);
        jaxrsObject.setBody(body);
        jaxrsObject.setContentType(invoke.getJaxrsContentType());
        if (BooleanUtils.isTrue(invoke.getAsync())) {
            ThisApplication.syncJaxrsInvokeQueue.send(jaxrsObject);
            return null;
        } else {
            InvokeExecutor executor = new InvokeExecutor();
            return executor.execute(jaxrsObject);
        }
    }

    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()))) {
            ScriptContext scriptContext = aeiObjects.scriptContext();
            CompiledScript cs = aeiObjects.business().element().getCompiledScript(aeiObjects.getWork().getApplication(),
                    aeiObjects.getActivity(), Business.EVENT_INVOKEJAXRSPARAMETER);
            scriptContext.getBindings(ScriptContext.ENGINE_SCOPE).put(ScriptingFactory.BINDING_NAME_JAXRSPARAMETERS,
                    parameters);
            // map有可能返回null
            Map<String, String> map = JsonScriptingExecutor.eval(cs, scriptContext,
                    new TypeToken<Map<String, String>>() {
                    }.getType());
            if (null != map) {
                parameters.putAll(map);
            }
        }
        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()))) {
            ScriptContext scriptContext = aeiObjects.scriptContext();
            CompiledScript cs = aeiObjects.business().element().getCompiledScript(aeiObjects.getApplication().getId(),
                    aeiObjects.getActivity(), Business.EVENT_INVOKEJAXRSBODY);
            scriptContext.getBindings(ScriptContext.ENGINE_SCOPE).put(ScriptingFactory.BINDING_NAME_JAXRSBODY,
                    jaxrsBody);
            JsonScriptingExecutor.jsonElement(cs, scriptContext, o -> {
                if (!o.isJsonNull()) {
                    jaxrsBody.set(gson.toJson(o));
                }
            });
        }
        return jaxrsBody.get();
    }

    private Map<String, String> jaxrsEvalHeader(AeiObjects aeiObjects, Invoke invoke) throws Exception {
        Map<String, String> map = new LinkedHashMap<>();
        if ((StringUtils.isNotEmpty(invoke.getJaxrsHeadScript()))
                || (StringUtils.isNotEmpty(invoke.getJaxrsHeadScriptText()))) {
            ScriptContext scriptContext = aeiObjects.scriptContext();
            CompiledScript cs = aeiObjects.business().element().getCompiledScript(aeiObjects.getWork().getApplication(),
                    aeiObjects.getActivity(), Business.EVENT_INVOKEJAXRSHEAD);
            scriptContext.getBindings(ScriptContext.ENGINE_SCOPE).put(ScriptingFactory.BINDING_NAME_JAXRSHEADERS, map);
            map.putAll(JsonScriptingExecutor.eval(cs, scriptContext, new TypeToken<Map<String, String>>() {
            }.getType()));
        }
        return map;
    }

    public class JaxrsBody {

        private String value;

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

        public void set(String value) {
            this.value = value;
        }

    }

    @Override
    protected void arrivingCommitted(AeiObjects aeiObjects, Invoke invoke) throws Exception {
        // nothing
    }

    @Override
    protected void executingCommitted(AeiObjects aeiObjects, Invoke invoke, List<Work> works) throws Exception {
        // nothing
    }

    @Override
    protected void inquiringCommitted(AeiObjects aeiObjects, Invoke invoke) throws Exception {
        // nothing
    }
R
roo00 已提交
557 558

}