Skip to content

Commit 6eef9ec

Browse files
authored
Simplifying JaxRSAccessTockenProvider code (#1468)
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 <ftirados@ibm.com>
1 parent de5723c commit 6eef9ec

1 file changed

Lines changed: 26 additions & 45 deletions

File tree

impl/http/src/main/java/io/serverlessworkflow/impl/executors/http/auth/JaxRSAccessTokenProvider.java

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import java.util.HashMap;
4141
import java.util.List;
4242
import java.util.Map;
43-
import java.util.function.Supplier;
4443

4544
class JaxRSAccessTokenProvider implements AccessTokenProvider {
4645

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

7372
private Map<String, Object> invoke(
7473
WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel model) {
75-
return execute(
76-
taskContext,
77-
() -> {
78-
try (Response response = executeRequest(workflowContext, taskContext, model)) {
79-
ensureSuccessful(response, taskContext, "obtain token");
80-
return response.readEntity(new GenericType<>() {});
81-
}
82-
});
74+
try (Response response = executeRequest(workflowContext, taskContext, model)) {
75+
int status = response.getStatus();
76+
if (status < 200 || status >= 300) {
77+
StringBuilder message = new StringBuilder("Failed to obtain token. Error code: " + status);
78+
if (response.hasEntity()) {
79+
message.append(". Message: ").append(response.readEntity(String.class));
80+
}
81+
throw new WorkflowException(
82+
WorkflowError.communication(status, taskContext, message.toString()).build());
83+
}
84+
return response.readEntity(new GenericType<>() {});
85+
} catch (ResponseProcessingException e) {
86+
throw new WorkflowException(
87+
WorkflowError.communication(
88+
e.getResponse().getStatus(),
89+
taskContext,
90+
"Failed to process response: " + e.getMessage())
91+
.build(),
92+
e);
93+
} catch (ProcessingException e) {
94+
throw new WorkflowException(
95+
WorkflowError.communication(
96+
taskContext, "Failed to connect or process request: " + e.getMessage())
97+
.build(),
98+
e);
99+
}
83100
}
84101

85102
private Response executeRequest(WorkflowContext workflow, TaskContext task, WorkflowModel model) {
@@ -130,40 +147,4 @@ private Invocation.Builder commonHeaders(
130147
}
131148
return builder;
132149
}
133-
134-
private void ensureSuccessful(Response response, TaskContext task, String action) {
135-
int status = response.getStatus();
136-
if (status < 200 || status >= 300) {
137-
throw new WorkflowException(
138-
WorkflowError.communication(
139-
status,
140-
task,
141-
"Failed to " + action + ": HTTP " + status + " — " + readError(response))
142-
.build());
143-
}
144-
}
145-
146-
private static String readError(Response response) {
147-
return response.hasEntity() ? response.readEntity(String.class) : "";
148-
}
149-
150-
private <T> T execute(TaskContext task, Supplier<T> call) {
151-
try {
152-
return call.get();
153-
} catch (ResponseProcessingException e) {
154-
throw new WorkflowException(
155-
WorkflowError.communication(
156-
e.getResponse().getStatus(),
157-
task,
158-
"Failed to process response: " + e.getMessage())
159-
.build(),
160-
e);
161-
} catch (ProcessingException e) {
162-
throw new WorkflowException(
163-
WorkflowError.communication(
164-
task, "Failed to connect or process request: " + e.getMessage())
165-
.build(),
166-
e);
167-
}
168-
}
169150
}

0 commit comments

Comments
 (0)