-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
KOGITO-9522, KOGITO-8841 Add Jobs and process operations to Data Index addons gatewayAPI #1824
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
...x/data-index-common/src/main/java/org/kie/kogito/index/api/KogitoRuntimeCommonClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
* Copyright 2023 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.kie.kogito.index.api; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
|
||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.kie.kogito.index.model.Job; | ||
import org.kie.kogito.index.service.DataIndexServiceException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import io.quarkus.security.credential.TokenCredential; | ||
import io.quarkus.security.identity.SecurityIdentity; | ||
import io.vertx.core.AsyncResult; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.buffer.Buffer; | ||
import io.vertx.core.json.JsonObject; | ||
import io.vertx.ext.web.client.HttpResponse; | ||
import io.vertx.ext.web.client.WebClient; | ||
import io.vertx.ext.web.client.WebClientOptions; | ||
|
||
import static java.lang.String.format; | ||
|
||
@ApplicationScoped | ||
public class KogitoRuntimeCommonClient { | ||
|
||
public static final String CANCEL_JOB_PATH = "/jobs/%s"; | ||
public static final String RESCHEDULE_JOB_PATH = "/jobs/%s"; | ||
|
||
public static final String FROM_PROCESS_INSTANCE_WITH_ID = "from ProcessInstance with id: "; | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(KogitoRuntimeCommonClient.class); | ||
|
||
protected Vertx vertx; | ||
|
||
protected SecurityIdentity identity; | ||
|
||
protected Map<String, WebClient> serviceWebClientMap = new HashMap<>(); | ||
|
||
@ConfigProperty(name = "kogito.dataindex.gateway.url") | ||
protected Optional<String> gatewayTargetUrl; | ||
|
||
public void setGatewayTargetUrl(Optional<String> gatewayTargetUrl) { | ||
this.gatewayTargetUrl = gatewayTargetUrl; | ||
} | ||
|
||
public void addServiceWebClient(String serviceUrl, WebClient webClient) { | ||
serviceWebClientMap.put(serviceUrl, webClient); | ||
} | ||
|
||
protected WebClient getWebClient(String runtimeServiceUrl) { | ||
if (runtimeServiceUrl == null) { | ||
throw new DataIndexServiceException("Runtime service URL not defined, please review the kogito.service.url system property to point the public URL for this runtime."); | ||
} else { | ||
return serviceWebClientMap.computeIfAbsent(runtimeServiceUrl, url -> WebClient.create(vertx, getWebClientToURLOptions(runtimeServiceUrl))); | ||
} | ||
} | ||
|
||
public WebClientOptions getWebClientToURLOptions(String targetHttpURL) { | ||
try { | ||
URL dataIndexURL = new URL(targetHttpURL); | ||
return new WebClientOptions() | ||
.setDefaultHost(gatewayTargetUrl.orElse(dataIndexURL.getHost())) | ||
.setDefaultPort((dataIndexURL.getPort() != -1 ? dataIndexURL.getPort() : dataIndexURL.getDefaultPort())) | ||
.setSsl(dataIndexURL.getProtocol().compareToIgnoreCase("https") == 0); | ||
} catch (MalformedURLException ex) { | ||
LOGGER.error(String.format("Invalid runtime service URL: %s", targetHttpURL), ex); | ||
return null; | ||
} | ||
} | ||
|
||
public CompletableFuture<String> cancelJob(String serviceURL, Job job) { | ||
String requestURI = format(CANCEL_JOB_PATH, job.getId()); | ||
LOGGER.debug("Sending DELETE to URI {}", requestURI); | ||
return sendDeleteClientRequest(getWebClient(serviceURL), requestURI, "CANCEL Job with id: " + job.getId()); | ||
} | ||
|
||
public CompletableFuture<String> rescheduleJob(String serviceURL, Job job, String newJobData) { | ||
String requestURI = format(RESCHEDULE_JOB_PATH, job.getId()); | ||
LOGGER.debug("Sending body: {} PATCH to URI {}", newJobData, requestURI); | ||
return sendPatchClientRequest(getWebClient(serviceURL), requestURI, | ||
"RESCHEDULED JOB with id: " + job.getId(), new JsonObject(newJobData)); | ||
} | ||
|
||
public CompletableFuture sendDeleteClientRequest(WebClient webClient, String requestURI, String logMessage) { | ||
CompletableFuture future = new CompletableFuture<>(); | ||
webClient.delete(requestURI) | ||
.putHeader("Authorization", getAuthHeader()) | ||
.send(res -> asyncHttpResponseTreatment(res, future, logMessage)); | ||
LOGGER.debug("Sending DELETE to URI {}", requestURI); | ||
return future; | ||
} | ||
|
||
protected void asyncHttpResponseTreatment(AsyncResult<HttpResponse<Buffer>> res, CompletableFuture future, String logMessage) { | ||
if (res.succeeded() && (res.result().statusCode() == 200 || res.result().statusCode() == 201)) { | ||
future.complete(res.result().bodyAsString() != null ? res.result().bodyAsString() : "Successfully performed: " + logMessage); | ||
} else { | ||
future.completeExceptionally(new DataIndexServiceException(getErrorMessage(logMessage, res.result()))); | ||
} | ||
} | ||
|
||
public CompletableFuture sendPatchClientRequest(WebClient webClient, String requestURI, String logMessage, JsonObject jsonBody) { | ||
CompletableFuture future = new CompletableFuture<>(); | ||
webClient.patch(requestURI) | ||
.putHeader("Authorization", getAuthHeader()) | ||
.sendJson(jsonBody, res -> asyncHttpResponseTreatment(res, future, logMessage)); | ||
return future; | ||
} | ||
|
||
protected String getErrorMessage(String logMessage, HttpResponse<Buffer> result) { | ||
String errorMessage = "FAILED: " + logMessage; | ||
if (result != null) { | ||
errorMessage += " errorCode:" + result.statusCode() + | ||
" errorStatus:" + result.statusMessage() + | ||
" errorMessage:" + (result.body() != null ? result.body().toString() : "-"); | ||
} | ||
return errorMessage; | ||
} | ||
|
||
public String getAuthHeader() { | ||
if (identity != null && identity.getCredential(TokenCredential.class) != null) { | ||
return "Bearer " + identity.getCredential(TokenCredential.class).getToken(); | ||
} | ||
return ""; | ||
} | ||
|
||
@Inject | ||
public void setIdentity(SecurityIdentity identity) { | ||
this.identity = identity; | ||
} | ||
|
||
@Inject | ||
public void setVertx(Vertx vertx) { | ||
this.vertx = vertx; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there are a few places where env.getArgument is called without checking the value. Can we do these calls with null values? I guess in most cases we would need to check.