Skip to content

Commit bde53ac

Browse files
committed
Use redirect property on HTTP and OpenAPI calls
Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com>
1 parent f5a623d commit bde53ac

File tree

8 files changed

+78
-11
lines changed

8 files changed

+78
-11
lines changed

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

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@
1515
*/
1616
package io.serverlessworkflow.impl.executors.http;
1717

18+
import io.serverlessworkflow.impl.TaskContext;
1819
import io.serverlessworkflow.impl.WorkflowApplication;
20+
import io.serverlessworkflow.impl.WorkflowError;
21+
import io.serverlessworkflow.impl.WorkflowException;
1922
import io.serverlessworkflow.impl.WorkflowFilter;
2023
import io.serverlessworkflow.impl.WorkflowUtils;
2124
import io.serverlessworkflow.impl.WorkflowValueResolver;
2225
import jakarta.ws.rs.HttpMethod;
2326
import jakarta.ws.rs.client.WebTarget;
27+
import jakarta.ws.rs.core.Response;
2428
import java.net.URI;
2529
import java.util.Map;
2630
import java.util.Optional;
@@ -35,13 +39,18 @@ abstract class AbstractHttpExecutorBuilder {
3539
protected boolean redirect;
3640

3741
protected static RequestSupplier buildRequestSupplier(
38-
String method, Object body, WorkflowApplication application) {
42+
String method, Object body, boolean redirect, WorkflowApplication application) {
3943

4044
switch (method.toUpperCase()) {
4145
case HttpMethod.POST:
4246
WorkflowFilter bodyFilter = WorkflowUtils.buildWorkflowFilter(application, body);
4347
return (request, w, t, node) -> {
4448
HttpModelConverter converter = HttpConverterResolver.converter(w, t);
49+
Response res =
50+
request.buildPost(converter.toEntity(bodyFilter.apply(w, t, node))).invoke();
51+
52+
applyRedirectValidation(redirect, t, res);
53+
4554
return w.definition()
4655
.application()
4756
.modelFactory()
@@ -51,11 +60,38 @@ protected static RequestSupplier buildRequestSupplier(
5160
};
5261
case HttpMethod.GET:
5362
default:
54-
return (request, w, t, n) ->
55-
w.definition()
56-
.application()
57-
.modelFactory()
58-
.fromAny(request.get(HttpConverterResolver.converter(w, t).responseType()));
63+
return (request, w, t, n) -> {
64+
Response res = request.buildGet().invoke();
65+
applyRedirectValidation(redirect, t, res);
66+
67+
return w.definition()
68+
.application()
69+
.modelFactory()
70+
.fromAny(res.readEntity(HttpConverterResolver.converter(w, t).responseType()));
71+
};
72+
}
73+
}
74+
75+
private static void applyRedirectValidation(boolean redirect, TaskContext t, Response response) {
76+
Response.Status.Family statusFamily = response.getStatusInfo().getFamily();
77+
boolean isSuccess = statusFamily.equals(Response.Status.Family.SUCCESSFUL);
78+
boolean isRedirect = statusFamily.equals(Response.Status.Family.REDIRECTION);
79+
80+
boolean valid = redirect ? (isSuccess || isRedirect) : isSuccess;
81+
if (!valid) {
82+
String expectedRange = redirect ? "200-399" : "200-299";
83+
throw new WorkflowException(
84+
WorkflowError.communication(
85+
response.getStatus(),
86+
t,
87+
new RedirectValidationException(
88+
String.format(
89+
"The property 'redirect' is %s but received status %d (%s); expected status in the %s range",
90+
redirect,
91+
response.getStatus(),
92+
response.getStatusInfo().getReasonPhrase(),
93+
expectedRange)))
94+
.build());
5995
}
6096
}
6197

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ public void init(CallHTTP task, WorkflowDefinition definition, WorkflowMutablePo
6363
}
6464
this.requestFunction =
6565
buildRequestSupplier(
66-
httpArgs.getMethod().toUpperCase(), httpArgs.getBody(), definition.application());
66+
httpArgs.getMethod().toUpperCase(),
67+
httpArgs.getBody(),
68+
httpArgs.isRedirect(),
69+
definition.application());
6770
}
6871

6972
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public HttpExecutor build(String uri) {
8383
}
8484

8585
public HttpExecutor build(WorkflowValueResolver<URI> uriSupplier) {
86-
this.requestFunction = buildRequestSupplier(method, body, definition.application());
86+
this.requestFunction = buildRequestSupplier(method, body, redirect, definition.application());
8787
this.targetSupplier =
8888
pathSupplier == null
8989
? getTargetSupplier(uriSupplier)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.impl.executors.http;
17+
18+
public class RedirectValidationException extends RuntimeException {
19+
private static final long serialVersionUID = 1L;
20+
21+
public RedirectValidationException(String message) {
22+
super(message);
23+
}
24+
}

impl/test/src/test/java/io/serverlessworkflow/impl/test/OpenAPITest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,9 @@ public void testOpenAPIBearerQueryInlinedBodyWithNegativeResponse() throws Excep
202202
.asMap()
203203
.orElseThrow());
204204
assertInstanceOf(WorkflowException.class, exception.getCause());
205-
assertTrue(exception.getMessage().contains("status=409"));
206-
assertTrue(exception.getMessage().contains("title=HTTP 409 Client Error"));
205+
assertTrue(
206+
exception.getMessage().contains("property 'redirect' is true but received status 409"));
207+
assertTrue(exception.getMessage().contains("409 (Client Error)"));
207208

208209
RecordedRequest restRequest = restServer.takeRequest();
209210
assertEquals("POST", restRequest.getMethod());

impl/test/src/test/resources/workflows-samples/openapi/project-post-positive.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ do:
2121
validateOnly: false
2222
notifyMembers: true
2323
lang: en
24-
Authorization: "Bearer eyJhbnNpc2l0b3IuYm9sdXMubWFnbnVz"
24+
Authorization: "Bearer eyJhbnNpc2l0b3IuYm9sdXMubWFnbnVz"
25+
redirect: true

impl/test/src/test/resources/workflows-samples/try-catch-retry-inline.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ do:
1111
with:
1212
method: get
1313
endpoint: http://localhost:9797
14+
redirect: true
1415
catch:
1516
errors:
1617
with:

impl/test/src/test/resources/workflows-samples/try-catch-retry-reusable.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ do:
2121
with:
2222
method: get
2323
endpoint: http://localhost:9797
24+
redirect: true
2425
catch:
2526
errors:
2627
with:

0 commit comments

Comments
 (0)