Skip to content

Commit d165101

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

File tree

8 files changed

+87
-14
lines changed

8 files changed

+87
-14
lines changed
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;
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/http/src/main/java/io/serverlessworkflow/impl/executors/http/AbstractHttpExecutorBuilder.java

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

18+
import io.serverlessworkflow.impl.RedirectValidationException;
19+
import io.serverlessworkflow.impl.TaskContext;
1820
import io.serverlessworkflow.impl.WorkflowApplication;
21+
import io.serverlessworkflow.impl.WorkflowError;
22+
import io.serverlessworkflow.impl.WorkflowException;
1923
import io.serverlessworkflow.impl.WorkflowFilter;
2024
import io.serverlessworkflow.impl.WorkflowUtils;
2125
import io.serverlessworkflow.impl.WorkflowValueResolver;
2226
import jakarta.ws.rs.HttpMethod;
2327
import jakarta.ws.rs.client.WebTarget;
28+
import jakarta.ws.rs.core.Response;
2429
import java.net.URI;
2530
import java.util.Map;
2631
import java.util.Optional;
@@ -35,27 +40,64 @@ abstract class AbstractHttpExecutorBuilder {
3540
protected boolean redirect;
3641

3742
protected static RequestSupplier buildRequestSupplier(
38-
String method, Object body, WorkflowApplication application) {
43+
String method, Object body, boolean redirect, WorkflowApplication application) {
3944

4045
switch (method.toUpperCase()) {
4146
case HttpMethod.POST:
4247
WorkflowFilter bodyFilter = WorkflowUtils.buildWorkflowFilter(application, body);
4348
return (request, w, t, node) -> {
4449
HttpModelConverter converter = HttpConverterResolver.converter(w, t);
50+
Response res =
51+
request.buildPost(converter.toEntity(bodyFilter.apply(w, t, node))).invoke();
52+
53+
handleWithRedirect(redirect, t, res);
54+
4555
return w.definition()
4656
.application()
4757
.modelFactory()
48-
.fromAny(
49-
request.post(
50-
converter.toEntity(bodyFilter.apply(w, t, node)), converter.responseType()));
58+
.fromAny(res.readEntity(converter.responseType()));
5159
};
5260
case HttpMethod.GET:
5361
default:
54-
return (request, w, t, n) ->
55-
w.definition()
56-
.application()
57-
.modelFactory()
58-
.fromAny(request.get(HttpConverterResolver.converter(w, t).responseType()));
62+
return (request, w, t, n) -> {
63+
Response res = request.buildGet().invoke();
64+
handleWithRedirect(redirect, t, res);
65+
66+
return w.definition()
67+
.application()
68+
.modelFactory()
69+
.fromAny(res.readEntity(HttpConverterResolver.converter(w, t).responseType()));
70+
};
71+
}
72+
}
73+
74+
private static void handleWithRedirect(boolean redirect, TaskContext t, Response response) {
75+
Response.Status.Family statusFamily = response.getStatusInfo().getFamily();
76+
77+
if (redirect
78+
&& (!statusFamily.equals(Response.Status.Family.SUCCESSFUL)
79+
&& !statusFamily.equals(Response.Status.Family.REDIRECTION))) {
80+
throw new WorkflowException(
81+
WorkflowError.communication(
82+
response.getStatus(),
83+
t,
84+
new RedirectValidationException(
85+
String.format(
86+
"The property 'redirect' is true but received status %d (%s); expected status in the 200-399 range",
87+
response.getStatus(), response.getStatusInfo().getReasonPhrase())))
88+
.build());
89+
}
90+
91+
if (!redirect && !statusFamily.equals(Response.Status.Family.SUCCESSFUL)) {
92+
throw new WorkflowException(
93+
WorkflowError.communication(
94+
response.getStatus(),
95+
t,
96+
new RedirectValidationException(
97+
String.format(
98+
"The property 'redirect' is false but received status %d (%s); expected status in the 200-299 range",
99+
response.getStatus(), response.getStatusInfo().getReasonPhrase())))
100+
.build());
59101
}
60102
}
61103

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)

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)