From cb585321753444e370ef30afc5423577c15c9715 Mon Sep 17 00:00:00 2001 From: Gilles Barbier Date: Thu, 19 Nov 2026 22:18:42 +0100 Subject: [PATCH 1/3] Move reusable SDK modules into common Extract resolved HTTP execution and OpenAPI operation preparation into common/http, common/http-jakarta and common/openapi, with common artifact coordinates, a shared Maven parent and root dependency management. Keep production dependencies directed from impl to common and leave gRPC to its own PR. Keep the JSON/YAML Jackson reader, services, native-image metadata and tests in the existing openworkflow-impl-openapi artifact. Document reusable execution components as the third SDK usage in the root README and update examples and dependencies. Preserve HTTP/OpenAPI behavior, SDK authentication and error policy, multivalued headers and effective JDK request metadata after redirects; Jakarta explicitly reports unavailable final-request metadata. Combine the original extraction and agreed reorganization in one independently usable commit on current main. Retain main's dependency updates and AssertJ migrations. Reserve shared request/response contracts and injectable converters for the next, independently revertible prototype commit. Verify 124 passing targeted JDK/Jakarta HTTP, standalone OpenAPI and SDK compatibility tests with two existing skips. Check production dependency trees, custom engine-free reader usage, SDK reader discovery and packaging, and repository-wide format/style checks before starting the prototype. Keep Jersey test scope local to consumers after moving dependency management. Restore native body preparation before header snapshots, and propagate provider Errors during Jakarta buffering so delivery cannot remain pending. Cover the latter with a regression observed failing before the fix. Remove engine-specific references from the common README examples. Signed-off-by: Gilles Barbier --- README.md | 8 + common/http-jakarta/pom.xml | 45 ++ .../http/jakarta/JakartaHttpTransport.java | 136 ++++ .../jakarta/JakartaHttpTransportTest.java | 313 +++++++++ common/http/README.md | 166 +++++ common/http/pom.xml | 27 + .../sdk/http/HttpRequestData.java | 66 ++ .../sdk/http/HttpRequestEncoding.java | 55 ++ .../sdk/http/HttpResponseData.java | 135 ++++ .../openworkflow/sdk/http/HttpTransport.java | 39 ++ .../sdk/http/JdkHttpTransport.java | 135 ++++ .../sdk/impl/http/URIDecodedBuilder.java | 0 .../sdk/http/HttpRequestDataTest.java | 144 ++++ .../sdk/http/HttpResponseDataTest.java | 282 ++++++++ .../sdk/http/JdkHttpTransportTest.java | 317 +++++++++ common/openapi/README.md | 136 ++++ common/openapi/pom.xml | 26 + .../impl/executors/openapi/OpenAPIClient.java | 116 ++++ .../openapi/OpenAPIPathResolver.java | 618 ++++++++++++++++++ .../executors/openapi/OpenAPIProcessor.java | 6 +- .../executors/openapi/OpenAPIRequest.java | 151 +++++ .../openapi/OperationDefinition.java | 24 +- .../openapi/ParameterDefinition.java | 2 +- .../executors/openapi/UnifiedOpenAPI.java | 0 .../openapi/UnifiedOpenAPIReader.java | 0 .../openapi/CustomOpenAPIReaderTest.java | 58 ++ common/pom.xml | 6 + impl/README.md | 4 + impl/core/pom.xml | 9 + .../executors/openapi/OpenAPIExecutor.java | 92 +-- .../openapi/OpenAPIExecutorBuilder.java | 2 +- .../sdk/impl/http/DefaultHttpRequest.java | 74 +-- .../impl/http/DefaultURITemplateResolver.java | 596 +---------------- ...WorkflowHttpRequestInfoBuilderFactory.java | 18 + .../impl/http/DefaultHttpResponseTest.java | 81 +++ .../impl/http/HttpErrorTranslationTest.java | 51 ++ impl/http/pom.xml | 6 +- .../impl/jakarta/http/JakartaHttpRequest.java | 98 ++- impl/openapi-jackson/pom.xml | 3 + .../jackson/StandaloneOpenAPITest.java | 497 ++++++++++++++ impl/pom.xml | 13 - impl/test/pom.xml | 1 + .../test/HttpAdapterCompatibilityTest.java | 220 +++++++ .../test/OpenAPIAdapterCompatibilityTest.java | 131 ++++ pom.xml | 28 + 45 files changed, 4145 insertions(+), 790 deletions(-) create mode 100644 common/http-jakarta/pom.xml create mode 100644 common/http-jakarta/src/main/java/org/openworkflow/sdk/http/jakarta/JakartaHttpTransport.java create mode 100644 common/http-jakarta/src/test/java/org/openworkflow/sdk/http/jakarta/JakartaHttpTransportTest.java create mode 100644 common/http/README.md create mode 100644 common/http/pom.xml create mode 100644 common/http/src/main/java/org/openworkflow/sdk/http/HttpRequestData.java create mode 100644 common/http/src/main/java/org/openworkflow/sdk/http/HttpRequestEncoding.java create mode 100644 common/http/src/main/java/org/openworkflow/sdk/http/HttpResponseData.java create mode 100644 common/http/src/main/java/org/openworkflow/sdk/http/HttpTransport.java create mode 100644 common/http/src/main/java/org/openworkflow/sdk/http/JdkHttpTransport.java rename {impl/core => common/http}/src/main/java/org/openworkflow/sdk/impl/http/URIDecodedBuilder.java (100%) create mode 100644 common/http/src/test/java/org/openworkflow/sdk/http/HttpRequestDataTest.java create mode 100644 common/http/src/test/java/org/openworkflow/sdk/http/HttpResponseDataTest.java create mode 100644 common/http/src/test/java/org/openworkflow/sdk/http/JdkHttpTransportTest.java create mode 100644 common/openapi/README.md create mode 100644 common/openapi/pom.xml create mode 100644 common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIClient.java create mode 100644 common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIPathResolver.java rename {impl/core => common/openapi}/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIProcessor.java (88%) create mode 100644 common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIRequest.java rename {impl/core => common/openapi}/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OperationDefinition.java (91%) rename {impl/core => common/openapi}/src/main/java/org/openworkflow/sdk/impl/executors/openapi/ParameterDefinition.java (95%) rename {impl/core => common/openapi}/src/main/java/org/openworkflow/sdk/impl/executors/openapi/UnifiedOpenAPI.java (100%) rename {impl/core => common/openapi}/src/main/java/org/openworkflow/sdk/impl/executors/openapi/UnifiedOpenAPIReader.java (100%) create mode 100644 common/openapi/src/test/java/org/openworkflow/sdk/impl/executors/openapi/CustomOpenAPIReaderTest.java create mode 100644 common/pom.xml create mode 100644 impl/core/src/test/java/org/openworkflow/sdk/impl/http/DefaultHttpResponseTest.java create mode 100644 impl/core/src/test/java/org/openworkflow/sdk/impl/http/HttpErrorTranslationTest.java create mode 100644 impl/openapi-jackson/src/test/java/org/openworkflow/sdk/impl/executors/openapi/jackson/StandaloneOpenAPITest.java create mode 100644 impl/test/src/test/java/org/openworkflow/sdk/impl/test/HttpAdapterCompatibilityTest.java create mode 100644 impl/test/src/test/java/org/openworkflow/sdk/impl/test/OpenAPIAdapterCompatibilityTest.java diff --git a/README.md b/README.md index ceb6bd900..0d51ae6e5 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,14 @@ With the SDK you can: * Test your workflow definitions using the reference implementation. +## Three ways to use the SDK + +- Use the generated Java models to read and write specification documents in your own implementation. +- Run workflows with the customizable reference engine, standalone or embedded in an application. +- Reuse execution components from `common/` in another workflow engine, without creating SDK workflow contexts. For example, use `openworkflow-common-openapi` to bind an operation and prepare its request, then send it with `openworkflow-common-http` (JDK) or `openworkflow-common-http-jakarta`. + +Common OpenAPI consumers can supply a `UnifiedOpenAPIReader` of their own. To use the SDK's JSON/YAML Jackson reader, add `org.openworkflow.sdk:openworkflow-impl-openapi` at the same SDK version; this optional reader retains its SDK implementation dependencies. See the [HTTP example](common/http/README.md) and [OpenAPI example](common/openapi/README.md). Production dependencies point from the reference implementation to common components. gRPC reorganization is handled separately. + ## Status | Latest Releases | Conformance to spec version | diff --git a/common/http-jakarta/pom.xml b/common/http-jakarta/pom.xml new file mode 100644 index 000000000..ea51f39ca --- /dev/null +++ b/common/http-jakarta/pom.xml @@ -0,0 +1,45 @@ + + 4.0.0 + + org.openworkflow.sdk + openworkflow-common + 8.0.0-SNAPSHOT + + openworkflow-common-http-jakarta + Open Workflow :: HTTP Client :: Jakarta + + + org.openworkflow.sdk + openworkflow-common-http + + + jakarta.ws.rs + jakarta.ws.rs-api + + + org.glassfish.jersey.core + jersey-client + test + + + org.junit.jupiter + junit-jupiter-engine + test + + + org.assertj + assertj-core + test + + + org.mockito + mockito-core + test + + + com.squareup.okhttp3 + mockwebserver + test + + + diff --git a/common/http-jakarta/src/main/java/org/openworkflow/sdk/http/jakarta/JakartaHttpTransport.java b/common/http-jakarta/src/main/java/org/openworkflow/sdk/http/jakarta/JakartaHttpTransport.java new file mode 100644 index 000000000..15136102b --- /dev/null +++ b/common/http-jakarta/src/main/java/org/openworkflow/sdk/http/jakarta/JakartaHttpTransport.java @@ -0,0 +1,136 @@ +/* + * Copyright 2020-Present The Open Workflow Specification Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openworkflow.sdk.http.jakarta; + +import jakarta.ws.rs.client.Client; +import jakarta.ws.rs.client.ClientBuilder; +import jakarta.ws.rs.client.Entity; +import jakarta.ws.rs.client.Invocation; +import jakarta.ws.rs.client.WebTarget; +import jakarta.ws.rs.core.Response; +import java.util.Objects; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; +import java.util.function.Function; +import org.openworkflow.sdk.http.HttpRequestData; +import org.openworkflow.sdk.http.HttpResponseData; +import org.openworkflow.sdk.http.HttpTransport; + +/** + * Jakarta transport. Redirect, connect timeout and read timeout settings belong to the Jakarta + * client and its provider. The default constructor uses the provider's defaults. A provider is + * required at runtime; this module does not select one for the application. Effective request + * metadata is unavailable: Jakarta REST does not portably expose the final request after + * connector-managed redirects, so buffered responses leave request() empty. + */ +public final class JakartaHttpTransport implements HttpTransport { + + private final Client client; + private final boolean ownsClient; + + /** Creates a client owned by this transport. */ + public JakartaHttpTransport() { + this(ClientBuilder.newClient(), true); + } + + /** Borrows a client, preserving its configured providers, timeouts and redirect behavior. */ + public JakartaHttpTransport(Client client) { + this(client, false); + } + + private JakartaHttpTransport(Client client, boolean ownsClient) { + this.client = Objects.requireNonNull(client, "client"); + this.ownsClient = ownsClient; + } + + @Override + public CompletableFuture execute(HttpRequestData request) { + CompletableFuture result = new CompletableFuture<>(); + exchange( + request, + builder -> { + Object contentType = + request.headers().entrySet().stream() + .filter(e -> e.getKey().equalsIgnoreCase("Content-Type")) + .map(java.util.Map.Entry::getValue) + .findFirst() + .orElse("application/json"); + byte[] body = request.body(); + return CompletableFuture.completedFuture( + body == null ? null : Entity.entity(body, contentType.toString())); + }) + .whenCompleteAsync((response, error) -> { + if (error != null) { + result.completeExceptionally(error); + } else { + // Always consume/close the response, even when delivery has been cancelled. + HttpResponseData buffered; + try (response) { + buffered = + new HttpResponseData( + response.getStatus(), + response.getStringHeaders(), + response.hasEntity() ? response.readEntity(byte[].class) : new byte[0]); + } catch (Exception | Error ex) { + result.completeExceptionally(ex); + return; + } + result.complete(buffered); + } + }); + return result; + } + + /** + * Prepares a native request asynchronously and supplies its entity (null means no entity). The + * request's byte body is ignored by this overload, allowing existing entity converters and + * request decorators to participate. The recipient owns and must close the returned Response. + * Cancelling delivery does not cancel the network call; a response arriving after cancellation is + * closed here. + */ + public CompletableFuture exchange( + HttpRequestData request, + Function>> prepare) { + CompletableFuture result = new CompletableFuture<>(); + WebTarget target = client.target(request.uri()); + for (var entry : request.queryParams().entrySet()) { + target = target.queryParam(entry.getKey(), entry.getValue()); + } + Invocation.Builder builder = target.request(); + request.headers().forEach(builder::header); + prepare + .apply(builder) + .thenCompose(entity -> + entity == null + ? builder.rx().method(request.method()) + : builder.rx().method(request.method(), entity)) + .whenComplete((response, error) -> { + if (error != null) { + result.completeExceptionally(error); + } else if (!result.complete(response)) { + response.close(); + } + }); + return result; + } + + @Override + public void close() { + if (ownsClient) { + client.close(); + } + } +} diff --git a/common/http-jakarta/src/test/java/org/openworkflow/sdk/http/jakarta/JakartaHttpTransportTest.java b/common/http-jakarta/src/test/java/org/openworkflow/sdk/http/jakarta/JakartaHttpTransportTest.java new file mode 100644 index 000000000..f1bdd1126 --- /dev/null +++ b/common/http-jakarta/src/test/java/org/openworkflow/sdk/http/jakarta/JakartaHttpTransportTest.java @@ -0,0 +1,313 @@ +/* + * Copyright 2020-Present The Open Workflow Specification Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openworkflow.sdk.http.jakarta; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import jakarta.ws.rs.client.Client; +import jakarta.ws.rs.client.ClientBuilder; +import jakarta.ws.rs.client.CompletionStageRxInvoker; +import jakarta.ws.rs.client.Invocation; +import jakarta.ws.rs.client.WebTarget; +import jakarta.ws.rs.core.Response; +import java.net.SocketTimeoutException; +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import mockwebserver3.MockResponse; +import mockwebserver3.MockWebServer; +import org.junit.jupiter.api.Test; +import org.openworkflow.sdk.http.HttpRequestData; + +class JakartaHttpTransportTest { + + @Test + void execute_shouldPropagateBufferingErrorsAndCloseTheResponse() { + Client client = mock(Client.class); + WebTarget target = mock(WebTarget.class); + Invocation.Builder builder = mock(Invocation.Builder.class); + CompletionStageRxInvoker invoker = mock(CompletionStageRxInvoker.class); + Response response = mock(Response.class); + AssertionError failure = new AssertionError("provider buffering failure"); + when(client.target(any(URI.class))).thenReturn(target); + when(target.request()).thenReturn(builder); + when(builder.rx()).thenReturn(invoker); + when(invoker.method(anyString())).thenReturn(CompletableFuture.completedFuture(response)); + when(response.hasEntity()).thenThrow(failure); + var result = + new JakartaHttpTransport(client) + .execute( + new HttpRequestData( + URI.create("http://localhost/"), "GET", Map.of(), Map.of(), null)); + assertThatThrownBy(() -> result.get(5, TimeUnit.SECONDS)) + .isInstanceOf(java.util.concurrent.ExecutionException.class) + .hasCause(failure); + verify(response).close(); + } + + @Test + void execute_shouldNotClaimToKnowTheRequestAfterProviderRedirects() throws Exception { + try (MockWebServer server = new MockWebServer(); + JakartaHttpTransport transport = new JakartaHttpTransport()) { + server.start(); + server.enqueue( + new MockResponse.Builder() + .code(303) + .addHeader("Location", "/final?source=redirect") + .build()); + server.enqueue(new MockResponse.Builder().body("final response").build()); + var response = + transport + .execute( + new HttpRequestData( + server.url("/start").uri(), + "POST", + Map.of("X-Request", "sent"), + Map.of(), + "body".getBytes(StandardCharsets.UTF_8))) + .get(5, TimeUnit.SECONDS); + assertThat(response.status()).isEqualTo(200); + assertThat(response.request()).isEmpty(); + assertThat(server.takeRequest(5, TimeUnit.SECONDS).getMethod()).isEqualTo("POST"); + var redirected = server.takeRequest(5, TimeUnit.SECONDS); + assertThat(redirected.getMethod()).isEqualTo("GET"); + assertThat(redirected.getUrl().uri()).isEqualTo(server.url("/final?source=redirect").uri()); + } + } + + @Test + void exchange_shouldPreserveSynchronousPreparationErrors() { + try (JakartaHttpTransport transport = new JakartaHttpTransport()) { + IllegalArgumentException error = new IllegalArgumentException("invalid credentials"); + assertThatThrownBy(() -> + transport.exchange( + new HttpRequestData( + URI.create("http://localhost/"), "GET", Map.of(), Map.of(), null), + builder -> { + throw error; + })) + .isSameAs(error); + } + } + + @Test + void close_shouldCloseAnOwnedClient() { + JakartaHttpTransport transport = new JakartaHttpTransport(); + transport.close(); + assertThatThrownBy(() -> + transport + .execute( + new HttpRequestData( + URI.create("http://localhost/"), "GET", Map.of(), Map.of(), null)) + .get(5, TimeUnit.SECONDS)) + .isInstanceOf(IllegalStateException.class); + } + + @Test + void execute_shouldSendResolvedDataAndReturnBufferedSuccessAndError() throws Exception { + try (MockWebServer server = new MockWebServer(); + JakartaHttpTransport transport = new JakartaHttpTransport()) { + server.start(); + for (int status : new int[] {201, 409}) { + server.enqueue( + new MockResponse.Builder() + .code(status) + .addHeader("X-Result", "one") + .addHeader("X-Result", "two") + .body("body " + status) + .build()); + var response = + transport + .execute( + new HttpRequestData( + server.url("/items?existing=yes").uri(), + "POST", + Map.of( + "X-Request", + "value", + "Content-Type", + "text/plain", + "Authorization", + "Basic resolved"), + Map.of("search", "café + tea"), + "café".getBytes(StandardCharsets.UTF_8))) + .get(5, TimeUnit.SECONDS); + assertThat(response.status()).isEqualTo(status); + assertThat(response.request()).isEmpty(); + assertThat(response.headers().get("x-result")).containsExactly("one", "two"); + assertThat(new String(response.body(), StandardCharsets.UTF_8)).isEqualTo("body " + status); + var recorded = server.takeRequest(5, TimeUnit.SECONDS); + assertThat(recorded.getMethod()).isEqualTo("POST"); + assertThat(recorded.getUrl().queryParameter("search")).isEqualTo("café + tea"); + assertThat(recorded.getUrl().queryParameter("existing")).isEqualTo("yes"); + assertThat(recorded.getHeaders().get("X-Request")).isEqualTo("value"); + assertThat(recorded.getHeaders().get("Authorization")).isEqualTo("Basic resolved"); + assertThat(recorded.getBody().utf8()).isEqualTo("café"); + } + } + } + + @Test + void execute_shouldHonorInjectedReadTimeoutAndKeepClientOpen() throws Exception { + try (MockWebServer server = new MockWebServer(); + Client client = + ClientBuilder.newBuilder().readTimeout(100, TimeUnit.MILLISECONDS).build()) { + server.start(); + server.enqueue( + new MockResponse.Builder().headersDelay(1, TimeUnit.SECONDS).body("late").build()); + HttpRequestData request = + new HttpRequestData(server.url("/").uri(), "GET", Map.of(), Map.of(), null); + JakartaHttpTransport transport = new JakartaHttpTransport(client); + assertThatThrownBy(() -> transport.execute(request).get(5, TimeUnit.SECONDS)) + .hasRootCauseInstanceOf(SocketTimeoutException.class); + transport.close(); + server.enqueue(new MockResponse.Builder().body("still usable").build()); + assertThat( + new JakartaHttpTransport(client).execute(request).get(5, TimeUnit.SECONDS).status()) + .isEqualTo(200); + } + } + + @Test + void cancel_shouldStillSendThePreparedRequestAndKeepSharedClientOpen() throws Exception { + try (MockWebServer server = new MockWebServer(); + Client client = ClientBuilder.newClient()) { + server.start(); + server.enqueue(new MockResponse.Builder().body("late").build()); + server.enqueue(new MockResponse.Builder().body("next").build()); + HttpRequestData request = + new HttpRequestData(server.url("/").uri(), "GET", Map.of(), Map.of(), null); + JakartaHttpTransport transport = new JakartaHttpTransport(client); + CompletableFuture> preparation = new CompletableFuture<>(); + var cancelled = transport.exchange(request, builder -> preparation); + assertThat(cancelled.cancel(true)).isTrue(); + preparation.complete(null); + assertThat(server.takeRequest(5, TimeUnit.SECONDS)).isNotNull(); + assertThat(cancelled.isCancelled()).isTrue(); + transport.close(); + assertThat( + new JakartaHttpTransport(client).execute(request).get(5, TimeUnit.SECONDS).status()) + .isEqualTo(200); + } + } + + @Test + void cancelledDelivery_shouldCloseLateNativeResponses() throws Exception { + assertLateResponseClosed(false); + } + + @Test + void cancelledBufferedDelivery_shouldConsumeAndCloseLateNativeResponses() throws Exception { + assertLateResponseClosed(true); + } + + private void assertLateResponseClosed(boolean buffered) throws Exception { + Client client = mock(Client.class); + WebTarget target = mock(WebTarget.class); + Invocation.Builder builder = mock(Invocation.Builder.class); + CompletionStageRxInvoker invoker = mock(CompletionStageRxInvoker.class); + Response response = mock(Response.class); + CountDownLatch closed = new CountDownLatch(1); + doAnswer( + ignored -> { + closed.countDown(); + return null; + }) + .when(response) + .close(); + CompletableFuture network = new CompletableFuture<>(); + when(client.target(any(URI.class))).thenReturn(target); + when(target.request()).thenReturn(builder); + when(builder.rx()).thenReturn(invoker); + when(invoker.method(anyString())).thenReturn(network); + HttpRequestData request = + new HttpRequestData(URI.create("http://localhost/"), "GET", Map.of(), Map.of(), null); + JakartaHttpTransport transport = new JakartaHttpTransport(client); + CompletableFuture delivery = + buffered + ? transport.execute(request) + : transport.exchange(request, ignored -> CompletableFuture.completedFuture(null)); + if (buffered) { + when(response.getStringHeaders()).thenReturn(new jakarta.ws.rs.core.MultivaluedHashMap<>()); + when(response.hasEntity()).thenReturn(true); + when(response.readEntity(byte[].class)).thenReturn(new byte[] {1, 2}); + } + delivery.cancel(true); + assertThat(network.isCancelled()).isFalse(); + network.complete(response); + assertThat(closed.await(5, TimeUnit.SECONDS)).isTrue(); + verify(response).close(); + if (buffered) { + verify(response).readEntity(byte[].class); + } + } + + @Test + void execute_shouldNotReadTheResponseBodyOnTheCallingThread() throws Exception { + Client client = mock(Client.class); + WebTarget target = mock(WebTarget.class); + Invocation.Builder builder = mock(Invocation.Builder.class); + CompletionStageRxInvoker invoker = mock(CompletionStageRxInvoker.class); + Response response = mock(Response.class); + CountDownLatch releaseBody = new CountDownLatch(1); + when(client.target(any(URI.class))).thenReturn(target); + when(target.request()).thenReturn(builder); + when(builder.rx()).thenReturn(invoker); + when(invoker.method(anyString())).thenReturn(CompletableFuture.completedFuture(response)); + when(response.getStringHeaders()).thenReturn(new jakarta.ws.rs.core.MultivaluedHashMap<>()); + when(response.hasEntity()).thenReturn(true); + when(response.readEntity(byte[].class)) + .thenAnswer(ignored -> { + if (!releaseBody.await(5, TimeUnit.SECONDS)) { + throw new IllegalStateException("Body was never released"); + } + return new byte[] {42}; + }); + try (var caller = Executors.newSingleThreadExecutor()) { + try { + var result = + caller + .submit(() -> + new JakartaHttpTransport(client) + .execute( + new HttpRequestData( + URI.create("http://localhost/"), "GET", Map.of(), Map.of(), null))) + .get(1, TimeUnit.SECONDS); + assertThat(result.isDone()).isFalse(); + var delivered = + result.thenApply(data -> { + verify(response).close(); + return data; + }); + releaseBody.countDown(); + assertThat(delivered.get(5, TimeUnit.SECONDS).body()).containsExactly((byte) 42); + } finally { + releaseBody.countDown(); + } + } + } +} diff --git a/common/http/README.md b/common/http/README.md new file mode 100644 index 000000000..9ed9f651f --- /dev/null +++ b/common/http/README.md @@ -0,0 +1,166 @@ +# HTTP execution without the workflow engine + +Other workflow engines can execute already resolved HTTP requests using +`openworkflow-common-http`. This artifact has no production dependencies. The +optional `openworkflow-common-http-jakarta` artifact adds Jakarta REST support and +depends only on this artifact and the Jakarta REST API. Neither brings in +`openworkflow-impl-core`, workflow models, expression evaluation, or a JSON library. + +```xml + + org.openworkflow.sdk + openworkflow-common-http + 8.0.0-SNAPSHOT + +``` + +Use a long-lived transport and pass serialized bytes, resolved headers and query +parameters. For example, a third-party activity executor can return this future +directly (or await it from a coroutine): + +```java +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import org.openworkflow.sdk.http.*; + +class HttpActivity { + private final HttpTransport transport; + + HttpActivity(HttpTransport transport) { + this.transport = transport; + } + + CompletableFuture execute(URI uri, String resolvedToken) { + return transport.execute(new HttpRequestData( + uri, "POST", + Map.of("Content-Type", "application/json", "Authorization", "Bearer " + resolvedToken), + Map.of("search", "café + tea"), + "{\"name\":\"café\"}".getBytes(StandardCharsets.UTF_8))); + } +} +``` + +Create a `JdkHttpTransport()` at application startup, or inject an existing +`java.net.http.HttpClient` with `new JdkHttpTransport(client)`. The equivalent +Jakarta API is `org.openworkflow.sdk.http.jakarta.JakartaHttpTransport`; add the +`openworkflow-common-http-jakarta` dependency and a Jakarta REST client provider +chosen by your application. The standalone tests run this example's request in a +module without the workflow engine on its classpath. + +`HttpResponseData` exposes the HTTP status, all header values (case-insensitive +lookup), and body bytes, including for non-2xx statuses. HTTP error statuses are +responses, not exceptional completions. Transport failures complete the future +exceptionally with transport exceptions, without task metadata. Bodies are fully +buffered; this API is intended for the same non-streaming usage as the existing +SDK HTTP calls. Request and response byte arrays are defensively copied. +Malformed requests and synchronous preparation failures can throw before a +future is returned, retaining the existing native request validation behavior. + +## Effective request metadata + +`HttpResponseData.request()` is an `Optional`. +When present, it describes the request associated with the final response, +including changes caused by redirects. For example, after a POST receives a 303, +the JDK transport reports the redirected GET and its final URI, including query: + +```java +transport.execute(request).thenAccept(response -> { + response.request().ifPresent(effective -> { + String method = effective.method(); + URI uri = effective.uri(); + Map> headers = effective.headers(); + // Use these values for the DSL response.request fields. + }); +}); +``` + +- **JDK:** snapshots method, URI and headers from + [`HttpResponse.request()`](https://docs.oracle.com/en/java/javase/21/docs/api/java.net.http/java/net/http/HttpResponse.html#request()). + This includes the final query and changes made by native preparation hooks. + Headers are those exposed by `HttpRequest.headers()`; headers added internally + by the client or network stack are not guaranteed to appear. +- **Jakarta:** always returns `Optional.empty()`, including for direct calls. + Jakarta REST's portable `Response` API does not expose the final request. + A [`ClientResponseFilter`](https://jakarta.ee/specifications/restful-ws/4.0/apidocs/jakarta.ws.rs/jakarta/ws/rs/client/clientresponsefilter) + receives a request context, but the API does not guarantee that context reflects + connector-managed redirect changes. Provider-specific response types can expose + additional information; this transport does not depend on those extensions or + reconstruct a final request from the initial one. Tests use Jersey 4.0.3 and + confirm a POST/303/GET exchange with explicitly absent metadata. +- **Third-party transports:** the existing `new HttpResponseData(status, headers, + body)` constructor remains available and leaves metadata absent. Transports + that can observe the final request may use the four-argument constructor with + `Optional.of(new HttpResponseData.RequestMetadata(method, uri, requestHeaders))`. + Absence means unavailable; callers must not substitute the initial request as + if it described the final exchange. + +Metadata owns an immutable snapshot of its header map and each value list. +Header lookup is case-insensitive; the request metadata and response records use +case-insensitive header-name equality and hashing. Case-equivalent entries +are merged without dropping values, retaining input iteration and value order. +Response equality and hashing include the optional metadata and body contents. +Defensive body copies and the legacy constructor's behavior remain available. +The transport adds no header logging, and metadata string representations omit +the request URI and headers because they can contain credentials. + +## Workflow boundary and compatibility + +The existing `WorkflowHttpRequestFactory`, `WorkflowHttpRequest`, and response +entry points remain available. The JDK and Jakarta implementations delegate to +these transports. Custom workflow transport factories continue to work without +implementing a new SPI. The existing ServiceLoader entries remain unchanged. +Jakarta client suppliers, ordered request decorators, and model converters still +run in the workflow adapter, including the Jackson converter. + +The workflow adapter evaluates expressions, resolves resource/secret references +and authentication policies, serializes workflow models, converts responses, +updates `TaskContext` authorization, and attaches task positions to errors. +Basic, Bearer, Digest, OAuth2 and OpenID providers keep their existing paths; +their auxiliary HTTP requests use the same delegated transports. Standalone +callers supply resolved authorization headers, or use the narrow native request +preparation hooks to obtain headers asynchronously. Workflow authentication +policy resolution is not exported as part of the standalone API. + +JDK query/form encoding and default JSON content type retain the existing rules. +Jakarta uses its client's query/entity encoding and registered providers. +The adapters retain the SDK's content-type conversion and `with.output` handling +for content, response envelopes, and raw HTTP responses. The JDK `exchange` hook +accepts a native body publisher and retains the response's protocol version, so +custom body serialization and raw HTTP/2 response formatting stay in the adapter. +Redirects follow the configured transport client: the default JDK client uses +`Redirect.NORMAL`, and Jakarta uses the provider's defaults. The existing SDK +`redirect` status validation stays in the workflow adapter; this extraction does +not change that behavior, `with.output`, or retries. + +## Lifetimes, deadlines and cancellation + +- A transport created with its no-argument constructor owns its client. Close it + at application shutdown after outstanding work finishes. Closing may wait for + active requests according to the client implementation. +- An injected client is borrowed. Neither a call nor `transport.close()` closes + it. The caller remains responsible for the client and any supplied executor. +- `execute` returns a buffered response with no resources to close. Jakarta + consumes and closes its native response even when delivery is cancelled. Body + buffering runs on the default `CompletableFuture` asynchronous executor so an + already available native response does not block the calling thread on reads. +- Jakarta's advanced `exchange` hook returns a native `Response` to preserve + custom entity converters. The recipient must close that response. Responses + that arrive after the returned future has been cancelled are closed by the + transport. +- JDK connect timeouts come from the injected client. The overload + `execute(request, Duration)` additionally sets the native JDK request timeout; + its scope follows `HttpRequest.Builder.timeout`, not an independent deadline + for every body read. Jakarta connect/read timeouts come from `ClientBuilder` + and its provider. No new default timeouts are imposed on existing SDK calls. +- Cancelling the returned future abandons delivery only. It does **not** cancel + preparation, interrupt the network exchange, or roll back server effects. + Tests verify that requests can still reach the local server after cancellation + and that late Jakarta responses are closed. A coroutine cancelling its await + therefore must not assume that the remote operation was stopped. +- `orTimeout` or a timeout while awaiting a future is a delivery deadline, not a + network timeout. Configure transport timeouts to bound network resource use. + +This prepares HTTP reuse by other workflow engines. Streaming, network abort guarantees, authentication-policy portability, +and normalization of transport-specific HTTP behavior remain outside this API. diff --git a/common/http/pom.xml b/common/http/pom.xml new file mode 100644 index 000000000..0f6437908 --- /dev/null +++ b/common/http/pom.xml @@ -0,0 +1,27 @@ + + 4.0.0 + + org.openworkflow.sdk + openworkflow-common + 8.0.0-SNAPSHOT + + openworkflow-common-http + Open Workflow :: HTTP Client + + + org.junit.jupiter + junit-jupiter-engine + test + + + org.assertj + assertj-core + test + + + com.squareup.okhttp3 + mockwebserver + test + + + diff --git a/common/http/src/main/java/org/openworkflow/sdk/http/HttpRequestData.java b/common/http/src/main/java/org/openworkflow/sdk/http/HttpRequestData.java new file mode 100644 index 000000000..7ee231a72 --- /dev/null +++ b/common/http/src/main/java/org/openworkflow/sdk/http/HttpRequestData.java @@ -0,0 +1,66 @@ +/* + * Copyright 2020-Present The Open Workflow Specification Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openworkflow.sdk.http; + +import java.net.URI; +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Objects; + +/** + * A resolved request. Bodies are already serialized; authentication is supplied as headers or + * through a transport's asynchronous preparation hook. Header and query values retain each + * transport's existing conversion rules. A null body means no entity. Equality and hashing use the + * body's byte contents, while access to the body remains defensively copied. + */ +public record HttpRequestData( + URI uri, + String method, + Map headers, + Map queryParams, + byte[] body) { + + public HttpRequestData { + Objects.requireNonNull(uri, "uri"); + Objects.requireNonNull(method, "method"); + headers = Collections.unmodifiableMap(new LinkedHashMap<>(headers)); + queryParams = Collections.unmodifiableMap(new LinkedHashMap<>(queryParams)); + body = body == null ? null : body.clone(); + } + + @Override + public byte[] body() { + return body == null ? null : body.clone(); + } + + @Override + public boolean equals(Object object) { + return this == object + || object instanceof HttpRequestData other + && uri.equals(other.uri) + && method.equals(other.method) + && headers.equals(other.headers) + && queryParams.equals(other.queryParams) + && Arrays.equals(body, other.body); + } + + @Override + public int hashCode() { + return 31 * Objects.hash(uri, method, headers, queryParams) + Arrays.hashCode(body); + } +} diff --git a/common/http/src/main/java/org/openworkflow/sdk/http/HttpRequestEncoding.java b/common/http/src/main/java/org/openworkflow/sdk/http/HttpRequestEncoding.java new file mode 100644 index 000000000..908950e04 --- /dev/null +++ b/common/http/src/main/java/org/openworkflow/sdk/http/HttpRequestEncoding.java @@ -0,0 +1,55 @@ +/* + * Copyright 2020-Present The Open Workflow Specification Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openworkflow.sdk.http; + +import java.net.URI; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; +import org.openworkflow.sdk.impl.http.URIDecodedBuilder; + +/** The SDK's existing JDK query and form encoding rules. */ +public final class HttpRequestEncoding { + + public static String encodeForm(Object part) { + return part == null ? "" : URLEncoder.encode(part.toString(), StandardCharsets.UTF_8); + } + + public static String encodeQuery(Object part) { + return encodeForm(part).replace("+", "%20"); + } + + public static String encodeMap(Map map, Function encoder) { + return map.entrySet().stream() + .map(entry -> encoder.apply(entry.getKey()) + "=" + encoder.apply(entry.getValue())) + .collect(Collectors.joining("&")); + } + + public static URI addQueryParams(URI uri, Map queryParams) { + if (queryParams.isEmpty()) { + return uri; + } + String addedQuery = encodeMap(queryParams, HttpRequestEncoding::encodeQuery); + String rawQuery = uri.getRawQuery(); + return new URIDecodedBuilder(uri) + .query(rawQuery == null || rawQuery.isEmpty() ? addedQuery : rawQuery + "&" + addedQuery) + .build(); + } + + private HttpRequestEncoding() {} +} diff --git a/common/http/src/main/java/org/openworkflow/sdk/http/HttpResponseData.java b/common/http/src/main/java/org/openworkflow/sdk/http/HttpResponseData.java new file mode 100644 index 000000000..22d4931e3 --- /dev/null +++ b/common/http/src/main/java/org/openworkflow/sdk/http/HttpResponseData.java @@ -0,0 +1,135 @@ +/* + * Copyright 2020-Present The Open Workflow Specification Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openworkflow.sdk.http; + +import java.net.URI; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.TreeMap; + +/** + * A fully buffered response, including non-2xx responses. It owns no open network resources. + * Equality and hashing use body contents and case-insensitive header names; header value order + * remains significant. Case-equivalent header names are merged in the input map's iteration order. + */ +public record HttpResponseData( + int status, Map> headers, byte[] body, Optional request) { + + /** For transports that cannot report the effective request associated with this response. */ + public HttpResponseData(int status, Map> headers, byte[] body) { + this(status, headers, body, Optional.empty()); + } + + public HttpResponseData { + headers = copyHeaders(headers); + body = body.clone(); + request = Objects.requireNonNull(request, "request"); + } + + private static Map> copyHeaders(Map> headers) { + Map> copy = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + headers.forEach( + (name, values) -> copy.computeIfAbsent(name, key -> new ArrayList<>()).addAll(values)); + copy.replaceAll((name, values) -> List.copyOf(values)); + return Collections.unmodifiableMap(copy); + } + + @Override + public byte[] body() { + return body.clone(); + } + + @Override + public boolean equals(Object object) { + return this == object + || object instanceof HttpResponseData other + && status == other.status + && headers.equals(other.headers) + && Arrays.equals(body, other.body) + && request.equals(other.request); + } + + @Override + public int hashCode() { + int hash = 31 * (31 * Integer.hashCode(status) + headersHash(headers)) + Arrays.hashCode(body); + return 31 * hash + request.hashCode(); + } + + private static int headersHash(Map> headers) { + // TreeMap compares names case-insensitively, but its default hash uses original key casing. + int headersHash = 0; + for (var header : headers.entrySet()) { + headersHash += headerNameHash(header.getKey()) ^ header.getValue().hashCode(); + } + return headersHash; + } + + private static int headerNameHash(String name) { + int hash = 0; + for (int offset = 0; offset < name.length(); ) { + int codePoint = name.codePointAt(offset); + // Match String.CASE_INSENSITIVE_ORDER without locale-dependent or expanding case mappings. + hash = 31 * hash + Character.toLowerCase(Character.toUpperCase(codePoint)); + offset += Character.charCount(codePoint); + } + return hash; + } + + /** Returns the first header value, or null when absent. */ + public String firstHeader(String name) { + List values = headers.get(name); + return values == null || values.isEmpty() ? null : values.getFirst(); + } + + /** + * Immutable transport-visible request associated with the final response, after any redirects. + * The URI includes its query. Headers preserve all visible values, with case-insensitive names; + * they need not include headers added internally by the HTTP client. The URI and headers are + * omitted from toString because they may contain credentials. + */ + public record RequestMetadata(String method, URI uri, Map> headers) { + + public RequestMetadata { + method = Objects.requireNonNull(method, "method"); + uri = Objects.requireNonNull(uri, "uri"); + headers = copyHeaders(headers); + } + + @Override + public boolean equals(Object object) { + return this == object + || object instanceof RequestMetadata other + && method.equals(other.method) + && uri.equals(other.uri) + && headers.equals(other.headers); + } + + @Override + public int hashCode() { + return 31 * Objects.hash(method, uri) + headersHash(headers); + } + + @Override + public String toString() { + return "RequestMetadata[method=" + method + ", uri=, headers=]"; + } + } +} diff --git a/common/http/src/main/java/org/openworkflow/sdk/http/HttpTransport.java b/common/http/src/main/java/org/openworkflow/sdk/http/HttpTransport.java new file mode 100644 index 000000000..09abf572a --- /dev/null +++ b/common/http/src/main/java/org/openworkflow/sdk/http/HttpTransport.java @@ -0,0 +1,39 @@ +/* + * Copyright 2020-Present The Open Workflow Specification Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openworkflow.sdk.http; + +import java.util.concurrent.CompletableFuture; + +/** Executes resolved HTTP requests without a workflow engine. */ +public interface HttpTransport extends AutoCloseable { + + /** + * Returns a buffered response for every HTTP status; transport failures complete exceptionally + * with the underlying transport error. Cancelling the returned future abandons delivery, not the + * network exchange. The exchange may still reach the server and complete. Buffered responses need + * no close. Invalid requests and synchronous preparation failures may throw before returning a + * future, following native request validation behavior. + * + *

The response's optional request metadata describes the effective request associated with the + * final response, including redirects, when observable by the transport. An empty optional means + * it is unavailable; callers must not substitute the initial request as if it were the final one. + */ + CompletableFuture execute(HttpRequestData request); + + /** Closes clients created by this transport; injected clients remain owned by the caller. */ + @Override + void close(); +} diff --git a/common/http/src/main/java/org/openworkflow/sdk/http/JdkHttpTransport.java b/common/http/src/main/java/org/openworkflow/sdk/http/JdkHttpTransport.java new file mode 100644 index 000000000..17a9d20b2 --- /dev/null +++ b/common/http/src/main/java/org/openworkflow/sdk/http/JdkHttpTransport.java @@ -0,0 +1,135 @@ +/* + * Copyright 2020-Present The Open Workflow Specification Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openworkflow.sdk.http; + +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.time.Duration; +import java.util.Objects; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; +import java.util.function.Function; + +/** JDK HTTP transport. Redirects and connect timeouts follow the configured client. */ +public final class JdkHttpTransport implements HttpTransport { + + private final HttpClient client; + private final boolean ownsClient; + + /** Creates an owned client with the SDK's existing NORMAL redirect policy and no timeout. */ + public JdkHttpTransport() { + this(HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NORMAL).build(), true); + } + + /** Borrows a client; closing this transport never closes that client. */ + public JdkHttpTransport(HttpClient client) { + this(client, false); + } + + private JdkHttpTransport(HttpClient client, boolean ownsClient) { + this.client = Objects.requireNonNull(client, "client"); + this.ownsClient = ownsClient; + } + + @Override + public CompletableFuture execute(HttpRequestData request) { + return execute(request, builder -> CompletableFuture.completedFuture(null)); + } + + /** + * Sets a JDK request timeout, covering response receipt as defined by + * HttpRequest.Builder.timeout. This is separate from a caller timing out its wait on the returned + * future. + */ + public CompletableFuture execute(HttpRequestData request, Duration timeout) { + return execute( + request, + builder -> { + builder.timeout(timeout); + return CompletableFuture.completedFuture(null); + }); + } + + /** + * Prepares the native request asynchronously after headers and body are set, before sending. This + * narrow hook supports asynchronous authentication without requiring a service registry. + * Cancellation has the same delivery-only semantics as execute(HttpRequestData). + */ + public CompletableFuture execute( + HttpRequestData request, + Function> prepare) { + byte[] body = request.body(); + return exchange( + request, + body == null + ? HttpRequest.BodyPublishers.noBody() + : HttpRequest.BodyPublishers.ofByteArray(body), + prepare) + .thenApply(response -> { + HttpRequest effectiveRequest = response.request(); + return new HttpResponseData( + response.statusCode(), + response.headers().map(), + response.body(), + Optional.of( + new HttpResponseData.RequestMetadata( + effectiveRequest.method(), + effectiveRequest.uri(), + effectiveRequest.headers().map()))); + }); + } + + /** + * Executes with a native body publisher instead of the request's byte body. This preserves custom + * serialization hooks and returns the native HTTP metadata, including protocol version. The body + * is fully buffered before completion, with no response resources to close. Cancellation abandons + * delivery only, as with execute(HttpRequestData). + */ + public CompletableFuture> exchange( + HttpRequestData request, + HttpRequest.BodyPublisher body, + Function> prepare) { + HttpRequest.Builder builder = + HttpRequest.newBuilder() + .method(request.method(), body) + .uri(HttpRequestEncoding.addQueryParams(request.uri(), request.queryParams())) + .header("Content-Type", "application/json"); + request.headers().forEach((name, value) -> builder.setHeader(name, value.toString())); + CompletableFuture> delivery = new CompletableFuture<>(); + // Do not expose the preparation/send stage: cancelling it can skip the send callback. + prepare + .apply(builder) + .thenCompose( + ignored -> client.sendAsync(builder.build(), HttpResponse.BodyHandlers.ofByteArray())) + .whenComplete((response, error) -> { + if (error != null) { + delivery.completeExceptionally(error); + } else { + delivery.complete(response); + } + }); + return delivery; + } + + @Override + public void close() { + if (ownsClient) { + client.close(); + } + } +} diff --git a/impl/core/src/main/java/org/openworkflow/sdk/impl/http/URIDecodedBuilder.java b/common/http/src/main/java/org/openworkflow/sdk/impl/http/URIDecodedBuilder.java similarity index 100% rename from impl/core/src/main/java/org/openworkflow/sdk/impl/http/URIDecodedBuilder.java rename to common/http/src/main/java/org/openworkflow/sdk/impl/http/URIDecodedBuilder.java diff --git a/common/http/src/test/java/org/openworkflow/sdk/http/HttpRequestDataTest.java b/common/http/src/test/java/org/openworkflow/sdk/http/HttpRequestDataTest.java new file mode 100644 index 000000000..903f300ed --- /dev/null +++ b/common/http/src/test/java/org/openworkflow/sdk/http/HttpRequestDataTest.java @@ -0,0 +1,144 @@ +/* + * Copyright 2020-Present The Open Workflow Specification Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openworkflow.sdk.http; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.net.URI; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; +import org.junit.jupiter.api.Test; + +class HttpRequestDataTest { + + private static final URI URI_VALUE = URI.create("http://localhost/items"); + + private static HttpRequestData request(byte[] body) { + return new HttpRequestData( + URI_VALUE, "POST", Map.of("X-Request", "value"), Map.of("page", 1), body); + } + + @Test + void equalsAndHashCode_shouldCompareBodyContents() { + var first = request(new byte[] {1, 2, 3}); + var second = request(new byte[] {1, 2, 3}); + var third = request(new byte[] {1, 2, 3}); + assertThat(first).isEqualTo(first).isEqualTo(second).isEqualTo(third); + assertThat(second).isEqualTo(first).isEqualTo(third); + assertThat(first.hashCode()).isEqualTo(second.hashCode()).isEqualTo(third.hashCode()); + assertThat(first).isNotEqualTo(null).isNotEqualTo("not a request"); + } + + @Test + void recordCopy_shouldRemainEqualAndUsableAsAHashKey() { + var original = request(new byte[] {1, 2, 3}); + var copy = + new HttpRequestData( + original.uri(), + original.method(), + original.headers(), + original.queryParams(), + original.body()); + assertThat(copy).isEqualTo(original); + assertThat(copy.hashCode()).isEqualTo(original.hashCode()); + Map indexed = new HashMap<>(); + indexed.put(original, "stored"); + assertThat(indexed.get(copy)).isEqualTo("stored"); + } + + @Test + void equals_shouldIncludeEveryComponent() { + var original = request(new byte[] {1, 2, 3}); + assertThat(original) + .isNotEqualTo( + new HttpRequestData( + URI.create("http://localhost/other"), + original.method(), + original.headers(), + original.queryParams(), + original.body())); + assertThat(original) + .isNotEqualTo( + new HttpRequestData( + original.uri(), + "PUT", + original.headers(), + original.queryParams(), + original.body())); + assertThat(original) + .isNotEqualTo( + new HttpRequestData( + original.uri(), + original.method(), + Map.of("X-Request", "other"), + original.queryParams(), + original.body())); + assertThat(original) + .isNotEqualTo( + new HttpRequestData( + original.uri(), + original.method(), + original.headers(), + Map.of("page", 2), + original.body())); + assertThat(original).isNotEqualTo(request(new byte[] {1, 2, 4})); + } + + @Test + void equalsAndHashCode_shouldDistinguishAbsentAndEmptyBodies() { + var absent = request(null); + var absentCopy = request(null); + var empty = request(new byte[0]); + var emptyCopy = request(new byte[0]); + assertThat(absent).isEqualTo(absentCopy).isNotEqualTo(empty); + assertThat(absent.hashCode()).isEqualTo(absentCopy.hashCode()); + assertThat(empty).isEqualTo(emptyCopy); + assertThat(empty.hashCode()).isEqualTo(emptyCopy.hashCode()); + assertThat(new HashSet<>(Set.of(absent, empty)).contains(emptyCopy)).isTrue(); + } + + @Test + void bodyCopies_shouldKeepHashKeysStable() { + byte[] source = {1, 2, 3}; + var original = request(source); + var equivalent = request(new byte[] {1, 2, 3}); + Set keys = new HashSet<>(Set.of(original)); + int hash = original.hashCode(); + source[0] = 9; + original.body()[1] = 9; + assertThat(original.body()).containsExactly((byte) 1, (byte) 2, (byte) 3); + assertThat(original).isEqualTo(equivalent); + assertThat(original.hashCode()).isEqualTo(hash).isEqualTo(equivalent.hashCode()); + assertThat(keys.contains(equivalent)).isTrue(); + } + + @Test + void equalsAndHashCode_shouldIgnoreMapInsertionOrder() { + Map forward = new LinkedHashMap<>(); + forward.put("first", 1); + forward.put("second", 2); + Map reverse = new LinkedHashMap<>(); + reverse.put("second", 2); + reverse.put("first", 1); + var first = new HttpRequestData(URI_VALUE, "POST", forward, reverse, new byte[] {1}); + var second = new HttpRequestData(URI_VALUE, "POST", reverse, forward, new byte[] {1}); + assertThat(first).isEqualTo(second); + assertThat(first.hashCode()).isEqualTo(second.hashCode()); + } +} diff --git a/common/http/src/test/java/org/openworkflow/sdk/http/HttpResponseDataTest.java b/common/http/src/test/java/org/openworkflow/sdk/http/HttpResponseDataTest.java new file mode 100644 index 000000000..bfad33354 --- /dev/null +++ b/common/http/src/test/java/org/openworkflow/sdk/http/HttpResponseDataTest.java @@ -0,0 +1,282 @@ +/* + * Copyright 2020-Present The Open Workflow Specification Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openworkflow.sdk.http; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.net.URI; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import org.junit.jupiter.api.Test; + +class HttpResponseDataTest { + + @Test + void thirdPartyTransport_shouldRetainTheLegacyConstructorWithExplicitlyAbsentMetadata() { + HttpTransport transport = + new HttpTransport() { + @Override + public CompletableFuture execute(HttpRequestData request) { + return CompletableFuture.completedFuture( + new HttpResponseData( + 200, Map.of("X-Response", List.of("third-party")), new byte[0])); + } + + @Override + public void close() {} + }; + var response = + transport + .execute( + new HttpRequestData( + URI.create("https://example.test/initial"), "POST", Map.of(), Map.of(), null)) + .join(); + assertThat(response.status()).isEqualTo(200); + assertThat(response.request()).isEmpty(); + assertThat(response.firstHeader("X-Response")).isEqualTo("third-party"); + assertThat(response) + .isEqualTo(new HttpResponseData(200, response.headers(), new byte[0], Optional.empty())); + } + + @Test + void requestMetadata_shouldSnapshotAllHeaderValuesAndKeepHashKeysStable() { + var values = new ArrayList<>(List.of("one", "two")); + Map> source = new LinkedHashMap<>(); + source.put("X-Request", values); + source.put("x-request", List.of("three", "one")); + var original = + new HttpResponseData.RequestMetadata( + "GET", URI.create("https://example.test/final?q=yes"), source); + var equivalent = + new HttpResponseData.RequestMetadata( + "GET", original.uri(), Map.of("X-REQUEST", List.of("one", "two", "three", "one"))); + Set keys = new HashSet<>(Set.of(original)); + int hash = original.hashCode(); + values.clear(); + source.clear(); + assertThat(original.headers().keySet()).containsExactly("X-Request"); + assertThat(original.headers().get("x-request")).containsExactly("one", "two", "three", "one"); + assertThatThrownBy(() -> original.headers().put("Other", List.of("value"))) + .isInstanceOf(UnsupportedOperationException.class); + assertThatThrownBy(() -> original.headers().get("X-Request").clear()) + .isInstanceOf(UnsupportedOperationException.class); + assertThat(original).isEqualTo(equivalent); + assertThat(equivalent).isEqualTo(original); + assertThat(original.hashCode()).isEqualTo(hash).isEqualTo(equivalent.hashCode()); + assertThat(keys.contains(equivalent)).isTrue(); + } + + @Test + void requestMetadata_shouldParticipateInResponseEqualityAndRecordCopies() { + var request = + new HttpResponseData.RequestMetadata( + "GET", + URI.create("https://example.test/final?source=redirect"), + Map.of("X-Request", List.of("one", "two"))); + byte[] body = {1, 2}; + var original = new HttpResponseData(200, Map.of(), body, Optional.of(request)); + var copy = + new HttpResponseData( + original.status(), original.headers(), original.body(), original.request()); + var equivalent = + new HttpResponseData( + 200, + Map.of(), + new byte[] {1, 2}, + Optional.of( + new HttpResponseData.RequestMetadata( + "GET", request.uri(), Map.of("x-request", List.of("one", "two"))))); + int hash = original.hashCode(); + body[0] = 9; + original.body()[1] = 9; + assertThat(original).isEqualTo(copy).isEqualTo(equivalent); + assertThat(original.hashCode()) + .isEqualTo(hash) + .isEqualTo(copy.hashCode()) + .isEqualTo(equivalent.hashCode()); + assertThat(new HashSet<>(Set.of(original)).contains(equivalent)).isTrue(); + assertThat(original.body()).containsExactly((byte) 1, (byte) 2); + assertThat(original).isNotEqualTo(new HttpResponseData(200, Map.of(), new byte[] {1, 2})); + for (var other : + List.of( + new HttpResponseData.RequestMetadata("POST", request.uri(), request.headers()), + new HttpResponseData.RequestMetadata( + "GET", URI.create("https://example.test/initial"), request.headers()), + new HttpResponseData.RequestMetadata( + "GET", request.uri(), Map.of("X-Request", List.of("two", "one"))))) { + assertThat(request).isNotEqualTo(other); + assertThat(original) + .isNotEqualTo(new HttpResponseData(200, Map.of(), new byte[] {1, 2}, Optional.of(other))); + } + } + + @Test + void requestMetadata_shouldOmitRequestCredentialsFromStringRepresentations() { + var request = + new HttpResponseData.RequestMetadata( + "GET", + URI.create("https://example.test/?apiKey=query-credential"), + Map.of("Authorization", List.of("sensitive-credential"))); + assertThat(request.toString()).doesNotContain("sensitive-credential", "query-credential"); + assertThat(new HttpResponseData(200, Map.of(), new byte[0], Optional.of(request)).toString()) + .doesNotContain("sensitive-credential", "query-credential"); + } + + @Test + void headers_shouldMergeCaseEquivalentNamesWithoutLosingValues() { + var cookies = new ArrayList<>(List.of("session=abc", "theme=dark")); + Map> headers = new LinkedHashMap<>(); + headers.put("Set-Cookie", cookies); + headers.put("X-Result", List.of("ok")); + headers.put("set-cookie", List.of("csrf=xyz")); + headers.put("SET-COOKIE", List.of("session=abc")); + + var response = new HttpResponseData(200, headers, new byte[0]); + cookies.clear(); + headers.clear(); + + assertThat(response.headers()).hasSize(2); + assertThat(response.headers().keySet()).containsExactly("Set-Cookie", "X-Result"); + assertThat(response.headers().get("sEt-CoOkIe")) + .containsExactly("session=abc", "theme=dark", "csrf=xyz", "session=abc"); + assertThat(response.firstHeader("SET-cookie")).isEqualTo("session=abc"); + assertThat(response.firstHeader("x-result")).isEqualTo("ok"); + assertThatThrownBy(() -> response.headers().get("set-cookie").add("another=value")) + .isInstanceOf(UnsupportedOperationException.class); + assertThatThrownBy(() -> response.headers().put("Other", List.of("value"))) + .isInstanceOf(UnsupportedOperationException.class); + + var equivalent = + new HttpResponseData( + 200, + Map.of( + "set-cookie", List.of("session=abc", "theme=dark", "csrf=xyz", "session=abc"), + "x-result", List.of("ok")), + new byte[0]); + assertThat(response).isEqualTo(equivalent); + assertThat(response.hashCode()).isEqualTo(equivalent.hashCode()); + } + + private static HttpResponseData response(byte[] body) { + return new HttpResponseData(200, Map.of("X-Result", List.of("one", "two")), body); + } + + @Test + void equalsAndHashCode_shouldCompareBodyContents() { + var first = response(new byte[] {1, 2, 3}); + var second = response(new byte[] {1, 2, 3}); + var third = response(new byte[] {1, 2, 3}); + assertThat(first).isEqualTo(first).isEqualTo(second).isEqualTo(third); + assertThat(second).isEqualTo(first).isEqualTo(third); + assertThat(first.hashCode()).isEqualTo(second.hashCode()).isEqualTo(third.hashCode()); + assertThat(first).isNotEqualTo(null).isNotEqualTo("not a response"); + } + + @Test + void recordCopy_shouldRemainEqualAndUsableAsAHashKey() { + var original = response(new byte[] {1, 2, 3}); + var copy = new HttpResponseData(original.status(), original.headers(), original.body()); + assertThat(copy).isEqualTo(original); + assertThat(copy.hashCode()).isEqualTo(original.hashCode()); + assertThat(new HashSet<>(Set.of(original)).contains(copy)).isTrue(); + } + + @Test + void equals_shouldIncludeEveryComponent() { + var original = response(new byte[] {1, 2, 3}); + assertThat(original) + .isNotEqualTo(new HttpResponseData(201, original.headers(), original.body())); + assertThat(original) + .isNotEqualTo( + new HttpResponseData(200, Map.of("X-Other", List.of("one", "two")), original.body())); + assertThat(original) + .isNotEqualTo( + new HttpResponseData(200, Map.of("X-Result", List.of("two", "one")), original.body())); + assertThat(original).isNotEqualTo(response(new byte[] {1, 2, 4})); + } + + @Test + void equalsAndHashCode_shouldSupportEmptyBodies() { + var first = response(new byte[0]); + var second = response(first.body()); + assertThat(first).isEqualTo(second); + assertThat(first.hashCode()).isEqualTo(second.hashCode()); + assertThat(new HashSet<>(Set.of(first)).contains(second)).isTrue(); + } + + @Test + void bodyCopies_shouldKeepHashKeysStable() { + byte[] source = {1, 2, 3}; + var original = response(source); + var equivalent = response(new byte[] {1, 2, 3}); + Set keys = new HashSet<>(Set.of(original)); + int hash = original.hashCode(); + source[0] = 9; + original.body()[1] = 9; + assertThat(original.body()).containsExactly((byte) 1, (byte) 2, (byte) 3); + assertThat(original).isEqualTo(equivalent); + assertThat(original.hashCode()).isEqualTo(hash).isEqualTo(equivalent.hashCode()); + assertThat(keys.contains(equivalent)).isTrue(); + } + + @Test + void equalsAndHashCode_shouldHonorTheHeaderComparator() { + for (var names : + List.of( + Map.entry("X-Result", "x-result"), + Map.entry("\u0130", "i"), + Map.entry("\u03c3", "\u03c2"), + Map.entry("\ud801\udc00", "\ud801\udc28"))) { + var first = + new HttpResponseData(200, Map.of(names.getKey(), List.of("value")), new byte[] {1}); + var second = + new HttpResponseData(200, Map.of(names.getValue(), List.of("value")), new byte[] {1}); + assertThat(first).isEqualTo(second); + assertThat(second).isEqualTo(first); + assertThat(first.hashCode()).isEqualTo(second.hashCode()); + assertThat(new HashSet<>(Set.of(first)).contains(second)).isTrue(); + assertThat(first.headers().keySet()).containsExactly(names.getKey()); + var firstRequest = + new HttpResponseData.RequestMetadata( + "GET", URI.create("https://example.test/"), first.headers()); + var secondRequest = + new HttpResponseData.RequestMetadata("GET", firstRequest.uri(), second.headers()); + assertThat(firstRequest).isEqualTo(secondRequest); + assertThat(secondRequest).isEqualTo(firstRequest); + assertThat(firstRequest.hashCode()).isEqualTo(secondRequest.hashCode()); + assertThat(new HashSet<>(Set.of(firstRequest)).contains(secondRequest)).isTrue(); + } + } + + @Test + void equalsAndHashCode_shouldIgnoreHeaderInsertionOrder() { + var first = + new HttpResponseData( + 200, Map.of("X-First", List.of("one"), "X-Second", List.of("two")), new byte[] {1}); + var second = + new HttpResponseData( + 200, Map.of("x-second", List.of("two"), "x-first", List.of("one")), new byte[] {1}); + assertThat(first).isEqualTo(second); + assertThat(first.hashCode()).isEqualTo(second.hashCode()); + } +} diff --git a/common/http/src/test/java/org/openworkflow/sdk/http/JdkHttpTransportTest.java b/common/http/src/test/java/org/openworkflow/sdk/http/JdkHttpTransportTest.java new file mode 100644 index 000000000..6d0a9cc0e --- /dev/null +++ b/common/http/src/test/java/org/openworkflow/sdk/http/JdkHttpTransportTest.java @@ -0,0 +1,317 @@ +/* + * Copyright 2020-Present The Open Workflow Specification Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openworkflow.sdk.http; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpTimeoutException; +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; +import mockwebserver3.MockResponse; +import mockwebserver3.MockWebServer; +import org.junit.jupiter.api.Test; + +class JdkHttpTransportTest { + + @Test + void execute_shouldExposeTheEffectiveGetAfterASeeOtherRedirect() throws Exception { + try (MockWebServer server = new MockWebServer(); + HttpTransport transport = new JdkHttpTransport()) { + server.start(); + server.enqueue( + new MockResponse.Builder() + .code(303) + .addHeader("Location", "/final?source=redirect") + .build()); + server.enqueue(new MockResponse.Builder().body("final response").build()); + var response = + transport + .execute( + new HttpRequestData( + server.url("/start?source=initial").uri(), + "POST", + Map.of("X-Request", "redirected"), + Map.of(), + "initial body".getBytes(StandardCharsets.UTF_8))) + .get(5, TimeUnit.SECONDS); + + assertThat(response.status()).isEqualTo(200); + var effective = response.request().orElseThrow(); + assertThat(effective.method()).isEqualTo("GET"); + assertThat(effective.uri()).isEqualTo(server.url("/final?source=redirect").uri()); + assertThat(effective.headers().get("x-request")).containsExactly("redirected"); + var initial = server.takeRequest(5, TimeUnit.SECONDS); + assertThat(initial.getMethod()).isEqualTo("POST"); + assertThat(initial.getBody().utf8()).isEqualTo("initial body"); + var redirected = server.takeRequest(5, TimeUnit.SECONDS); + assertThat(redirected.getMethod()).isEqualTo("GET"); + assertThat(redirected.getUrl().uri()).isEqualTo(effective.uri()); + } + } + + @Test + void execute_shouldDistinguishDirectRequestHeadersFromResponseHeaders() throws Exception { + try (MockWebServer server = new MockWebServer(); + HttpTransport transport = new JdkHttpTransport()) { + server.start(); + server.enqueue(new MockResponse.Builder().addHeader("X-Response", "received").build()); + var response = + transport + .execute( + new HttpRequestData( + server.url("/direct?existing=a%2Bb").uri(), + "GET", + Map.of("X-Request", "sent"), + Map.of("search", "tea + coffee"), + null)) + .get(5, TimeUnit.SECONDS); + var effective = response.request().orElseThrow(); + assertThat(effective.method()).isEqualTo("GET"); + assertThat(effective.uri()) + .isEqualTo(server.url("/direct?existing=a%2Bb&search=tea%20%2B%20coffee").uri()); + assertThat(effective.headers().get("x-request")).containsExactly("sent"); + assertThat(effective.headers()).doesNotContainKey("X-Response"); + assertThat(response.firstHeader("X-Response")).isEqualTo("received"); + assertThat(response.headers()).doesNotContainKey("X-Request"); + assertThat(server.takeRequest(5, TimeUnit.SECONDS).getUrl().uri()).isEqualTo(effective.uri()); + } + } + + @Test + void execute_shouldCapturePreparedRequestChangesAndEveryHeaderValue() throws Exception { + try (MockWebServer server = new MockWebServer(); + JdkHttpTransport transport = new JdkHttpTransport()) { + server.start(); + server.enqueue(new MockResponse.Builder().build()); + var response = + transport + .execute( + new HttpRequestData( + server.url("/initial").uri(), "POST", Map.of(), Map.of(), null), + builder -> { + builder.uri(server.url("/prepared?actual=yes").uri()); + builder.method("GET", HttpRequest.BodyPublishers.noBody()); + builder.header("X-Multiple", "one").header("X-Multiple", "two"); + return CompletableFuture.completedFuture(null); + }) + .get(5, TimeUnit.SECONDS); + var effective = response.request().orElseThrow(); + assertThat(effective.method()).isEqualTo("GET"); + assertThat(effective.uri()).isEqualTo(server.url("/prepared?actual=yes").uri()); + assertThat(effective.headers().get("x-multiple")).containsExactly("one", "two"); + assertThat(server.takeRequest(5, TimeUnit.SECONDS).getHeaders().values("X-Multiple")) + .containsExactly("one", "two"); + } + } + + @Test + void exchange_shouldAbandonDeliveryWithoutCancellingPreparation() throws Exception { + try (MockWebServer server = new MockWebServer(); + JdkHttpTransport transport = new JdkHttpTransport()) { + server.start(); + server.enqueue(new MockResponse.Builder().body("sent after cancellation").build()); + HttpRequestData request = + new HttpRequestData(server.url("/").uri(), "GET", Map.of(), Map.of(), null); + CompletableFuture preparation = new CompletableFuture<>(); + var delivery = + transport.exchange(request, HttpRequest.BodyPublishers.noBody(), builder -> preparation); + assertThat(delivery.cancel(true)).isTrue(); + assertThat(preparation.isCancelled()).isFalse(); + preparation.complete(null); + assertThat(server.takeRequest(5, TimeUnit.SECONDS)).isNotNull(); + assertThat(delivery.isCancelled()).isTrue(); + } + } + + @Test + void exchange_shouldPreserveNativePublisherAndResponseMetadata() throws Exception { + try (MockWebServer server = new MockWebServer(); + JdkHttpTransport transport = new JdkHttpTransport()) { + server.start(); + server.enqueue( + new MockResponse.Builder() + .code(201) + .addHeader("X-Response", "native") + .body("response body") + .build()); + HttpRequestData request = + new HttpRequestData( + server.url("/").uri(), + "POST", + Map.of(), + Map.of(), + "unused bytes".getBytes(StandardCharsets.UTF_8)); + var response = + transport + .exchange( + request, + HttpRequest.BodyPublishers.ofString("custom publisher"), + builder -> { + builder.setHeader("Content-Type", "text/plain"); + builder.header("X-Prepared", "yes"); + return CompletableFuture.completedFuture(null); + }) + .get(5, TimeUnit.SECONDS); + assertThat(response.statusCode()).isEqualTo(201); + assertThat(response.version()).isEqualTo(HttpClient.Version.HTTP_1_1); + assertThat(response.headers().firstValue("x-response")).contains("native"); + assertThat(new String(response.body(), StandardCharsets.UTF_8)).isEqualTo("response body"); + var recorded = server.takeRequest(5, TimeUnit.SECONDS); + assertThat(recorded.getBody().utf8()).isEqualTo("custom publisher"); + assertThat(recorded.getHeaders().get("X-Prepared")).isEqualTo("yes"); + } + } + + @Test + void execute_shouldPreserveSynchronousPreparationErrors() { + try (JdkHttpTransport transport = new JdkHttpTransport()) { + IllegalArgumentException error = new IllegalArgumentException("invalid credentials"); + assertThatThrownBy(() -> + transport.execute( + new HttpRequestData( + java.net.URI.create("http://localhost/"), "GET", Map.of(), Map.of(), null), + builder -> { + throw error; + })) + .isSameAs(error); + } + } + + @Test + void close_shouldCloseAnOwnedClient() throws Exception { + try (MockWebServer server = new MockWebServer()) { + server.start(); + JdkHttpTransport transport = new JdkHttpTransport(); + transport.close(); + assertThatThrownBy(() -> + transport + .execute( + new HttpRequestData(server.url("/").uri(), "GET", Map.of(), Map.of(), null)) + .get(5, TimeUnit.SECONDS)) + .isInstanceOf(java.util.concurrent.ExecutionException.class); + assertThat(server.getRequestCount()).isZero(); + } + } + + // This example compiles and runs in a module with no engine on its classpath. + private static CompletableFuture thirdPartyCall( + HttpTransport transport, java.net.URI uri) { + return transport.execute( + new HttpRequestData( + uri, + "POST", + Map.of("Content-Type", "application/json", "Authorization", "Bearer resolved-token"), + Map.of("search", "café + tea"), + "{\"name\":\"café\"}".getBytes(StandardCharsets.UTF_8))); + } + + @Test + void thirdPartyCall_shouldSendResolvedDataAndExposeErrorResponses() throws Exception { + try (MockWebServer server = new MockWebServer(); + JdkHttpTransport transport = new JdkHttpTransport()) { + server.start(); + server.enqueue( + new MockResponse.Builder() + .code(422) + .addHeader("X-Result", "invalid") + .addHeader("X-Result", "second") + .body("validation details") + .build()); + + HttpResponseData response = + thirdPartyCall(transport, server.url("/items?existing=a%2Bb").uri()) + .get(5, TimeUnit.SECONDS); + + assertThat(response.status()).isEqualTo(422); + assertThat(response.firstHeader("x-result")).isEqualTo("invalid"); + assertThat(response.headers().get("X-RESULT")).containsExactly("invalid", "second"); + assertThat(new String(response.body(), StandardCharsets.UTF_8)) + .isEqualTo("validation details"); + var recorded = server.takeRequest(5, TimeUnit.SECONDS); + assertThat(recorded.getMethod()).isEqualTo("POST"); + assertThat(recorded.getUrl().encodedQuery()) + .isEqualTo("existing=a%2Bb&search=caf%C3%A9%20%2B%20tea"); + assertThat(recorded.getHeaders().get("Authorization")).isEqualTo("Bearer resolved-token"); + assertThat(recorded.getBody().utf8()).isEqualTo("{\"name\":\"café\"}"); + } + } + + @Test + void execute_shouldUseTheJdkRequestTimeout() throws Exception { + try (MockWebServer server = new MockWebServer(); + JdkHttpTransport transport = new JdkHttpTransport()) { + server.start(); + server.enqueue( + new MockResponse.Builder().headersDelay(1, TimeUnit.SECONDS).body("late").build()); + HttpRequestData request = + new HttpRequestData(server.url("/").uri(), "GET", Map.of(), Map.of(), null); + assertThatThrownBy( + () -> transport.execute(request, Duration.ofMillis(100)).get(5, TimeUnit.SECONDS)) + .hasCauseInstanceOf(HttpTimeoutException.class); + } + } + + @Test + void cancel_shouldAbandonDeliveryWithoutCancellingPreparationOrClosingSharedClient() + throws Exception { + try (MockWebServer server = new MockWebServer(); + HttpClient client = HttpClient.newHttpClient()) { + server.start(); + server.enqueue(new MockResponse.Builder().body("sent after cancellation").build()); + server.enqueue(new MockResponse.Builder().body("still usable").build()); + JdkHttpTransport transport = new JdkHttpTransport(client); + HttpRequestData request = + new HttpRequestData(server.url("/").uri(), "GET", Map.of(), Map.of(), null); + CompletableFuture auth = new CompletableFuture<>(); + CompletableFuture cancelled = transport.execute(request, builder -> auth); + assertThat(cancelled.cancel(true)).isTrue(); + assertThat(auth.isCancelled()).isFalse(); + auth.complete(null); + assertThat(server.takeRequest(5, TimeUnit.SECONDS)).isNotNull(); + assertThat(cancelled.isCancelled()).isTrue(); + transport.close(); + assertThat(new JdkHttpTransport(client).execute(request).get(5, TimeUnit.SECONDS).status()) + .isEqualTo(200); + } + } + + @Test + void execute_shouldPreserveConfiguredRedirectBehavior() throws Exception { + try (MockWebServer server = new MockWebServer(); + JdkHttpTransport normal = new JdkHttpTransport(); + HttpClient client = + HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NEVER).build()) { + server.start(); + HttpRequestData request = + new HttpRequestData(server.url("/start").uri(), "GET", Map.of(), Map.of(), null); + server.enqueue(new MockResponse.Builder().code(302).addHeader("Location", "/end").build()); + server.enqueue(new MockResponse.Builder().body("redirected").build()); + assertThat( + new String( + normal.execute(request).get(5, TimeUnit.SECONDS).body(), StandardCharsets.UTF_8)) + .isEqualTo("redirected"); + server.enqueue(new MockResponse.Builder().code(302).addHeader("Location", "/end").build()); + assertThat(new JdkHttpTransport(client).execute(request).get(5, TimeUnit.SECONDS).status()) + .isEqualTo(302); + } + } +} diff --git a/common/openapi/README.md b/common/openapi/README.md new file mode 100644 index 000000000..9aff01764 --- /dev/null +++ b/common/openapi/README.md @@ -0,0 +1,136 @@ +# OpenAPI calls without the workflow engine + +Other workflow engines can reuse OpenAPI interpretation and request +preparation without creating a workflow application, definition, task or model. + +Use `org.openworkflow.sdk:openworkflow-common-openapi:8.0.0-SNAPSHOT` for the +Jackson-free contract. Its only production dependency is `openworkflow-common-http` +in this repository. +Add `openworkflow-impl-openapi` for the existing JSON/YAML reader. +The optional SDK reader lives in `openworkflow-impl-openapi` and brings the SDK +implementation and API serialization dependencies. The common contract has no engine dependency. Existing package names are retained for compatibility. + +## Minimal third-party usage + +```java +import java.util.Map; +import org.openworkflow.sdk.api.WorkflowFormat; +import org.openworkflow.sdk.http.*; +import org.openworkflow.sdk.impl.executors.openapi.*; +import org.openworkflow.sdk.impl.executors.openapi.jackson.JacksonUnifiedOpenAPIReader; + +// documentBytes, resolvedParameters and the transport belong to the caller. +UnifiedOpenAPI document = new JacksonUnifiedOpenAPIReader().read("api.yaml", documentBytes); +OpenAPIClient api = new OpenAPIClient(document, "save"); +HttpRequestData request = api.prepare(0, resolvedParameters, + body -> WorkflowFormat.JSON.mapper().writeValueAsBytes(body)); +// Inspect the request, use your own HttpTransport, or use JdkHttpTransport / JakartaHttpTransport. +var responseFuture = transport.execute(request); +``` + +`StandaloneOpenAPITest.documentToRequestToInjectedJdkTransport` compiles and runs +this flow against a local server using the SDK Jackson reader. +Provide any `UnifiedOpenAPIReader` to replace Jackson. The contract takes an +explicit body encoder (`Function, byte[]>`), allowing callers +to choose JSON or another serialization without importing Jackson in the contract. +A null body means no entity; the encoder is called only for a present body. +Authentication headers must be declared as header parameters in the document to +be placed there by parameter binding. Alternatively, prepare the request and +supply resolved headers through a new `HttpRequestData`, or use the transport's +asynchronous native preparation hook. No authentication policy is inferred from +OpenAPI `security` declarations. + +`OpenAPIProcessor.parse` exposes operation lookup; `OperationDefinition` exposes +the method, path, servers and parameter metadata. `OpenAPIRequest.bind` splits +parameters into per-call path, query, header and body values. `OpenAPIClient.prepare` +selects a server by index and returns `HttpRequestData` without networking. +An optional document-origin URI resolves relative server URLs in the standalone +API. The reader never downloads documents or external references; the caller +supplies bytes and preserves their origin. No new cache is introduced. + +## Servers, errors and ownership + +`OpenAPIClient.execute(parameters, encoder, transport, fallback)` additionally +executes asynchronously. Choose `ServerFallback.NONE` to use the first server, +or `ON_HTTP_OR_TRANSPORT_FAILURE` to try subsequent servers after a non-2xx +response or exceptional completion. This policy can **replay POSTs and other +operations with side effects**, even when a server already applied a request. +It is a separate decision from transport execution and workflow retries. + +The final HTTP response, including a non-2xx status, headers and body, remains +`HttpResponseData`. Transport failures remain exceptional completions with their +original causes. Preparation/serialization may throw synchronously; absent +operations, missing required parameters, null required path values and empty +server lists produce `IllegalArgumentException`. Malformed documents retain the +reader's parse error. There is no full OpenAPI document/schema validation. +Resolved server URIs must be absolute and contain a host; malformed values such +as `https:/api` are rejected before calling the transport. + +The workflow adapter retains its existing fallback after *any* exceptional +workflow HTTP result, including HTTP status validation and response conversion. +Its `redirect` handling and custom HTTP factories remain unchanged. Thus, a 3xx +accepted by the workflow adapter is not equivalent to a standalone non-2xx +fallback decision. Workflow exceptions and task positions stay in the adapter. + +Operations carry no invocation builder or resolved values. Each binding creates +fresh maps; each request owns its body bytes. Callers must treat documents, +metadata, and nested parameter values as read-only during use. Body serialization +is performed once per `execute`, before network fallback. Calls do not close the +injected transport or client; their owner closes them at application shutdown. +Responses are fully buffered in memory. Cancellation abandons result delivery +without guaranteeing network cancellation or preventing remote side effects. +Transport-specific query/header conversion and redirect behavior retain #1841's +limits; no streaming or stronger cancellation guarantee is added. + +## Existing SDK compatibility + +Existing `UnifiedOpenAPI`, `UnifiedOpenAPIReader`, Jackson reader/mixin class +names, the additional-object key and ServiceLoader entries remain available +transitively through the existing artifacts. `openworkflow-impl-openapi` still +registers the reader with the application; the Jackson module service and native-image reflection registrations remain +with the SDK reader. +Custom readers continue to work unchanged. + +The SDK delegates operation interpretation and parameter binding to this module. +It retains DSL interpretation, expression evaluation, resource and secret +resolution, document-fetch authentication, API-call authentication, model +conversion, response/error enrichment and retries. Document and API credentials +are resolved independently. Each invocation copies the initialized workflow HTTP +builder template, preserving authentication-provider lifetime and build-time +validation while isolating mutable builder fields. The existing default URI-template implementation is reused through a +compatibility delegate; custom URI-template resolvers continue to run in the SDK. + +## Actual supported subset + +This extraction preserves the existing subset rather than adding a complete +OpenAPI implementation: + +- Swagger 2.x and the existing OpenAPI 3-style model; JSON for `.json` names, + YAML otherwise. Version numbers are not strictly validated. +- Swagger host/basePath and the first scheme (HTTPS when absent), or OpenAPI + server URLs selected from operation, path item, then root. Empty server lists + fail. Server variables are not expanded. +- Operation lookup by `operationId` for GET, POST, PUT, DELETE, PATCH, HEAD and + OPTIONS. Path-item parameters are inherited; operation parameters override + inherited entries with the same location and name. +- Path, query and header parameters, including chained local parameter references + from Swagger `#/parameters/` and OpenAPI `#/components/parameters/`. + A supplied name can populate multiple locations. Required missing parameters + use `schema.default` when present, otherwise fail. Optional defaults are not + applied. Required null path values fail after expression resolution in the SDK. + Other null headers are omitted and null queries become empty strings. +- Existing URI-template expansion for paths; query values use the selected HTTP + transport's encoding. No OpenAPI `style`, `explode`, collection-format or + cookie serialization. Unsupported/unconsumed values remain in the body map. +- Swagger body schema properties and OpenAPI `application/json` request-body + properties are exposed as metadata. Remaining arguments form the body map; + required body fields and body defaults are not validated/applied. A request + body declaration can cause an empty body map to be sent. No general media-type + negotiation or generated clients; standalone serialization is caller-supplied. +- Direct local body-schema references to `#/definitions/` and + `#/components/schemas/` only. No nested schema, external or request-body + reference resolution, implicit reference downloads, or security-scheme handling. + +The tests characterize the supported subset and deliberately preserve its +limitations. Expanding conformance or changing replay policy requires a separate +behavior change. diff --git a/common/openapi/pom.xml b/common/openapi/pom.xml new file mode 100644 index 000000000..868802265 --- /dev/null +++ b/common/openapi/pom.xml @@ -0,0 +1,26 @@ + + 4.0.0 + + org.openworkflow.sdk + openworkflow-common + 8.0.0-SNAPSHOT + + openworkflow-common-openapi + Open Workflow :: OpenAPI Client + + + org.openworkflow.sdk + openworkflow-common-http + + + org.junit.jupiter + junit-jupiter-engine + test + + + org.assertj + assertj-core + test + + + diff --git a/common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIClient.java b/common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIClient.java new file mode 100644 index 000000000..b3313a80a --- /dev/null +++ b/common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIClient.java @@ -0,0 +1,116 @@ +/* + * Copyright 2020-Present The Open Workflow Specification Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openworkflow.sdk.impl.executors.openapi; + +import java.net.URI; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.CompletableFuture; +import java.util.function.Function; +import org.openworkflow.sdk.http.HttpRequestData; +import org.openworkflow.sdk.http.HttpResponseData; +import org.openworkflow.sdk.http.HttpTransport; + +/** OpenAPI request preparation and asynchronous execution using caller-owned HTTP resources. */ +public final class OpenAPIClient { + /** Legacy fallback may replay operations with side effects, including after HTTP errors. */ + public enum ServerFallback { + NONE, + ON_HTTP_OR_TRANSPORT_FAILURE + } + + private final OperationDefinition operation; + private final URI documentOrigin; + + public OpenAPIClient(UnifiedOpenAPI document, String operationId) { + this(document, operationId, null); + } + + /** Origin is optional and used only to resolve relative server URLs; no resources are loaded. */ + public OpenAPIClient(UnifiedOpenAPI document, String operationId, URI documentOrigin) { + this.operation = new OpenAPIProcessor(operationId).parse(Objects.requireNonNull(document)); + this.documentOrigin = documentOrigin; + } + + public OperationDefinition operation() { + return operation; + } + + public HttpRequestData prepare( + int serverIndex, + Map resolvedParameters, + Function, byte[]> bodyEncoder) { + return OpenAPIRequest.bind(operation, resolvedParameters) + .prepare(server(serverIndex), bodyEncoder); + } + + private URI server(int index) { + List servers = operation.getServers(); + if (servers.isEmpty()) throw new IllegalArgumentException("List of servers is empty"); + URI server = URI.create(servers.get(index)); + if (!server.isAbsolute()) { + if (documentOrigin == null) + throw new IllegalArgumentException( + "Relative OpenAPI server requires a document origin: " + server); + server = documentOrigin.resolve(server); + } + return server; + } + + /** + * Does not close the injected transport. HTTP errors remain responses; only transport or + * preparation errors complete exceptionally. Fallback is distinct from workflow retries. + * Cancellation abandons delivery and does not guarantee network cancellation. + */ + public CompletableFuture execute( + Map resolvedParameters, + Function, byte[]> bodyEncoder, + HttpTransport transport, + ServerFallback fallback) { + Objects.requireNonNull(transport); + Objects.requireNonNull(fallback); + // Serialize once per invocation, before any asynchronous fallback can observe caller mutations. + OpenAPIRequest bound = OpenAPIRequest.bind(operation, resolvedParameters); + byte[] encodedBody = bound.body() == null ? null : bodyEncoder.apply(bound.body()); + byte[] body = encodedBody == null ? null : encodedBody.clone(); + return execute(bound, body, transport, fallback, 0); + } + + private CompletableFuture execute( + OpenAPIRequest bound, + byte[] body, + HttpTransport transport, + ServerFallback fallback, + int index) { + CompletableFuture attempt; + try { + attempt = transport.execute(bound.prepare(server(index), ignored -> body)); + } catch (RuntimeException e) { + attempt = CompletableFuture.failedFuture(e); + } + if (fallback == ServerFallback.NONE || index + 1 >= operation.getServers().size()) + return attempt; + return attempt + .handle((response, error) -> { + if (error != null || response.status() < 200 || response.status() >= 300) { + return execute(bound, body, transport, fallback, index + 1); + } + return CompletableFuture.completedFuture(response); + }) + .thenCompose(Function.identity()); + } +} diff --git a/common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIPathResolver.java b/common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIPathResolver.java new file mode 100644 index 000000000..0bf892a92 --- /dev/null +++ b/common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIPathResolver.java @@ -0,0 +1,618 @@ +/* + * Copyright 2020-Present The Open Workflow Specification Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openworkflow.sdk.impl.executors.openapi; + +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class OpenAPIPathResolver { + + public URI resolveTemplates(String uri, Map map) { + return URI.create(expandImpl(uri, map)); + } + + // Private implementation + private enum Operator { + NO_OP, + PLUS, + HASH, + DOT, + SLASH, + SEMICOLON, + QUESTION_MARK, + AMP; + } + + private static void checkVarname(String token, int col) { + if (token.startsWith(".") || token.endsWith(".")) { + throw new IllegalArgumentException( + "Variable name cannot start or end with a dot at col:" + col); + } + if (token.contains("..")) { + throw new IllegalArgumentException( + "Variable name cannot contain consecutive dots at col:" + col); + } + for (int i = 0; i < token.length(); i++) { + if (token.charAt(i) == '%') { + if (i + 2 >= token.length() + || !isHexDigit(token.charAt(i + 1)) + || !isHexDigit(token.charAt(i + 2))) { + throw new IllegalArgumentException( + "Invalid percent-encoding in variable name at col:" + col); + } + } + } + } + + private static boolean isHexDigit(char c) { + return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); + } + + private static void validateLiteral(Character c, int col) { + switch (c) { + case '+': + case '#': + case '/': + case ';': + case '?': + case '&': + case ' ': + case '!': + case '=': + case '$': + case '|': + case '*': + case ':': + case '~': + throw new IllegalArgumentException( + "Illegal character identified in the token at col:" + col); + default: + break; + } + } + + private static int getMaxChar(StringBuilder buffer, int col) { + if (buffer == null || buffer.length() == 0) { + return -1; + } else { + String value = buffer.toString(); + + if (value.isEmpty()) { + return -1; + } else { + try { + if (value.charAt(0) == '0') { + throw new IllegalArgumentException( + "Leading zero not allowed in max chars at col:" + col); + } + int result = Integer.parseInt(value); + if (result < 1 || result > 9999) { + throw new IllegalArgumentException("Max chars out of range (1-9999) at col:" + col); + } + return result; + } catch (NumberFormatException e) { + throw new IllegalArgumentException("Cannot parse max chars at col:" + col); + } + } + } + } + + private static Operator getOperator(Character c, StringBuilder token, int col) { + switch (c) { + case '+': + return Operator.PLUS; + case '#': + return Operator.HASH; + case '.': + return Operator.DOT; + case '/': + return Operator.SLASH; + case ';': + return Operator.SEMICOLON; + case '?': + return Operator.QUESTION_MARK; + case '&': + return Operator.AMP; + default: + validateLiteral(c, col); + token.append(c); + return Operator.NO_OP; + } + } + + private static String expandImpl(String str, Map substitutions) { + final StringBuilder result = new StringBuilder(str.length() * 2); + + boolean toToken = false; + final StringBuilder token = new StringBuilder(); + + Operator operator = null; + boolean composite = false; + boolean toMaxCharBuffer = false; + final StringBuilder maxCharBuffer = new StringBuilder(3); + boolean firstToken = true; + + for (int i = 0; i < str.length(); i++) { + char character = str.charAt(i); + switch (character) { + case '{': + toToken = true; + token.setLength(0); + firstToken = true; + break; + case '}': + if (toToken) { + if (toMaxCharBuffer && maxCharBuffer.length() == 0) { + throw new IllegalArgumentException("Empty prefix modifier at col:" + i); + } + boolean expanded = + expandToken( + operator, + token.toString(), + composite, + getMaxChar(maxCharBuffer, i), + firstToken, + substitutions, + result, + i); + if (expanded && firstToken) { + firstToken = false; + } + toToken = false; + token.setLength(0); + operator = null; + composite = false; + toMaxCharBuffer = false; + maxCharBuffer.setLength(0); + } else { + throw new IllegalArgumentException("Failed to expand token, invalid at col:" + i); + } + break; + case ',': + if (toToken) { + if (toMaxCharBuffer && maxCharBuffer.length() == 0) { + throw new IllegalArgumentException("Empty prefix modifier at col:" + i); + } + boolean expanded = + expandToken( + operator, + token.toString(), + composite, + getMaxChar(maxCharBuffer, i), + firstToken, + substitutions, + result, + i); + if (expanded && firstToken) { + firstToken = false; + } + token.setLength(0); + composite = false; + toMaxCharBuffer = false; + maxCharBuffer.setLength(0); + break; + } + // Intentional fall-through for commas outside the {} + default: + if (toToken) { + if (operator == null) { + operator = getOperator(character, token, i); + } else if (toMaxCharBuffer) { + if (Character.isDigit(character)) { + maxCharBuffer.append(character); + } else { + throw new IllegalArgumentException( + "Illegal character identified in the token at col:" + i); + } + } else { + if (character == ':') { + toMaxCharBuffer = true; + maxCharBuffer.setLength(0); + } else if (character == '*') { + composite = true; + } else { + validateLiteral(character, i); + token.append(character); + } + } + } else { + if (character > 0x7F || Character.isHighSurrogate(character)) { + String toEncode; + if (Character.isHighSurrogate(character) + && i + 1 < str.length() + && Character.isLowSurrogate(str.charAt(i + 1))) { + toEncode = new String(new char[] {character, str.charAt(++i)}); + } else { + toEncode = Character.toString(character); + } + for (byte b : toEncode.getBytes(StandardCharsets.UTF_8)) { + result.append(String.format("%%%02X", b & 0xFF)); + } + } else { + result.append(character); + } + } + break; + } + } + + if (!toToken) { + return result.toString(); + } else { + throw new IllegalArgumentException("Unterminated token"); + } + } + + private static void addPrefix(Operator op, StringBuilder result) { + switch (op) { + case HASH: + result.append('#'); + break; + case DOT: + result.append('.'); + break; + case SLASH: + result.append('/'); + break; + case SEMICOLON: + result.append(';'); + break; + case QUESTION_MARK: + result.append('?'); + break; + case AMP: + result.append('&'); + break; + default: + return; + } + } + + private static void addSeparator(Operator op, StringBuilder result) { + switch (op) { + case DOT: + result.append('.'); + break; + case SLASH: + result.append('/'); + break; + case SEMICOLON: + result.append(';'); + break; + case QUESTION_MARK: + case AMP: + result.append('&'); + break; + default: + result.append(','); + return; + } + } + + private static void addValue( + Operator op, String token, Object value, StringBuilder result, int maxChar) { + switch (op) { + case PLUS: + case HASH: + addExpandedValue(null, value, result, maxChar, false); + break; + case QUESTION_MARK: + case AMP: + result.append(token + '='); + addExpandedValue(null, value, result, maxChar, true); + break; + case SEMICOLON: + result.append(token); + addExpandedValue("=", value, result, maxChar, true); + break; + case DOT: + case SLASH: + case NO_OP: + addExpandedValue(null, value, result, maxChar, true); + } + } + + private static void addValueElement( + Operator op, String token, Object value, StringBuilder result, int maxChar) { + switch (op) { + case PLUS: + case HASH: + addExpandedValue(null, value, result, maxChar, false); + break; + case QUESTION_MARK: + case AMP: + case SEMICOLON: + case DOT: + case SLASH: + case NO_OP: + addExpandedValue(null, value, result, maxChar, true); + } + } + + private static boolean isSurrogate(char cp) { + return (cp >= 0xD800 && cp <= 0xDFFF); + } + + private static boolean isIprivate(char cp) { + return (0xE000 <= cp && cp <= 0xF8FF); + } + + private static boolean isUcschar(char cp) { + return (0xA0 <= cp && cp <= 0xD7FF) + || (0xF900 <= cp && cp <= 0xFDCF) + || (0xFDF0 <= cp && cp <= 0xFFEF); + } + + private static void addExpandedValue( + String prefix, Object value, StringBuilder result, int maxChar, boolean replaceReserved) { + String stringValue = convertNativeTypes(value); + int cpCount = stringValue.codePointCount(0, stringValue.length()); + int max = (maxChar != -1) ? Math.min(maxChar, cpCount) : cpCount; + result.ensureCapacity(max * 2); // hint to SB + boolean toReserved = false; + final StringBuilder reservedBuffer = new StringBuilder(3); + + if (max > 0 && prefix != null) { + result.append(prefix); + } + + int charCount = 0; + for (int i = 0; i < stringValue.length() && charCount < max; i++) { + char character = stringValue.charAt(i); + charCount++; + + if (character == '%' && !replaceReserved) { + toReserved = true; + reservedBuffer.setLength(0); + } + + String toAppend = Character.toString(character); + try { + if (isSurrogate(character)) { + StringBuilder sb = new StringBuilder(); + sb.append(Character.toChars(stringValue.codePointAt(i++))); + toAppend = URLEncoder.encode(sb.toString(), StandardCharsets.UTF_8.name()); + } else if (replaceReserved || isUcschar(character) || isIprivate(character)) { + toAppend = + URLEncoder.encode(toAppend, StandardCharsets.UTF_8.name()) + .replace("*", "%2A") + .replace("%7E", "~"); + } + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + + if (toReserved) { + reservedBuffer.append(toAppend); + + if (reservedBuffer.length() == 3) { + boolean isEncoded = false; + try { + URLDecoder.decode(reservedBuffer.toString(), StandardCharsets.UTF_8.name()); + isEncoded = true; + } catch (Exception e) { + // ignore + } + + if (isEncoded) { + result.append(reservedBuffer); + } else { + result.append("%25"); + // only if !replaceReserved + result.append(reservedBuffer.substring(1)); + } + toReserved = false; + reservedBuffer.setLength(0); + } + } else { + if (character == ' ') { + result.append("%20"); + } else if (character == '%') { + result.append("%25"); + } else { + result.append(toAppend); + } + } + } + + if (toReserved) { + result.append("%25"); + result.append(reservedBuffer.substring(1)); + } + } + + private static boolean isList(Object value) { + return value instanceof ArrayList + || // checking concrete instances first as it's faster + value instanceof List; + } + + private static boolean isMap(Object value) { + return value instanceof HashMap + || // checking concrete instances first as it's faster + value instanceof Map; + } + + enum SubstitutionType { + EMPTY, + STRING, + LIST, + MAP; + } + + private static SubstitutionType getSubstitutionType(Object value, int col) { + if (value == null) { + return SubstitutionType.EMPTY; + } else if (isNativeType(value)) { + return SubstitutionType.STRING; + } else if (isList(value)) { + return SubstitutionType.LIST; + } else if (isMap(value)) { + return SubstitutionType.MAP; + } else { + throw new IllegalArgumentException( + "Illegal class passed as substitution, found " + value.getClass() + " at col:" + col); + } + } + + private static boolean isEmpty(SubstitutionType substType, Object value) { + if (value == null) { + return true; + } else { + switch (substType) { + case STRING: + return false; + case LIST: + return ((List) value).isEmpty(); + case MAP: + return ((Map) value).isEmpty(); + default: + return true; + } + } + } + + private static boolean isNativeType(Object value) { + if (value instanceof String || value instanceof Boolean || value instanceof Number) { + return true; + } + return false; + } + + private static String convertNativeTypes(Object value) { + if (value instanceof String) { + return (String) value; + } else if (value instanceof Boolean || value instanceof Number) { + return value.toString(); + } + throw new IllegalArgumentException( + "Illegal class passed as substitution, found " + value.getClass()); + } + + // returns true if expansion happened + private static boolean expandToken( + Operator operator, + String token, + boolean composite, + int maxChar, + boolean firstToken, + Map substitutions, + StringBuilder result, + int col) { + if (token.isEmpty()) { + throw new IllegalArgumentException("Found an empty token at col:" + col); + } + + checkVarname(token, col); + + Object value = substitutions.get(token); + SubstitutionType substType = getSubstitutionType(value, col); + if (substType == SubstitutionType.EMPTY || isEmpty(substType, value)) { + return false; + } + + if (firstToken) { + addPrefix(operator, result); + } else { + addSeparator(operator, result); + } + + switch (substType) { + case STRING: + addStringValue(operator, token, (Object) value, result, maxChar); + break; + case LIST: + addListValue(operator, token, (List) value, result, maxChar, composite); + break; + case MAP: + addMapValue(operator, token, (Map) value, result, maxChar, composite); + break; + } + + return true; + } + + private static boolean addStringValue( + Operator operator, String token, Object value, StringBuilder result, int maxChar) { + addValue(operator, token, value, result, maxChar); + return true; + } + + private static boolean addListValue( + Operator operator, + String token, + List value, + StringBuilder result, + int maxChar, + boolean composite) { + boolean first = true; + for (Object v : value) { + if (first) { + addValue(operator, token, v, result, maxChar); + first = false; + } else { + if (composite) { + addSeparator(operator, result); + addValue(operator, token, v, result, maxChar); + } else { + result.append(','); + addValueElement(operator, token, v, result, maxChar); + } + } + } + return !first; + } + + private static boolean addMapValue( + Operator operator, + String token, + Map value, + StringBuilder result, + int maxChar, + boolean composite) { + boolean first = true; + if (maxChar != -1) { + throw new IllegalArgumentException("Value trimming is not allowed on Maps"); + } + for (Map.Entry v : value.entrySet()) { + if (composite) { + if (!first) { + addSeparator(operator, result); + } + addValueElement(operator, token, v.getKey(), result, maxChar); + result.append('='); + } else { + if (first) { + addValue(operator, token, v.getKey(), result, maxChar); + } else { + result.append(','); + addValueElement(operator, token, v.getKey(), result, maxChar); + } + result.append(','); + } + addValueElement(operator, token, v.getValue(), result, maxChar); + first = false; + } + return !first; + } +} diff --git a/impl/core/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIProcessor.java b/common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIProcessor.java similarity index 88% rename from impl/core/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIProcessor.java rename to common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIProcessor.java index 9267576c7..61b552b40 100644 --- a/impl/core/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIProcessor.java +++ b/common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIProcessor.java @@ -18,15 +18,15 @@ import java.util.Objects; import java.util.Optional; -class OpenAPIProcessor { +public class OpenAPIProcessor { private final String operationId; - OpenAPIProcessor(String operationId) { + public OpenAPIProcessor(String operationId) { this.operationId = Objects.requireNonNull(operationId); } - OperationDefinition parse(UnifiedOpenAPI unifiedOpenAPI) { + public OperationDefinition parse(UnifiedOpenAPI unifiedOpenAPI) { Optional operationDefinition = unifiedOpenAPI.findOperationById(this.operationId); return operationDefinition.orElseThrow(() -> diff --git a/common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIRequest.java b/common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIRequest.java new file mode 100644 index 000000000..baf998e13 --- /dev/null +++ b/common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIRequest.java @@ -0,0 +1,151 @@ +/* + * Copyright 2020-Present The Open Workflow Specification Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openworkflow.sdk.impl.executors.openapi; + +import java.net.URI; +import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; +import java.util.function.Function; +import org.openworkflow.sdk.http.HttpRequestData; + +/** + * Per-invocation values. No expression evaluation or authentication policy resolution occurs here. + */ +public record OpenAPIRequest( + String method, + String path, + Map pathParameters, + Set mandatoryParameters, + Map headers, + Map queryParameters, + Map body) { + + public OpenAPIRequest { + pathParameters = copy(pathParameters); + mandatoryParameters = Set.copyOf(mandatoryParameters); + headers = copy(headers); + queryParameters = copy(queryParameters); + body = body == null ? null : copy(body); + } + + private static Map copy(Map values) { + return Collections.unmodifiableMap(new LinkedHashMap<>(values)); + } + + /** Preserves the SDK's parameter placement, required/default and leftover-body rules. */ + public static OpenAPIRequest bind(OperationDefinition operation, Map parameters) { + Map body = new LinkedHashMap<>(parameters); + Map headers = new LinkedHashMap<>(); + Map query = new LinkedHashMap<>(); + Map path = new LinkedHashMap<>(); + Set mandatory = new HashSet<>(); + Set missing = new HashSet<>(); + boolean hasBody = operation.getOperation().hasRequestBody(); + for (ParameterDefinition parameter : operation.getParameters()) { + Map target = + switch (parameter.in()) { + case "header" -> headers; + case "path" -> path; + case "query" -> query; + default -> null; + }; + if ("body".equals(parameter.in())) { + hasBody = true; + } + if (target == null) { + continue; + } + String name = parameter.name(); + if (parameter.required()) { + mandatory.add(name); + } + if (parameters.containsKey(name)) { + target.put(name, parameters.get(name)); + body.remove(name); + } else if (parameter.required()) { + Object value = parameter.schema() == null ? null : parameter.schema()._default(); + if (value == null) { + missing.add(name); + } else { + target.put(name, value); + } + } + } + if (!missing.isEmpty()) { + throw new IllegalArgumentException( + "Missing required OpenAPI parameters for operation '" + + operation.getOperation().operationId() + + "': " + + missing); + } + return new OpenAPIRequest( + operation.getMethod(), + operation.getPath(), + path, + mandatory, + headers, + query, + hasBody || !body.isEmpty() ? body : null); + } + + /** + * Prepares a buffered request for a resolved server URI. The encoder receives the body map only + * when an entity is present; callers choose serialization without a JSON dependency. Nested input + * values must not be mutated while preparing a call. + */ + public HttpRequestData prepare(URI server, Function, byte[]> bodyEncoder) { + if (!server.isAbsolute() || server.getHost() == null) { + throw new IllegalArgumentException( + "OpenAPI server requires an absolute URI with a host: " + server); + } + var nullPaths = + pathParameters.entrySet().stream() + .filter(e -> e.getValue() == null && mandatoryParameters.contains(e.getKey())) + .map(Map.Entry::getKey) + .toList(); + if (!nullPaths.isEmpty()) { + throw new IllegalArgumentException( + "Following mandatory OpenAPI path parameters has null values after expression resolution " + + nullPaths); + } + URI child = new OpenAPIPathResolver().resolveTemplates(path, pathParameters); + URI uri; + if (child.isAbsolute()) { + // Preserve WorkflowUtils.concatURI behavior: a resolved absolute path overrides the base. + // Server selection is not an origin allowlist; document trust remains the caller's policy. + uri = child; + } else { + String basePath = server.getRawPath(); + if (basePath == null || basePath.isEmpty()) basePath = "/"; + if (!basePath.endsWith("/")) basePath += "/"; + String relative = child.toString(); + while (relative.startsWith("/")) relative = relative.substring(1); + uri = URI.create(server.getScheme() + "://" + server.getRawAuthority() + basePath + relative); + } + Map normalizedHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + headers.forEach((name, value) -> { + if (value != null) normalizedHeaders.put(name, value); + }); + Map query = new LinkedHashMap<>(); + queryParameters.forEach((name, value) -> query.put(name, value == null ? "" : value)); + return new HttpRequestData( + uri, method, normalizedHeaders, query, body == null ? null : bodyEncoder.apply(body)); + } +} diff --git a/impl/core/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OperationDefinition.java b/common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OperationDefinition.java similarity index 91% rename from impl/core/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OperationDefinition.java rename to common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OperationDefinition.java index 4935e7d4c..ed9168d19 100644 --- a/impl/core/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OperationDefinition.java +++ b/common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OperationDefinition.java @@ -21,7 +21,7 @@ import java.util.Map; import java.util.Objects; -class OperationDefinition { +public class OperationDefinition { private final UnifiedOpenAPI.Operation operation; private final UnifiedOpenAPI.PathItem pathItem; @@ -29,7 +29,17 @@ class OperationDefinition { private final UnifiedOpenAPI openAPI; private final String path; - OperationDefinition( + public OperationDefinition( + UnifiedOpenAPI openAPI, UnifiedOpenAPI.Operation operation, String path, String method) { + this( + openAPI, + openAPI.paths() == null ? null : openAPI.paths().get(path), + operation, + path, + method); + } + + public OperationDefinition( UnifiedOpenAPI openAPI, UnifiedOpenAPI.PathItem pathItem, UnifiedOpenAPI.Operation operation, @@ -43,19 +53,19 @@ class OperationDefinition { this.method = Objects.requireNonNull(method, "method cannot be null"); } - String getMethod() { + public String getMethod() { return method; } - String getPath() { + public String getPath() { return path; } - UnifiedOpenAPI.Operation getOperation() { + public UnifiedOpenAPI.Operation getOperation() { return operation; } - List getServers() { + public List getServers() { if (openAPI.swaggerVersion() == UnifiedOpenAPI.SwaggerVersion.OPENAPI_V3) { if (operation.servers() != null && !operation.servers().isEmpty()) { return toUrls(operation.servers()); @@ -89,7 +99,7 @@ private void merge(Map merged, UnifiedOpenAPI. merged.put(resolved.in() + ":" + resolved.name(), resolved); } - List getParameters() { + public List getParameters() { List allParameters = mergedParameters(); List paramDefinitions = new ArrayList<>(); for (UnifiedOpenAPI.Parameter parameter : allParameters) { diff --git a/impl/core/src/main/java/org/openworkflow/sdk/impl/executors/openapi/ParameterDefinition.java b/common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/ParameterDefinition.java similarity index 95% rename from impl/core/src/main/java/org/openworkflow/sdk/impl/executors/openapi/ParameterDefinition.java rename to common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/ParameterDefinition.java index 674c36f2f..afee36dc9 100644 --- a/impl/core/src/main/java/org/openworkflow/sdk/impl/executors/openapi/ParameterDefinition.java +++ b/common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/ParameterDefinition.java @@ -15,5 +15,5 @@ */ package org.openworkflow.sdk.impl.executors.openapi; -record ParameterDefinition( +public record ParameterDefinition( String name, String in, boolean required, UnifiedOpenAPI.Schema schema) {} diff --git a/impl/core/src/main/java/org/openworkflow/sdk/impl/executors/openapi/UnifiedOpenAPI.java b/common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/UnifiedOpenAPI.java similarity index 100% rename from impl/core/src/main/java/org/openworkflow/sdk/impl/executors/openapi/UnifiedOpenAPI.java rename to common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/UnifiedOpenAPI.java diff --git a/impl/core/src/main/java/org/openworkflow/sdk/impl/executors/openapi/UnifiedOpenAPIReader.java b/common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/UnifiedOpenAPIReader.java similarity index 100% rename from impl/core/src/main/java/org/openworkflow/sdk/impl/executors/openapi/UnifiedOpenAPIReader.java rename to common/openapi/src/main/java/org/openworkflow/sdk/impl/executors/openapi/UnifiedOpenAPIReader.java diff --git a/common/openapi/src/test/java/org/openworkflow/sdk/impl/executors/openapi/CustomOpenAPIReaderTest.java b/common/openapi/src/test/java/org/openworkflow/sdk/impl/executors/openapi/CustomOpenAPIReaderTest.java new file mode 100644 index 000000000..c3fe924e7 --- /dev/null +++ b/common/openapi/src/test/java/org/openworkflow/sdk/impl/executors/openapi/CustomOpenAPIReaderTest.java @@ -0,0 +1,58 @@ +/* + * Copyright 2020-Present The Open Workflow Specification Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openworkflow.sdk.impl.executors.openapi; + +import static org.assertj.core.api.Assertions.*; + +import java.net.URI; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.Test; + +class CustomOpenAPIReaderTest { + @Test + void preparesWithAConsumerReaderWithoutTheEngine() { + UnifiedOpenAPIReader reader = + (name, bytes) -> + new UnifiedOpenAPI( + null, + List.of(new UnifiedOpenAPI.Server("https://example.test")), + null, + null, + null, + Map.of( + "/items", + new UnifiedOpenAPI.PathItem( + null, + null, + new UnifiedOpenAPI.Operation("list", null, null, null), + null, + null, + null, + null, + null, + null)), + null, + null, + null); + var request = + new OpenAPIClient(reader.read("custom", new byte[0]), "list") + .prepare(0, Map.of(), ignored -> new byte[0]); + assertThat(request.uri()).isEqualTo(URI.create("https://example.test/items")); + assertThatExceptionOfType(ClassNotFoundException.class) + .isThrownBy(() -> Class.forName("org.openworkflow.sdk.impl.WorkflowApplication")); + } +} diff --git a/common/pom.xml b/common/pom.xml new file mode 100644 index 000000000..4efb9550e --- /dev/null +++ b/common/pom.xml @@ -0,0 +1,6 @@ + + 4.0.0 + org.openworkflow.sdkopenworkflow-parent8.0.0-SNAPSHOT + openworkflow-commonpomOpen Workflow :: Common + httphttp-jakartaopenapi + diff --git a/impl/README.md b/impl/README.md index 1468c9bba..4d9cf7203 100644 --- a/impl/README.md +++ b/impl/README.md @@ -112,6 +112,10 @@ There are also companion modules/docs for: ### Http dependencies +For HTTP execution from another engine without workflow contexts, use the +[standalone HTTP client](../common/http/README.md). The existing workflow HTTP APIs +remain adapters over the same transports. + By default, the SDK reference implementation use `java.net.HttpRequest` to support Http/OpenAPI calls. Therefore, no dependency (besides the jackson modules described above) needs to be added for using Http Call task or downloading http resources. However, for further configuration of Http functionality, users might include the Jakarta based modules, which will allow customizing `jakarta.ws.rs.client.Client` in Jakarta EE environments. diff --git a/impl/core/pom.xml b/impl/core/pom.xml index a6fc68270..b0a88a5eb 100644 --- a/impl/core/pom.xml +++ b/impl/core/pom.xml @@ -8,6 +8,15 @@ openworkflow-impl-core Open Workflow :: Impl :: Core + + org.openworkflow.sdk + openworkflow-common-openapi + + + + org.openworkflow.sdk + openworkflow-common-http + org.openworkflow.sdk openworkflow-types diff --git a/impl/core/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIExecutor.java b/impl/core/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIExecutor.java index e861e93e2..ebe657b6e 100644 --- a/impl/core/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIExecutor.java +++ b/impl/core/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIExecutor.java @@ -16,12 +16,10 @@ package org.openworkflow.sdk.impl.executors.openapi; import java.net.URI; -import java.util.HashMap; -import java.util.HashSet; import java.util.Iterator; import java.util.Map; -import java.util.Set; import java.util.concurrent.CompletableFuture; +import java.util.function.Supplier; import org.openworkflow.sdk.api.types.ExternalResource; import org.openworkflow.sdk.impl.TaskContext; import org.openworkflow.sdk.impl.WorkflowApplication; @@ -37,7 +35,7 @@ class OpenAPIExecutor implements CallableTask { private final OpenAPIProcessor processor; private final ExternalResource resource; private final Map parameters; - private final WorkflowHttpRequestInfoBuilderFactory builder; + private final Supplier builderFactory; private final UnifiedOpenAPIReader openAPIReader; OpenAPIExecutor( @@ -45,12 +43,12 @@ class OpenAPIExecutor implements CallableTask { OpenAPIProcessor processor, ExternalResource resource, Map parameters, - WorkflowHttpRequestInfoBuilderFactory builder) { + Supplier builderFactory) { this.openAPIReader = openAPIReader; this.processor = processor; this.resource = resource; this.parameters = parameters; - this.builder = builder; + this.builderFactory = builderFactory; } @Override @@ -63,7 +61,9 @@ public CompletableFuture apply( .thenApply(u -> processor.parse(u)) .thenComposeAsync( operationDefinition -> { - fillHttpBuilder(workflowContext.definition().application(), operationDefinition); + WorkflowHttpRequestInfoBuilderFactory builder = builderFactory.get(); + fillHttpBuilder( + workflowContext.definition().application(), operationDefinition, builder); Builder httpInfoBuilder = builder.build(workflowContext, taskContext, input); Iterator iter = operationDefinition.getServers().iterator(); @@ -86,73 +86,21 @@ public CompletableFuture apply( workflowContext.definition().application().executorService()); } - private void fillHttpBuilder(WorkflowApplication application, OperationDefinition operation) { - Map headers = new HashMap<>(); - Map queryMap = new HashMap<>(); - Map pathParameters = new HashMap<>(); - Set mandatoryParameters = new HashSet<>(); - Set missingParams = new HashSet<>(); - boolean hasBody = operation.getOperation().hasRequestBody(); - Map bodyParameters = new HashMap<>(parameters); - - // Parameters are unique per (in, name) - for (ParameterDefinition parameter : operation.getParameters()) { - switch (parameter.in()) { - case "header": - param(parameter, bodyParameters, headers, missingParams, mandatoryParameters); - break; - case "path": - param(parameter, bodyParameters, pathParameters, missingParams, mandatoryParameters); - break; - case "query": - param(parameter, bodyParameters, queryMap, missingParams, mandatoryParameters); - break; - case "body": - hasBody = true; - break; - } - } - - if (!missingParams.isEmpty()) { - throw new IllegalArgumentException( - "Missing required OpenAPI parameters for operation '" - + (operation.getOperation().operationId() != null - ? operation.getOperation().operationId() - : "" + "': ") - + missingParams); - } + private void fillHttpBuilder( + WorkflowApplication application, + OperationDefinition operation, + WorkflowHttpRequestInfoBuilderFactory builder) { + OpenAPIRequest request = OpenAPIRequest.bind(operation, parameters); builder - .withBody(hasBody || !bodyParameters.isEmpty() ? bodyParameters : null) - .withQueryMap(!queryMap.isEmpty() ? queryMap : null) - .withHeaders(!headers.isEmpty() ? headers : null) - .withMethod(operation.getMethod()) + .withBody(request.body()) + .withQueryMap(request.queryParameters()) + .withHeaders(request.headers()) + .withMethod(request.method()) .withPath( new OperationPathResolver( - operation.getPath(), application, pathParameters, mandatoryParameters)); - } - - private void param( - ParameterDefinition parameter, - Map bodyParameters, - Map collectorMap, - Set missingParams, - Set mandatoryParams) { - String name = parameter.name(); - boolean isRequired = parameter.required(); - if (isRequired) { - mandatoryParams.add(name); - } - if (parameters.containsKey(name)) { - collectorMap.put(name, parameters.get(name)); - bodyParameters.remove(name); - } else if (isRequired) { - UnifiedOpenAPI.Schema schema = parameter.schema(); - Object defaultValue = schema != null ? schema._default() : null; - if (defaultValue != null) { - collectorMap.put(name, defaultValue); - } else { - missingParams.add(name); - } - } + request.path(), + application, + request.pathParameters(), + request.mandatoryParameters())); } } diff --git a/impl/core/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIExecutorBuilder.java b/impl/core/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIExecutorBuilder.java index 7ff10ebc9..660821bf9 100644 --- a/impl/core/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIExecutorBuilder.java +++ b/impl/core/src/main/java/org/openworkflow/sdk/impl/executors/openapi/OpenAPIExecutorBuilder.java @@ -49,7 +49,7 @@ public CallableTaskFactory init( .withAuth(with.getAuthentication()) .redirect(with.isRedirect()); UnifiedOpenAPIReader reader = reader(definition.application()); - return () -> new OpenAPIExecutor(reader, processor, resource, parameters, builder); + return () -> new OpenAPIExecutor(reader, processor, resource, parameters, builder::copy); } private UnifiedOpenAPIReader reader(WorkflowApplication app) { diff --git a/impl/core/src/main/java/org/openworkflow/sdk/impl/http/DefaultHttpRequest.java b/impl/core/src/main/java/org/openworkflow/sdk/impl/http/DefaultHttpRequest.java index 08a256168..ff52ffd44 100644 --- a/impl/core/src/main/java/org/openworkflow/sdk/impl/http/DefaultHttpRequest.java +++ b/impl/core/src/main/java/org/openworkflow/sdk/impl/http/DefaultHttpRequest.java @@ -15,18 +15,13 @@ */ package org.openworkflow.sdk.impl.http; -import java.net.URI; -import java.net.URLEncoder; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpRequest.BodyPublisher; -import java.net.http.HttpRequest.Builder; -import java.net.http.HttpResponse; -import java.nio.charset.StandardCharsets; -import java.util.Map; import java.util.concurrent.CompletableFuture; -import java.util.function.Function; -import java.util.stream.Collectors; +import org.openworkflow.sdk.http.HttpRequestData; +import org.openworkflow.sdk.http.HttpRequestEncoding; +import org.openworkflow.sdk.http.JdkHttpTransport; import org.openworkflow.sdk.impl.TaskContext; import org.openworkflow.sdk.impl.WorkflowApplication; import org.openworkflow.sdk.impl.WorkflowContext; @@ -36,37 +31,34 @@ public class DefaultHttpRequest implements WorkflowHttpRequest { protected final WorkflowHttpRequestInfo httpInfo; protected final WorkflowApplication app; - private final HttpClient client; + private final JdkHttpTransport transport; public DefaultHttpRequest( WorkflowApplication app, HttpClient client, WorkflowHttpRequestInfo httpInfo) { this.httpInfo = httpInfo; - this.client = client; + this.transport = new JdkHttpTransport(client); this.app = app; } @Override public CompletableFuture invoke( WorkflowContext workflowContext, TaskContext taskContext) { - Builder builder = HttpRequest.newBuilder(); - builder.method(httpInfo.method(), buildBody()); - Map headers = httpInfo.headers(); - // add default content type, it might be override by specified headers. - builder.header(WorkflowHttpContentType.HEADER_NAME, "application/json"); - headers.forEach((k, v) -> builder.setHeader(k, v.toString())); - URI uri = httpInfo.uri(); - Map queryParams = httpInfo.queryParams(); - if (!queryParams.isEmpty()) { - uri = addQueryParams(uri, queryParams); - } - builder.uri(uri); - String ifModifiedSince = httpInfo.ifModifiedSince(); - if (ifModifiedSince != null) { - builder.header(WorkflowHttpHelper.IF_MODIFIED_SINCE_HEADER, ifModifiedSince); - } - return WorkflowHttpHelper.processAuth(httpInfo, builder::header, workflowContext, taskContext) - .thenCompose( - __ -> client.sendAsync(builder.build(), HttpResponse.BodyHandlers.ofByteArray())) + BodyPublisher body = buildBody(); + HttpRequestData request = + new HttpRequestData( + httpInfo.uri(), httpInfo.method(), httpInfo.headers(), httpInfo.queryParams(), null); + return transport + .exchange( + request, + body, + builder -> { + if (httpInfo.ifModifiedSince() != null) { + builder.header( + WorkflowHttpHelper.IF_MODIFIED_SINCE_HEADER, httpInfo.ifModifiedSince()); + } + return WorkflowHttpHelper.processAuth( + httpInfo, builder::header, workflowContext, taskContext); + }) .exceptionallyCompose( ex -> CompletableFuture.failedFuture(WorkflowHttpHelper.fromError(ex, taskContext))) .thenApply(response -> new DefaultHttpResponse(app, response)); @@ -79,7 +71,7 @@ protected BodyPublisher buildBody() { return HttpRequest.BodyPublishers.noBody(); } else if (WorkflowHttpContentType.isURLEncodedDataType(contentType)) { return HttpRequest.BodyPublishers.ofString( - encodeMap( + HttpRequestEncoding.encodeMap( WorkflowHttpHelper.requireMap(body, contentType), DefaultHttpRequest::encodeForm)); } else if (isJson(contentType)) { return HttpRequest.BodyPublishers.ofByteArray( @@ -111,25 +103,11 @@ protected boolean isText(String contentType) { return WorkflowHttpContentType.isTextMediaType(contentType); } - private static URI addQueryParams(URI uri, Map queryParams) { - String addedQuery = encodeMap(queryParams, DefaultHttpRequest::encodeQuery); - String rawQuery = uri.getRawQuery(); - return new URIDecodedBuilder(uri) - .query(rawQuery == null || rawQuery.isEmpty() ? addedQuery : rawQuery + "&" + addedQuery) - .build(); - } - - private static String encodeMap(Map map, Function decoder) { - return map.entrySet().stream() - .map(entry -> decoder.apply(entry.getKey()) + "=" + decoder.apply(entry.getValue())) - .collect(Collectors.joining("&")); - } - - private static String encodeForm(Object part) { - return part == null ? "" : URLEncoder.encode(part.toString(), StandardCharsets.UTF_8); + public static String encodeForm(Object part) { + return HttpRequestEncoding.encodeForm(part); } - private static String encodeQuery(Object part) { - return encodeForm(part).replace("+", "%20"); + public static String encodeQuery(Object part) { + return HttpRequestEncoding.encodeQuery(part); } } diff --git a/impl/core/src/main/java/org/openworkflow/sdk/impl/http/DefaultURITemplateResolver.java b/impl/core/src/main/java/org/openworkflow/sdk/impl/http/DefaultURITemplateResolver.java index 6d9373ee4..8729a1cdb 100644 --- a/impl/core/src/main/java/org/openworkflow/sdk/impl/http/DefaultURITemplateResolver.java +++ b/impl/core/src/main/java/org/openworkflow/sdk/impl/http/DefaultURITemplateResolver.java @@ -15,605 +15,13 @@ */ package org.openworkflow.sdk.impl.http; -import java.io.UnsupportedEncodingException; import java.net.URI; -import java.net.URLDecoder; -import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; import java.util.Map; +import org.openworkflow.sdk.impl.executors.openapi.OpenAPIPathResolver; public class DefaultURITemplateResolver implements URITemplateResolver { - @Override public URI resolveTemplates(String uri, Map map) { - return URI.create(expandImpl(uri, map)); - } - - // Private implementation - private enum Operator { - NO_OP, - PLUS, - HASH, - DOT, - SLASH, - SEMICOLON, - QUESTION_MARK, - AMP; - } - - private static void checkVarname(String token, int col) { - if (token.startsWith(".") || token.endsWith(".")) { - throw new IllegalArgumentException( - "Variable name cannot start or end with a dot at col:" + col); - } - if (token.contains("..")) { - throw new IllegalArgumentException( - "Variable name cannot contain consecutive dots at col:" + col); - } - for (int i = 0; i < token.length(); i++) { - if (token.charAt(i) == '%') { - if (i + 2 >= token.length() - || !isHexDigit(token.charAt(i + 1)) - || !isHexDigit(token.charAt(i + 2))) { - throw new IllegalArgumentException( - "Invalid percent-encoding in variable name at col:" + col); - } - } - } - } - - private static boolean isHexDigit(char c) { - return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); - } - - private static void validateLiteral(Character c, int col) { - switch (c) { - case '+': - case '#': - case '/': - case ';': - case '?': - case '&': - case ' ': - case '!': - case '=': - case '$': - case '|': - case '*': - case ':': - case '~': - throw new IllegalArgumentException( - "Illegal character identified in the token at col:" + col); - default: - break; - } - } - - private static int getMaxChar(StringBuilder buffer, int col) { - if (buffer == null || buffer.length() == 0) { - return -1; - } else { - String value = buffer.toString(); - - if (value.isEmpty()) { - return -1; - } else { - try { - if (value.charAt(0) == '0') { - throw new IllegalArgumentException( - "Leading zero not allowed in max chars at col:" + col); - } - int result = Integer.parseInt(value); - if (result < 1 || result > 9999) { - throw new IllegalArgumentException("Max chars out of range (1-9999) at col:" + col); - } - return result; - } catch (NumberFormatException e) { - throw new IllegalArgumentException("Cannot parse max chars at col:" + col); - } - } - } - } - - private static Operator getOperator(Character c, StringBuilder token, int col) { - switch (c) { - case '+': - return Operator.PLUS; - case '#': - return Operator.HASH; - case '.': - return Operator.DOT; - case '/': - return Operator.SLASH; - case ';': - return Operator.SEMICOLON; - case '?': - return Operator.QUESTION_MARK; - case '&': - return Operator.AMP; - default: - validateLiteral(c, col); - token.append(c); - return Operator.NO_OP; - } - } - - private static String expandImpl(String str, Map substitutions) { - final StringBuilder result = new StringBuilder(str.length() * 2); - - boolean toToken = false; - final StringBuilder token = new StringBuilder(); - - Operator operator = null; - boolean composite = false; - boolean toMaxCharBuffer = false; - final StringBuilder maxCharBuffer = new StringBuilder(3); - boolean firstToken = true; - - for (int i = 0; i < str.length(); i++) { - char character = str.charAt(i); - switch (character) { - case '{': - toToken = true; - token.setLength(0); - firstToken = true; - break; - case '}': - if (toToken) { - if (toMaxCharBuffer && maxCharBuffer.length() == 0) { - throw new IllegalArgumentException("Empty prefix modifier at col:" + i); - } - boolean expanded = - expandToken( - operator, - token.toString(), - composite, - getMaxChar(maxCharBuffer, i), - firstToken, - substitutions, - result, - i); - if (expanded && firstToken) { - firstToken = false; - } - toToken = false; - token.setLength(0); - operator = null; - composite = false; - toMaxCharBuffer = false; - maxCharBuffer.setLength(0); - } else { - throw new IllegalArgumentException("Failed to expand token, invalid at col:" + i); - } - break; - case ',': - if (toToken) { - if (toMaxCharBuffer && maxCharBuffer.length() == 0) { - throw new IllegalArgumentException("Empty prefix modifier at col:" + i); - } - boolean expanded = - expandToken( - operator, - token.toString(), - composite, - getMaxChar(maxCharBuffer, i), - firstToken, - substitutions, - result, - i); - if (expanded && firstToken) { - firstToken = false; - } - token.setLength(0); - composite = false; - toMaxCharBuffer = false; - maxCharBuffer.setLength(0); - break; - } - // Intentional fall-through for commas outside the {} - default: - if (toToken) { - if (operator == null) { - operator = getOperator(character, token, i); - } else if (toMaxCharBuffer) { - if (Character.isDigit(character)) { - maxCharBuffer.append(character); - } else { - throw new IllegalArgumentException( - "Illegal character identified in the token at col:" + i); - } - } else { - if (character == ':') { - toMaxCharBuffer = true; - maxCharBuffer.setLength(0); - } else if (character == '*') { - composite = true; - } else { - validateLiteral(character, i); - token.append(character); - } - } - } else { - if (character > 0x7F || Character.isHighSurrogate(character)) { - String toEncode; - if (Character.isHighSurrogate(character) - && i + 1 < str.length() - && Character.isLowSurrogate(str.charAt(i + 1))) { - toEncode = new String(new char[] {character, str.charAt(++i)}); - } else { - toEncode = Character.toString(character); - } - for (byte b : toEncode.getBytes(StandardCharsets.UTF_8)) { - result.append(String.format("%%%02X", b & 0xFF)); - } - } else { - result.append(character); - } - } - break; - } - } - - if (!toToken) { - return result.toString(); - } else { - throw new IllegalArgumentException("Unterminated token"); - } - } - - private static void addPrefix(Operator op, StringBuilder result) { - switch (op) { - case HASH: - result.append('#'); - break; - case DOT: - result.append('.'); - break; - case SLASH: - result.append('/'); - break; - case SEMICOLON: - result.append(';'); - break; - case QUESTION_MARK: - result.append('?'); - break; - case AMP: - result.append('&'); - break; - default: - return; - } - } - - private static void addSeparator(Operator op, StringBuilder result) { - switch (op) { - case DOT: - result.append('.'); - break; - case SLASH: - result.append('/'); - break; - case SEMICOLON: - result.append(';'); - break; - case QUESTION_MARK: - case AMP: - result.append('&'); - break; - default: - result.append(','); - return; - } - } - - private static void addValue( - Operator op, String token, Object value, StringBuilder result, int maxChar) { - switch (op) { - case PLUS: - case HASH: - addExpandedValue(null, value, result, maxChar, false); - break; - case QUESTION_MARK: - case AMP: - result.append(token + '='); - addExpandedValue(null, value, result, maxChar, true); - break; - case SEMICOLON: - result.append(token); - addExpandedValue("=", value, result, maxChar, true); - break; - case DOT: - case SLASH: - case NO_OP: - addExpandedValue(null, value, result, maxChar, true); - } - } - - private static void addValueElement( - Operator op, String token, Object value, StringBuilder result, int maxChar) { - switch (op) { - case PLUS: - case HASH: - addExpandedValue(null, value, result, maxChar, false); - break; - case QUESTION_MARK: - case AMP: - case SEMICOLON: - case DOT: - case SLASH: - case NO_OP: - addExpandedValue(null, value, result, maxChar, true); - } - } - - private static boolean isSurrogate(char cp) { - return (cp >= 0xD800 && cp <= 0xDFFF); - } - - private static boolean isIprivate(char cp) { - return (0xE000 <= cp && cp <= 0xF8FF); - } - - private static boolean isUcschar(char cp) { - return (0xA0 <= cp && cp <= 0xD7FF) - || (0xF900 <= cp && cp <= 0xFDCF) - || (0xFDF0 <= cp && cp <= 0xFFEF); - } - - private static void addExpandedValue( - String prefix, Object value, StringBuilder result, int maxChar, boolean replaceReserved) { - String stringValue = convertNativeTypes(value); - int cpCount = stringValue.codePointCount(0, stringValue.length()); - int max = (maxChar != -1) ? Math.min(maxChar, cpCount) : cpCount; - result.ensureCapacity(max * 2); // hint to SB - boolean toReserved = false; - final StringBuilder reservedBuffer = new StringBuilder(3); - - if (max > 0 && prefix != null) { - result.append(prefix); - } - - int charCount = 0; - for (int i = 0; i < stringValue.length() && charCount < max; i++) { - char character = stringValue.charAt(i); - charCount++; - - if (character == '%' && !replaceReserved) { - toReserved = true; - reservedBuffer.setLength(0); - } - - String toAppend = Character.toString(character); - try { - if (isSurrogate(character)) { - StringBuilder sb = new StringBuilder(); - sb.append(Character.toChars(stringValue.codePointAt(i++))); - toAppend = URLEncoder.encode(sb.toString(), StandardCharsets.UTF_8.name()); - } else if (replaceReserved || isUcschar(character) || isIprivate(character)) { - toAppend = - URLEncoder.encode(toAppend, StandardCharsets.UTF_8.name()) - .replace("*", "%2A") - .replace("%7E", "~"); - } - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - - if (toReserved) { - reservedBuffer.append(toAppend); - - if (reservedBuffer.length() == 3) { - boolean isEncoded = false; - try { - URLDecoder.decode(reservedBuffer.toString(), StandardCharsets.UTF_8.name()); - isEncoded = true; - } catch (Exception e) { - // ignore - } - - if (isEncoded) { - result.append(reservedBuffer); - } else { - result.append("%25"); - // only if !replaceReserved - result.append(reservedBuffer.substring(1)); - } - toReserved = false; - reservedBuffer.setLength(0); - } - } else { - if (character == ' ') { - result.append("%20"); - } else if (character == '%') { - result.append("%25"); - } else { - result.append(toAppend); - } - } - } - - if (toReserved) { - result.append("%25"); - result.append(reservedBuffer.substring(1)); - } - } - - private static boolean isList(Object value) { - return value instanceof ArrayList - || // checking concrete instances first as it's faster - value instanceof List; - } - - private static boolean isMap(Object value) { - return value instanceof HashMap - || // checking concrete instances first as it's faster - value instanceof Map; - } - - enum SubstitutionType { - EMPTY, - STRING, - LIST, - MAP; - } - - private static SubstitutionType getSubstitutionType(Object value, int col) { - if (value == null) { - return SubstitutionType.EMPTY; - } else if (isNativeType(value)) { - return SubstitutionType.STRING; - } else if (isList(value)) { - return SubstitutionType.LIST; - } else if (isMap(value)) { - return SubstitutionType.MAP; - } else { - throw new IllegalArgumentException( - "Illegal class passed as substitution, found " + value.getClass() + " at col:" + col); - } - } - - private static boolean isEmpty(SubstitutionType substType, Object value) { - if (value == null) { - return true; - } else { - switch (substType) { - case STRING: - return false; - case LIST: - return ((List) value).isEmpty(); - case MAP: - return ((Map) value).isEmpty(); - default: - return true; - } - } - } - - private static boolean isNativeType(Object value) { - if (value instanceof String || value instanceof Boolean || value instanceof Number) { - return true; - } - return false; - } - - private static String convertNativeTypes(Object value) { - if (value instanceof String) { - return (String) value; - } else if (value instanceof Boolean || value instanceof Number) { - return value.toString(); - } - throw new IllegalArgumentException( - "Illegal class passed as substitution, found " + value.getClass()); - } - - // returns true if expansion happened - private static boolean expandToken( - Operator operator, - String token, - boolean composite, - int maxChar, - boolean firstToken, - Map substitutions, - StringBuilder result, - int col) { - if (token.isEmpty()) { - throw new IllegalArgumentException("Found an empty token at col:" + col); - } - - checkVarname(token, col); - - Object value = substitutions.get(token); - SubstitutionType substType = getSubstitutionType(value, col); - if (substType == SubstitutionType.EMPTY || isEmpty(substType, value)) { - return false; - } - - if (firstToken) { - addPrefix(operator, result); - } else { - addSeparator(operator, result); - } - - switch (substType) { - case STRING: - addStringValue(operator, token, (Object) value, result, maxChar); - break; - case LIST: - addListValue(operator, token, (List) value, result, maxChar, composite); - break; - case MAP: - addMapValue(operator, token, (Map) value, result, maxChar, composite); - break; - } - - return true; - } - - private static boolean addStringValue( - Operator operator, String token, Object value, StringBuilder result, int maxChar) { - addValue(operator, token, value, result, maxChar); - return true; - } - - private static boolean addListValue( - Operator operator, - String token, - List value, - StringBuilder result, - int maxChar, - boolean composite) { - boolean first = true; - for (Object v : value) { - if (first) { - addValue(operator, token, v, result, maxChar); - first = false; - } else { - if (composite) { - addSeparator(operator, result); - addValue(operator, token, v, result, maxChar); - } else { - result.append(','); - addValueElement(operator, token, v, result, maxChar); - } - } - } - return !first; - } - - private static boolean addMapValue( - Operator operator, - String token, - Map value, - StringBuilder result, - int maxChar, - boolean composite) { - boolean first = true; - if (maxChar != -1) { - throw new IllegalArgumentException("Value trimming is not allowed on Maps"); - } - for (Map.Entry v : value.entrySet()) { - if (composite) { - if (!first) { - addSeparator(operator, result); - } - addValueElement(operator, token, v.getKey(), result, maxChar); - result.append('='); - } else { - if (first) { - addValue(operator, token, v.getKey(), result, maxChar); - } else { - result.append(','); - addValueElement(operator, token, v.getKey(), result, maxChar); - } - result.append(','); - } - addValueElement(operator, token, v.getValue(), result, maxChar); - first = false; - } - return !first; + return new OpenAPIPathResolver().resolveTemplates(uri, map); } } diff --git a/impl/core/src/main/java/org/openworkflow/sdk/impl/http/WorkflowHttpRequestInfoBuilderFactory.java b/impl/core/src/main/java/org/openworkflow/sdk/impl/http/WorkflowHttpRequestInfoBuilderFactory.java index 60bf33a25..d71425b99 100644 --- a/impl/core/src/main/java/org/openworkflow/sdk/impl/http/WorkflowHttpRequestInfoBuilderFactory.java +++ b/impl/core/src/main/java/org/openworkflow/sdk/impl/http/WorkflowHttpRequestInfoBuilderFactory.java @@ -49,6 +49,24 @@ private WorkflowHttpRequestInfoBuilderFactory(WorkflowDefinition definition) { this.definition = definition; } + /** + * Copies this template's configuration without resolving authentication again. The returned + * factory owns its mutable builder fields; configured providers and resolvers retain their + * existing lifetime and are shared. + */ + public WorkflowHttpRequestInfoBuilderFactory copy() { + WorkflowHttpRequestInfoBuilderFactory copy = + new WorkflowHttpRequestInfoBuilderFactory(definition); + copy.pathSupplier = pathSupplier; + copy.bodyFilter = bodyFilter; + copy.method = method; + copy.auth = auth; + copy.redirect = redirect; + copy.headers = headers; + copy.queryMap = queryMap; + return copy; + } + public WorkflowHttpRequestInfoBuilderFactory withAuth(ReferenceableAuthenticationPolicy policy) { if (policy != null) { if (policy.getAuthenticationPolicyReference() != null) { diff --git a/impl/core/src/test/java/org/openworkflow/sdk/impl/http/DefaultHttpResponseTest.java b/impl/core/src/test/java/org/openworkflow/sdk/impl/http/DefaultHttpResponseTest.java new file mode 100644 index 000000000..6c17b70bf --- /dev/null +++ b/impl/core/src/test/java/org/openworkflow/sdk/impl/http/DefaultHttpResponseTest.java @@ -0,0 +1,81 @@ +/* + * Copyright 2020-Present The Open Workflow Specification Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openworkflow.sdk.impl.http; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.net.http.HttpClient; +import java.net.http.HttpHeaders; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.Test; +import org.openworkflow.sdk.impl.WorkflowApplication; + +class DefaultHttpResponseTest { + + @Test + void rawResponse_shouldRetainNativeHttpVersion() { + HttpResponse nativeResponse = nativeResponse(); + when(nativeResponse.version()).thenReturn(HttpClient.Version.HTTP_2); + when(nativeResponse.body()).thenReturn(new byte[0]); + DefaultHttpResponse response = + new DefaultHttpResponse(mock(WorkflowApplication.class), nativeResponse); + assertThat(new String(response.responseAsByteArray(), StandardCharsets.ISO_8859_1)) + .startsWith("HTTP/2 204\r\n"); + } + + @Test + void nativeConstructor_shouldAcceptNativeResponsesWithNullBodies() { + HttpResponse nativeResponse = nativeResponse(); + DefaultHttpResponse response = + new DefaultHttpResponse(mock(WorkflowApplication.class), nativeResponse); + assertThat(response.contentAsByteArray()).isNull(); + assertThat(response.status()).isEqualTo(204); + assertThat(response.getHeader("x-result")).isEqualTo("value"); + assertThat(response.errorDetails()).isEqualTo("No error information"); + assertThat(response.lastModified()).hasValue(784111777000L); + } + + @Test + void nativeConstructor_shouldRetainNativeBodyAccess() { + HttpResponse nativeResponse = nativeResponse(); + byte[] body = "message".getBytes(StandardCharsets.UTF_8); + when(nativeResponse.body()).thenReturn(body); + DefaultHttpResponse response = + new DefaultHttpResponse(mock(WorkflowApplication.class), nativeResponse); + assertThat(response.contentAsByteArray()).isSameAs(body); + response.contentAsByteArray()[0] = 'M'; + assertThat(response.errorDetails()).isEqualTo("Message"); + } + + @SuppressWarnings("unchecked") + private static HttpResponse nativeResponse() { + HttpResponse response = mock(HttpResponse.class); + when(response.statusCode()).thenReturn(204); + when(response.headers()) + .thenReturn( + HttpHeaders.of( + Map.of( + "X-Result", List.of("value"), + "Last-Modified", List.of("Sun, 06 Nov 1994 08:49:37 GMT")), + (name, value) -> true)); + return response; + } +} diff --git a/impl/core/src/test/java/org/openworkflow/sdk/impl/http/HttpErrorTranslationTest.java b/impl/core/src/test/java/org/openworkflow/sdk/impl/http/HttpErrorTranslationTest.java new file mode 100644 index 000000000..b13479a47 --- /dev/null +++ b/impl/core/src/test/java/org/openworkflow/sdk/impl/http/HttpErrorTranslationTest.java @@ -0,0 +1,51 @@ +/* + * Copyright 2020-Present The Open Workflow Specification Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openworkflow.sdk.impl.http; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.net.SocketTimeoutException; +import java.net.http.HttpTimeoutException; +import java.util.List; +import java.util.concurrent.CompletionException; +import java.util.concurrent.TimeoutException; +import org.junit.jupiter.api.Test; +import org.openworkflow.sdk.impl.TaskContext; +import org.openworkflow.sdk.impl.WorkflowPosition; +import org.openworkflow.sdk.types.Errors; + +class HttpErrorTranslationTest { + + @Test + void fromError_shouldPreserveTimeoutClassificationAndTaskPosition() { + TaskContext task = mock(TaskContext.class); + WorkflowPosition position = mock(WorkflowPosition.class); + when(task.position()).thenReturn(position); + when(position.jsonPointer()).thenReturn("do/0/callApi"); + for (Exception cause : + List.of( + new TimeoutException("deadline"), + new SocketTimeoutException("read"), + new HttpTimeoutException("request"))) { + var error = + WorkflowHttpHelper.fromError(new CompletionException(cause), task).getWorkflowError(); + assertThat(error.status()).isEqualTo(Errors.TIMEOUT.status()); + assertThat(error.instance()).isEqualTo("do/0/callApi"); + } + } +} diff --git a/impl/http/pom.xml b/impl/http/pom.xml index fe34db6d2..37fc15ff1 100644 --- a/impl/http/pom.xml +++ b/impl/http/pom.xml @@ -8,6 +8,10 @@ openworkflow-impl-http Open Workflow :: Impl :: HTTP + + org.openworkflow.sdk + openworkflow-common-http-jakarta + org.openworkflow.sdk openworkflow-impl-core @@ -47,4 +51,4 @@ test - \ No newline at end of file + diff --git a/impl/http/src/main/java/org/openworkflow/sdk/impl/jakarta/http/JakartaHttpRequest.java b/impl/http/src/main/java/org/openworkflow/sdk/impl/jakarta/http/JakartaHttpRequest.java index 12d66b896..c9e769afc 100644 --- a/impl/http/src/main/java/org/openworkflow/sdk/impl/jakarta/http/JakartaHttpRequest.java +++ b/impl/http/src/main/java/org/openworkflow/sdk/impl/jakarta/http/JakartaHttpRequest.java @@ -17,16 +17,14 @@ import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.client.Client; -import jakarta.ws.rs.client.Invocation.Builder; -import jakarta.ws.rs.client.WebTarget; import jakarta.ws.rs.core.MediaType; -import jakarta.ws.rs.core.Response; import java.util.Collection; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionStage; +import java.util.concurrent.atomic.AtomicReference; +import org.openworkflow.sdk.http.HttpRequestData; +import org.openworkflow.sdk.http.jakarta.JakartaHttpTransport; import org.openworkflow.sdk.impl.TaskContext; import org.openworkflow.sdk.impl.WorkflowApplication; import org.openworkflow.sdk.impl.WorkflowContext; @@ -58,56 +56,56 @@ public JakartaHttpRequest( @Override public CompletableFuture invoke( WorkflowContext workflowContext, TaskContext taskContext) { - WebTarget target = + Client client = application .additionalObject( HttpClientResolver.HTTP_CLIENT_PROVIDER, workflowContext, taskContext) - .orElse(defaultClient) - .target(requestInfo.uri()); - Map queryParams = requestInfo.queryParams(); - for (Entry entry : queryParams.entrySet()) { - target = target.queryParam(entry.getKey(), entry.getValue()); - } - Builder request = target.request(); - String ifModifiedSince = requestInfo.ifModifiedSince(); - if (ifModifiedSince != null) { - request.header(WorkflowHttpHelper.IF_MODIFIED_SINCE_HEADER, ifModifiedSince); - } - requestInfo.headers().forEach(request::header); - if (workflowContext != null) { - requestDecorators.forEach(d -> d.decorate(request, workflowContext, taskContext)); - } - return WorkflowHttpHelper.processAuth( - requestInfo, request::header, workflowContext, taskContext) - .thenCompose(__ -> - doRequest( - request, - HttpConverterResolver.converter(application, workflowContext, taskContext), - taskContext)) - .exceptionallyCompose(ex -> - ex instanceof WebApplicationException webAppEx - ? CompletableFuture.failedFuture( - new WorkflowException( + .orElse(defaultClient); + HttpRequestData request = + new HttpRequestData( + requestInfo.uri(), requestInfo.method(), Map.of(), requestInfo.queryParams(), null); + AtomicReference converter = new AtomicReference<>(); + CompletableFuture result = new CompletableFuture<>(); + new JakartaHttpTransport(client) + .exchange( + request, + builder -> { + if (requestInfo.ifModifiedSince() != null) { + builder.header( + WorkflowHttpHelper.IF_MODIFIED_SINCE_HEADER, requestInfo.ifModifiedSince()); + } + requestInfo.headers().forEach(builder::header); + if (workflowContext != null) { + requestDecorators.forEach(d -> d.decorate(builder, workflowContext, taskContext)); + } + return WorkflowHttpHelper.processAuth( + requestInfo, builder::header, workflowContext, taskContext) + .thenApply(ignored -> { + // Preserve resolver timing: custom converters may inspect authorization. + HttpModelConverter resolved = + HttpConverterResolver.converter(application, workflowContext, taskContext); + converter.set(resolved); + return requestInfo.body() == null || requestInfo.body().isNull() + ? null + : resolved.buildEntity( + requestInfo.body(), MediaType.valueOf(requestInfo.contentType())); + }); + }) + .whenComplete((response, error) -> { + if (error != null) { + result.completeExceptionally( + error instanceof WebApplicationException webAppEx + ? new WorkflowException( WorkflowError.communication( webAppEx.getResponse().getStatus(), taskContext, webAppEx) .build(), - webAppEx)) - : CompletableFuture.failedFuture(WorkflowHttpHelper.fromError(ex, taskContext))); - } - - private CompletableFuture doRequest( - Builder request, HttpModelConverter converter, TaskContext task) { - CompletionStage future = - requestInfo.body() == null || requestInfo.body().isNull() - ? request.rx().method(requestInfo.method()) - : request - .rx() - .method( - requestInfo.method(), - converter.buildEntity( - requestInfo.body(), MediaType.valueOf(requestInfo.contentType()))); - return future - .toCompletableFuture() - .thenApply(r -> new JakartaHttpResponse(application, task, converter, r)); + webAppEx) + : WorkflowHttpHelper.fromError(error, taskContext)); + } else if (!result.complete( + new JakartaHttpResponse(application, taskContext, converter.get(), response))) { + response.close(); + } + }); + return result; } } diff --git a/impl/openapi-jackson/pom.xml b/impl/openapi-jackson/pom.xml index 0a58c25ec..e6c61514e 100644 --- a/impl/openapi-jackson/pom.xml +++ b/impl/openapi-jackson/pom.xml @@ -8,6 +8,9 @@ openworkflow-impl-openapi Open Workflow :: Impl :: OpenAPI:: Jackson +org.openworkflow.sdkopenworkflow-common-openapi +com.squareup.okhttp3mockwebservertest + org.openworkflow.sdk openworkflow-impl-core diff --git a/impl/openapi-jackson/src/test/java/org/openworkflow/sdk/impl/executors/openapi/jackson/StandaloneOpenAPITest.java b/impl/openapi-jackson/src/test/java/org/openworkflow/sdk/impl/executors/openapi/jackson/StandaloneOpenAPITest.java new file mode 100644 index 000000000..6e57c2357 --- /dev/null +++ b/impl/openapi-jackson/src/test/java/org/openworkflow/sdk/impl/executors/openapi/jackson/StandaloneOpenAPITest.java @@ -0,0 +1,497 @@ +/* + * Copyright 2020-Present The Open Workflow Specification Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openworkflow.sdk.impl.executors.openapi.jackson; + +import static org.assertj.core.api.Assertions.*; + +import java.io.IOException; +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.TimeUnit; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import org.junit.jupiter.api.Test; +import org.openworkflow.sdk.api.WorkflowFormat; +import org.openworkflow.sdk.http.*; +import org.openworkflow.sdk.impl.executors.openapi.*; +import org.openworkflow.sdk.impl.executors.openapi.OpenAPIClient.ServerFallback; + +/** Compiled third-party usage example using the optional SDK Jackson reader. */ +class StandaloneOpenAPITest { + private static final String DOCUMENT = + """ + { + "openapi":"3.0.3", + "servers":[{"url":"%s"}], + "paths":{"/pets/{id}":{"post":{ + "operationId":"save", + "parameters":[ + {"name":"id","in":"path","required":true,"schema":{"type":"string"}}, + {"name":"q","in":"query","required":false,"schema":{"type":"string"}}, + {"name":"lang","in":"query","required":true,"schema":{"default":"en"}}, + {"name":"unused","in":"query","schema":{"default":"ignored"}}, + {"name":"Authorization","in":"header","schema":{"type":"string"}}, + {"name":"cookieValue","in":"cookie","schema":{"type":"string"}} + ], + "requestBody":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/Pet"}}}} + }}}, + "components":{"schemas":{"Pet":{"type":"object","required":["name"],"properties":{"name":{"type":"string"}}}}} + } + """; + + private UnifiedOpenAPI read(String server) { + return new JacksonUnifiedOpenAPIReader() + .read("api.json", DOCUMENT.formatted(server).getBytes(StandardCharsets.UTF_8)); + } + + private byte[] json(Map value) { + return WorkflowFormat.JSON.mapper().writeValueAsBytes(value); + } + + @Test + void discoversTheSdkReaderFromTheExistingArtifact() { + var reader = + java.util.ServiceLoader.load( + org.openworkflow.sdk.impl.additional.NamedSuppliedAdditionalObject.class) + .stream() + .map(java.util.ServiceLoader.Provider::get) + .filter( + provider -> provider.name().equals(UnifiedOpenAPIReader.UNIFIED_OPEN_API_READER)) + .findFirst() + .orElseThrow() + .get(); + assertThat(reader instanceof JacksonUnifiedOpenAPIReader).isTrue(); + } + + @Test + void bindsInheritedReferencesAndSharedParameterNames() { + var document = + new JacksonUnifiedOpenAPIReader() + .read( + "api.yaml", + """ + openapi: 3.0.3 + servers: + - url: https://example.test + paths: + /items/{id}: + parameters: + - name: id + in: path + required: true + - $ref: '#/components/parameters/IdAlias' + - name: lang + in: query + required: true + schema: {default: en} + post: + operationId: save + parameters: + - name: id + in: query + required: true + - name: lang + in: query + required: true + schema: {default: fr} + components: + parameters: + IdAlias: + $ref: '#/components/parameters/Id' + Id: + name: id + in: header + required: true + """ + .getBytes(StandardCharsets.UTF_8)); + var api = new OpenAPIClient(document, "save"); + var operation = api.operation(); + var reconstructed = + new OperationDefinition( + document, operation.getOperation(), operation.getPath(), operation.getMethod()); + assertThat(reconstructed.getParameters()).isEqualTo(operation.getParameters()); + var request = api.prepare(0, Map.of("id", "shared", "extra", "body"), this::json); + assertThat(request.uri()).isEqualTo(URI.create("https://example.test/items/shared")); + assertThat(request.headers()).isEqualTo(Map.of("id", "shared")); + assertThat(request.queryParams()).isEqualTo(Map.of("id", "shared", "lang", "fr")); + assertThat(WorkflowFormat.JSON.mapper().readValue(request.body(), Map.class)) + .isEqualTo(Map.of("extra", "body")); + } + + @Test + void selectsOperationThenPathThenRootServers() { + var document = + new JacksonUnifiedOpenAPIReader() + .read( + "api.yaml", + """ + openapi: 3.0.3 + servers: + - url: /root + paths: + /items: + servers: + - url: /path + get: + operationId: operation + servers: + - url: /operation + post: + operationId: path + /other: + get: + operationId: root + """ + .getBytes(StandardCharsets.UTF_8)); + for (var expected : + Map.of( + "operation", "https://example.test/operation/items", + "path", "https://example.test/path/items", + "root", "https://example.test/root/other") + .entrySet()) { + var api = + new OpenAPIClient( + document, expected.getKey(), URI.create("https://example.test/api.yaml")); + var operation = api.operation(); + var reconstructed = + new OperationDefinition( + document, operation.getOperation(), operation.getPath(), operation.getMethod()); + assertThat(reconstructed.getServers()).isEqualTo(operation.getServers()); + assertThat(api.prepare(0, Map.of(), this::json).uri()) + .isEqualTo(URI.create(expected.getValue())); + } + } + + @Test + void rejectsServerUrisWithoutAnAbsoluteHost() { + for (String server : List.of("https:/api", "https:api", "https:///api")) { + var api = withServers(List.of(server)); + assertThatExceptionOfType(IllegalArgumentException.class) + .as(server) + .isThrownBy(() -> api.prepare(0, Map.of("id", 1), this::json)); + } + var bound = + OpenAPIRequest.bind( + withServers(List.of("https://example.test")).operation(), Map.of("id", 1)); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> bound.prepare(URI.create("/relative"), this::json)); + } + + @Test + void fallbackOwnsSerializedBodySnapshot() { + var api = withServers(List.of("http://first", "http://second")); + byte[] encoded = "body".getBytes(StandardCharsets.UTF_8); + List requests = new ArrayList<>(); + CompletableFuture first = new CompletableFuture<>(); + HttpTransport transport = + new HttpTransport() { + public CompletableFuture execute(HttpRequestData request) { + requests.add(request); + return requests.size() == 1 + ? first + : CompletableFuture.completedFuture( + new HttpResponseData(200, Map.of(), new byte[0])); + } + + public void close() { + fail("must remain borrowed"); + } + }; + var result = + api.execute( + Map.of("id", 1), + ignored -> encoded, + transport, + ServerFallback.ON_HTTP_OR_TRANSPORT_FAILURE); + encoded[0] = 'X'; + first.complete(new HttpResponseData(503, Map.of(), new byte[0])); + assertThat(result.join().status()).isEqualTo(200); + assertThat(requests.size()).isEqualTo(2); + assertThat(requests.get(1).body()).isEqualTo("body".getBytes(StandardCharsets.UTF_8)); + } + + @Test + void packagesReflectionMetadataWithStandaloneReader() throws Exception { + try (var stream = + getClass() + .getResourceAsStream( + "/META-INF/native-image/org.openworkflow.sdk/openworkflow-impl-openapi/reflect-config.json")) { + assertThat(stream).isNotNull(); + var registrations = WorkflowFormat.JSON.mapper().readValue(stream, List.class); + var names = registrations.stream().map(entry -> ((Map) entry).get("name")).toList(); + assertThat(names.contains(UnifiedOpenAPI.class.getName())).isTrue(); + for (Class nested : UnifiedOpenAPI.class.getDeclaredClasses()) { + assertThat(names.contains(nested.getName())).as(nested.getName()).isTrue(); + } + } + } + + @Test + void documentToRequestToInjectedJdkTransport() throws Exception { + try (MockWebServer server = new MockWebServer(); + JdkHttpTransport transport = new JdkHttpTransport()) { + server.start(); + server.enqueue(new MockResponse().setResponseCode(201).setBody("saved")); + OpenAPIClient api = new OpenAPIClient(read(server.url("/v1").toString()), "save"); + HttpRequestData request = + api.prepare( + 0, + Map.of( + "id", + "café /+", + "q", + "tea + café", + "name", + "N", + "Authorization", + "Bearer resolved"), + this::json); + assertThat(request.method()).isEqualTo("POST"); + assertThat(request.uri().getRawPath()).isEqualTo("/v1/pets/caf%C3%A9%20%2F%2B"); + assertThat(request.queryParams()).isEqualTo(Map.of("q", "tea + café", "lang", "en")); + HttpResponseData response = transport.execute(request).get(10, TimeUnit.SECONDS); + assertThat(response.status()).isEqualTo(201); + assertThat(new String(response.body(), StandardCharsets.UTF_8)).isEqualTo("saved"); + var received = server.takeRequest(10, TimeUnit.SECONDS); + assertThat(received).isNotNull(); + assertThat(received.getHeader("Authorization")).isEqualTo("Bearer resolved"); + assertThat(received.getPath().contains("q=tea%20%2B%20caf%C3%A9")).isTrue(); + assertThat( + WorkflowFormat.JSON.mapper().readValue(received.getBody().readByteArray(), Map.class)) + .isEqualTo(Map.of("name", "N")); + } + } + + @Test + void validatesDocumentOperationAndRequiredPath() { + var reader = new JacksonUnifiedOpenAPIReader(); + assertThatExceptionOfType(RuntimeException.class) + .isThrownBy(() -> reader.read("broken.json", "{".getBytes(StandardCharsets.UTF_8))); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> new OpenAPIClient(read("http://localhost"), "absent")) + .withMessageContaining("absent"); + OpenAPIClient api = new OpenAPIClient(read("http://localhost"), "save"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> api.prepare(0, Map.of(), this::json)) + .withMessageContaining("id"); + Map nullId = new HashMap<>(); + nullId.put("id", null); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> api.prepare(0, nullId, this::json)); + } + + @Test + void retainsRequiredDefaultsLeftoverBodyAndUnvalidatedBodyProperties() { + OpenAPIClient api = new OpenAPIClient(read("http://localhost"), "save"); + // Body required fields and cookies were never validated/placed by the SDK. + HttpRequestData request = + api.prepare(0, Map.of("id", 3, "extra", 4, "cookieValue", "c"), this::json); + assertThat(request.queryParams()).isEqualTo(Map.of("lang", "en")); + assertThat(WorkflowFormat.JSON.mapper().readValue(request.body(), Map.class)) + .isEqualTo(Map.of("extra", 4, "cookieValue", "c")); + assertThat(api.operation().getParameters().stream().filter(p -> p.in().equals("body")).count()) + .isEqualTo(1); + } + + @Test + void readsSwaggerJsonAndOpenApiYamlAndLocalReferences() { + var reader = new JacksonUnifiedOpenAPIReader(); + for (String format : List.of("json", "yaml")) { + String text = + """ + {"swagger":"2.0","host":"localhost:1234","schemes":["http"],"basePath":"/v2", + "paths":{"/pet":{"post":{"operationId":"add","parameters":[{"name":"body","in":"body","schema":{"$ref":"#/definitions/Pet"}}]}}}, + "definitions":{"Pet":{"properties":{"name":{"type":"string"}}}}} + """; + var document = reader.read("api." + format, text.getBytes(StandardCharsets.UTF_8)); + var api = new OpenAPIClient(document, "add"); + assertThat(document.swaggerVersion()).isEqualTo(UnifiedOpenAPI.SwaggerVersion.SWAGGER_V2); + assertThat(api.prepare(0, Map.of("name", "pet"), this::json).uri()) + .isEqualTo(URI.create("http://localhost:1234/v2/pet")); + assertThat(api.operation().getParameters().getFirst().in()).isEqualTo("body"); + } + var yaml = + reader.read( + "api.yaml", + """ + openapi: 3.0.3 + servers: + - url: /v1 + paths: + /pet: + get: + operationId: get + """ + .getBytes(StandardCharsets.UTF_8)); + var api = new OpenAPIClient(yaml, "get", URI.create("http://localhost/spec/api.yaml")); + assertThat(api.prepare(0, Map.of(), this::json).uri()) + .isEqualTo(URI.create("http://localhost/v1/pet")); + assertThat(api.prepare(0, Map.of(), this::json).body()).isNull(); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> new OpenAPIClient(yaml, "get").prepare(0, Map.of(), this::json)); + } + + private OpenAPIClient withServers(List servers) { + UnifiedOpenAPI d = read("http://localhost"); + return new OpenAPIClient( + new UnifiedOpenAPI( + d.swagger(), + servers.stream().map(UnifiedOpenAPI.Server::new).toList(), + d.host(), + d.basePath(), + d.schemes(), + d.paths(), + d.components(), + d.definitions(), + d.parameters()), + "save"); + } + + @Test + void fallbackIsExplicitAndPreservesHttpResponsesAndTransportErrors() throws Exception { + try (MockWebServer first = new MockWebServer(); + MockWebServer second = new MockWebServer(); + JdkHttpTransport transport = new JdkHttpTransport()) { + first.start(); + second.start(); + var api = withServers(List.of(first.url("/").toString(), second.url("/").toString())); + first.enqueue(new MockResponse().setResponseCode(503).setBody("first")); + assertThat( + api.execute(Map.of("id", 1), this::json, transport, ServerFallback.NONE) + .get(10, TimeUnit.SECONDS) + .status()) + .isEqualTo(503); + assertThat(second.getRequestCount()).isEqualTo(0); + first.enqueue(new MockResponse().setResponseCode(503)); + second.enqueue(new MockResponse().setResponseCode(409).setBody("conflict")); + HttpResponseData finalResponse = + api.execute( + Map.of("id", 1), + this::json, + transport, + ServerFallback.ON_HTTP_OR_TRANSPORT_FAILURE) + .get(10, TimeUnit.SECONDS); + assertThat(finalResponse.status()).isEqualTo(409); + assertThat(new String(finalResponse.body(), StandardCharsets.UTF_8)).isEqualTo("conflict"); + assertThat(second.getRequestCount()).isEqualTo(1); + assertThat(second.takeRequest(10, TimeUnit.SECONDS).getMethod()).isEqualTo("POST"); + } + IOException failure = new IOException("network failure"); + HttpTransport failed = + new HttpTransport() { + public CompletableFuture execute(HttpRequestData request) { + return CompletableFuture.failedFuture(failure); + } + + public void close() { + fail("OpenAPI does not own the transport"); + } + }; + var api = withServers(List.of("http://first", "http://second")); + assertThat( + catchThrowableOfType( + CompletionException.class, + () -> + api.execute( + Map.of("id", 1), + this::json, + failed, + ServerFallback.ON_HTTP_OR_TRANSPORT_FAILURE) + .join()) + .getCause()) + .isSameAs(failure); + List attempted = new ArrayList<>(); + HttpTransport recovering = + new HttpTransport() { + public CompletableFuture execute(HttpRequestData request) { + attempted.add(request.uri()); + return attempted.size() == 1 + ? CompletableFuture.failedFuture(failure) + : CompletableFuture.completedFuture( + new HttpResponseData(200, Map.of(), new byte[0])); + } + + public void close() { + fail("OpenAPI does not own the transport"); + } + }; + assertThat( + api.execute( + Map.of("id", 2), + this::json, + recovering, + ServerFallback.ON_HTTP_OR_TRANSPORT_FAILURE) + .join() + .status()) + .isEqualTo(200); + assertThat(attempted) + .isEqualTo(List.of(URI.create("http://first/pets/2"), URI.create("http://second/pets/2"))); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> withServers(List.of()).prepare(0, Map.of("id", 1), this::json)); + } + + @Test + void concurrentInvocationsShareOnlyOperationDefinition() { + var api = new OpenAPIClient(read("http://localhost"), "save"); + List requests = new ArrayList<>(); + List> pending = new ArrayList<>(); + HttpTransport transport = + new HttpTransport() { + public CompletableFuture execute(HttpRequestData request) { + requests.add(request); + var result = new CompletableFuture(); + pending.add(result); + return result; + } + + public void close() { + fail("must remain borrowed"); + } + }; + var first = + api.execute( + Map.of("id", "one", "name", "first", "Authorization", "Bearer one"), + this::json, + transport, + ServerFallback.NONE); + var second = + api.execute( + Map.of("id", "two", "name", "second", "Authorization", "Bearer two"), + this::json, + transport, + ServerFallback.NONE); + assertThat(first.isDone()).isFalse(); + assertThat(second.isDone()).isFalse(); + pending.get(1).complete(new HttpResponseData(202, Map.of(), new byte[0])); + pending.get(0).complete(new HttpResponseData(201, Map.of(), new byte[0])); + assertThat(first.join().status()).isEqualTo(201); + assertThat(second.join().status()).isEqualTo(202); + assertThat(requests.get(0).uri().getPath()).isEqualTo("/pets/one"); + assertThat(requests.get(1).uri().getPath()).isEqualTo("/pets/two"); + assertThat(requests.get(0).headers().get("Authorization")).isEqualTo("Bearer one"); + assertThat(requests.get(1).headers().get("Authorization")).isEqualTo("Bearer two"); + assertThat(WorkflowFormat.JSON.mapper().readValue(requests.get(0).body(), Map.class)) + .isEqualTo(Map.of("name", "first")); + assertThat(WorkflowFormat.JSON.mapper().readValue(requests.get(1).body(), Map.class)) + .isEqualTo(Map.of("name", "second")); + } +} diff --git a/impl/pom.xml b/impl/pom.xml index 5a8c70f37..13d5f6346 100644 --- a/impl/pom.xml +++ b/impl/pom.xml @@ -11,9 +11,7 @@ 2.5.252 5.2.4 - 4.0.0 2.0.0-alpha1 - 4.0.3 9.2.1 3.7.1 25.5.5.0.3 @@ -135,17 +133,6 @@ ulid-creator ${version.com.github.f4b6a3} - - jakarta.ws.rs - jakarta.ws.rs-api - ${version.jakarta.ws.rs} - - - org.glassfish.jersey.core - jersey-client - ${version.org.glassfish.jersey} - test - org.glassfish.jersey.media jersey-media-json-jackson3 diff --git a/impl/test/pom.xml b/impl/test/pom.xml index 11e050c2a..242d6a590 100644 --- a/impl/test/pom.xml +++ b/impl/test/pom.xml @@ -65,6 +65,7 @@ org.glassfish.jersey.core jersey-client + test org.junit.jupiter diff --git a/impl/test/src/test/java/org/openworkflow/sdk/impl/test/HttpAdapterCompatibilityTest.java b/impl/test/src/test/java/org/openworkflow/sdk/impl/test/HttpAdapterCompatibilityTest.java new file mode 100644 index 000000000..d524c7f07 --- /dev/null +++ b/impl/test/src/test/java/org/openworkflow/sdk/impl/test/HttpAdapterCompatibilityTest.java @@ -0,0 +1,220 @@ +/* + * Copyright 2020-Present The Open Workflow Specification Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openworkflow.sdk.impl.test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.catchThrowableOfType; +import static org.openworkflow.sdk.api.WorkflowReader.readWorkflowFromString; + +import jakarta.ws.rs.client.Client; +import jakarta.ws.rs.client.ClientBuilder; +import jakarta.ws.rs.client.ClientRequestFilter; +import jakarta.ws.rs.client.Entity; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import mockwebserver3.MockResponse; +import mockwebserver3.MockWebServer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.openworkflow.sdk.api.WorkflowFormat; +import org.openworkflow.sdk.api.types.Workflow; +import org.openworkflow.sdk.impl.WorkflowApplication; +import org.openworkflow.sdk.impl.WorkflowException; +import org.openworkflow.sdk.impl.WorkflowModel; +import org.openworkflow.sdk.impl.WorkflowModelFactory; +import org.openworkflow.sdk.impl.http.DefaultHttpRequestBuilderFactory; +import org.openworkflow.sdk.impl.jakarta.http.DefaultHttpModelConverter; +import org.openworkflow.sdk.impl.jakarta.http.HttpClientResolver; +import org.openworkflow.sdk.impl.jakarta.http.HttpConverterResolver; +import org.openworkflow.sdk.impl.jakarta.http.HttpModelConverter; +import org.openworkflow.sdk.impl.jakarta.http.HttpRequestDecorator; +import org.openworkflow.sdk.impl.jakarta.http.JakartaRequestExecutorFactory; +import org.openworkflow.sdk.types.Errors; + +class HttpAdapterCompatibilityTest { + + private static WorkflowApplication application(boolean jdk) { + var builder = WorkflowApplication.builder(); + if (jdk) { + builder.withHttpRequestBuilderFactory(new DefaultHttpRequestBuilderFactory()); + } + return builder.build(); + } + + private static Workflow workflow(String uri) { + return readWorkflowFromString( + """ + document: + dsl: 1.0.0-alpha1 + namespace: test + name: http-adapter-compatibility + version: 1.0.0 + do: + - callApi: + call: http + with: + method: post + endpoint: + uri: %s + authentication: + basic: + username: user + password: password + body: + value: ${.value} + export: + as: + info: ${$authorization} + """ + .formatted(uri), + WorkflowFormat.YAML); + } + + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void execute_shouldKeepAuthenticationAndModelConversion(boolean jdk) throws Exception { + try (MockWebServer server = new MockWebServer(); + WorkflowApplication app = application(jdk)) { + server.start(); + if (!jdk) { + assertThat(app.httpRequestFactory()).isInstanceOf(JakartaRequestExecutorFactory.class); + } + server.enqueue( + new MockResponse.Builder() + .addHeader("Content-Type", "application/json") + .body("{\"result\":42}") + .build()); + var instance = + app.workflowDefinition(workflow(server.url("/").toString())) + .instance(Map.of("value", "resolved")); + assertThat(instance.start().get(5, TimeUnit.SECONDS).asMap().orElseThrow()) + .containsEntry("result", 42); + var recorded = server.takeRequest(5, TimeUnit.SECONDS); + assertThat(recorded.getHeaders().get("Authorization")) + .isEqualTo("Basic dXNlcjpwYXNzd29yZA=="); + assertThat(recorded.getBody().utf8()).contains("\"value\":\"resolved\""); + assertThat(((Map) instance.context().asMap().orElseThrow().get("info")).get("scheme")) + .isEqualTo("Basic"); + } + } + + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void execute_shouldTranslateHttpErrorsWithTaskPosition(boolean jdk) throws Exception { + try (MockWebServer server = new MockWebServer(); + WorkflowApplication app = application(jdk)) { + server.start(); + server.enqueue(new MockResponse.Builder().code(409).body("conflict details").build()); + ExecutionException error = + catchThrowableOfType( + ExecutionException.class, + () -> + app.workflowDefinition(workflow(server.url("/").toString())) + .instance(Map.of("value", 1)) + .start() + .get(5, TimeUnit.SECONDS)); + assertThat(error).isNotNull(); + assertThat(error.getCause()).isInstanceOf(WorkflowException.class); + var workflowError = ((WorkflowException) error.getCause()).getWorkflowError(); + assertThat(workflowError.status()).isEqualTo(409); + assertThat(workflowError.instance()).isEqualTo("do/0/callApi"); + assertThat(workflowError.type()).isEqualTo(Errors.COMMUNICATION.toString()); + assertThat(workflowError.detail()).isEqualTo("conflict details"); + } + } + + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void execute_shouldTranslateTransportErrorsWithTaskPosition(boolean jdk) throws Exception { + String uri; + try (MockWebServer server = new MockWebServer()) { + server.start(); + uri = server.url("/").toString(); + } + try (WorkflowApplication app = application(jdk)) { + ExecutionException error = + catchThrowableOfType( + ExecutionException.class, + () -> + app.workflowDefinition(workflow(uri)) + .instance(Map.of("value", 1)) + .start() + .get(5, TimeUnit.SECONDS)); + assertThat(error).isNotNull(); + assertThat(error.getCause()).isInstanceOf(WorkflowException.class); + var workflowError = ((WorkflowException) error.getCause()).getWorkflowError(); + assertThat(workflowError.status()).isEqualTo(Errors.COMMUNICATION.status()); + assertThat(workflowError.instance()).isEqualTo("do/0/callApi"); + } + } + + @Test + void jakarta_shouldKeepClientDecoratorAndConverterHooks() throws Exception { + try (MockWebServer server = new MockWebServer(); + Client client = ClientBuilder.newClient()) { + server.start(); + client.register( + (ClientRequestFilter) request -> request.getHeaders().add("X-Client", "injected")); + HttpRequestDecorator decorator = + (request, workflow, task) -> request.header("X-Task", task.taskName()); + HttpModelConverter converter = + new DefaultHttpModelConverter() { + @Override + public Entity buildEntity(WorkflowModel model, MediaType type) { + assertThat(model.asMap().orElseThrow()).containsEntry("value", "resolved"); + return Entity.text("converted body"); + } + + @Override + public WorkflowModel buildModel(WorkflowModelFactory factory, Response response) { + return factory.from(response.readEntity(String.class)); + } + }; + try (WorkflowApplication app = + WorkflowApplication.builder() + .withAdditionalObject(HttpClientResolver.HTTP_CLIENT_PROVIDER, () -> client) + .withAdditionalObject(HttpConverterResolver.HTTP_MODEL_CONVERTER, () -> converter) + .withAdditionalObject("HttpRequestDecorators", () -> List.of(decorator)) + .build()) { + server.enqueue( + new MockResponse.Builder() + .addHeader("Content-Type", "text/plain") + .body("converted result") + .build()); + WorkflowModel result = + app.workflowDefinition(workflow(server.url("/").toString())) + .instance(Map.of("value", "resolved")) + .start() + .get(5, TimeUnit.SECONDS); + assertThat(result.asText().orElseThrow()).isEqualTo("converted result"); + var recorded = server.takeRequest(5, TimeUnit.SECONDS); + assertThat(recorded.getHeaders().get("X-Client")).isEqualTo("injected"); + assertThat(recorded.getHeaders().get("X-Task")).isEqualTo("callApi"); + assertThat(recorded.getBody().utf8()).isEqualTo("converted body"); + } + // Application shutdown must not close the client returned by the additional-object hook. + server.enqueue(new MockResponse.Builder().body("still open").build()); + try (var response = client.target(server.url("/").uri()).request().get()) { + assertThat(response.getStatus()).isEqualTo(200); + } + } + } +} diff --git a/impl/test/src/test/java/org/openworkflow/sdk/impl/test/OpenAPIAdapterCompatibilityTest.java b/impl/test/src/test/java/org/openworkflow/sdk/impl/test/OpenAPIAdapterCompatibilityTest.java new file mode 100644 index 000000000..381393217 --- /dev/null +++ b/impl/test/src/test/java/org/openworkflow/sdk/impl/test/OpenAPIAdapterCompatibilityTest.java @@ -0,0 +1,131 @@ +/* + * Copyright 2020-Present The Open Workflow Specification Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openworkflow.sdk.impl.test; + +import static org.assertj.core.api.Assertions.*; +import static org.openworkflow.sdk.api.WorkflowReader.readWorkflowFromString; + +import java.nio.charset.StandardCharsets; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.openworkflow.sdk.api.WorkflowFormat; +import org.openworkflow.sdk.impl.WorkflowApplication; +import org.openworkflow.sdk.impl.executors.openapi.UnifiedOpenAPIReader; +import org.openworkflow.sdk.impl.executors.openapi.jackson.JacksonUnifiedOpenAPIReader; +import org.openworkflow.sdk.impl.http.DefaultHttpRequestBuilderFactory; + +class OpenAPIAdapterCompatibilityTest { + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void preservesCustomReadersSeparateAuthenticationAndPostFallback(boolean jdk) throws Exception { + try (MockWebServer document = new MockWebServer(); + MockWebServer first = new MockWebServer(); + MockWebServer second = new MockWebServer()) { + document.start(); + first.start(); + second.start(); + String schema = + """ + {"openapi":"3.0.3","servers":[{"url":"%s"},{"url":"%s"}], + "paths":{"/items/{id}":{"post":{"operationId":"save","parameters":[ + {"name":"id","in":"path","required":true,"schema":{"type":"string"}}], + "requestBody":{"content":{"application/json":{"schema":{"type":"object"}}}}}}}} + """ + .formatted(first.url("/v1"), second.url("/v1")); + document.enqueue( + new MockResponse().setHeader("Content-Type", "application/json").setBody(schema)); + first.enqueue(new MockResponse().setResponseCode(503).setBody("unavailable")); + second.enqueue( + new MockResponse() + .setHeader("Content-Type", "application/json") + .setBody("{\"saved\":true}")); + AtomicInteger reads = new AtomicInteger(); + UnifiedOpenAPIReader customReader = + (name, bytes) -> { + reads.incrementAndGet(); + return new JacksonUnifiedOpenAPIReader().read(name, bytes); + }; + var builder = + WorkflowApplication.builder() + .withAdditionalObject( + UnifiedOpenAPIReader.UNIFIED_OPEN_API_READER, () -> customReader); + if (jdk) builder.withHttpRequestBuilderFactory(new DefaultHttpRequestBuilderFactory()); + try (WorkflowApplication app = builder.build()) { + var workflow = + readWorkflowFromString( + """ + document: + dsl: 1.0.0-alpha1 + namespace: test + name: openapi-adapter-compatibility + version: 1.0.0 + do: + - save: + call: openapi + with: + document: + endpoint: + uri: %s + authentication: + basic: + username: document + password: secret + operationId: save + authentication: + basic: + username: api + password: secret + parameters: + id: ${.id} + name: ${.name} + """ + .formatted(document.url("/schema.json")), + WorkflowFormat.YAML); + var definition = app.workflowDefinition(workflow); + assertThat( + definition + .instance(Map.of("id", "one", "name", "first")) + .start() + .get(10, TimeUnit.SECONDS) + .asMap() + .orElseThrow() + .get("saved")) + .isEqualTo(true); + assertThat(reads.get()).isEqualTo(1); + var loaded = document.takeRequest(10, TimeUnit.SECONDS); + assertThat(loaded).isNotNull(); + assertThat(loaded.getHeader("Authorization")).isEqualTo("Basic ZG9jdW1lbnQ6c2VjcmV0"); + for (MockWebServer server : new MockWebServer[] {first, second}) { + var request = server.takeRequest(10, TimeUnit.SECONDS); + assertThat(request).isNotNull(); + assertThat(request.getMethod()).isEqualTo("POST"); + assertThat(request.getPath()).isEqualTo("/v1/items/one"); + assertThat(request.getHeader("Authorization")).isEqualTo("Basic YXBpOnNlY3JldA=="); + assertThat( + WorkflowFormat.JSON + .mapper() + .readValue(request.getBody().readString(StandardCharsets.UTF_8), Map.class)) + .isEqualTo(Map.of("name", "first")); + } + } + } + } +} diff --git a/pom.xml b/pom.xml index 29562c8ff..344c739ab 100644 --- a/pom.xml +++ b/pom.xml @@ -38,6 +38,7 @@ api + common impl types annotations @@ -49,6 +50,8 @@ + 4.0.3 + 4.0.0 21 ${java.version} ${java.version} @@ -111,6 +114,31 @@ + + org.glassfish.jersey.core + jersey-client + ${version.org.glassfish.jersey} + + + jakarta.ws.rs + jakarta.ws.rs-api + ${version.jakarta.ws.rs} + + + org.openworkflow.sdk + openworkflow-common-http + ${project.version} + + + org.openworkflow.sdk + openworkflow-common-http-jakarta + ${project.version} + + + org.openworkflow.sdk + openworkflow-common-openapi + ${project.version} +