|
| 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; |
| 17 | + |
| 18 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 19 | +import com.fasterxml.jackson.databind.JsonNode; |
| 20 | +import io.serverlessworkflow.api.types.CallOpenAPI; |
| 21 | +import io.serverlessworkflow.api.types.Endpoint; |
| 22 | +import io.serverlessworkflow.api.types.EndpointUri; |
| 23 | +import io.serverlessworkflow.api.types.OpenAPIArguments; |
| 24 | +import io.serverlessworkflow.api.types.TaskBase; |
| 25 | +import io.serverlessworkflow.api.types.UriTemplate; |
| 26 | +import io.serverlessworkflow.impl.TaskContext; |
| 27 | +import io.serverlessworkflow.impl.WorkflowContext; |
| 28 | +import io.serverlessworkflow.impl.WorkflowDefinition; |
| 29 | +import io.serverlessworkflow.impl.WorkflowError; |
| 30 | +import io.serverlessworkflow.impl.WorkflowException; |
| 31 | +import io.serverlessworkflow.impl.WorkflowUtils; |
| 32 | +import io.serverlessworkflow.impl.expressions.Expression; |
| 33 | +import io.serverlessworkflow.impl.expressions.ExpressionFactory; |
| 34 | +import io.serverlessworkflow.impl.json.JsonUtils; |
| 35 | +import jakarta.ws.rs.client.Client; |
| 36 | +import jakarta.ws.rs.client.ClientBuilder; |
| 37 | +import jakarta.ws.rs.client.WebTarget; |
| 38 | +import java.util.Map; |
| 39 | +import java.util.Optional; |
| 40 | + |
| 41 | +public class OpenAPIExecutor implements CallableTask<CallOpenAPI> { |
| 42 | + private static final Client client = ClientBuilder.newClient(); |
| 43 | + private TargetSupplier targetSupplier; |
| 44 | + |
| 45 | + @FunctionalInterface |
| 46 | + private interface TargetSupplier { |
| 47 | + WebTarget apply(WorkflowContext workflow, TaskContext<?> task, JsonNode node); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void init(CallOpenAPI task, WorkflowDefinition definition) { |
| 52 | + OpenAPIArguments args = task.getWith(); |
| 53 | + this.targetSupplier = getTargetSupplier(args, definition.expressionFactory()); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public JsonNode apply( |
| 58 | + WorkflowContext workflowContext, TaskContext<CallOpenAPI> taskContext, JsonNode input) { |
| 59 | + |
| 60 | + WebTarget target = this.targetSupplier.apply(workflowContext, taskContext, input); |
| 61 | + |
| 62 | + System.out.println("target: " + target.getUri()); |
| 63 | + |
| 64 | + return input; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public boolean accept(Class<? extends TaskBase> clazz) { |
| 69 | + return clazz.isAssignableFrom(CallOpenAPI.class); |
| 70 | + } |
| 71 | + |
| 72 | + private static TargetSupplier getURISupplier(UriTemplate template, String operationId) { |
| 73 | + if (template.getLiteralUri() != null) { |
| 74 | + |
| 75 | + Optional<JsonNode> jsonNode = |
| 76 | + WorkflowUtils.classpathResourceToNode(template.getLiteralUri().toString()); |
| 77 | + |
| 78 | + if (jsonNode.isEmpty()) { |
| 79 | + throw new IllegalArgumentException( |
| 80 | + "Invalid OpenAPI specification " + template.getLiteralUri().toString()); |
| 81 | + } |
| 82 | + |
| 83 | + String host = OpenAPIReader.getHost(jsonNode.get()); |
| 84 | + |
| 85 | + Optional<JsonNode> possibleOperation = |
| 86 | + OpenAPIReader.readOperation(jsonNode.get(), operationId); |
| 87 | + |
| 88 | + if (possibleOperation.isEmpty()) { |
| 89 | + throw new WorkflowException( |
| 90 | + WorkflowError.error(WorkflowError.RUNTIME_TYPE, 400) |
| 91 | + .title("Invalid OpenAPI Specification") |
| 92 | + .details("There is no operation ID " + operationId) |
| 93 | + .build()); |
| 94 | + } |
| 95 | + |
| 96 | + return (w, t, n) -> client.target(host); |
| 97 | + } else if (template.getLiteralUriTemplate() != null) { |
| 98 | + return (w, t, n) -> |
| 99 | + client |
| 100 | + .target(template.getLiteralUriTemplate()) |
| 101 | + .resolveTemplates( |
| 102 | + JsonUtils.mapper().convertValue(n, new TypeReference<Map<String, Object>>() {})); |
| 103 | + } |
| 104 | + throw new IllegalArgumentException("Invalid uritemplate definition " + template); |
| 105 | + } |
| 106 | + |
| 107 | + private static class ExpressionURISupplier implements TargetSupplier { |
| 108 | + private Expression expr; |
| 109 | + |
| 110 | + public ExpressionURISupplier(Expression expr) { |
| 111 | + this.expr = expr; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public WebTarget apply(WorkflowContext workflow, TaskContext<?> task, JsonNode node) { |
| 116 | + return client.target(expr.eval(workflow, task, node).asText()); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private static TargetSupplier getTargetSupplier( |
| 121 | + OpenAPIArguments args, ExpressionFactory expressionFactory) { |
| 122 | + |
| 123 | + Endpoint endpoint = args.getDocument().getEndpoint(); |
| 124 | + String operationId = args.getOperationId(); |
| 125 | + |
| 126 | + if (endpoint.getEndpointConfiguration() != null) { |
| 127 | + EndpointUri uri = endpoint.getEndpointConfiguration().getUri(); |
| 128 | + if (uri.getLiteralEndpointURI() != null) { |
| 129 | + return getURISupplier(uri.getLiteralEndpointURI(), operationId); |
| 130 | + } else if (uri.getExpressionEndpointURI() != null) { |
| 131 | + return new ExpressionURISupplier( |
| 132 | + expressionFactory.getExpression(uri.getExpressionEndpointURI())); |
| 133 | + } |
| 134 | + } else if (endpoint.getRuntimeExpression() != null) { |
| 135 | + return new ExpressionURISupplier( |
| 136 | + expressionFactory.getExpression(endpoint.getRuntimeExpression())); |
| 137 | + } else if (endpoint.getUriTemplate() != null) { |
| 138 | + return getURISupplier(endpoint.getUriTemplate(), operationId); |
| 139 | + } |
| 140 | + throw new IllegalArgumentException("Invalid endpoint definition " + endpoint); |
| 141 | + } |
| 142 | +} |
0 commit comments