Search in sources :

Example 1 with RestWorkItemHandlerBodyBuilder

use of org.kogito.workitem.rest.bodybuilders.RestWorkItemHandlerBodyBuilder in project kogito-runtimes by kiegroup.

the class RestWorkItemHandler method executeWorkItem.

@Override
public void executeWorkItem(KogitoWorkItem workItem, KogitoWorkItemManager manager) {
    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 = HttpMethod.valueOf(getParam(parameters, METHOD, String.class, "GET").toUpperCase());
    Object inputModel = getParam(parameters, CONTENT_DATA, Object.class, null);
    String user = getParam(parameters, USER, String.class, null);
    String password = getParam(parameters, PASSWORD, String.class, null);
    String hostProp = getParam(parameters, HOST, String.class, "localhost");
    int portProp = getParam(parameters, PORT, Integer.class, 8080);
    RestWorkItemHandlerResult resultHandler = getParam(parameters, RESULT_HANDLER, RestWorkItemHandlerResult.class, DEFAULT_RESULT_HANDLER);
    RestWorkItemHandlerBodyBuilder bodyBuilder = getBodyBuilder(parameters);
    logger.debug("Filtered parameters are {}", parameters);
    // create request
    endPoint = resolvePathParams(endPoint, parameters);
    Optional<URL> url = getUrl(endPoint);
    String host = url.map(java.net.URL::getHost).orElse(hostProp);
    int port = url.map(java.net.URL::getPort).orElse(portProp);
    // fix issue with spaces in the path
    String path = url.map(java.net.URL::getPath).orElse(endPoint).replace(" ", "%20");
    HttpRequest<Buffer> request = client.request(method, port, host, path);
    if (user != null && !user.trim().isEmpty() && password != null && !password.trim().isEmpty()) {
        request.basicAuthentication(user, password);
    }
    HttpResponse<Buffer> response = method == HttpMethod.POST || method == HttpMethod.PUT ? request.sendJsonAndAwait(bodyBuilder.apply(inputModel, parameters)) : request.sendAndAwait();
    manager.completeWorkItem(workItem.getStringId(), Collections.singletonMap(RESULT, resultHandler.apply(response, targetInfo)));
}
Also used : Buffer(io.vertx.mutiny.core.buffer.Buffer) RestWorkItemHandlerResult(org.kogito.workitem.rest.resulthandlers.RestWorkItemHandlerResult) DefaultRestWorkItemHandlerResult(org.kogito.workitem.rest.resulthandlers.DefaultRestWorkItemHandlerResult) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RestWorkItemHandlerBodyBuilder(org.kogito.workitem.rest.bodybuilders.RestWorkItemHandlerBodyBuilder) URL(java.net.URL) HttpMethod(io.vertx.core.http.HttpMethod)

Aggregations

HttpMethod (io.vertx.core.http.HttpMethod)1 Buffer (io.vertx.mutiny.core.buffer.Buffer)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 RestWorkItemHandlerBodyBuilder (org.kogito.workitem.rest.bodybuilders.RestWorkItemHandlerBodyBuilder)1 DefaultRestWorkItemHandlerResult (org.kogito.workitem.rest.resulthandlers.DefaultRestWorkItemHandlerResult)1 RestWorkItemHandlerResult (org.kogito.workitem.rest.resulthandlers.RestWorkItemHandlerResult)1