Skip to content

Commit af6ba65

Browse files
committed
Apply refactoring
Signed-off-by: Matheus Cruz <[email protected]>
1 parent 63c708b commit af6ba65

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

impl/http/src/main/java/io/serverlessworkflow/impl/executors/http/AbstractHttpExecutorBuilder.java

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import jakarta.ws.rs.HttpMethod;
2626
import jakarta.ws.rs.client.WebTarget;
2727
import jakarta.ws.rs.core.Response;
28+
2829
import java.net.URI;
2930
import java.util.Map;
3031
import java.util.Optional;
@@ -48,18 +49,21 @@ protected static RequestSupplier buildRequestSupplier(
4849
HttpModelConverter converter = HttpConverterResolver.converter(w, t);
4950
Response res =
5051
request.buildPost(converter.toEntity(bodyFilter.apply(w, t, node))).invoke();
51-
handleWithRedirect(redirect, t, res);
52+
53+
applyRedirectValidation(redirect, t, res);
5254

5355
return w.definition()
5456
.application()
5557
.modelFactory()
56-
.fromAny(res.readEntity(converter.responseType()));
58+
.fromAny(
59+
request.post(
60+
converter.toEntity(bodyFilter.apply(w, t, node)), converter.responseType()));
5761
};
5862
case HttpMethod.GET:
5963
default:
6064
return (request, w, t, n) -> {
6165
Response res = request.buildGet().invoke();
62-
handleWithRedirect(redirect, t, res);
66+
applyRedirectValidation(redirect, t, res);
6367

6468
return w.definition()
6569
.application()
@@ -69,32 +73,25 @@ protected static RequestSupplier buildRequestSupplier(
6973
}
7074
}
7175

72-
private static void handleWithRedirect(boolean redirect, TaskContext t, Response response) {
76+
private static void applyRedirectValidation(boolean redirect, TaskContext t, Response response) {
7377
Response.Status.Family statusFamily = response.getStatusInfo().getFamily();
78+
boolean isSuccess = statusFamily.equals(Response.Status.Family.SUCCESSFUL);
79+
boolean isRedirect = statusFamily.equals(Response.Status.Family.REDIRECTION);
7480

75-
if (redirect
76-
&& (!statusFamily.equals(Response.Status.Family.SUCCESSFUL)
77-
&& !statusFamily.equals(Response.Status.Family.REDIRECTION))) {
78-
throw new WorkflowException(
79-
WorkflowError.communication(
80-
response.getStatus(),
81-
t,
82-
new RedirectValidationException(
83-
String.format(
84-
"The property 'redirect' is true but received status %d (%s); expected status in the 200-399 range",
85-
response.getStatus(), response.getStatusInfo().getReasonPhrase())))
86-
.build());
87-
}
88-
89-
if (!redirect && !statusFamily.equals(Response.Status.Family.SUCCESSFUL)) {
81+
boolean valid = redirect ? (isSuccess || isRedirect) : isSuccess;
82+
if (!valid) {
83+
String expectedRange = redirect ? "200-399" : "200-299";
9084
throw new WorkflowException(
9185
WorkflowError.communication(
9286
response.getStatus(),
9387
t,
9488
new RedirectValidationException(
9589
String.format(
96-
"The property 'redirect' is false but received status %d (%s); expected status in the 200-299 range",
97-
response.getStatus(), response.getStatusInfo().getReasonPhrase())))
90+
"The property 'redirect' is %s but received status %d (%s); expected status in the %s range",
91+
redirect,
92+
response.getStatus(),
93+
response.getStatusInfo().getReasonPhrase(),
94+
expectedRange)))
9895
.build());
9996
}
10097
}

0 commit comments

Comments
 (0)