|
40 | 40 | import java.util.HashMap; |
41 | 41 | import java.util.List; |
42 | 42 | import java.util.Map; |
43 | | -import java.util.function.Supplier; |
44 | 43 |
|
45 | 44 | class JaxRSAccessTokenProvider implements AccessTokenProvider { |
46 | 45 |
|
@@ -72,14 +71,32 @@ public JWT validateAndGet(WorkflowContext workflow, TaskContext context, Workflo |
72 | 71 |
|
73 | 72 | private Map<String, Object> invoke( |
74 | 73 | 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 | + } |
83 | 100 | } |
84 | 101 |
|
85 | 102 | private Response executeRequest(WorkflowContext workflow, TaskContext task, WorkflowModel model) { |
@@ -130,40 +147,4 @@ private Invocation.Builder commonHeaders( |
130 | 147 | } |
131 | 148 | return builder; |
132 | 149 | } |
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 | | - } |
169 | 150 | } |
0 commit comments