|  | 
|  | 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 io.serverlessworkflow.api.types.CallOpenAPI; | 
|  | 19 | +import io.serverlessworkflow.api.types.OpenAPIArguments; | 
|  | 20 | +import io.serverlessworkflow.api.types.TaskBase; | 
|  | 21 | +import io.serverlessworkflow.api.types.UriTemplate; | 
|  | 22 | +import io.serverlessworkflow.api.types.WithOpenAPIParameters; | 
|  | 23 | +import io.serverlessworkflow.api.types.Workflow; | 
|  | 24 | +import io.serverlessworkflow.impl.TaskContext; | 
|  | 25 | +import io.serverlessworkflow.impl.WorkflowApplication; | 
|  | 26 | +import io.serverlessworkflow.impl.WorkflowContext; | 
|  | 27 | +import io.serverlessworkflow.impl.WorkflowModel; | 
|  | 28 | +import io.serverlessworkflow.impl.resources.ResourceLoader; | 
|  | 29 | +import io.swagger.v3.oas.models.OpenAPI; | 
|  | 30 | +import io.swagger.v3.parser.OpenAPIV3Parser; | 
|  | 31 | +import jakarta.ws.rs.client.Client; | 
|  | 32 | +import jakarta.ws.rs.client.ClientBuilder; | 
|  | 33 | +import jakarta.ws.rs.client.Invocation; | 
|  | 34 | +import jakarta.ws.rs.client.WebTarget; | 
|  | 35 | +import jakarta.ws.rs.core.MultivaluedMap; | 
|  | 36 | +import java.net.URI; | 
|  | 37 | +import java.util.concurrent.CompletableFuture; | 
|  | 38 | +import java.util.concurrent.atomic.AtomicReference; | 
|  | 39 | + | 
|  | 40 | +public class OpenAPIExecutor implements CallableTask<CallOpenAPI> { | 
|  | 41 | + | 
|  | 42 | +  private static final Client client = ClientBuilder.newClient(); | 
|  | 43 | +  private WebTargetSupplier webTargetSupplier; | 
|  | 44 | +  private RequestSupplier requestSupplier; | 
|  | 45 | +  private final OpenAPIModelConverter converter = new OpenAPIModelConverter() {}; | 
|  | 46 | + | 
|  | 47 | +  @FunctionalInterface | 
|  | 48 | +  private interface WebTargetSupplier { | 
|  | 49 | +    WebTarget apply(); | 
|  | 50 | +  } | 
|  | 51 | + | 
|  | 52 | +  @FunctionalInterface | 
|  | 53 | +  private interface RequestSupplier { | 
|  | 54 | +    WorkflowModel apply( | 
|  | 55 | +        Invocation.Builder request, WorkflowContext workflow, TaskContext task, WorkflowModel node); | 
|  | 56 | +  } | 
|  | 57 | + | 
|  | 58 | +  @Override | 
|  | 59 | +  public void init( | 
|  | 60 | +      CallOpenAPI task, Workflow workflow, WorkflowApplication application, ResourceLoader loader) { | 
|  | 61 | +    OpenAPIArguments args = task.getWith(); | 
|  | 62 | +    WithOpenAPIParameters withParams = args.getParameters(); | 
|  | 63 | + | 
|  | 64 | +    URI uri = getOpenAPIDocumentURI(args.getDocument().getEndpoint().getUriTemplate()); | 
|  | 65 | + | 
|  | 66 | +    OpenAPIV3Parser apiv3Parser = new OpenAPIV3Parser(); | 
|  | 67 | + | 
|  | 68 | +    OpenAPI openAPI = apiv3Parser.read(uri.toString()); | 
|  | 69 | + | 
|  | 70 | +    OpenAPIOperationContext ctx = generateContext(openAPI, args, uri); | 
|  | 71 | + | 
|  | 72 | +    this.webTargetSupplier = | 
|  | 73 | +        () -> { | 
|  | 74 | +          final AtomicReference<WebTarget> webTarget = | 
|  | 75 | +              new AtomicReference<>( | 
|  | 76 | +                  client | 
|  | 77 | +                      .target(openAPI.getServers().get(0).getUrl()) | 
|  | 78 | +                      .path(ctx.buildPath(withParams.getAdditionalProperties()))); | 
|  | 79 | + | 
|  | 80 | +          MultivaluedMap<String, Object> queryParams = | 
|  | 81 | +              ctx.buildQuery(withParams.getAdditionalProperties()); | 
|  | 82 | +          queryParams.forEach( | 
|  | 83 | +              (key, value) -> { | 
|  | 84 | +                for (Object o : value) { | 
|  | 85 | +                  webTarget.set(webTarget.get().queryParam(key, o)); | 
|  | 86 | +                } | 
|  | 87 | +              }); | 
|  | 88 | + | 
|  | 89 | +          return webTarget.get(); | 
|  | 90 | +        }; | 
|  | 91 | + | 
|  | 92 | +    this.requestSupplier = | 
|  | 93 | +        (request, w, taskContext, node) -> { | 
|  | 94 | +          Object response = request.method(ctx.httpMethodName(), node.objectClass()); | 
|  | 95 | +          return converter.toModel(application.modelFactory(), node, response); | 
|  | 96 | +        }; | 
|  | 97 | +  } | 
|  | 98 | + | 
|  | 99 | +  private static OpenAPIOperationContext generateContext( | 
|  | 100 | +      OpenAPI openAPI, OpenAPIArguments args, URI uri) { | 
|  | 101 | +    return openAPI.getPaths().entrySet().stream() | 
|  | 102 | +        .flatMap( | 
|  | 103 | +            pathEntry -> | 
|  | 104 | +                pathEntry.getValue().readOperationsMap().entrySet().stream() | 
|  | 105 | +                    .map( | 
|  | 106 | +                        operationEntry -> | 
|  | 107 | +                            new OpenAPIOperationContext( | 
|  | 108 | +                                operationEntry.getValue().getOperationId(), | 
|  | 109 | +                                pathEntry.getKey(), | 
|  | 110 | +                                operationEntry.getKey(), | 
|  | 111 | +                                operationEntry.getValue()))) | 
|  | 112 | +        .filter(c -> c.operationId().equals(args.getOperationId())) | 
|  | 113 | +        .findFirst() | 
|  | 114 | +        .orElseThrow( | 
|  | 115 | +            () -> | 
|  | 116 | +                new IllegalArgumentException( | 
|  | 117 | +                    "Operation with id " | 
|  | 118 | +                        + args.getOperationId() | 
|  | 119 | +                        + " not found in OpenAPI document " | 
|  | 120 | +                        + uri)); | 
|  | 121 | +  } | 
|  | 122 | + | 
|  | 123 | +  @Override | 
|  | 124 | +  public CompletableFuture<WorkflowModel> apply( | 
|  | 125 | +      WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) { | 
|  | 126 | + | 
|  | 127 | +    return CompletableFuture.supplyAsync( | 
|  | 128 | +        () -> { | 
|  | 129 | +          WebTarget target = webTargetSupplier.apply(); | 
|  | 130 | +          Invocation.Builder request = target.request(); | 
|  | 131 | +          return requestSupplier.apply(request, workflowContext, taskContext, input); | 
|  | 132 | +        }, | 
|  | 133 | +        workflowContext.definition().application().executorService()); | 
|  | 134 | +  } | 
|  | 135 | + | 
|  | 136 | +  @Override | 
|  | 137 | +  public boolean accept(Class<? extends TaskBase> clazz) { | 
|  | 138 | +    return clazz.equals(CallOpenAPI.class); | 
|  | 139 | +  } | 
|  | 140 | + | 
|  | 141 | +  private static URI getOpenAPIDocumentURI(UriTemplate template) { | 
|  | 142 | +    if (template.getLiteralUri() != null) { | 
|  | 143 | +      return template.getLiteralUri(); | 
|  | 144 | +    } else if (template.getLiteralUriTemplate() != null) { | 
|  | 145 | +      // TODO: Support | 
|  | 146 | +      // https://github.com/serverlessworkflow/specification/blob/main/dsl-reference.md#uri-template | 
|  | 147 | +      throw new UnsupportedOperationException( | 
|  | 148 | +          "URI templates with parameters are not supported yet"); | 
|  | 149 | +    } | 
|  | 150 | +    throw new IllegalArgumentException("Invalid UriTemplate definition " + template); | 
|  | 151 | +  } | 
|  | 152 | +} | 
0 commit comments