|
| 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.openapi; |
| 17 | + |
| 18 | +import io.serverlessworkflow.api.types.CallHTTP; |
| 19 | +import io.serverlessworkflow.api.types.CallOpenAPI; |
| 20 | +import io.serverlessworkflow.api.types.Endpoint; |
| 21 | +import io.serverlessworkflow.api.types.EndpointUri; |
| 22 | +import io.serverlessworkflow.api.types.TaskBase; |
| 23 | +import io.serverlessworkflow.api.types.UriTemplate; |
| 24 | +import io.serverlessworkflow.api.types.Workflow; |
| 25 | +import io.serverlessworkflow.impl.TaskContext; |
| 26 | +import io.serverlessworkflow.impl.WorkflowApplication; |
| 27 | +import io.serverlessworkflow.impl.WorkflowContext; |
| 28 | +import io.serverlessworkflow.impl.WorkflowDefinition; |
| 29 | +import io.serverlessworkflow.impl.WorkflowModel; |
| 30 | +import io.serverlessworkflow.impl.WorkflowValueResolver; |
| 31 | +import io.serverlessworkflow.impl.executors.CallableTask; |
| 32 | +import io.serverlessworkflow.impl.executors.http.HttpExecutor; |
| 33 | +import io.serverlessworkflow.impl.expressions.ExpressionDescriptor; |
| 34 | +import io.serverlessworkflow.impl.expressions.ExpressionFactory; |
| 35 | +import io.serverlessworkflow.impl.resources.ResourceLoader; |
| 36 | +import jakarta.ws.rs.core.UriBuilder; |
| 37 | +import java.net.URI; |
| 38 | +import java.util.Objects; |
| 39 | +import java.util.concurrent.CompletableFuture; |
| 40 | +import java.util.concurrent.ExecutionException; |
| 41 | +import java.util.stream.Collectors; |
| 42 | + |
| 43 | +public class OpenAPIExecutor implements CallableTask<CallOpenAPI> { |
| 44 | + |
| 45 | + private CallOpenAPI task; |
| 46 | + private Workflow workflow; |
| 47 | + private WorkflowDefinition definition; |
| 48 | + private WorkflowApplication application; |
| 49 | + private TargetSupplier targetSupplier; |
| 50 | + |
| 51 | + private ResourceLoader resourceLoader; |
| 52 | + |
| 53 | + private static TargetSupplier getTargetSupplier( |
| 54 | + Endpoint endpoint, ExpressionFactory expressionFactory) { |
| 55 | + if (endpoint.getEndpointConfiguration() != null) { |
| 56 | + EndpointUri uri = endpoint.getEndpointConfiguration().getUri(); |
| 57 | + if (uri.getLiteralEndpointURI() != null) { |
| 58 | + return getURISupplier(uri.getLiteralEndpointURI()); |
| 59 | + } else if (uri.getExpressionEndpointURI() != null) { |
| 60 | + return new ExpressionURISupplier( |
| 61 | + expressionFactory.resolveString( |
| 62 | + ExpressionDescriptor.from(uri.getExpressionEndpointURI()))); |
| 63 | + } |
| 64 | + } else if (endpoint.getRuntimeExpression() != null) { |
| 65 | + return new ExpressionURISupplier( |
| 66 | + expressionFactory.resolveString( |
| 67 | + ExpressionDescriptor.from(endpoint.getRuntimeExpression()))); |
| 68 | + } else if (endpoint.getUriTemplate() != null) { |
| 69 | + return getURISupplier(endpoint.getUriTemplate()); |
| 70 | + } |
| 71 | + throw new IllegalArgumentException("Invalid endpoint definition " + endpoint); |
| 72 | + } |
| 73 | + |
| 74 | + private static TargetSupplier getURISupplier(UriTemplate template) { |
| 75 | + if (template.getLiteralUri() != null) { |
| 76 | + return (w, t, n) -> template.getLiteralUri(); |
| 77 | + } else if (template.getLiteralUriTemplate() != null) { |
| 78 | + return (w, t, n) -> |
| 79 | + UriBuilder.fromUri(template.getLiteralUriTemplate()) |
| 80 | + .resolveTemplates(n.asMap().orElseThrow(), false) |
| 81 | + .build(); |
| 82 | + } |
| 83 | + throw new IllegalArgumentException("Invalid uritemplate definition " + template); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public boolean accept(Class<? extends TaskBase> clazz) { |
| 88 | + return clazz.equals(CallOpenAPI.class); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public CompletableFuture<WorkflowModel> apply( |
| 93 | + WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) { |
| 94 | + |
| 95 | + String operationId = task.getWith().getOperationId(); |
| 96 | + URI openAPIEndpoint = targetSupplier.apply(workflowContext, taskContext, input); |
| 97 | + OpenAPIProcessor processor = new OpenAPIProcessor(operationId, openAPIEndpoint); |
| 98 | + OperationDefinition operation = processor.parse(); |
| 99 | + |
| 100 | + OperationPathResolver pathResolver = |
| 101 | + new OperationPathResolver(operation.getPath(), input.asMap().orElseThrow()); |
| 102 | + URI resolvedPath = pathResolver.passPathParams().apply(workflowContext, taskContext, input); |
| 103 | + |
| 104 | + HttpCallAdapter httpCallAdapter = |
| 105 | + new HttpCallAdapter() |
| 106 | + .auth(task.getWith().getAuthentication()) |
| 107 | + .body(operation.getBody()) |
| 108 | + .contentType(operation.getContentType()) |
| 109 | + .headers( |
| 110 | + operation.getParameters().stream() |
| 111 | + .filter(p -> "header".equals(p.getIn())) |
| 112 | + .collect(Collectors.toUnmodifiableSet())) |
| 113 | + .method(operation.getMethod()) |
| 114 | + .query( |
| 115 | + operation.getParameters().stream() |
| 116 | + .filter(p -> "query".equals(p.getIn())) |
| 117 | + .collect(Collectors.toUnmodifiableSet())) |
| 118 | + .redirect(task.getWith().isRedirect()) |
| 119 | + .target(resolvedPath) |
| 120 | + .workflowParams(task.getWith().getParameters().getAdditionalProperties()); |
| 121 | + |
| 122 | + return CompletableFuture.supplyAsync( |
| 123 | + () -> { |
| 124 | + RuntimeException ex = null; |
| 125 | + for (var server : operation.getServers()) { |
| 126 | + CallHTTP callHTTP = httpCallAdapter.server(server).build(); |
| 127 | + HttpExecutor executor = new HttpExecutor(); |
| 128 | + executor.init(callHTTP, definition); |
| 129 | + |
| 130 | + try { |
| 131 | + return executor.apply(workflowContext, taskContext, input).get(); |
| 132 | + } catch (InterruptedException | RuntimeException | ExecutionException e) { |
| 133 | + ex = new RuntimeException(e); |
| 134 | + } |
| 135 | + } |
| 136 | + Objects.requireNonNull(ex, "Should have at least one exception"); |
| 137 | + throw ex; // if we there, we failed all servers and ex is not null |
| 138 | + }, |
| 139 | + workflowContext.definition().application().executorService()); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void init(CallOpenAPI task, WorkflowDefinition definition) { |
| 144 | + this.task = task; |
| 145 | + this.definition = definition; |
| 146 | + this.workflow = definition.workflow(); |
| 147 | + this.application = definition.application(); |
| 148 | + this.resourceLoader = definition.resourceLoader(); |
| 149 | + |
| 150 | + this.targetSupplier = |
| 151 | + getTargetSupplier( |
| 152 | + task.getWith().getDocument().getEndpoint(), application.expressionFactory()); |
| 153 | + } |
| 154 | + |
| 155 | + public interface TargetSupplier { |
| 156 | + URI apply(WorkflowContext workflow, TaskContext taskContext, WorkflowModel input); |
| 157 | + } |
| 158 | + |
| 159 | + private static class ExpressionURISupplier implements TargetSupplier { |
| 160 | + private WorkflowValueResolver<String> expr; |
| 161 | + |
| 162 | + public ExpressionURISupplier(WorkflowValueResolver<String> expr) { |
| 163 | + this.expr = expr; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public URI apply(WorkflowContext workflow, TaskContext task, WorkflowModel node) { |
| 168 | + return URI.create(expr.apply(workflow, task, node)); |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments