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)));
}
Aggregations