public Optional activateWorkItemHandler()

in kogito-workitems/kogito-rest-workitem/src/main/java/org/kogito/workitem/rest/RestWorkItemHandler.java [120:196]


    public Optional<WorkItemTransition> activateWorkItemHandler(KogitoWorkItemManager manager, KogitoWorkItemHandler handler, KogitoWorkItem workItem, WorkItemTransition transition) {
        Class<?> targetInfo = getTargetInfo(workItem);
        logger.debug("Using target {}", targetInfo);
        //retrieving parameters
        Map<String, Object> parameters = new HashMap<>(workItem.getParameters());
        //removing unnecessary parameter
        parameters.remove("TaskName");
        String endPoint = getParam(parameters, URL, String.class, null);
        if (endPoint == null) {
            throw new IllegalArgumentException("Missing required parameter " + URL);
        }

        HttpMethod method = getParam(parameters, METHOD, HttpMethod.class, HttpMethod.GET);
        RestWorkItemHandlerResult resultHandler = getClassParam(parameters, RESULT_HANDLER, RestWorkItemHandlerResult.class, DEFAULT_RESULT_HANDLER, resultHandlers);
        RestWorkItemHandlerBodyBuilder bodyBuilder = getClassParam(parameters, BODY_BUILDER, RestWorkItemHandlerBodyBuilder.class, DEFAULT_BODY_BUILDER, bodyBuilders);
        ParamsDecorator paramsDecorator = getClassParam(parameters, PARAMS_DECORATOR, ParamsDecorator.class, DEFAULT_PARAMS_DECORATOR, paramsDecorators);
        PathParamResolver pathParamResolver = getClassParam(parameters, PATH_PARAM_RESOLVER, PathParamResolver.class, DEFAULT_PATH_PARAM_RESOLVER, pathParamsResolvers);
        Collection<? extends AuthDecorator> authDecorators = getClassListParam(parameters, AUTH_METHOD, AuthDecorator.class, DEFAULT_AUTH_DECORATORS, authDecoratorsMap);

        logger.debug("Filtered parameters are {}", parameters);
        // create request
        endPoint = pathParamResolver.apply(endPoint, parameters);

        String protocol = null;
        String host = null;
        int port = -1;
        String path = null;
        try {
            URL uri = new URL(endPoint);
            protocol = uri.getProtocol();
            host = uri.getHost();
            port = uri.getPort();
            path = uri.getPath();
            String query = uri.getQuery();
            if (!isEmpty(path) && !isEmpty(query)) {
                path += "?" + query;
            }
        } catch (MalformedURLException ex) {
            logger.debug("Parameter endpoint {} is not valid uri {}", endPoint, ex.getMessage());
        }

        if (isEmpty(protocol)) {
            protocol = getParam(parameters, PROTOCOL, String.class, "http");
            logger.debug("Protocol not specified, using {}", protocol);
        }

        boolean isSsl = protocol.equalsIgnoreCase("https");

        if (isEmpty(host)) {
            host = getParam(parameters, HOST, String.class, "localhost");
            logger.debug("Host not specified, using {}", host);
        }
        if (port == -1) {
            port = getParam(parameters, PORT, Integer.class, isSsl ? DEFAULT_SSL_PORT : DEFAULT_PORT);
            logger.debug("Port not specified, using {}", port);
        }
        if (isEmpty(path)) {
            path = endPoint;
            logger.debug("Path is empty, using whole endpoint {}", endPoint);
        }
        logger.debug("Invoking request with protocol {} host {} port {} and endpoint {}", protocol, host, port, path);
        WebClient client = isSsl ? httpsClient : httpClient;
        HttpRequest<Buffer> request = client.request(method, port, host, path);
        requestDecorators.forEach(d -> d.decorate(workItem, parameters, request));
        authDecorators.forEach(d -> d.decorate(workItem, parameters, request));
        paramsDecorator.decorate(workItem, parameters, request);
        Duration requestTimeout = getRequestTimeout(parameters);
        HttpResponse<Buffer> response = method.equals(HttpMethod.POST) || method.equals(HttpMethod.PUT)
                ? sendJson(request, bodyBuilder.apply(parameters), requestTimeout)
                : send(request, requestTimeout);
        int statusCode = response.statusCode();
        if (statusCode < 200 || statusCode >= 300) {
            throw new WorkItemExecutionException(Integer.toString(statusCode), "Request for endpoint " + endPoint + " failed with message: " + response.statusMessage());
        }

        return Optional.of(this.workItemLifeCycle.newTransition("complete", workItem.getPhaseStatus(), Collections.singletonMap(RESULT, resultHandler.apply(response, targetInfo))));
    }