From 7d262f6637766fa9725b8ae10c4e60bcb597e568 Mon Sep 17 00:00:00 2001 From: fjtirado Date: Tue, 16 Jun 2026 11:30:15 +0200 Subject: [PATCH] Simplifying JaxRSAccessTockenProvider code This class was refactored for revocation, which was later discarsded, the resulting code was kind of confusing, it is better to have all the response processing in one place (the invoke method) Signed-off-by: fjtirado --- .../http/auth/JaxRSAccessTokenProvider.java | 71 +++++++------------ 1 file changed, 26 insertions(+), 45 deletions(-) diff --git a/impl/http/src/main/java/io/serverlessworkflow/impl/executors/http/auth/JaxRSAccessTokenProvider.java b/impl/http/src/main/java/io/serverlessworkflow/impl/executors/http/auth/JaxRSAccessTokenProvider.java index 8268b24c6..e13f5a744 100644 --- a/impl/http/src/main/java/io/serverlessworkflow/impl/executors/http/auth/JaxRSAccessTokenProvider.java +++ b/impl/http/src/main/java/io/serverlessworkflow/impl/executors/http/auth/JaxRSAccessTokenProvider.java @@ -40,7 +40,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.function.Supplier; class JaxRSAccessTokenProvider implements AccessTokenProvider { @@ -72,14 +71,32 @@ public JWT validateAndGet(WorkflowContext workflow, TaskContext context, Workflo private Map invoke( WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel model) { - return execute( - taskContext, - () -> { - try (Response response = executeRequest(workflowContext, taskContext, model)) { - ensureSuccessful(response, taskContext, "obtain token"); - return response.readEntity(new GenericType<>() {}); - } - }); + try (Response response = executeRequest(workflowContext, taskContext, model)) { + int status = response.getStatus(); + if (status < 200 || status >= 300) { + StringBuilder message = new StringBuilder("Failed to obtain token. Error code: " + status); + if (response.hasEntity()) { + message.append(". Message: ").append(response.readEntity(String.class)); + } + throw new WorkflowException( + WorkflowError.communication(status, taskContext, message.toString()).build()); + } + return response.readEntity(new GenericType<>() {}); + } catch (ResponseProcessingException e) { + throw new WorkflowException( + WorkflowError.communication( + e.getResponse().getStatus(), + taskContext, + "Failed to process response: " + e.getMessage()) + .build(), + e); + } catch (ProcessingException e) { + throw new WorkflowException( + WorkflowError.communication( + taskContext, "Failed to connect or process request: " + e.getMessage()) + .build(), + e); + } } private Response executeRequest(WorkflowContext workflow, TaskContext task, WorkflowModel model) { @@ -130,40 +147,4 @@ private Invocation.Builder commonHeaders( } return builder; } - - private void ensureSuccessful(Response response, TaskContext task, String action) { - int status = response.getStatus(); - if (status < 200 || status >= 300) { - throw new WorkflowException( - WorkflowError.communication( - status, - task, - "Failed to " + action + ": HTTP " + status + " — " + readError(response)) - .build()); - } - } - - private static String readError(Response response) { - return response.hasEntity() ? response.readEntity(String.class) : ""; - } - - private T execute(TaskContext task, Supplier call) { - try { - return call.get(); - } catch (ResponseProcessingException e) { - throw new WorkflowException( - WorkflowError.communication( - e.getResponse().getStatus(), - task, - "Failed to process response: " + e.getMessage()) - .build(), - e); - } catch (ProcessingException e) { - throw new WorkflowException( - WorkflowError.communication( - task, "Failed to connect or process request: " + e.getMessage()) - .build(), - e); - } - } }