Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -72,14 +71,32 @@ public JWT validateAndGet(WorkflowContext workflow, TaskContext context, Workflo

private Map<String, Object> 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) {
Expand Down Expand Up @@ -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> T execute(TaskContext task, Supplier<T> 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);
}
}
}
Loading