From 26e9ad3a2b3a5ff42b9a4061c0c3a2ed256b3685 Mon Sep 17 00:00:00 2001 From: Hiram Chirino Date: Mon, 21 Apr 2025 16:07:22 -0400 Subject: [PATCH 1/2] Refactoring classes to simplify user facing api. Signed-off-by: Hiram Chirino --- README.md | 10 +- pom.xml | 13 + .../io/roastedroot/proxywasm/ABIVersion.java | 23 -- .../roastedroot/proxywasm/MetricsHandler.java | 2 + .../java/io/roastedroot/proxywasm/Plugin.java | 293 +++++++++++++++ .../proxywasm/{plugin => }/PluginFactory.java | 2 +- .../proxywasm/SharedDataHandler.java | 2 + .../proxywasm/SharedQueueHandler.java | 2 + .../{plugin => }/SimpleMetricsHandler.java | 10 +- .../{plugin => }/SimpleSharedDataHandler.java | 10 +- .../SimpleSharedQueueHandler.java | 10 +- .../roastedroot/proxywasm/StartException.java | 3 + .../roastedroot/proxywasm/WasmException.java | 2 + .../proxywasm/{ => internal}/ABI.java | 15 +- .../proxywasm/{ => internal}/Action.java | 4 +- .../{ => internal}/ArrayBytesProxyMap.java | 8 +- .../{ => internal}/ArrayProxyMap.java | 2 +- .../proxywasm/{ => internal}/BufferType.java | 2 +- .../{ => internal}/ChainedHandler.java | 10 +- .../proxywasm/{ => internal}/Context.java | 2 +- .../{ => internal}/ContextHandler.java | 2 +- .../{ => internal}/CustomHandler.java | 2 +- .../proxywasm/{ => internal}/FFIHandler.java | 4 +- .../{ => internal}/GRPCContextHandler.java | 4 +- .../GrpcCallResponseHandler.java | 4 +- .../proxywasm/{ => internal}/Handler.java | 7 +- .../proxywasm/{ => internal}/Helpers.java | 2 +- .../{ => internal}/HttpCallHandler.java | 4 +- .../internal/HttpCallResponseHandler.java | 5 + .../proxywasm/{ => internal}/HttpContext.java | 4 +- .../{ => internal}/HttpContextHandler.java | 2 +- .../HttpRequestAdaptor.java | 8 +- .../proxywasm/{ => internal}/MapType.java | 2 +- .../{ => internal}/NetworkContext.java | 4 +- .../proxywasm/{ => internal}/PeerType.java | 2 +- .../{plugin => internal}/Plugin.java | 348 +++--------------- .../{ => internal}/PluginContext.java | 2 +- .../{ => internal}/PluginHandler.java | 4 +- .../PluginHttpContext.java} | 23 +- .../proxywasm/{plugin => internal}/Pool.java | 5 +- .../{ => internal}/PropertiesHandler.java | 3 +- .../proxywasm/{ => internal}/ProxyMap.java | 6 +- .../proxywasm/{ => internal}/ProxyWasm.java | 5 +- .../{plugin => internal}/SendResponse.java | 4 +- .../{plugin => internal}/ServerAdaptor.java | 5 +- .../{ => internal}/SplitProxyMap.java | 2 +- .../{ => internal}/StreamContextHandler.java | 2 +- .../proxywasm/{ => internal}/StreamType.java | 2 +- .../proxywasm/{ => internal}/WasmResult.java | 4 +- .../{ => internal}/WellKnownHeaders.java | 2 +- .../{ => internal}/WellKnownProperties.java | 2 +- .../proxywasm/plugin/GrpcCallResponse.java | 16 - .../proxywasm/plugin/HttpCallResponse.java | 16 - .../plugin/HttpCallResponseHandler.java | 5 - .../examples/DispatchCallOnTickTest.java | 2 +- .../proxywasm/examples/EchoHttpBodyTest.java | 10 +- .../examples/ForeignCallOnTickTest.java | 2 +- .../examples/HttpAuthRandomTest.java | 10 +- .../proxywasm/examples/HttpBodyChunkTest.java | 8 +- .../proxywasm/examples/HttpBodyTest.java | 12 +- .../proxywasm/examples/HttpHeadersTest.java | 6 +- .../proxywasm/examples/HttpRoutingTest.java | 4 +- .../examples/JsonValidationTest.java | 8 +- .../proxywasm/examples/MetricsTest.java | 4 +- .../proxywasm/examples/MockHandler.java | 16 +- .../proxywasm/examples/MockSharedHandler.java | 4 +- .../examples/MultipleDispatchesTest.java | 4 +- .../proxywasm/examples/NetworkTest.java | 8 +- .../examples/OnRequestHeadersTest.java | 4 +- .../examples/PostponeRequestsTest.java | 4 +- .../proxywasm/examples/PropertiesTest.java | 10 +- .../examples/RustEnvoyFilterMetadataTest.java | 8 +- .../examples/RustHelloWorldTest.java | 2 +- .../proxywasm/examples/SharedDataTest.java | 4 +- .../proxywasm/examples/SharedQueueTest.java | 6 +- .../examples/TimersClocksRandomTest.java | 4 +- .../examples/VmPluginConfigurationTest.java | 4 +- .../proxywasm/jaxrs/example/App.java | 4 +- .../jaxrs/example/tests/BaseTest.java | 3 +- .../proxywasm/jaxrs/WasmPlugin.java | 4 + .../proxywasm/jaxrs/WasmPluginFeature.java | 10 +- .../proxywasm/jaxrs/WasmPluginFilter.java | 26 +- .../proxywasm/jaxrs/cdi/ServerAdaptor.java | 5 +- .../jaxrs/cdi/WasmPluginFeature.java | 12 +- .../AbstractWasmPluginFeature.java | 14 +- .../JaxrsHttpRequestAdaptor.java | 108 +++--- .../{ => internal}/MultivaluedMapAdaptor.java | 4 +- .../jaxrs/{ => internal}/ServerAdaptor.java | 28 +- .../ServletJaxrsHttpRequestAdaptor.java | 2 +- .../proxywasm/jaxrs/example/App.java | 4 +- .../docs/modules/ROOT/pages/index.adoc | 7 +- .../java/io/quarkiverse/proxywasm/it/App.java | 4 +- .../runtime/VertxHttpRequestAdaptor.java | 2 +- .../proxywasm/runtime/VertxServerAdaptor.java | 27 +- .../proxywasm/corazawaf/example/App.java | 6 +- .../proxywasm/kuadrant/example/App.java | 6 +- 96 files changed, 708 insertions(+), 649 deletions(-) delete mode 100644 proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ABIVersion.java create mode 100644 proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/Plugin.java rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{plugin => }/PluginFactory.java (64%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{plugin => }/SimpleMetricsHandler.java (91%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{plugin => }/SimpleSharedDataHandler.java (81%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{plugin => }/SimpleSharedQueueHandler.java (91%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/ABI.java (99%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/Action.java (94%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/ArrayBytesProxyMap.java (93%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/ArrayProxyMap.java (97%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/BufferType.java (96%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/ChainedHandler.java (96%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/Context.java (97%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/ContextHandler.java (92%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/CustomHandler.java (95%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/FFIHandler.java (86%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/GRPCContextHandler.java (94%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{plugin => internal}/GrpcCallResponseHandler.java (69%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/Handler.java (68%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/Helpers.java (99%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/HttpCallHandler.java (93%) create mode 100644 proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/HttpCallResponseHandler.java rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/HttpContext.java (95%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/HttpContextHandler.java (97%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{plugin => internal}/HttpRequestAdaptor.java (65%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/MapType.java (96%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/NetworkContext.java (93%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/PeerType.java (95%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{plugin => internal}/Plugin.java (61%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/PluginContext.java (86%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/PluginHandler.java (93%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{plugin/HttpContext.java => internal/PluginHttpContext.java} (91%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{plugin => internal}/Pool.java (94%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/PropertiesHandler.java (75%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/ProxyMap.java (93%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/ProxyWasm.java (98%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{plugin => internal}/SendResponse.java (91%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{plugin => internal}/ServerAdaptor.java (86%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/SplitProxyMap.java (97%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/StreamContextHandler.java (97%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/StreamType.java (95%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/WasmResult.java (96%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/WellKnownHeaders.java (90%) rename proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/{ => internal}/WellKnownProperties.java (99%) delete mode 100644 proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/GrpcCallResponse.java delete mode 100644 proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/HttpCallResponse.java delete mode 100644 proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/HttpCallResponseHandler.java rename proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/{ => internal}/AbstractWasmPluginFeature.java (90%) rename proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/{ => internal}/JaxrsHttpRequestAdaptor.java (76%) rename proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/{ => internal}/MultivaluedMapAdaptor.java (96%) rename proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/{ => internal}/ServerAdaptor.java (81%) diff --git a/README.md b/README.md index f92259a..3f83272 100644 --- a/README.md +++ b/README.md @@ -40,11 +40,12 @@ The `WasmPlugin` annotation is used to specify the name of the plugin to be used ```java package io.roastedroot.proxywasm.jaxrs.example; +import com.dylibso.chicory.experimental.aot.AotMachine; import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; +import io.roastedroot.proxywasm.Plugin; +import io.roastedroot.proxywasm.PluginFactory; import io.roastedroot.proxywasm.StartException; -import io.roastedroot.proxywasm.plugin.Plugin; -import io.roastedroot.proxywasm.plugin.PluginFactory; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.inject.Produces; import java.nio.file.Path; @@ -60,10 +61,11 @@ public class App { @Produces public PluginFactory example() throws StartException { return () -> - Plugin.builder() + Plugin.builder(module) .withName("example") .withPluginConfig("{ \"type\": \"headerTests\" }") - .build(module); + .withMachineFactory(AotMachine::new) + .build(); } } ``` diff --git a/pom.xml b/pom.xml index db8b27a..94d582b 100644 --- a/pom.xml +++ b/pom.xml @@ -45,6 +45,7 @@ 3.5.2 ${surefire-plugin.version} 3.2.7 + 3.11.2 5.12.0 @@ -209,7 +210,19 @@ + + org.apache.maven.plugins + maven-javadoc-plugin + ${maven.javadoc.version} + + + **/internal/** + + + + + diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ABIVersion.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ABIVersion.java deleted file mode 100644 index 2308fae..0000000 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ABIVersion.java +++ /dev/null @@ -1,23 +0,0 @@ -package io.roastedroot.proxywasm; - -public enum ABIVersion { - // The values follow the same order as in the Go code using iota (0-based incrementing) - V0_1_0("proxy_abi_version_0_1_0"), - V0_2_0("proxy_abi_version_0_2_0"), - V0_2_1("proxy_abi_version_0_2_1"); - - private final String abiMarkerFunction; - - /** - * Constructor for ABIVersion enum. - * - * @param value The version string of the ABI - */ - ABIVersion(String value) { - this.abiMarkerFunction = value; - } - - public String getAbiMarkerFunction() { - return abiMarkerFunction; - } -} diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/MetricsHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/MetricsHandler.java index cb2ef98..cf2b356 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/MetricsHandler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/MetricsHandler.java @@ -1,5 +1,7 @@ package io.roastedroot.proxywasm; +import io.roastedroot.proxywasm.internal.WasmResult; + public interface MetricsHandler { MetricsHandler DEFAULT = new MetricsHandler() {}; diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/Plugin.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/Plugin.java new file mode 100644 index 0000000..679325b --- /dev/null +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/Plugin.java @@ -0,0 +1,293 @@ +package io.roastedroot.proxywasm; + +import static io.roastedroot.proxywasm.internal.Helpers.bytes; + +import com.dylibso.chicory.runtime.ImportMemory; +import com.dylibso.chicory.runtime.Instance; +import com.dylibso.chicory.runtime.Machine; +import com.dylibso.chicory.wasi.WasiOptions; +import com.dylibso.chicory.wasm.WasmModule; +import io.roastedroot.proxywasm.internal.ProxyWasm; +import java.net.URI; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Function; + +/** + * Plugin is an instance of a Proxy-Wasm plugin. + */ +public abstract class Plugin { + + protected Plugin() {} + + public abstract String name(); + + /** + * Creates a new Plugin builder. + * + * @return a new Plugin builder + */ + public static Plugin.Builder builder(WasmModule module) { + return new Plugin.Builder(module); + } + + /** + * Builder for creating a Plugin instance. + */ + public static final class Builder { + + private final WasmModule module; + private final ProxyWasm.Builder proxyWasmBuilder = ProxyWasm.builder().withStart(false); + private boolean shared = true; + private String name; + private HashMap foreignFunctions; + private HashMap upstreams; + private boolean strictUpstreams; + private int minTickPeriodMilliseconds; + private LogHandler logger; + private byte[] vmConfig; + private byte[] pluginConfig; + private MetricsHandler metricsHandler; + private SharedQueueHandler sharedQueueHandler; + private SharedDataHandler sharedDataHandler; + + /** + * Set the WASM module of the plugin. The module contains the plugin instructions. + * + * @param module the WASM module of the plugin + * @return this builder + */ + private Builder(WasmModule module) { + this.module = module; + } + + /** + * Set the name of the plugin. + * + * @param name the name of the plugin + * @return this builder + */ + public Builder withName(String name) { + this.name = name; + return this; + } + + /** + * Set the foreign functions of that can be called from the plugin. + * + * @param functions the foreign functions of the plugin + * @return this builder + */ + public Builder withForeignFunctions(Map functions) { + this.foreignFunctions = new HashMap<>(functions); + return this; + } + + /** + * Set the upstream server URL + * + * @param upstreams the upstream URI mappings. When a http or grpc call is made + * from the plugin, the upstream name is used to lookup the URL. + * @return this builder + */ + public Builder withUpstreams(Map upstreams) { + this.upstreams = new HashMap<>(upstreams); + return this; + } + + /** + * Set the strict upstreams mode of the plugin. If strict upstreams is enabled, + * then the plugin will throw an error if an upstream is not found. If disabled, + * then the upstream name is used as the URL. + * + * @param strictUpstreams the strict upstreams of the plugin + * @return this builder + */ + public Builder withStrictUpstreams(boolean strictUpstreams) { + this.strictUpstreams = strictUpstreams; + return this; + } + + /** + * Set the minimum tick period of the plugin. A pluign that requests + * a very small tick period will be ticked very frequently. Use this + * to protect the host from being overwhelmed by the plugin. + * + * @param minTickPeriodMilliseconds the minimum tick period of the plugin + * @return this builder + */ + public Builder withMinTickPeriodMilliseconds(int minTickPeriodMilliseconds) { + this.minTickPeriodMilliseconds = minTickPeriodMilliseconds; + return this; + } + + /** + * Set the logger of the plugin. + * + * @param logger the logger of the plugin + * @return this builder + */ + public Builder withLogger(LogHandler logger) { + this.logger = logger; + return this; + } + + /** + * Set the metrics handler of the plugin. If the metrics handler is not set, + * then calls by the guest to define/use metrics will result in UNIMPLEMENTED errors + * reported to the guest. + * + * @param metricsHandler the metrics handler of the plugin + * @return this builder + */ + public Builder withMetricsHandler(MetricsHandler metricsHandler) { + this.metricsHandler = metricsHandler; + return this; + } + + /** + * Set the shared queue handler of the plugin. If the sahred queue handler is not set, + * then calls by the guest to define/use shared queues will result in UNIMPLEMENTED errors + * reported to the guest. + * + * @param sharedQueueHandler the shared queue handler of the plugin + * @return this builder + */ + public Builder withSharedQueueHandler(SharedQueueHandler sharedQueueHandler) { + this.sharedQueueHandler = sharedQueueHandler; + return this; + } + + /** + * Set the shared data handler of the plugin. If the shared data handler is not set, + * then calls by the guest to define/use shared data will result in UNIMPLEMENTED errors + * reported to the guest. + * + * @param sharedDataHandler the shared data handler of the plugin + * @return this builder + */ + public Builder withSharedDataHandler(SharedDataHandler sharedDataHandler) { + this.sharedDataHandler = sharedDataHandler; + return this; + } + + /** + * Set whether the plugin is shared between host requests. If the plugin is shared, + * then the plugin will be created once and reused for each host request. If the plugin + * is not shared, then a new plugin MAY be use for each concurrent host request. + * + * @param shared whether the plugin is shared + * @return this builder + */ + public Builder withShared(boolean shared) { + this.shared = shared; + return this; + } + + /** + * Set the VM config of the plugin. + * + * @param vmConfig the VM config of the plugin + * @return this builder + */ + public Builder withVmConfig(byte[] vmConfig) { + this.vmConfig = vmConfig; + return this; + } + + /** + * Set the VM config of the plugin. + * + * @param vmConfig the VM config of the plugin + * @return this builder + */ + public Builder withVmConfig(String vmConfig) { + this.vmConfig = bytes(vmConfig); + return this; + } + + /** + * Set the plugin config of the plugin. + * + * @param pluginConfig the plugin config of the plugin + * @return this builder + */ + public Builder withPluginConfig(byte[] pluginConfig) { + this.pluginConfig = pluginConfig; + return this; + } + + /** + * Set the plugin config of the plugin. + * + * @param pluginConfig the plugin config of the plugin + * @return this builder + */ + public Builder withPluginConfig(String pluginConfig) { + this.pluginConfig = bytes(pluginConfig); + return this; + } + + /** + * Set the import memory of the plugin. + * + * @param memory the import memory of the plugin + * @return this builder + */ + public Builder withImportMemory(ImportMemory memory) { + proxyWasmBuilder.withImportMemory(memory); + return this; + } + + /** + * Set the machine factory of the plugin. The machine factory is used to control + * how instructions are executed. By default instructions are executed in a + * by an interpreter. To increase performance, you can use compile the + * was instructions to bytecode at runtime or at build time. For more information + * see https://chicory.dev/docs/experimental/aot + * + * @param machineFactory the machine factory of the plugin + * @return this builder + */ + public Builder withMachineFactory(Function machineFactory) { + proxyWasmBuilder.withMachineFactory(machineFactory); + return this; + } + + /** + * Set the WASI options of the plugin. A default WASI enviroment will be provided + * to the pluign. You can use this method to customize the WASI environment, + * for example to provide it access to some file system resources. + * + * @param options the WASI options of the plugin + * @return this builder + */ + public Builder withWasiOptions(WasiOptions options) { + proxyWasmBuilder.withWasiOptions(options); + return this; + } + + /** + * Build the plugin. + * + * @return the plugin + * @throws StartException if the plugin fails to start + */ + public io.roastedroot.proxywasm.internal.Plugin build() throws StartException { + return new io.roastedroot.proxywasm.internal.Plugin( + proxyWasmBuilder.build(module), + shared, + name, + foreignFunctions, + upstreams, + strictUpstreams, + minTickPeriodMilliseconds, + logger, + vmConfig, + pluginConfig, + metricsHandler, + sharedQueueHandler, + sharedDataHandler); + } + } +} diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/PluginFactory.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/PluginFactory.java similarity index 64% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/PluginFactory.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/PluginFactory.java index 80ac084..395fc1c 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/PluginFactory.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/PluginFactory.java @@ -1,4 +1,4 @@ -package io.roastedroot.proxywasm.plugin; +package io.roastedroot.proxywasm; public interface PluginFactory { Plugin create() throws Exception; diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SharedDataHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SharedDataHandler.java index 1079813..deef39a 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SharedDataHandler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SharedDataHandler.java @@ -1,5 +1,7 @@ package io.roastedroot.proxywasm; +import io.roastedroot.proxywasm.internal.WasmResult; + public interface SharedDataHandler { SharedDataHandler DEFAULT = new SharedDataHandler() {}; diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SharedQueueHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SharedQueueHandler.java index e94c32c..3c10fab 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SharedQueueHandler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SharedQueueHandler.java @@ -1,5 +1,7 @@ package io.roastedroot.proxywasm; +import io.roastedroot.proxywasm.internal.WasmResult; + public interface SharedQueueHandler { SharedQueueHandler DEFAULT = new SharedQueueHandler() {}; diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/SimpleMetricsHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SimpleMetricsHandler.java similarity index 91% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/SimpleMetricsHandler.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SimpleMetricsHandler.java index 6e9970e..bbd2274 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/SimpleMetricsHandler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SimpleMetricsHandler.java @@ -1,12 +1,12 @@ -package io.roastedroot.proxywasm.plugin; +package io.roastedroot.proxywasm; -import io.roastedroot.proxywasm.MetricType; -import io.roastedroot.proxywasm.MetricsHandler; -import io.roastedroot.proxywasm.WasmException; -import io.roastedroot.proxywasm.WasmResult; +import io.roastedroot.proxywasm.internal.WasmResult; import java.util.HashMap; import java.util.concurrent.atomic.AtomicInteger; +/** + * A simple implementation of the MetricsHandler interface that keeps track of metrics in memory. + */ public class SimpleMetricsHandler implements MetricsHandler { public static class Metric { diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/SimpleSharedDataHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SimpleSharedDataHandler.java similarity index 81% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/SimpleSharedDataHandler.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SimpleSharedDataHandler.java index 65c5081..a167b1f 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/SimpleSharedDataHandler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SimpleSharedDataHandler.java @@ -1,11 +1,11 @@ -package io.roastedroot.proxywasm.plugin; +package io.roastedroot.proxywasm; -import io.roastedroot.proxywasm.SharedData; -import io.roastedroot.proxywasm.SharedDataHandler; -import io.roastedroot.proxywasm.WasmException; -import io.roastedroot.proxywasm.WasmResult; +import io.roastedroot.proxywasm.internal.WasmResult; import java.util.HashMap; +/** + * A simple implementation of the SharedDataHandler interface that keeps the shared data in memory. + */ public class SimpleSharedDataHandler implements SharedDataHandler { private final HashMap sharedData = new HashMap<>(); diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/SimpleSharedQueueHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SimpleSharedQueueHandler.java similarity index 91% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/SimpleSharedQueueHandler.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SimpleSharedQueueHandler.java index 34e7501..9b5c1d3 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/SimpleSharedQueueHandler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SimpleSharedQueueHandler.java @@ -1,13 +1,13 @@ -package io.roastedroot.proxywasm.plugin; +package io.roastedroot.proxywasm; -import io.roastedroot.proxywasm.QueueName; -import io.roastedroot.proxywasm.SharedQueueHandler; -import io.roastedroot.proxywasm.WasmException; -import io.roastedroot.proxywasm.WasmResult; +import io.roastedroot.proxywasm.internal.WasmResult; import java.util.HashMap; import java.util.LinkedList; import java.util.concurrent.atomic.AtomicInteger; +/** + * Simple implementation of SharedQueueHandler that keep the queues in memory. + */ public class SimpleSharedQueueHandler implements SharedQueueHandler { public static class SharedQueue { diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/StartException.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/StartException.java index 3a7bd37..86fccb1 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/StartException.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/StartException.java @@ -1,5 +1,8 @@ package io.roastedroot.proxywasm; +/** + * StartException is thrown when there is an error during the start of a plugin. + */ public class StartException extends Exception { public StartException(String message) { super(message); diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/WasmException.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/WasmException.java index 215f430..f37fd13 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/WasmException.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/WasmException.java @@ -1,5 +1,7 @@ package io.roastedroot.proxywasm; +import io.roastedroot.proxywasm.internal.WasmResult; + /** * Represents WebAssembly result codes. * Converted from Go's WasmResult type. diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ABI.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ABI.java similarity index 99% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ABI.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ABI.java index fb05194..16fddca 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ABI.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ABI.java @@ -1,9 +1,9 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; -import static io.roastedroot.proxywasm.Helpers.bytes; -import static io.roastedroot.proxywasm.Helpers.replaceBytes; -import static io.roastedroot.proxywasm.Helpers.split; -import static io.roastedroot.proxywasm.Helpers.string; +import static io.roastedroot.proxywasm.internal.Helpers.bytes; +import static io.roastedroot.proxywasm.internal.Helpers.replaceBytes; +import static io.roastedroot.proxywasm.internal.Helpers.split; +import static io.roastedroot.proxywasm.internal.Helpers.string; import com.dylibso.chicory.experimental.hostmodule.annotations.HostModule; import com.dylibso.chicory.experimental.hostmodule.annotations.WasmExport; @@ -12,6 +12,11 @@ import com.dylibso.chicory.runtime.Memory; import com.dylibso.chicory.runtime.WasmRuntimeException; import com.dylibso.chicory.wasm.InvalidException; +import io.roastedroot.proxywasm.LogLevel; +import io.roastedroot.proxywasm.MetricType; +import io.roastedroot.proxywasm.QueueName; +import io.roastedroot.proxywasm.SharedData; +import io.roastedroot.proxywasm.WasmException; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/Action.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/Action.java similarity index 94% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/Action.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/Action.java index 278add4..46dec7d 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/Action.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/Action.java @@ -1,7 +1,9 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; /** * Action represents the action which Wasm contexts expects hosts to take. + * + * @hidden */ public enum Action { CONTINUE(0), diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ArrayBytesProxyMap.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ArrayBytesProxyMap.java similarity index 93% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ArrayBytesProxyMap.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ArrayBytesProxyMap.java index af6c8a7..4cfadc2 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ArrayBytesProxyMap.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ArrayBytesProxyMap.java @@ -1,8 +1,8 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; -import static io.roastedroot.proxywasm.Helpers.bytes; -import static io.roastedroot.proxywasm.Helpers.len; -import static io.roastedroot.proxywasm.Helpers.string; +import static io.roastedroot.proxywasm.internal.Helpers.bytes; +import static io.roastedroot.proxywasm.internal.Helpers.len; +import static io.roastedroot.proxywasm.internal.Helpers.string; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ArrayProxyMap.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ArrayProxyMap.java similarity index 97% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ArrayProxyMap.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ArrayProxyMap.java index a98db45..3073b2a 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ArrayProxyMap.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ArrayProxyMap.java @@ -1,4 +1,4 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; import java.util.ArrayList; import java.util.Map; diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/BufferType.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/BufferType.java similarity index 96% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/BufferType.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/BufferType.java index 61a2835..9ec9fe4 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/BufferType.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/BufferType.java @@ -1,4 +1,4 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; /** * Represents the type of buffer in proxy WASM. diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ChainedHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ChainedHandler.java similarity index 96% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ChainedHandler.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ChainedHandler.java index df4ef20..df80f6d 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ChainedHandler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ChainedHandler.java @@ -1,5 +1,11 @@ -package io.roastedroot.proxywasm; - +package io.roastedroot.proxywasm.internal; + +import io.roastedroot.proxywasm.ForeignFunction; +import io.roastedroot.proxywasm.LogLevel; +import io.roastedroot.proxywasm.MetricType; +import io.roastedroot.proxywasm.QueueName; +import io.roastedroot.proxywasm.SharedData; +import io.roastedroot.proxywasm.WasmException; import java.util.List; /** diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/Context.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/Context.java similarity index 97% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/Context.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/Context.java index e44554d..593660c 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/Context.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/Context.java @@ -1,4 +1,4 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; import java.io.Closeable; diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ContextHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ContextHandler.java similarity index 92% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ContextHandler.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ContextHandler.java index 5af83c5..810c7a9 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ContextHandler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ContextHandler.java @@ -1,4 +1,4 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; public interface ContextHandler { diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/CustomHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/CustomHandler.java similarity index 95% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/CustomHandler.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/CustomHandler.java index 8ced4ba..a7ca11a 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/CustomHandler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/CustomHandler.java @@ -1,4 +1,4 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; public interface CustomHandler { diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/FFIHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/FFIHandler.java similarity index 86% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/FFIHandler.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/FFIHandler.java index 76c58e4..67e1660 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/FFIHandler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/FFIHandler.java @@ -1,4 +1,6 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; + +import io.roastedroot.proxywasm.ForeignFunction; public interface FFIHandler { /** diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/GRPCContextHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/GRPCContextHandler.java similarity index 94% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/GRPCContextHandler.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/GRPCContextHandler.java index 08ca2dc..9ed28aa 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/GRPCContextHandler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/GRPCContextHandler.java @@ -1,4 +1,6 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; + +import io.roastedroot.proxywasm.WasmException; public interface GRPCContextHandler extends StreamContextHandler { diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/GrpcCallResponseHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/GrpcCallResponseHandler.java similarity index 69% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/GrpcCallResponseHandler.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/GrpcCallResponseHandler.java index fdb2949..6c4ff64 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/GrpcCallResponseHandler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/GrpcCallResponseHandler.java @@ -1,6 +1,4 @@ -package io.roastedroot.proxywasm.plugin; - -import io.roastedroot.proxywasm.ArrayBytesProxyMap; +package io.roastedroot.proxywasm.internal; public interface GrpcCallResponseHandler { diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/Handler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/Handler.java similarity index 68% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/Handler.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/Handler.java index c8b8179..d946cf2 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/Handler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/Handler.java @@ -1,4 +1,9 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; + +import io.roastedroot.proxywasm.LogHandler; +import io.roastedroot.proxywasm.MetricsHandler; +import io.roastedroot.proxywasm.SharedDataHandler; +import io.roastedroot.proxywasm.SharedQueueHandler; public interface Handler extends ContextHandler, diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/Helpers.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/Helpers.java similarity index 99% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/Helpers.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/Helpers.java index 7008775..a644401 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/Helpers.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/Helpers.java @@ -1,4 +1,4 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; import com.dylibso.chicory.runtime.HostFunction; import java.nio.ByteBuffer; diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/HttpCallHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/HttpCallHandler.java similarity index 93% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/HttpCallHandler.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/HttpCallHandler.java index 3bbd8e3..d7fb81c 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/HttpCallHandler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/HttpCallHandler.java @@ -1,4 +1,6 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; + +import io.roastedroot.proxywasm.WasmException; public interface HttpCallHandler { diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/HttpCallResponseHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/HttpCallResponseHandler.java new file mode 100644 index 0000000..63417e3 --- /dev/null +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/HttpCallResponseHandler.java @@ -0,0 +1,5 @@ +package io.roastedroot.proxywasm.internal; + +public interface HttpCallResponseHandler { + void call(int statusCode, ProxyMap headers, byte[] body); +} diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/HttpContext.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/HttpContext.java similarity index 95% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/HttpContext.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/HttpContext.java index defdb93..6ddfa88 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/HttpContext.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/HttpContext.java @@ -1,6 +1,6 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; -import static io.roastedroot.proxywasm.Helpers.len; +import static io.roastedroot.proxywasm.internal.Helpers.len; public class HttpContext extends Context { diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/HttpContextHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/HttpContextHandler.java similarity index 97% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/HttpContextHandler.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/HttpContextHandler.java index e07b52d..79b044a 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/HttpContextHandler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/HttpContextHandler.java @@ -1,4 +1,4 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; public interface HttpContextHandler extends StreamContextHandler { diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/HttpRequestAdaptor.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/HttpRequestAdaptor.java similarity index 65% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/HttpRequestAdaptor.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/HttpRequestAdaptor.java index ff920b2..5896366 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/HttpRequestAdaptor.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/HttpRequestAdaptor.java @@ -1,8 +1,6 @@ -package io.roastedroot.proxywasm.plugin; +package io.roastedroot.proxywasm.internal; -import io.roastedroot.proxywasm.ProxyMap; import io.roastedroot.proxywasm.WasmException; -import io.roastedroot.proxywasm.WasmResult; import java.util.List; /** @@ -32,7 +30,7 @@ public interface HttpRequestAdaptor { ProxyMap getGrpcReceiveTrailerMetaData(); - byte[] getProperty(HttpContext pluginRequest, List path) throws WasmException; + byte[] getProperty(PluginHttpContext pluginRequest, List path) throws WasmException; - WasmResult setProperty(HttpContext pluginRequest, List path, byte[] value); + WasmResult setProperty(PluginHttpContext pluginRequest, List path, byte[] value); } diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/MapType.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/MapType.java similarity index 96% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/MapType.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/MapType.java index ba57ea4..f8c311c 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/MapType.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/MapType.java @@ -1,4 +1,4 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; /** * Represents the type of map in proxy WASM. diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/NetworkContext.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/NetworkContext.java similarity index 93% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/NetworkContext.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/NetworkContext.java index ac9ee93..33b7a24 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/NetworkContext.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/NetworkContext.java @@ -1,6 +1,6 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; -import static io.roastedroot.proxywasm.Helpers.len; +import static io.roastedroot.proxywasm.internal.Helpers.len; public class NetworkContext extends Context { diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/PeerType.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/PeerType.java similarity index 95% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/PeerType.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/PeerType.java index 4cee2ff..6fa069e 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/PeerType.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/PeerType.java @@ -1,4 +1,4 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; /** * Represents the type of peer in proxy WASM. diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/Plugin.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/Plugin.java similarity index 61% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/Plugin.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/Plugin.java index ef2224b..0aef0d8 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/Plugin.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/Plugin.java @@ -1,50 +1,33 @@ -package io.roastedroot.proxywasm.plugin; - -import static io.roastedroot.proxywasm.Helpers.bytes; -import static io.roastedroot.proxywasm.WellKnownHeaders.AUTHORITY; -import static io.roastedroot.proxywasm.WellKnownHeaders.METHOD; -import static io.roastedroot.proxywasm.WellKnownHeaders.PATH; -import static io.roastedroot.proxywasm.WellKnownHeaders.SCHEME; -import static io.roastedroot.proxywasm.WellKnownProperties.PLUGIN_NAME; -import static io.roastedroot.proxywasm.WellKnownProperties.PLUGIN_VM_ID; - -import com.dylibso.chicory.runtime.ImportMemory; -import com.dylibso.chicory.runtime.Instance; -import com.dylibso.chicory.runtime.Machine; -import com.dylibso.chicory.wasi.WasiOptions; -import com.dylibso.chicory.wasm.WasmModule; -import io.roastedroot.proxywasm.ArrayBytesProxyMap; -import io.roastedroot.proxywasm.ArrayProxyMap; -import io.roastedroot.proxywasm.ChainedHandler; +package io.roastedroot.proxywasm.internal; + +import static io.roastedroot.proxywasm.internal.Helpers.bytes; +import static io.roastedroot.proxywasm.internal.WellKnownHeaders.AUTHORITY; +import static io.roastedroot.proxywasm.internal.WellKnownHeaders.METHOD; +import static io.roastedroot.proxywasm.internal.WellKnownHeaders.PATH; +import static io.roastedroot.proxywasm.internal.WellKnownHeaders.SCHEME; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.PLUGIN_NAME; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.PLUGIN_VM_ID; + import io.roastedroot.proxywasm.ForeignFunction; -import io.roastedroot.proxywasm.Handler; import io.roastedroot.proxywasm.LogHandler; import io.roastedroot.proxywasm.LogLevel; import io.roastedroot.proxywasm.MetricType; import io.roastedroot.proxywasm.MetricsHandler; -import io.roastedroot.proxywasm.ProxyMap; -import io.roastedroot.proxywasm.ProxyWasm; import io.roastedroot.proxywasm.QueueName; import io.roastedroot.proxywasm.SharedData; import io.roastedroot.proxywasm.SharedDataHandler; import io.roastedroot.proxywasm.SharedQueueHandler; import io.roastedroot.proxywasm.StartException; import io.roastedroot.proxywasm.WasmException; -import io.roastedroot.proxywasm.WasmResult; import java.net.URI; import java.net.URISyntaxException; import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.ReentrantLock; -import java.util.function.Function; -/** - * Plugin is an instance of a Proxy-Wasm plugin. - */ -public final class Plugin { +public final class Plugin extends io.roastedroot.proxywasm.Plugin { private final ReentrantLock lock = new ReentrantLock(); final ProxyWasm wasm; @@ -56,31 +39,44 @@ public final class Plugin { private final SharedQueueHandler sharedQueueHandler; private final SharedDataHandler sharedDataHandler; - private Plugin(Builder builder, ProxyWasm proxyWasm) throws StartException { + public Plugin( + ProxyWasm proxyWasm, + boolean shared, + String name, + HashMap foreignFunctions, + HashMap upstreams, + boolean strictUpstreams, + int minTickPeriodMilliseconds, + LogHandler logger, + byte[] vmConfig, + byte[] pluginConfig, + MetricsHandler metricsHandler, + SharedQueueHandler sharedQueueHandler, + SharedDataHandler sharedDataHandler) + throws StartException { Objects.requireNonNull(proxyWasm); - this.name = Objects.requireNonNullElse(builder.name, "default"); - this.shared = builder.shared; - this.foreignFunctions = - Objects.requireNonNullElseGet(builder.foreignFunctions, HashMap::new); - this.upstreams = Objects.requireNonNullElseGet(builder.upstreams, HashMap::new); - this.strictUpstreams = builder.strictUpstreams; - this.minTickPeriodMilliseconds = builder.minTickPeriodMilliseconds; - this.vmConfig = builder.vmConfig; - this.pluginConfig = builder.pluginConfig; - this.logger = Objects.requireNonNullElse(builder.logger, LogHandler.DEFAULT); + this.name = Objects.requireNonNullElse(name, "default"); + this.shared = shared; + this.foreignFunctions = Objects.requireNonNullElseGet(foreignFunctions, HashMap::new); + this.upstreams = Objects.requireNonNullElseGet(upstreams, HashMap::new); + this.strictUpstreams = strictUpstreams; + this.minTickPeriodMilliseconds = minTickPeriodMilliseconds; + this.vmConfig = vmConfig; + this.pluginConfig = pluginConfig; + this.logger = Objects.requireNonNullElse(logger, LogHandler.DEFAULT); ; - this.metricsHandler = - Objects.requireNonNullElse(builder.metricsHandler, MetricsHandler.DEFAULT); + this.metricsHandler = Objects.requireNonNullElse(metricsHandler, MetricsHandler.DEFAULT); this.sharedQueueHandler = - Objects.requireNonNullElse(builder.sharedQueueHandler, SharedQueueHandler.DEFAULT); + Objects.requireNonNullElse(sharedQueueHandler, SharedQueueHandler.DEFAULT); this.sharedDataHandler = - Objects.requireNonNullElse(builder.sharedDataHandler, SharedDataHandler.DEFAULT); + Objects.requireNonNullElse(sharedDataHandler, SharedDataHandler.DEFAULT); this.wasm = proxyWasm; this.wasm.setPluginHandler(new HandlerImpl()); this.wasm.start(); } + @Override public String name() { return name; } @@ -109,8 +105,8 @@ public LogHandler logger() { return logger; } - public HttpContext createHttpContext(HttpRequestAdaptor requestAdaptor) { - return new HttpContext(this, requestAdaptor); + public PluginHttpContext createHttpContext(HttpRequestAdaptor requestAdaptor) { + return new PluginHttpContext(this, requestAdaptor); } public void close() { @@ -135,262 +131,6 @@ public void close() { } } - /** - * Creates a new Plugin builder. - * - * @return a new Plugin builder - */ - public static Plugin.Builder builder(WasmModule module) { - return new Plugin.Builder(module); - } - - /** - * Builder for creating a Plugin instance. - */ - public static final class Builder { - - private final WasmModule module; - private final ProxyWasm.Builder proxyWasmBuilder = ProxyWasm.builder().withStart(false); - private boolean shared = true; - private String name; - private HashMap foreignFunctions; - private HashMap upstreams; - private boolean strictUpstreams; - private int minTickPeriodMilliseconds; - private LogHandler logger; - private byte[] vmConfig; - private byte[] pluginConfig; - private MetricsHandler metricsHandler; - private SharedQueueHandler sharedQueueHandler; - private SharedDataHandler sharedDataHandler; - - /** - * Set the WASM module of the plugin. The module contains the plugin instructions. - * - * @param module the WASM module of the plugin - * @return this builder - */ - private Builder(WasmModule module) { - this.module = module; - } - - /** - * Set the name of the plugin. - * - * @param name the name of the plugin - * @return this builder - */ - public Plugin.Builder withName(String name) { - this.name = name; - return this; - } - - /** - * Set the foreign functions of that can be called from the plugin. - * - * @param functions the foreign functions of the plugin - * @return this builder - */ - public Builder withForeignFunctions(Map functions) { - this.foreignFunctions = new HashMap<>(functions); - return this; - } - - /** - * Set the upstream server URL - * - * @param upstreams the upstream URI mappings. When a http or grpc call is made - * from the plugin, the upstream name is used to lookup the URL. - * @return this builder - */ - public Builder withUpstreams(Map upstreams) { - this.upstreams = new HashMap<>(upstreams); - return this; - } - - /** - * Set the strict upstreams mode of the plugin. If strict upstreams is enabled, - * then the plugin will throw an error if an upstream is not found. If disabled, - * then the upstream name is used as the URL. - * - * @param strictUpstreams the strict upstreams of the plugin - * @return this builder - */ - public Builder withStrictUpstreams(boolean strictUpstreams) { - this.strictUpstreams = strictUpstreams; - return this; - } - - /** - * Set the minimum tick period of the plugin. A pluign that requests - * a very small tick period will be ticked very frequently. Use this - * to protect the host from being overwhelmed by the plugin. - * - * @param minTickPeriodMilliseconds the minimum tick period of the plugin - * @return this builder - */ - public Builder withMinTickPeriodMilliseconds(int minTickPeriodMilliseconds) { - this.minTickPeriodMilliseconds = minTickPeriodMilliseconds; - return this; - } - - /** - * Set the logger of the plugin. - * - * @param logger the logger of the plugin - * @return this builder - */ - public Builder withLogger(LogHandler logger) { - this.logger = logger; - return this; - } - - /** - * Set the metrics handler of the plugin. If the metrics handler is not set, - * then calls by the guest to define/use metrics will result in UNIMPLEMENTED errors - * reported to the guest. - * - * @param metricsHandler the metrics handler of the plugin - * @return this builder - */ - public Builder withMetricsHandler(MetricsHandler metricsHandler) { - this.metricsHandler = metricsHandler; - return this; - } - - /** - * Set the shared queue handler of the plugin. If the sahred queue handler is not set, - * then calls by the guest to define/use shared queues will result in UNIMPLEMENTED errors - * reported to the guest. - * - * @param sharedQueueHandler the shared queue handler of the plugin - * @return this builder - */ - public Builder withSharedQueueHandler(SharedQueueHandler sharedQueueHandler) { - this.sharedQueueHandler = sharedQueueHandler; - return this; - } - - /** - * Set the shared data handler of the plugin. If the shared data handler is not set, - * then calls by the guest to define/use shared data will result in UNIMPLEMENTED errors - * reported to the guest. - * - * @param sharedDataHandler the shared data handler of the plugin - * @return this builder - */ - public Builder withSharedDataHandler(SharedDataHandler sharedDataHandler) { - this.sharedDataHandler = sharedDataHandler; - return this; - } - - /** - * Set whether the plugin is shared between host requests. If the plugin is shared, - * then the plugin will be created once and reused for each host request. If the plugin - * is not shared, then a new plugin MAY be use for each concurrent host request. - * - * @param shared whether the plugin is shared - * @return this builder - */ - public Builder withShared(boolean shared) { - this.shared = shared; - return this; - } - - /** - * Set the VM config of the plugin. - * - * @param vmConfig the VM config of the plugin - * @return this builder - */ - public Builder withVmConfig(byte[] vmConfig) { - this.vmConfig = vmConfig; - return this; - } - - /** - * Set the VM config of the plugin. - * - * @param vmConfig the VM config of the plugin - * @return this builder - */ - public Builder withVmConfig(String vmConfig) { - this.vmConfig = bytes(vmConfig); - return this; - } - - /** - * Set the plugin config of the plugin. - * - * @param pluginConfig the plugin config of the plugin - * @return this builder - */ - public Builder withPluginConfig(byte[] pluginConfig) { - this.pluginConfig = pluginConfig; - return this; - } - - /** - * Set the plugin config of the plugin. - * - * @param pluginConfig the plugin config of the plugin - * @return this builder - */ - public Builder withPluginConfig(String pluginConfig) { - this.pluginConfig = bytes(pluginConfig); - return this; - } - - /** - * Set the import memory of the plugin. - * - * @param memory the import memory of the plugin - * @return this builder - */ - public Builder withImportMemory(ImportMemory memory) { - proxyWasmBuilder.withImportMemory(memory); - return this; - } - - /** - * Set the machine factory of the plugin. The machine factory is used to control - * how instructions are executed. By default instructions are executed in a - * by an interpreter. To increase performance, you can use compile the - * was instructions to bytecode at runtime or at build time. For more information - * see https://chicory.dev/docs/experimental/aot - * - * @param machineFactory the machine factory of the plugin - * @return this builder - */ - public Builder withMachineFactory(Function machineFactory) { - proxyWasmBuilder.withMachineFactory(machineFactory); - return this; - } - - /** - * Set the WASI options of the plugin. A default WASI enviroment will be provided - * to the pluign. You can use this method to customize the WASI environment, - * for example to provide it access to some file system resources. - * - * @param options the WASI options of the plugin - * @return this builder - */ - public Builder withWasiOptions(WasiOptions options) { - proxyWasmBuilder.withWasiOptions(options); - return this; - } - - /** - * Build the plugin. - * - * @return the plugin - * @throws StartException if the plugin fails to start - */ - public Plugin build() throws StartException { - return new Plugin(this, proxyWasmBuilder.build(module)); - } - } - public LogHandler logger; static final boolean DEBUG = "true".equals(System.getenv("DEBUG")); byte[] vmConfig; @@ -596,14 +336,16 @@ public int httpCall( body, trailers, timeoutMilliseconds, - (resp) -> { + (statusCode, respHeaders, respBody) -> { + // todo: add the status + // respHeaders.put(STATUS, "" + statusCode); lock(); try { if (httpCalls.remove(id) == null) { return; // the call could have already been cancelled } wasm.sendHttpCallResponse( - id, resp.headers, new ArrayProxyMap(), resp.body); + id, respHeaders, new ArrayProxyMap(), respBody); } finally { unlock(); } diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/PluginContext.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/PluginContext.java similarity index 86% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/PluginContext.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/PluginContext.java index 3f196de..c4611fc 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/PluginContext.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/PluginContext.java @@ -1,4 +1,4 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; public class PluginContext extends Context { diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/PluginHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/PluginHandler.java similarity index 93% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/PluginHandler.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/PluginHandler.java index 969a17c..7b1e0d5 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/PluginHandler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/PluginHandler.java @@ -1,4 +1,6 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; + +import io.roastedroot.proxywasm.WasmException; public interface PluginHandler { diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/HttpContext.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/PluginHttpContext.java similarity index 91% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/HttpContext.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/PluginHttpContext.java index ce91abf..a579f75 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/HttpContext.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/PluginHttpContext.java @@ -1,21 +1,14 @@ -package io.roastedroot.proxywasm.plugin; - -import io.roastedroot.proxywasm.Action; -import io.roastedroot.proxywasm.ChainedHandler; -import io.roastedroot.proxywasm.Handler; -import io.roastedroot.proxywasm.Helpers; -import io.roastedroot.proxywasm.ProxyMap; -import io.roastedroot.proxywasm.StreamType; +package io.roastedroot.proxywasm.internal; + import io.roastedroot.proxywasm.WasmException; -import io.roastedroot.proxywasm.WasmResult; import java.util.HashMap; import java.util.List; import java.util.concurrent.CountDownLatch; -public class HttpContext { +public class PluginHttpContext { private final Plugin plugin; - private final io.roastedroot.proxywasm.HttpContext context; + private final io.roastedroot.proxywasm.internal.HttpContext context; private final HttpRequestAdaptor requestAdaptor; private final long startedAt = System.currentTimeMillis(); @@ -29,7 +22,7 @@ public class HttpContext { private Action action; private CountDownLatch resumeLatch; - HttpContext(Plugin plugin, HttpRequestAdaptor requestAdaptor) { + PluginHttpContext(Plugin plugin, HttpRequestAdaptor requestAdaptor) { this.plugin = plugin; this.requestAdaptor = requestAdaptor; this.context = plugin.wasm.createHttpContext(new HandlerImpl()); @@ -39,7 +32,7 @@ public Plugin plugin() { return plugin; } - public io.roastedroot.proxywasm.HttpContext context() { + public io.roastedroot.proxywasm.internal.HttpContext context() { return context; } @@ -263,7 +256,7 @@ public WasmResult setAction(StreamType streamType, Action actionValue) { // ////////////////////////////////////////////////////////////////////// @Override public byte[] getProperty(List path) throws WasmException { - byte[] result = requestAdaptor.getProperty(HttpContext.this, path); + byte[] result = requestAdaptor.getProperty(PluginHttpContext.this, path); if (result == null) { result = properties.get(path); } @@ -272,7 +265,7 @@ public byte[] getProperty(List path) throws WasmException { @Override public WasmResult setProperty(List path, byte[] value) { - var result = requestAdaptor.setProperty(HttpContext.this, path, value); + var result = requestAdaptor.setProperty(PluginHttpContext.this, path, value); if (result == WasmResult.NOT_FOUND) { properties.put(path, value); } diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/Pool.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/Pool.java similarity index 94% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/Pool.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/Pool.java index acb4a27..8e8a3fd 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/Pool.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/Pool.java @@ -1,5 +1,6 @@ -package io.roastedroot.proxywasm.plugin; +package io.roastedroot.proxywasm.internal; +import io.roastedroot.proxywasm.PluginFactory; import io.roastedroot.proxywasm.StartException; import java.util.Collection; import java.util.List; @@ -71,7 +72,7 @@ public String name() { public Plugin borrow() throws StartException { Plugin plugin = null; try { - plugin = factory.create(); + plugin = (Plugin) factory.create(); } catch (Throwable e) { throw new StartException("Plugin create failed.", e); } diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/PropertiesHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/PropertiesHandler.java similarity index 75% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/PropertiesHandler.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/PropertiesHandler.java index afd5e6f..29bb194 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/PropertiesHandler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/PropertiesHandler.java @@ -1,5 +1,6 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; +import io.roastedroot.proxywasm.WasmException; import java.util.List; public interface PropertiesHandler { diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ProxyMap.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ProxyMap.java similarity index 93% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ProxyMap.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ProxyMap.java index b2c0165..adb9dad 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ProxyMap.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ProxyMap.java @@ -1,7 +1,7 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; -import static io.roastedroot.proxywasm.Helpers.bytes; -import static io.roastedroot.proxywasm.Helpers.len; +import static io.roastedroot.proxywasm.internal.Helpers.bytes; +import static io.roastedroot.proxywasm.internal.Helpers.len; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ProxyWasm.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ProxyWasm.java similarity index 98% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ProxyWasm.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ProxyWasm.java index ff7b43e..a6ad796 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ProxyWasm.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ProxyWasm.java @@ -1,6 +1,6 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; -import static io.roastedroot.proxywasm.Helpers.len; +import static io.roastedroot.proxywasm.internal.Helpers.len; import com.dylibso.chicory.runtime.ByteArrayMemory; import com.dylibso.chicory.runtime.HostFunction; @@ -13,6 +13,7 @@ import com.dylibso.chicory.wasm.WasmModule; import com.dylibso.chicory.wasm.types.MemoryLimits; import com.dylibso.chicory.wasm.types.ValueType; +import io.roastedroot.proxywasm.StartException; import java.io.Closeable; import java.util.HashMap; import java.util.List; diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/SendResponse.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/SendResponse.java similarity index 91% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/SendResponse.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/SendResponse.java index 272a47b..5d40692 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/SendResponse.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/SendResponse.java @@ -1,6 +1,4 @@ -package io.roastedroot.proxywasm.plugin; - -import io.roastedroot.proxywasm.ProxyMap; +package io.roastedroot.proxywasm.internal; public class SendResponse { diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/ServerAdaptor.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ServerAdaptor.java similarity index 86% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/ServerAdaptor.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ServerAdaptor.java index 54b4557..bd63576 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/ServerAdaptor.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ServerAdaptor.java @@ -1,10 +1,9 @@ -package io.roastedroot.proxywasm.plugin; +package io.roastedroot.proxywasm.internal; -import io.roastedroot.proxywasm.ProxyMap; import java.net.URI; /** - * This interface will help us deal with differences in the http server impl. + * The ServerAdaptor interface provides adapting plugins to the server environment. */ public interface ServerAdaptor { diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SplitProxyMap.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/SplitProxyMap.java similarity index 97% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SplitProxyMap.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/SplitProxyMap.java index 8b29147..d6bd0de 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SplitProxyMap.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/SplitProxyMap.java @@ -1,4 +1,4 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; import java.util.Map; import java.util.stream.Collectors; diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/StreamContextHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/StreamContextHandler.java similarity index 97% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/StreamContextHandler.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/StreamContextHandler.java index 40c02bb..d86b10b 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/StreamContextHandler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/StreamContextHandler.java @@ -1,4 +1,4 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; public interface StreamContextHandler { diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/StreamType.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/StreamType.java similarity index 95% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/StreamType.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/StreamType.java index 19e8e80..314f7b5 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/StreamType.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/StreamType.java @@ -1,4 +1,4 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; /** * Represents the type of map in proxy WASM. diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/WasmResult.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/WasmResult.java similarity index 96% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/WasmResult.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/WasmResult.java index f4b151e..4cb2a19 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/WasmResult.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/WasmResult.java @@ -1,4 +1,6 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; + +import io.roastedroot.proxywasm.WasmException; /** * Represents WebAssembly result codes. diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/WellKnownHeaders.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/WellKnownHeaders.java similarity index 90% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/WellKnownHeaders.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/WellKnownHeaders.java index dfdd842..448336c 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/WellKnownHeaders.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/WellKnownHeaders.java @@ -1,4 +1,4 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; /** * Holds constants for the well-known header keys. diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/WellKnownProperties.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/WellKnownProperties.java similarity index 99% rename from proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/WellKnownProperties.java rename to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/WellKnownProperties.java index ddef5aa..aea7143 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/WellKnownProperties.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/WellKnownProperties.java @@ -1,4 +1,4 @@ -package io.roastedroot.proxywasm; +package io.roastedroot.proxywasm.internal; import java.util.List; diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/GrpcCallResponse.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/GrpcCallResponse.java deleted file mode 100644 index e9fd1f3..0000000 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/GrpcCallResponse.java +++ /dev/null @@ -1,16 +0,0 @@ -package io.roastedroot.proxywasm.plugin; - -import io.roastedroot.proxywasm.ProxyMap; - -public class GrpcCallResponse { - - public final int statusCode; - public final ProxyMap headers; - public final byte[] body; - - public GrpcCallResponse(int statusCode, ProxyMap headers, byte[] body) { - this.statusCode = statusCode; - this.headers = headers; - this.body = body; - } -} diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/HttpCallResponse.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/HttpCallResponse.java deleted file mode 100644 index 0e586fb..0000000 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/HttpCallResponse.java +++ /dev/null @@ -1,16 +0,0 @@ -package io.roastedroot.proxywasm.plugin; - -import io.roastedroot.proxywasm.ProxyMap; - -public class HttpCallResponse { - - public final int statusCode; - public final ProxyMap headers; - public final byte[] body; - - public HttpCallResponse(int statusCode, ProxyMap headers, byte[] body) { - this.statusCode = statusCode; - this.headers = headers; - this.body = body; - } -} diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/HttpCallResponseHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/HttpCallResponseHandler.java deleted file mode 100644 index a2de329..0000000 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/HttpCallResponseHandler.java +++ /dev/null @@ -1,5 +0,0 @@ -package io.roastedroot.proxywasm.plugin; - -public interface HttpCallResponseHandler { - void call(HttpCallResponse response); -} diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/DispatchCallOnTickTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/DispatchCallOnTickTest.java index b98d383..4284025 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/DispatchCallOnTickTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/DispatchCallOnTickTest.java @@ -6,8 +6,8 @@ import com.dylibso.chicory.runtime.Instance; import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; -import io.roastedroot.proxywasm.ProxyWasm; import io.roastedroot.proxywasm.StartException; +import io.roastedroot.proxywasm.internal.ProxyWasm; import java.nio.file.Path; import java.util.Map; import org.junit.jupiter.api.Test; diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/EchoHttpBodyTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/EchoHttpBodyTest.java index 4201ee2..654b8e0 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/EchoHttpBodyTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/EchoHttpBodyTest.java @@ -1,16 +1,16 @@ package io.roastedroot.proxywasm.examples; -import static io.roastedroot.proxywasm.Helpers.bytes; -import static io.roastedroot.proxywasm.Helpers.string; +import static io.roastedroot.proxywasm.internal.Helpers.bytes; +import static io.roastedroot.proxywasm.internal.Helpers.string; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; -import io.roastedroot.proxywasm.Action; -import io.roastedroot.proxywasm.HttpContext; -import io.roastedroot.proxywasm.ProxyWasm; import io.roastedroot.proxywasm.StartException; +import io.roastedroot.proxywasm.internal.Action; +import io.roastedroot.proxywasm.internal.HttpContext; +import io.roastedroot.proxywasm.internal.ProxyWasm; import java.nio.file.Path; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/ForeignCallOnTickTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/ForeignCallOnTickTest.java index 896dd76..82ef85f 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/ForeignCallOnTickTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/ForeignCallOnTickTest.java @@ -4,8 +4,8 @@ import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; -import io.roastedroot.proxywasm.ProxyWasm; import io.roastedroot.proxywasm.StartException; +import io.roastedroot.proxywasm.internal.ProxyWasm; import java.nio.file.Path; import org.junit.jupiter.api.Test; diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpAuthRandomTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpAuthRandomTest.java index 8b16656..31804bc 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpAuthRandomTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpAuthRandomTest.java @@ -1,17 +1,17 @@ package io.roastedroot.proxywasm.examples; -import static io.roastedroot.proxywasm.Helpers.bytes; -import static io.roastedroot.proxywasm.Helpers.string; +import static io.roastedroot.proxywasm.internal.Helpers.bytes; +import static io.roastedroot.proxywasm.internal.Helpers.string; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; -import io.roastedroot.proxywasm.Action; -import io.roastedroot.proxywasm.ProxyMap; -import io.roastedroot.proxywasm.ProxyWasm; import io.roastedroot.proxywasm.StartException; +import io.roastedroot.proxywasm.internal.Action; +import io.roastedroot.proxywasm.internal.ProxyMap; +import io.roastedroot.proxywasm.internal.ProxyWasm; import java.nio.file.Path; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpBodyChunkTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpBodyChunkTest.java index 5b6e270..13829f4 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpBodyChunkTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpBodyChunkTest.java @@ -1,15 +1,15 @@ package io.roastedroot.proxywasm.examples; -import static io.roastedroot.proxywasm.Helpers.bytes; +import static io.roastedroot.proxywasm.internal.Helpers.bytes; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; -import io.roastedroot.proxywasm.Action; -import io.roastedroot.proxywasm.HttpContext; -import io.roastedroot.proxywasm.ProxyWasm; import io.roastedroot.proxywasm.StartException; +import io.roastedroot.proxywasm.internal.Action; +import io.roastedroot.proxywasm.internal.HttpContext; +import io.roastedroot.proxywasm.internal.ProxyWasm; import java.nio.file.Path; import java.util.Map; import org.junit.jupiter.api.AfterEach; diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpBodyTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpBodyTest.java index 8b75253..7608c6e 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpBodyTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpBodyTest.java @@ -1,17 +1,17 @@ package io.roastedroot.proxywasm.examples; -import static io.roastedroot.proxywasm.Helpers.bytes; -import static io.roastedroot.proxywasm.Helpers.string; +import static io.roastedroot.proxywasm.internal.Helpers.bytes; +import static io.roastedroot.proxywasm.internal.Helpers.string; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; -import io.roastedroot.proxywasm.Action; -import io.roastedroot.proxywasm.HttpContext; -import io.roastedroot.proxywasm.ProxyMap; -import io.roastedroot.proxywasm.ProxyWasm; import io.roastedroot.proxywasm.StartException; +import io.roastedroot.proxywasm.internal.Action; +import io.roastedroot.proxywasm.internal.HttpContext; +import io.roastedroot.proxywasm.internal.ProxyMap; +import io.roastedroot.proxywasm.internal.ProxyWasm; import java.nio.charset.StandardCharsets; import java.nio.file.Path; import org.junit.jupiter.api.AfterEach; diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpHeadersTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpHeadersTest.java index 0f53925..87b8855 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpHeadersTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpHeadersTest.java @@ -5,10 +5,10 @@ import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; -import io.roastedroot.proxywasm.Action; -import io.roastedroot.proxywasm.ProxyMap; -import io.roastedroot.proxywasm.ProxyWasm; import io.roastedroot.proxywasm.StartException; +import io.roastedroot.proxywasm.internal.Action; +import io.roastedroot.proxywasm.internal.ProxyMap; +import io.roastedroot.proxywasm.internal.ProxyWasm; import java.nio.file.Path; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpRoutingTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpRoutingTest.java index 348bafd..40be4e0 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpRoutingTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpRoutingTest.java @@ -4,9 +4,9 @@ import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; -import io.roastedroot.proxywasm.Action; -import io.roastedroot.proxywasm.ProxyWasm; import io.roastedroot.proxywasm.StartException; +import io.roastedroot.proxywasm.internal.Action; +import io.roastedroot.proxywasm.internal.ProxyWasm; import java.nio.file.Path; import java.util.Map; import org.junit.jupiter.api.Test; diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/JsonValidationTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/JsonValidationTest.java index 1fa401e..9900f0c 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/JsonValidationTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/JsonValidationTest.java @@ -1,14 +1,14 @@ package io.roastedroot.proxywasm.examples; -import static io.roastedroot.proxywasm.Helpers.bytes; +import static io.roastedroot.proxywasm.internal.Helpers.bytes; import static org.junit.jupiter.api.Assertions.assertEquals; import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; -import io.roastedroot.proxywasm.Action; -import io.roastedroot.proxywasm.HttpContext; -import io.roastedroot.proxywasm.ProxyWasm; import io.roastedroot.proxywasm.StartException; +import io.roastedroot.proxywasm.internal.Action; +import io.roastedroot.proxywasm.internal.HttpContext; +import io.roastedroot.proxywasm.internal.ProxyWasm; import java.nio.file.Path; import java.util.Map; import org.junit.jupiter.api.AfterEach; diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/MetricsTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/MetricsTest.java index 65683b4..209a418 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/MetricsTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/MetricsTest.java @@ -5,10 +5,10 @@ import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; -import io.roastedroot.proxywasm.Action; import io.roastedroot.proxywasm.MetricType; -import io.roastedroot.proxywasm.ProxyWasm; import io.roastedroot.proxywasm.StartException; +import io.roastedroot.proxywasm.internal.Action; +import io.roastedroot.proxywasm.internal.ProxyWasm; import java.nio.file.Path; import java.util.Map; import org.junit.jupiter.api.Assertions; diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/MockHandler.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/MockHandler.java index e27cb10..d5e7076 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/MockHandler.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/MockHandler.java @@ -4,18 +4,18 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -import io.roastedroot.proxywasm.Action; -import io.roastedroot.proxywasm.ArrayProxyMap; -import io.roastedroot.proxywasm.ChainedHandler; import io.roastedroot.proxywasm.ForeignFunction; -import io.roastedroot.proxywasm.Handler; -import io.roastedroot.proxywasm.Helpers; import io.roastedroot.proxywasm.LogLevel; import io.roastedroot.proxywasm.MetricType; -import io.roastedroot.proxywasm.ProxyMap; -import io.roastedroot.proxywasm.StreamType; import io.roastedroot.proxywasm.WasmException; -import io.roastedroot.proxywasm.WasmResult; +import io.roastedroot.proxywasm.internal.Action; +import io.roastedroot.proxywasm.internal.ArrayProxyMap; +import io.roastedroot.proxywasm.internal.ChainedHandler; +import io.roastedroot.proxywasm.internal.Handler; +import io.roastedroot.proxywasm.internal.Helpers; +import io.roastedroot.proxywasm.internal.ProxyMap; +import io.roastedroot.proxywasm.internal.StreamType; +import io.roastedroot.proxywasm.internal.WasmResult; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/MockSharedHandler.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/MockSharedHandler.java index 16d7f24..4f9ac9f 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/MockSharedHandler.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/MockSharedHandler.java @@ -1,10 +1,10 @@ package io.roastedroot.proxywasm.examples; -import io.roastedroot.proxywasm.Handler; import io.roastedroot.proxywasm.QueueName; import io.roastedroot.proxywasm.SharedData; import io.roastedroot.proxywasm.WasmException; -import io.roastedroot.proxywasm.WasmResult; +import io.roastedroot.proxywasm.internal.Handler; +import io.roastedroot.proxywasm.internal.WasmResult; import java.util.HashMap; import java.util.LinkedList; import java.util.concurrent.atomic.AtomicInteger; diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/MultipleDispatchesTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/MultipleDispatchesTest.java index 9bae17c..0eef6a3 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/MultipleDispatchesTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/MultipleDispatchesTest.java @@ -4,9 +4,9 @@ import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; -import io.roastedroot.proxywasm.Action; -import io.roastedroot.proxywasm.ProxyWasm; import io.roastedroot.proxywasm.StartException; +import io.roastedroot.proxywasm.internal.Action; +import io.roastedroot.proxywasm.internal.ProxyWasm; import java.nio.file.Path; import java.util.HashMap; import java.util.Map; diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/NetworkTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/NetworkTest.java index a015d5e..153b3d0 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/NetworkTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/NetworkTest.java @@ -1,15 +1,15 @@ package io.roastedroot.proxywasm.examples; -import static io.roastedroot.proxywasm.Helpers.bytes; +import static io.roastedroot.proxywasm.internal.Helpers.bytes; import static org.junit.jupiter.api.Assertions.assertEquals; import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; -import io.roastedroot.proxywasm.Action; import io.roastedroot.proxywasm.MetricType; -import io.roastedroot.proxywasm.NetworkContext; -import io.roastedroot.proxywasm.ProxyWasm; import io.roastedroot.proxywasm.StartException; +import io.roastedroot.proxywasm.internal.Action; +import io.roastedroot.proxywasm.internal.NetworkContext; +import io.roastedroot.proxywasm.internal.ProxyWasm; import java.nio.file.Path; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/OnRequestHeadersTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/OnRequestHeadersTest.java index 1cd2f34..ccaf7f1 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/OnRequestHeadersTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/OnRequestHeadersTest.java @@ -1,12 +1,12 @@ package io.roastedroot.proxywasm.examples; -import static io.roastedroot.proxywasm.Helpers.bytes; +import static io.roastedroot.proxywasm.internal.Helpers.bytes; import static org.junit.jupiter.api.Assertions.assertEquals; import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; -import io.roastedroot.proxywasm.ProxyWasm; import io.roastedroot.proxywasm.StartException; +import io.roastedroot.proxywasm.internal.ProxyWasm; import java.nio.file.Path; import java.util.List; import java.util.Map; diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/PostponeRequestsTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/PostponeRequestsTest.java index 8d9b416..256b0ea 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/PostponeRequestsTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/PostponeRequestsTest.java @@ -4,9 +4,9 @@ import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; -import io.roastedroot.proxywasm.Action; -import io.roastedroot.proxywasm.ProxyWasm; import io.roastedroot.proxywasm.StartException; +import io.roastedroot.proxywasm.internal.Action; +import io.roastedroot.proxywasm.internal.ProxyWasm; import java.nio.file.Path; import org.junit.jupiter.api.Test; diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/PropertiesTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/PropertiesTest.java index 8c9e594..4d71a26 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/PropertiesTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/PropertiesTest.java @@ -1,18 +1,18 @@ package io.roastedroot.proxywasm.examples; -import static io.roastedroot.proxywasm.Helpers.append; -import static io.roastedroot.proxywasm.Helpers.bytes; -import static io.roastedroot.proxywasm.Helpers.string; +import static io.roastedroot.proxywasm.internal.Helpers.append; +import static io.roastedroot.proxywasm.internal.Helpers.bytes; +import static io.roastedroot.proxywasm.internal.Helpers.string; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; -import io.roastedroot.proxywasm.Action; -import io.roastedroot.proxywasm.ProxyWasm; import io.roastedroot.proxywasm.StartException; import io.roastedroot.proxywasm.WasmException; +import io.roastedroot.proxywasm.internal.Action; +import io.roastedroot.proxywasm.internal.ProxyWasm; import java.nio.file.Path; import java.util.Arrays; import java.util.List; diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/RustEnvoyFilterMetadataTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/RustEnvoyFilterMetadataTest.java index 39c828a..759b17b 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/RustEnvoyFilterMetadataTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/RustEnvoyFilterMetadataTest.java @@ -1,15 +1,15 @@ package io.roastedroot.proxywasm.examples; -import static io.roastedroot.proxywasm.Helpers.bytes; -import static io.roastedroot.proxywasm.Helpers.string; +import static io.roastedroot.proxywasm.internal.Helpers.bytes; +import static io.roastedroot.proxywasm.internal.Helpers.string; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; -import io.roastedroot.proxywasm.ProxyMap; -import io.roastedroot.proxywasm.ProxyWasm; import io.roastedroot.proxywasm.StartException; +import io.roastedroot.proxywasm.internal.ProxyMap; +import io.roastedroot.proxywasm.internal.ProxyWasm; import java.nio.file.Path; import java.util.List; import org.junit.jupiter.api.Test; diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/RustHelloWorldTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/RustHelloWorldTest.java index bb70676..bbb3692 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/RustHelloWorldTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/RustHelloWorldTest.java @@ -4,8 +4,8 @@ import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; -import io.roastedroot.proxywasm.ProxyWasm; import io.roastedroot.proxywasm.StartException; +import io.roastedroot.proxywasm.internal.ProxyWasm; import java.nio.file.Path; import org.junit.jupiter.api.Test; diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/SharedDataTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/SharedDataTest.java index 4ea7b7a..71ac5f6 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/SharedDataTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/SharedDataTest.java @@ -4,9 +4,9 @@ import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; -import io.roastedroot.proxywasm.Action; -import io.roastedroot.proxywasm.ProxyWasm; import io.roastedroot.proxywasm.StartException; +import io.roastedroot.proxywasm.internal.Action; +import io.roastedroot.proxywasm.internal.ProxyWasm; import java.nio.file.Path; import org.junit.jupiter.api.Test; diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/SharedQueueTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/SharedQueueTest.java index 80caac6..0913ad0 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/SharedQueueTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/SharedQueueTest.java @@ -1,16 +1,16 @@ package io.roastedroot.proxywasm.examples; -import static io.roastedroot.proxywasm.Helpers.bytes; +import static io.roastedroot.proxywasm.internal.Helpers.bytes; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; -import io.roastedroot.proxywasm.Action; -import io.roastedroot.proxywasm.ProxyWasm; import io.roastedroot.proxywasm.QueueName; import io.roastedroot.proxywasm.StartException; import io.roastedroot.proxywasm.WasmException; +import io.roastedroot.proxywasm.internal.Action; +import io.roastedroot.proxywasm.internal.ProxyWasm; import java.io.Closeable; import java.io.IOException; import java.nio.file.Path; diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/TimersClocksRandomTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/TimersClocksRandomTest.java index 403dfa7..2fffffe 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/TimersClocksRandomTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/TimersClocksRandomTest.java @@ -5,9 +5,9 @@ import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; -import io.roastedroot.proxywasm.ProxyWasm; import io.roastedroot.proxywasm.StartException; -import io.roastedroot.proxywasm.WasmResult; +import io.roastedroot.proxywasm.internal.ProxyWasm; +import io.roastedroot.proxywasm.internal.WasmResult; import java.nio.file.Path; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/VmPluginConfigurationTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/VmPluginConfigurationTest.java index c43bc62..8afeebc 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/VmPluginConfigurationTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/VmPluginConfigurationTest.java @@ -1,12 +1,12 @@ package io.roastedroot.proxywasm.examples; -import static io.roastedroot.proxywasm.Helpers.bytes; +import static io.roastedroot.proxywasm.internal.Helpers.bytes; import static org.junit.jupiter.api.Assertions.assertEquals; import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; -import io.roastedroot.proxywasm.ProxyWasm; import io.roastedroot.proxywasm.StartException; +import io.roastedroot.proxywasm.internal.ProxyWasm; import java.nio.file.Path; import java.util.List; import org.junit.jupiter.api.Test; diff --git a/proxy-wasm-jaxrs-jersey/src/test/java/io/roastedroot/proxywasm/jaxrs/example/App.java b/proxy-wasm-jaxrs-jersey/src/test/java/io/roastedroot/proxywasm/jaxrs/example/App.java index f92b746..8382e9b 100644 --- a/proxy-wasm-jaxrs-jersey/src/test/java/io/roastedroot/proxywasm/jaxrs/example/App.java +++ b/proxy-wasm-jaxrs-jersey/src/test/java/io/roastedroot/proxywasm/jaxrs/example/App.java @@ -4,9 +4,9 @@ import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; import com.google.gson.Gson; +import io.roastedroot.proxywasm.Plugin; +import io.roastedroot.proxywasm.PluginFactory; import io.roastedroot.proxywasm.StartException; -import io.roastedroot.proxywasm.plugin.Plugin; -import io.roastedroot.proxywasm.plugin.PluginFactory; import java.net.URI; import java.nio.file.Path; import java.util.Map; diff --git a/proxy-wasm-jaxrs-jersey/src/test/java/io/roastedroot/proxywasm/jaxrs/example/tests/BaseTest.java b/proxy-wasm-jaxrs-jersey/src/test/java/io/roastedroot/proxywasm/jaxrs/example/tests/BaseTest.java index 38ee894..d5ea457 100644 --- a/proxy-wasm-jaxrs-jersey/src/test/java/io/roastedroot/proxywasm/jaxrs/example/tests/BaseTest.java +++ b/proxy-wasm-jaxrs-jersey/src/test/java/io/roastedroot/proxywasm/jaxrs/example/tests/BaseTest.java @@ -4,6 +4,7 @@ import io.roastedroot.proxywasm.jaxrs.WasmPluginFeature; import io.roastedroot.proxywasm.jaxrs.example.App; import io.roastedroot.proxywasm.jaxrs.example.Resources; +import io.roastedroot.proxywasm.jaxrs.internal.ServerAdaptor; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; @@ -32,7 +33,7 @@ public void setUp() throws Exception { // Create mock Instance for WasmPluginFeature resourceConfig.register( new WasmPluginFeature( - new io.roastedroot.proxywasm.jaxrs.ServerAdaptor(), + new ServerAdaptor(), App.headerTests(), App.headerTestsNotShared(), App.tickTests(), diff --git a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPlugin.java b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPlugin.java index c4db714..db0d390 100644 --- a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPlugin.java +++ b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPlugin.java @@ -6,6 +6,10 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +/** + * This annotation is used to mark a JAX-RS resource class or method + * as being filters by one or more Wasm-Plugins. + */ @NameBinding @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) diff --git a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPluginFeature.java b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPluginFeature.java index b457929..8a0d44c 100644 --- a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPluginFeature.java +++ b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPluginFeature.java @@ -1,12 +1,16 @@ package io.roastedroot.proxywasm.jaxrs; +import io.roastedroot.proxywasm.PluginFactory; import io.roastedroot.proxywasm.StartException; -import io.roastedroot.proxywasm.plugin.PluginFactory; -import io.roastedroot.proxywasm.plugin.ServerAdaptor; +import io.roastedroot.proxywasm.internal.ServerAdaptor; +import io.roastedroot.proxywasm.jaxrs.internal.AbstractWasmPluginFeature; import java.util.Arrays; +/** + * WasmPluginFeature is a JAX-RS feature that allows the use of Wasm-Plugins to filter JAX-RS + * requests. + */ public class WasmPluginFeature extends AbstractWasmPluginFeature { - public WasmPluginFeature(ServerAdaptor httpServer, PluginFactory... factories) throws StartException { init(Arrays.asList(factories), httpServer); diff --git a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPluginFilter.java b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPluginFilter.java index e669de8..240ae7b 100644 --- a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPluginFilter.java +++ b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPluginFilter.java @@ -1,13 +1,14 @@ package io.roastedroot.proxywasm.jaxrs; -import static io.roastedroot.proxywasm.Helpers.string; +import static io.roastedroot.proxywasm.internal.Helpers.string; -import io.roastedroot.proxywasm.Action; import io.roastedroot.proxywasm.StartException; -import io.roastedroot.proxywasm.plugin.HttpContext; -import io.roastedroot.proxywasm.plugin.Plugin; -import io.roastedroot.proxywasm.plugin.Pool; -import io.roastedroot.proxywasm.plugin.SendResponse; +import io.roastedroot.proxywasm.internal.Action; +import io.roastedroot.proxywasm.internal.Plugin; +import io.roastedroot.proxywasm.internal.PluginHttpContext; +import io.roastedroot.proxywasm.internal.Pool; +import io.roastedroot.proxywasm.internal.SendResponse; +import io.roastedroot.proxywasm.jaxrs.internal.JaxrsHttpRequestAdaptor; import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.container.ContainerRequestContext; import jakarta.ws.rs.container.ContainerRequestFilter; @@ -20,9 +21,14 @@ import java.io.IOException; import java.util.List; +/** + * This class implements the JAX-RS filters to intercept requests and responses by + * one or more Wasm-Plugins. + */ public class WasmPluginFilter implements ContainerRequestFilter, WriterInterceptor, ContainerResponseFilter { - private static final String FILTER_CONTEXT_PROPERTY_NAME = HttpContext.class.getName() + ":"; + private static final String FILTER_CONTEXT_PROPERTY_NAME = + PluginHttpContext.class.getName() + ":"; private final List pluginPools; @@ -125,7 +131,7 @@ private void filter( Pool pluginPool) throws IOException { var httpContext = - (HttpContext) + (PluginHttpContext) requestContext.getProperty( FILTER_CONTEXT_PROPERTY_NAME + pluginPool.name()); if (httpContext == null) { @@ -202,7 +208,7 @@ public void aroundWriteTo(WriterInterceptorContext ctx) for (var pluginPool : pluginPools) { var httpContext = - (HttpContext) + (PluginHttpContext) ctx.getProperty(FILTER_CONTEXT_PROPERTY_NAME + pluginPool.name()); if (httpContext == null) { throw new WebApplicationException(interalServerError()); @@ -238,7 +244,7 @@ public void aroundWriteTo(WriterInterceptorContext ctx) } finally { for (var pluginPool : pluginPools) { var httpContext = - (HttpContext) + (PluginHttpContext) ctx.getProperty(FILTER_CONTEXT_PROPERTY_NAME + pluginPool.name()); // allow other request to use the plugin. diff --git a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/cdi/ServerAdaptor.java b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/cdi/ServerAdaptor.java index 0d28d4d..8cca862 100644 --- a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/cdi/ServerAdaptor.java +++ b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/cdi/ServerAdaptor.java @@ -4,7 +4,10 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.inject.Alternative; +/** + * ServerAdaptor is a CDI alternative for the {@link io.roastedroot.proxywasm.jaxrs.internal.ServerAdaptor} + */ @Alternative @Priority(100) @ApplicationScoped -public class ServerAdaptor extends io.roastedroot.proxywasm.jaxrs.ServerAdaptor {} +public class ServerAdaptor extends io.roastedroot.proxywasm.jaxrs.internal.ServerAdaptor {} diff --git a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/cdi/WasmPluginFeature.java b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/cdi/WasmPluginFeature.java index 4137df9..0853dc2 100644 --- a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/cdi/WasmPluginFeature.java +++ b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/cdi/WasmPluginFeature.java @@ -1,8 +1,9 @@ package io.roastedroot.proxywasm.jaxrs.cdi; +import io.roastedroot.proxywasm.PluginFactory; import io.roastedroot.proxywasm.StartException; -import io.roastedroot.proxywasm.plugin.PluginFactory; -import io.roastedroot.proxywasm.plugin.ServerAdaptor; +import io.roastedroot.proxywasm.internal.ServerAdaptor; +import io.roastedroot.proxywasm.jaxrs.internal.AbstractWasmPluginFeature; import jakarta.annotation.PostConstruct; import jakarta.annotation.PreDestroy; import jakarta.enterprise.context.ApplicationScoped; @@ -11,9 +12,14 @@ import jakarta.inject.Inject; import jakarta.ws.rs.ext.Provider; +/** + * This class is a CDI provider for the WasmPluginFeature. + * It initializes the plugin factories and server adaptor. + * It also handles the lifecycle of the feature. + */ @Provider @ApplicationScoped -public class WasmPluginFeature extends io.roastedroot.proxywasm.jaxrs.AbstractWasmPluginFeature { +public class WasmPluginFeature extends AbstractWasmPluginFeature { @Inject Instance factories; diff --git a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/AbstractWasmPluginFeature.java b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/internal/AbstractWasmPluginFeature.java similarity index 90% rename from proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/AbstractWasmPluginFeature.java rename to proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/internal/AbstractWasmPluginFeature.java index 5e80e12..8f21e92 100644 --- a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/AbstractWasmPluginFeature.java +++ b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/internal/AbstractWasmPluginFeature.java @@ -1,10 +1,12 @@ -package io.roastedroot.proxywasm.jaxrs; +package io.roastedroot.proxywasm.jaxrs.internal; +import io.roastedroot.proxywasm.PluginFactory; import io.roastedroot.proxywasm.StartException; -import io.roastedroot.proxywasm.plugin.Plugin; -import io.roastedroot.proxywasm.plugin.PluginFactory; -import io.roastedroot.proxywasm.plugin.Pool; -import io.roastedroot.proxywasm.plugin.ServerAdaptor; +import io.roastedroot.proxywasm.internal.Plugin; +import io.roastedroot.proxywasm.internal.Pool; +import io.roastedroot.proxywasm.internal.ServerAdaptor; +import io.roastedroot.proxywasm.jaxrs.WasmPlugin; +import io.roastedroot.proxywasm.jaxrs.WasmPluginFilter; import jakarta.ws.rs.container.DynamicFeature; import jakarta.ws.rs.container.ResourceInfo; import jakarta.ws.rs.core.FeatureContext; @@ -27,7 +29,7 @@ public void init(Iterable factories, ServerAdaptor serverAdaptor) for (var factory : factories) { Plugin plugin = null; try { - plugin = factory.create(); + plugin = (Plugin) factory.create(); } catch (Throwable e) { throw new StartException("Plugin create failed.", e); } diff --git a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/JaxrsHttpRequestAdaptor.java b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/internal/JaxrsHttpRequestAdaptor.java similarity index 76% rename from proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/JaxrsHttpRequestAdaptor.java rename to proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/internal/JaxrsHttpRequestAdaptor.java index cebd165..58478cf 100644 --- a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/JaxrsHttpRequestAdaptor.java +++ b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/internal/JaxrsHttpRequestAdaptor.java @@ -1,55 +1,55 @@ -package io.roastedroot.proxywasm.jaxrs; - -import static io.roastedroot.proxywasm.Helpers.bytes; -import static io.roastedroot.proxywasm.Helpers.int32; -import static io.roastedroot.proxywasm.Helpers.string; -import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_DNS_SAN_LOCAL_CERTIFICATE; -import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_DNS_SAN_PEER_CERTIFICATE; -import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_ID; -import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_MTLS; -import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_REQUESTED_SERVER_NAME; -import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_SHA256_PEER_CERTIFICATE_DIGEST; -import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_SUBJECT_LOCAL_CERTIFICATE; -import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_SUBJECT_PEER_CERTIFICATE; -import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_URI_SAN_LOCAL_CERTIFICATE; -import static io.roastedroot.proxywasm.WellKnownProperties.CONNECTION_URI_SAN_PEER_CERTIFICATE; -import static io.roastedroot.proxywasm.WellKnownProperties.DESTINATION_ADDRESS; -import static io.roastedroot.proxywasm.WellKnownProperties.DESTINATION_PORT; -import static io.roastedroot.proxywasm.WellKnownProperties.REQUEST_DURATION; -import static io.roastedroot.proxywasm.WellKnownProperties.REQUEST_HEADERS; -import static io.roastedroot.proxywasm.WellKnownProperties.REQUEST_HOST; -import static io.roastedroot.proxywasm.WellKnownProperties.REQUEST_METHOD; -import static io.roastedroot.proxywasm.WellKnownProperties.REQUEST_PATH; -import static io.roastedroot.proxywasm.WellKnownProperties.REQUEST_PROTOCOL; -import static io.roastedroot.proxywasm.WellKnownProperties.REQUEST_QUERY; -import static io.roastedroot.proxywasm.WellKnownProperties.REQUEST_REFERER; -import static io.roastedroot.proxywasm.WellKnownProperties.REQUEST_SCHEME; -import static io.roastedroot.proxywasm.WellKnownProperties.REQUEST_SIZE; -import static io.roastedroot.proxywasm.WellKnownProperties.REQUEST_TIME; -import static io.roastedroot.proxywasm.WellKnownProperties.REQUEST_TOTAL_SIZE; -import static io.roastedroot.proxywasm.WellKnownProperties.REQUEST_URL_PATH; -import static io.roastedroot.proxywasm.WellKnownProperties.REQUEST_USERAGENT; -import static io.roastedroot.proxywasm.WellKnownProperties.RESPONSE_BACKEND_LATENCY; -import static io.roastedroot.proxywasm.WellKnownProperties.RESPONSE_CODE; -import static io.roastedroot.proxywasm.WellKnownProperties.RESPONSE_CODE_DETAILS; -import static io.roastedroot.proxywasm.WellKnownProperties.RESPONSE_FLAGS; -import static io.roastedroot.proxywasm.WellKnownProperties.RESPONSE_GRPC_STATUS; -import static io.roastedroot.proxywasm.WellKnownProperties.RESPONSE_HEADERS; -import static io.roastedroot.proxywasm.WellKnownProperties.RESPONSE_SIZE; -import static io.roastedroot.proxywasm.WellKnownProperties.RESPONSE_TOTAL_SIZE; -import static io.roastedroot.proxywasm.WellKnownProperties.RESPONSE_TRAILERS; -import static io.roastedroot.proxywasm.WellKnownProperties.SOURCE_ADDRESS; -import static io.roastedroot.proxywasm.WellKnownProperties.SOURCE_PORT; - -import io.roastedroot.proxywasm.ArrayProxyMap; -import io.roastedroot.proxywasm.ProxyMap; -import io.roastedroot.proxywasm.SplitProxyMap; +package io.roastedroot.proxywasm.jaxrs.internal; + +import static io.roastedroot.proxywasm.internal.Helpers.bytes; +import static io.roastedroot.proxywasm.internal.Helpers.int32; +import static io.roastedroot.proxywasm.internal.Helpers.string; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.CONNECTION_DNS_SAN_LOCAL_CERTIFICATE; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.CONNECTION_DNS_SAN_PEER_CERTIFICATE; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.CONNECTION_ID; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.CONNECTION_MTLS; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.CONNECTION_REQUESTED_SERVER_NAME; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.CONNECTION_SHA256_PEER_CERTIFICATE_DIGEST; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.CONNECTION_SUBJECT_LOCAL_CERTIFICATE; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.CONNECTION_SUBJECT_PEER_CERTIFICATE; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.CONNECTION_URI_SAN_LOCAL_CERTIFICATE; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.CONNECTION_URI_SAN_PEER_CERTIFICATE; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.DESTINATION_ADDRESS; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.DESTINATION_PORT; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.REQUEST_DURATION; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.REQUEST_HEADERS; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.REQUEST_HOST; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.REQUEST_METHOD; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.REQUEST_PATH; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.REQUEST_PROTOCOL; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.REQUEST_QUERY; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.REQUEST_REFERER; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.REQUEST_SCHEME; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.REQUEST_SIZE; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.REQUEST_TIME; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.REQUEST_TOTAL_SIZE; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.REQUEST_URL_PATH; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.REQUEST_USERAGENT; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.RESPONSE_BACKEND_LATENCY; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.RESPONSE_CODE; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.RESPONSE_CODE_DETAILS; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.RESPONSE_FLAGS; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.RESPONSE_GRPC_STATUS; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.RESPONSE_HEADERS; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.RESPONSE_SIZE; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.RESPONSE_TOTAL_SIZE; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.RESPONSE_TRAILERS; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.SOURCE_ADDRESS; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.SOURCE_PORT; + import io.roastedroot.proxywasm.WasmException; -import io.roastedroot.proxywasm.WasmResult; -import io.roastedroot.proxywasm.WellKnownHeaders; -import io.roastedroot.proxywasm.WellKnownProperties; -import io.roastedroot.proxywasm.plugin.HttpContext; -import io.roastedroot.proxywasm.plugin.HttpRequestAdaptor; +import io.roastedroot.proxywasm.internal.ArrayProxyMap; +import io.roastedroot.proxywasm.internal.HttpRequestAdaptor; +import io.roastedroot.proxywasm.internal.PluginHttpContext; +import io.roastedroot.proxywasm.internal.ProxyMap; +import io.roastedroot.proxywasm.internal.SplitProxyMap; +import io.roastedroot.proxywasm.internal.WasmResult; +import io.roastedroot.proxywasm.internal.WellKnownHeaders; +import io.roastedroot.proxywasm.internal.WellKnownProperties; import jakarta.ws.rs.container.ContainerRequestContext; import jakarta.ws.rs.container.ContainerResponseContext; import jakarta.ws.rs.core.UriBuilder; @@ -165,7 +165,8 @@ public ProxyMap getGrpcReceiveTrailerMetaData() { } @Override - public byte[] getProperty(HttpContext pluginRequest, List path) throws WasmException { + public byte[] getProperty(PluginHttpContext pluginRequest, List path) + throws WasmException { // Check to see if it's a well known property @@ -343,7 +344,8 @@ else if (RESPONSE_CODE.equals(path)) { } @Override - public WasmResult setProperty(HttpContext pluginRequest, List path, byte[] value) { + public WasmResult setProperty( + PluginHttpContext pluginRequest, List path, byte[] value) { // Check to see if it's a well known property if (REQUEST_PATH.equals(path)) { diff --git a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/MultivaluedMapAdaptor.java b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/internal/MultivaluedMapAdaptor.java similarity index 96% rename from proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/MultivaluedMapAdaptor.java rename to proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/internal/MultivaluedMapAdaptor.java index 513e2e4..ede9462 100644 --- a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/MultivaluedMapAdaptor.java +++ b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/internal/MultivaluedMapAdaptor.java @@ -1,6 +1,6 @@ -package io.roastedroot.proxywasm.jaxrs; +package io.roastedroot.proxywasm.jaxrs.internal; -import io.roastedroot.proxywasm.ProxyMap; +import io.roastedroot.proxywasm.internal.ProxyMap; import jakarta.ws.rs.core.MultivaluedHashMap; import jakarta.ws.rs.core.MultivaluedMap; import java.util.List; diff --git a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/ServerAdaptor.java b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/internal/ServerAdaptor.java similarity index 81% rename from proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/ServerAdaptor.java rename to proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/internal/ServerAdaptor.java index 8e49d25..0c90f55 100644 --- a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/ServerAdaptor.java +++ b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/internal/ServerAdaptor.java @@ -1,10 +1,9 @@ -package io.roastedroot.proxywasm.jaxrs; +package io.roastedroot.proxywasm.jaxrs.internal; -import io.roastedroot.proxywasm.ArrayProxyMap; -import io.roastedroot.proxywasm.ProxyMap; -import io.roastedroot.proxywasm.plugin.HttpCallResponse; -import io.roastedroot.proxywasm.plugin.HttpCallResponseHandler; -import io.roastedroot.proxywasm.plugin.HttpRequestAdaptor; +import io.roastedroot.proxywasm.internal.ArrayProxyMap; +import io.roastedroot.proxywasm.internal.HttpCallResponseHandler; +import io.roastedroot.proxywasm.internal.HttpRequestAdaptor; +import io.roastedroot.proxywasm.internal.ProxyMap; import jakarta.ws.rs.core.UriBuilder; import java.net.URI; import java.net.http.HttpClient; @@ -16,12 +15,25 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -public class ServerAdaptor implements io.roastedroot.proxywasm.plugin.ServerAdaptor { +public class ServerAdaptor implements io.roastedroot.proxywasm.internal.ServerAdaptor { ScheduledExecutorService tickExecutorService = Executors.newScheduledThreadPool(1); ExecutorService executorService = Executors.newWorkStealingPool(5); HttpClient client = HttpClient.newHttpClient(); + private static class HttpCallResponse { + + public final int statusCode; + public final ProxyMap headers; + public final byte[] body; + + public HttpCallResponse(int statusCode, ProxyMap headers, byte[] body) { + this.statusCode = statusCode; + this.headers = headers; + this.body = body; + } + } + @Override public Runnable scheduleTick(long delay, Runnable task) { var f = tickExecutorService.scheduleAtFixedRate(task, delay, delay, TimeUnit.MILLISECONDS); @@ -51,7 +63,7 @@ public Runnable scheduleHttpCall( Callable task = () -> { var resp = httpCall(method, host, port, uri, headers, body); - handler.call(resp); + handler.call(resp.statusCode, resp.headers, resp.body); return null; }; var f = executorService.submit(task); diff --git a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/servlet/ServletJaxrsHttpRequestAdaptor.java b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/servlet/ServletJaxrsHttpRequestAdaptor.java index fc50b28..413d7a8 100644 --- a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/servlet/ServletJaxrsHttpRequestAdaptor.java +++ b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/servlet/ServletJaxrsHttpRequestAdaptor.java @@ -1,6 +1,6 @@ package io.roastedroot.proxywasm.jaxrs.servlet; -import io.roastedroot.proxywasm.jaxrs.JaxrsHttpRequestAdaptor; +import io.roastedroot.proxywasm.jaxrs.internal.JaxrsHttpRequestAdaptor; import jakarta.servlet.http.HttpServletRequest; public class ServletJaxrsHttpRequestAdaptor extends JaxrsHttpRequestAdaptor { diff --git a/quarkus-proxy-wasm-example/src/main/java/io/roastedroot/proxywasm/jaxrs/example/App.java b/quarkus-proxy-wasm-example/src/main/java/io/roastedroot/proxywasm/jaxrs/example/App.java index 23df933..37feec0 100644 --- a/quarkus-proxy-wasm-example/src/main/java/io/roastedroot/proxywasm/jaxrs/example/App.java +++ b/quarkus-proxy-wasm-example/src/main/java/io/roastedroot/proxywasm/jaxrs/example/App.java @@ -3,9 +3,9 @@ import com.dylibso.chicory.experimental.aot.AotMachine; import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; +import io.roastedroot.proxywasm.Plugin; +import io.roastedroot.proxywasm.PluginFactory; import io.roastedroot.proxywasm.StartException; -import io.roastedroot.proxywasm.plugin.Plugin; -import io.roastedroot.proxywasm.plugin.PluginFactory; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.inject.Produces; import java.nio.file.Path; diff --git a/quarkus-proxy-wasm/docs/modules/ROOT/pages/index.adoc b/quarkus-proxy-wasm/docs/modules/ROOT/pages/index.adoc index df11aea..72db69e 100644 --- a/quarkus-proxy-wasm/docs/modules/ROOT/pages/index.adoc +++ b/quarkus-proxy-wasm/docs/modules/ROOT/pages/index.adoc @@ -93,7 +93,6 @@ The example above will apply the `waf` plugin instance to all requests to the `/ === Configuring the Proxy-Wasm plugin - [source,java] ---- package org.example; @@ -101,9 +100,9 @@ package org.example; import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; import io.roastedroot.proxywasm.StartException; -import io.roastedroot.proxywasm.plugin.Plugin; -import io.roastedroot.proxywasm.plugin.PluginFactory; -import io.roastedroot.proxywasm.plugin.SimpleMetricsHandler; +import io.roastedroot.proxywasm.Plugin; +import io.roastedroot.proxywasm.PluginFactory; +import io.roastedroot.proxywasm.SimpleMetricsHandler; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.inject.Produces; import java.io.IOException; diff --git a/quarkus-proxy-wasm/integration-tests/src/main/java/io/quarkiverse/proxywasm/it/App.java b/quarkus-proxy-wasm/integration-tests/src/main/java/io/quarkiverse/proxywasm/it/App.java index df44499..8ae3262 100644 --- a/quarkus-proxy-wasm/integration-tests/src/main/java/io/quarkiverse/proxywasm/it/App.java +++ b/quarkus-proxy-wasm/integration-tests/src/main/java/io/quarkiverse/proxywasm/it/App.java @@ -4,9 +4,9 @@ import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; import com.google.gson.Gson; +import io.roastedroot.proxywasm.Plugin; +import io.roastedroot.proxywasm.PluginFactory; import io.roastedroot.proxywasm.StartException; -import io.roastedroot.proxywasm.plugin.Plugin; -import io.roastedroot.proxywasm.plugin.PluginFactory; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.inject.Produces; import java.net.URI; diff --git a/quarkus-proxy-wasm/runtime/src/main/java/io/quarkiverse/proxywasm/runtime/VertxHttpRequestAdaptor.java b/quarkus-proxy-wasm/runtime/src/main/java/io/quarkiverse/proxywasm/runtime/VertxHttpRequestAdaptor.java index e46338d..0d19ab4 100644 --- a/quarkus-proxy-wasm/runtime/src/main/java/io/quarkiverse/proxywasm/runtime/VertxHttpRequestAdaptor.java +++ b/quarkus-proxy-wasm/runtime/src/main/java/io/quarkiverse/proxywasm/runtime/VertxHttpRequestAdaptor.java @@ -1,6 +1,6 @@ package io.quarkiverse.proxywasm.runtime; -import io.roastedroot.proxywasm.jaxrs.JaxrsHttpRequestAdaptor; +import io.roastedroot.proxywasm.jaxrs.internal.JaxrsHttpRequestAdaptor; import jakarta.annotation.Priority; import jakarta.enterprise.context.RequestScoped; import jakarta.enterprise.inject.Alternative; diff --git a/quarkus-proxy-wasm/runtime/src/main/java/io/quarkiverse/proxywasm/runtime/VertxServerAdaptor.java b/quarkus-proxy-wasm/runtime/src/main/java/io/quarkiverse/proxywasm/runtime/VertxServerAdaptor.java index 8a2c854..45c94a0 100644 --- a/quarkus-proxy-wasm/runtime/src/main/java/io/quarkiverse/proxywasm/runtime/VertxServerAdaptor.java +++ b/quarkus-proxy-wasm/runtime/src/main/java/io/quarkiverse/proxywasm/runtime/VertxServerAdaptor.java @@ -7,13 +7,12 @@ import io.grpc.Metadata; import io.grpc.MethodDescriptor; import io.grpc.Status; -import io.roastedroot.proxywasm.ArrayBytesProxyMap; -import io.roastedroot.proxywasm.ProxyMap; -import io.roastedroot.proxywasm.plugin.GrpcCallResponseHandler; -import io.roastedroot.proxywasm.plugin.HttpCallResponse; -import io.roastedroot.proxywasm.plugin.HttpCallResponseHandler; -import io.roastedroot.proxywasm.plugin.HttpRequestAdaptor; -import io.roastedroot.proxywasm.plugin.ServerAdaptor; +import io.roastedroot.proxywasm.internal.ArrayBytesProxyMap; +import io.roastedroot.proxywasm.internal.GrpcCallResponseHandler; +import io.roastedroot.proxywasm.internal.HttpCallResponseHandler; +import io.roastedroot.proxywasm.internal.HttpRequestAdaptor; +import io.roastedroot.proxywasm.internal.ProxyMap; +import io.roastedroot.proxywasm.internal.ServerAdaptor; import io.vertx.core.Vertx; import io.vertx.core.buffer.Buffer; import io.vertx.core.http.HttpClient; @@ -103,17 +102,15 @@ public Runnable scheduleHttpCall( e.getKey(), e.getValue())); handler.call( - new HttpCallResponse( - result.statusCode(), - h, - bodyHandler.getBytes())); + result.statusCode(), + h, + bodyHandler.getBytes()); }); } else { handler.call( - new HttpCallResponse( - 500, - ProxyMap.of(), - resp.cause().getMessage().getBytes())); + 500, + ProxyMap.of(), + resp.cause().getMessage().getBytes()); } }); diff --git a/quarkus-x-corazawaf-example/src/main/java/io/roastedroot/proxywasm/corazawaf/example/App.java b/quarkus-x-corazawaf-example/src/main/java/io/roastedroot/proxywasm/corazawaf/example/App.java index f13625d..baff31e 100644 --- a/quarkus-x-corazawaf-example/src/main/java/io/roastedroot/proxywasm/corazawaf/example/App.java +++ b/quarkus-x-corazawaf-example/src/main/java/io/roastedroot/proxywasm/corazawaf/example/App.java @@ -3,10 +3,10 @@ import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; import io.roastedroot.proxywasm.LogHandler; +import io.roastedroot.proxywasm.Plugin; +import io.roastedroot.proxywasm.PluginFactory; +import io.roastedroot.proxywasm.SimpleMetricsHandler; import io.roastedroot.proxywasm.StartException; -import io.roastedroot.proxywasm.plugin.Plugin; -import io.roastedroot.proxywasm.plugin.PluginFactory; -import io.roastedroot.proxywasm.plugin.SimpleMetricsHandler; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.inject.Produces; import java.io.IOException; diff --git a/quarkus-x-kuadrant-example/src/main/java/io/roastedroot/proxywasm/kuadrant/example/App.java b/quarkus-x-kuadrant-example/src/main/java/io/roastedroot/proxywasm/kuadrant/example/App.java index 26f0f48..8925c90 100644 --- a/quarkus-x-kuadrant-example/src/main/java/io/roastedroot/proxywasm/kuadrant/example/App.java +++ b/quarkus-x-kuadrant-example/src/main/java/io/roastedroot/proxywasm/kuadrant/example/App.java @@ -1,10 +1,10 @@ package io.roastedroot.proxywasm.kuadrant.example; import io.roastedroot.proxywasm.LogHandler; +import io.roastedroot.proxywasm.Plugin; +import io.roastedroot.proxywasm.PluginFactory; +import io.roastedroot.proxywasm.SimpleMetricsHandler; import io.roastedroot.proxywasm.StartException; -import io.roastedroot.proxywasm.plugin.Plugin; -import io.roastedroot.proxywasm.plugin.PluginFactory; -import io.roastedroot.proxywasm.plugin.SimpleMetricsHandler; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.inject.Produces; import java.io.IOException; From 96fa052db6d9a01b9b1b941d68edacff2aa3a483 Mon Sep 17 00:00:00 2001 From: Hiram Chirino Date: Mon, 21 Apr 2025 17:26:19 -0400 Subject: [PATCH 2/2] Add more javadoc. Signed-off-by: Hiram Chirino --- pom.xml | 14 +- proxy-wasm-java-host/pom.xml | 15 ++ .../proxywasm/ForeignFunction.java | 7 +- .../io/roastedroot/proxywasm/LogHandler.java | 41 ++- .../io/roastedroot/proxywasm/LogLevel.java | 32 ++- .../io/roastedroot/proxywasm/MetricType.java | 30 ++- .../roastedroot/proxywasm/MetricsHandler.java | 52 ++++ .../java/io/roastedroot/proxywasm/Plugin.java | 234 ++++++++++++------ .../roastedroot/proxywasm/PluginFactory.java | 18 ++ .../io/roastedroot/proxywasm/QueueName.java | 35 ++- .../io/roastedroot/proxywasm/SharedData.java | 31 +++ .../proxywasm/SharedDataHandler.java | 45 ++++ .../proxywasm/SharedQueueHandler.java | 50 ++++ .../proxywasm/SimpleMetricsHandler.java | 89 ++++++- .../proxywasm/SimpleSharedDataHandler.java | 62 ++++- .../proxywasm/SimpleSharedQueueHandler.java | 61 ++++- .../roastedroot/proxywasm/StartException.java | 19 +- .../roastedroot/proxywasm/WasmException.java | 26 +- .../proxywasm/jaxrs/example/App.java | 3 +- .../proxywasm/jaxrs/WasmPlugin.java | 2 +- .../proxywasm/jaxrs/WasmPluginFeature.java | 2 +- .../proxywasm/jaxrs/WasmPluginFilter.java | 2 +- .../proxywasm/jaxrs/example/App.java | 1 + .../java/io/quarkiverse/proxywasm/it/App.java | 3 +- .../proxywasm/corazawaf/example/App.java | 1 + 25 files changed, 738 insertions(+), 137 deletions(-) diff --git a/pom.xml b/pom.xml index 94d582b..19b2c91 100644 --- a/pom.xml +++ b/pom.xml @@ -45,7 +45,8 @@ 3.5.2 ${surefire-plugin.version} 3.2.7 - 3.11.2 + 3.11.2 + 3.8.1 5.12.0 @@ -210,17 +211,6 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - ${maven.javadoc.version} - - - **/internal/** - - - - diff --git a/proxy-wasm-java-host/pom.xml b/proxy-wasm-java-host/pom.xml index c7c6555..8957725 100644 --- a/proxy-wasm-java-host/pom.xml +++ b/proxy-wasm-java-host/pom.xml @@ -100,6 +100,21 @@ + + org.apache.maven.plugins + maven-javadoc-plugin + ${javadoc-plugin.version} + + + **/internal/** + + + https://www.javadoc.io/doc/com.dylibso.chicory/wasm/${chicory.version}/ + https://www.javadoc.io/doc/com.dylibso.chicory/runtime/${chicory.version}/ + + + + diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ForeignFunction.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ForeignFunction.java index b6f811c..0d68090 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ForeignFunction.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ForeignFunction.java @@ -1,5 +1,6 @@ package io.roastedroot.proxywasm; -public interface ForeignFunction { - byte[] apply(byte[] data); -} +/** + * A functional interface that represents a foreign function call in the Proxy-Wasm. + */ +public interface ForeignFunction extends java.util.function.Function {} diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/LogHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/LogHandler.java index c096105..3e36836 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/LogHandler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/LogHandler.java @@ -1,9 +1,35 @@ package io.roastedroot.proxywasm; +/** + * Interface for handling logging sent form the guest of Proxy-WASM environment. + *

+ * This interface provides methods to log messages at different levels and to retrieve the current + * log level. + */ public interface LogHandler { - LogHandler DEFAULT = new LogHandler() {}; + /** + * A default, no-operation {@code LogHandler}. + * It ignores all log messages and reports {@link LogLevel#CRITICAL} as the current log level, + * effectively disabling logging. + * + *

Useful as a placeholder or when logging is explicitly disabled. + */ + LogHandler DEFAULT = + new LogHandler() { + @Override + public LogLevel getLogLevel() throws WasmException { + // since we are not logging anything, we can return the highest log level + return LogLevel.CRITICAL; + } + }; + /** + * A simple {@code LogHandler} that logs all messages to {@link System#out}. + * Messages are prefixed with their corresponding {@link LogLevel}. + * The effective log level for this handler is {@link LogLevel#TRACE}, + * meaning all messages will be printed. + */ LogHandler SYSTEM = new LogHandler() { @Override @@ -12,8 +38,21 @@ public void log(LogLevel level, String message) throws WasmException { } }; + /** + * Logs a message at the specified log level. + * + * @param level the log level + * @param message the message to log + * @throws WasmException the result to provide the plugin + */ default void log(LogLevel level, String message) throws WasmException {} + /** + * Gets the current log level. + * + * @return the current log level + * @throws WasmException the result to provide the plugin + */ default LogLevel getLogLevel() throws WasmException { return LogLevel.TRACE; } diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/LogLevel.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/LogLevel.java index 18d9874..d87c096 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/LogLevel.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/LogLevel.java @@ -1,43 +1,57 @@ package io.roastedroot.proxywasm; /** - * Represents log levels for proxy WASM. - * Converted from Go's LogLevel type. + * Represents log levels used within the Proxy-WASM ABI specification. + * These levels correspond to the values expected by the host environment + * when logging messages from a WASM module. + *

+ * Converted from Go's LogLevel type in the proxy-wasm-go-sdk. */ public enum LogLevel { - // The values follow the same order as in the Go code using iota (0-based incrementing) + + /** Trace log level. Value: 0 */ TRACE(0), + /** Debug log level. Value: 1 */ DEBUG(1), + /** Info log level. Value: 2 */ INFO(2), + /** Warn log level. Value: 3 */ WARN(3), + /** Error log level. Value: 4 */ ERROR(4), + /** Critical log level. Value: 5 */ CRITICAL(5); + /** The integer representation of the log level, as defined by the Proxy-WASM ABI. */ private final int value; /** * Constructor for LogLevel enum. * - * @param value The integer value of the log level + * @param value The integer value corresponding to the log level in the ABI. */ LogLevel(int value) { this.value = value; } /** - * Get the integer value of this log level. + * Get the integer value of this log level as defined by the Proxy-WASM ABI. * - * @return The integer value + * @return The integer value representing the log level. */ public int value() { return value; } /** - * Convert an integer value to a LogLevel. + * Convert an integer value to its corresponding LogLevel enum constant. + * This is useful for interpreting log level values received from the host + * or specified in configurations. * - * @param value The integer value to convert - * @return The corresponding LogLevel, or null if not found + * @param value The integer value to convert. + * @return The corresponding LogLevel enum constant. + * @throws IllegalArgumentException if the provided integer value does not + * match any known LogLevel. */ public static LogLevel fromInt(int value) { for (LogLevel level : values()) { diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/MetricType.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/MetricType.java index 441a7df..b6396e7 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/MetricType.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/MetricType.java @@ -1,38 +1,54 @@ package io.roastedroot.proxywasm; /** - * Represents the type of metric in proxy WASM. + * Represents the types of metrics that can be defined and manipulated + * via the Proxy-WASM ABI. */ public enum MetricType { + /** + * A metric that only increments. + * Value: 0 + */ COUNTER(0), + /** + * A metric that can be arbitrarily set. + * Value: 1 + */ GAUGE(1), + /** + * A metric that accumulates observations into predefined buckets + * and a sum of observations. + * Value: 2 + */ HISTOGRAM(2); + /** The integer representation of the metric type, as defined by the Proxy-WASM ABI. */ private final int value; /** * Constructor for MetricType enum. * - * @param value The integer value of the metric type + * @param value The integer value corresponding to the metric type in the ABI. */ MetricType(int value) { this.value = value; } /** - * Get the integer value of this metric type. + * Get the integer value of this metric type as defined by the Proxy-WASM ABI. * - * @return The integer value + * @return The integer value representing the metric type. */ public int getValue() { return value; } /** - * Convert an integer value to a MetricType. + * Convert an integer value to its corresponding MetricType enum constant. * - * @param value The integer value to convert - * @return The corresponding MetricType or null if the value doesn't match any MetricType + * @param value The integer value to convert. + * @return The corresponding MetricType enum constant, or {@code null} if the + * provided integer value does not match any known MetricType. */ public static MetricType fromInt(int value) { for (MetricType type : values()) { diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/MetricsHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/MetricsHandler.java index cf2b356..ec062b9 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/MetricsHandler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/MetricsHandler.java @@ -2,26 +2,78 @@ import io.roastedroot.proxywasm.internal.WasmResult; +/** + * Defines the contract for handling metrics operations initiated by a Proxy-WASM module. + * Implementations of this interface are responsible for interacting with the underlying + * metrics system of the host environment (Prometheus etc.). + * + *

The host environment provides implementations of these methods to allow WASM modules + * to define, manipulate, and retrieve metric values. + */ public interface MetricsHandler { + /** + * A default, non-functional instance of {@code MetricsHandler}. + * This instance throws {@link WasmException} with {@link WasmResult#UNIMPLEMENTED} + * for methods that define or retrieve metrics, and returns {@link WasmResult#UNIMPLEMENTED} + * for methods that modify or remove metrics. + * Useful as a placeholder or base when only a subset of metrics functionality is needed. + */ MetricsHandler DEFAULT = new MetricsHandler() {}; + /** + * Defines a new metric. + * + * @param metricType The type of the metric (e.g., Counter, Gauge, Histogram). + * @param name The name of the metric. + * @return A unique identifier (metric ID) for the newly defined metric. + * @throws WasmException If the metric cannot be defined (e.g., name conflict, unsupported type, + * or if the operation is unimplemented by the host). + */ default int defineMetric(MetricType metricType, String name) throws WasmException { throw new WasmException(WasmResult.UNIMPLEMENTED); } + /** + * Removes or deletes a previously defined metric. + * + * @param metricId The unique identifier of the metric to remove. + * @return A {@link WasmResult} indicating the outcome of the operation (e.g., OK, NOT_FOUND, UNIMPLEMENTED). + */ default WasmResult removeMetric(int metricId) { return WasmResult.UNIMPLEMENTED; } + /** + * Records a value for a metric (typically used for Gauges or Histograms). + * + * @param metricId The unique identifier of the metric. + * @param value The value to record. + * @return A {@link WasmResult} indicating the outcome of the operation (e.g., OK, BAD_ARGUMENT, UNIMPLEMENTED). + */ default WasmResult recordMetric(int metricId, long value) { return WasmResult.UNIMPLEMENTED; } + /** + * Increments a metric's value (typically used for Counters). + * + * @param metricId The unique identifier of the metric. + * @param value The amount to increment by (can be negative to decrement, although convention is usually positive). + * @return A {@link WasmResult} indicating the outcome of the operation (e.g., OK, BAD_ARGUMENT, UNIMPLEMENTED). + */ default WasmResult incrementMetric(int metricId, long value) { return WasmResult.UNIMPLEMENTED; } + /** + * Retrieves the current value of a metric. + * + * @param metricId The unique identifier of the metric. + * @return The current value of the metric. + * @throws WasmException If the metric cannot be retrieved (e.g., metric not found, type mismatch, + * or if the operation is unimplemented by the host). + */ default long getMetric(int metricId) throws WasmException { throw new WasmException(WasmResult.UNIMPLEMENTED); } diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/Plugin.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/Plugin.java index 679325b..21a225a 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/Plugin.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/Plugin.java @@ -14,18 +14,33 @@ import java.util.function.Function; /** - * Plugin is an instance of a Proxy-Wasm plugin. + * Represents an instantiated Proxy-WASM plugin, providing the bridge between the host + * environment and the WASM module. + * + *

This abstract class defines the core structure and lifecycle of a plugin. + * Concrete plugin instances are created using the {@link Builder}. + * The actual WASM instance and interaction logic are managed internally. */ public abstract class Plugin { + /** + * Protected constructor for subclasses. + */ protected Plugin() {} + /** + * Returns the configured name of this plugin instance. + * + * @return the plugin name, which might be null if not explicitly set via the builder. + */ public abstract String name(); /** - * Creates a new Plugin builder. + * Creates a new {@link Builder} to configure and construct a {@link Plugin} instance + * from the given WASM module. * - * @return a new Plugin builder + * @param module the compiled {@link WasmModule} representing the plugin's code. + * @return a new {@link Plugin.Builder} instance. */ public static Plugin.Builder builder(WasmModule module) { return new Plugin.Builder(module); @@ -38,7 +53,7 @@ public static final class Builder { private final WasmModule module; private final ProxyWasm.Builder proxyWasmBuilder = ProxyWasm.builder().withStart(false); - private boolean shared = true; + private boolean shared; private String name; private HashMap foreignFunctions; private HashMap upstreams; @@ -52,20 +67,21 @@ public static final class Builder { private SharedDataHandler sharedDataHandler; /** - * Set the WASM module of the plugin. The module contains the plugin instructions. + * Private constructor for the Builder. + * Initializes the builder with the essential WASM module. * - * @param module the WASM module of the plugin - * @return this builder + * @param module The compiled {@link WasmModule} containing the plugin code. */ private Builder(WasmModule module) { this.module = module; } /** - * Set the name of the plugin. + * Sets the optional name for this plugin instance. + * This name can be used for identification and logging purposes. * - * @param name the name of the plugin - * @return this builder + * @param name the desired name for the plugin. + * @return this {@code Builder} instance for method chaining. */ public Builder withName(String name) { this.name = name; @@ -73,10 +89,14 @@ public Builder withName(String name) { } /** - * Set the foreign functions of that can be called from the plugin. + * Registers foreign (host-provided) functions that can be called by the WASM plugin. + * These functions allow the plugin to interact with the host environment beyond the standard + * Proxy-WASM ABI calls. * - * @param functions the foreign functions of the plugin - * @return this builder + * @param functions A map where keys are the function names expected by the WASM module, + * and values are {@link ForeignFunction} implementations provided by the host. + * @return this {@code Builder} instance for method chaining. + * @see ForeignFunction */ public Builder withForeignFunctions(Map functions) { this.foreignFunctions = new HashMap<>(functions); @@ -84,11 +104,13 @@ public Builder withForeignFunctions(Map functions) { } /** - * Set the upstream server URL + * Defines mappings from logical upstream names (used within the plugin) to actual network URIs. + * This allows the plugin to make network calls (e.g., HTTP, gRPC) to services known by name, + * without needing to hardcode addresses. * - * @param upstreams the upstream URI mappings. When a http or grpc call is made - * from the plugin, the upstream name is used to lookup the URL. - * @return this builder + * @param upstreams A map where keys are the logical upstream names used by the plugin, + * and values are the corresponding {@link URI}s of the target services. + * @return this {@code Builder} instance for method chaining. */ public Builder withUpstreams(Map upstreams) { this.upstreams = new HashMap<>(upstreams); @@ -96,12 +118,18 @@ public Builder withUpstreams(Map upstreams) { } /** - * Set the strict upstreams mode of the plugin. If strict upstreams is enabled, - * then the plugin will throw an error if an upstream is not found. If disabled, - * then the upstream name is used as the URL. + * Configures the behavior when a plugin attempts to call an upstream that is not defined + * in the `upstreams` map provided via {@link #withUpstreams(Map)}. + * + *

If {@code strictUpstreams} is {@code true}, attempting to use an undefined upstream name + * will result in an error being reported back to the plugin. * - * @param strictUpstreams the strict upstreams of the plugin - * @return this builder + *

If {@code strictUpstreams} is {@code false} (the default behavior if this method is not called), + * the host will try to parse the upstream name as URI. + * + * @param strictUpstreams {@code true} to enforce that all used upstream names must be explicitly mapped, + * {@code false} to allow fallback resolution. + * @return this {@code Builder} instance for method chaining. */ public Builder withStrictUpstreams(boolean strictUpstreams) { this.strictUpstreams = strictUpstreams; @@ -109,12 +137,17 @@ public Builder withStrictUpstreams(boolean strictUpstreams) { } /** - * Set the minimum tick period of the plugin. A pluign that requests - * a very small tick period will be ticked very frequently. Use this - * to protect the host from being overwhelmed by the plugin. + * Sets a minimum interval for the plugin's periodic timer ticks ({@code proxy_on_tick}). + * The Proxy-WASM ABI allows plugins to request a timer tick period. This setting enforces + * a lower bound on that period to prevent plugins from requesting excessively frequent ticks, + * which could overload the host. + * + *

If the plugin requests a tick period shorter than this minimum, the host will use + * this minimum value instead. * - * @param minTickPeriodMilliseconds the minimum tick period of the plugin - * @return this builder + * @param minTickPeriodMilliseconds the minimum allowed tick period in milliseconds. A value of 0 or less + * implies no minimum enforcement (host default behavior). + * @return this {@code Builder} instance for method chaining. */ public Builder withMinTickPeriodMilliseconds(int minTickPeriodMilliseconds) { this.minTickPeriodMilliseconds = minTickPeriodMilliseconds; @@ -122,10 +155,13 @@ public Builder withMinTickPeriodMilliseconds(int minTickPeriodMilliseconds) { } /** - * Set the logger of the plugin. + * Provides a {@link LogHandler} implementation for the plugin to use. + * This handler receives log messages generated by the WASM module via the {@code proxy_log} ABI call. + * If no logger is provided, {@link LogHandler#DEFAULT} (a no-op logger) is used. * - * @param logger the logger of the plugin - * @return this builder + * @param logger the {@link LogHandler} implementation to handle plugin logs. + * @return this {@code Builder} instance for method chaining. + * @see LogHandler */ public Builder withLogger(LogHandler logger) { this.logger = logger; @@ -133,12 +169,15 @@ public Builder withLogger(LogHandler logger) { } /** - * Set the metrics handler of the plugin. If the metrics handler is not set, - * then calls by the guest to define/use metrics will result in UNIMPLEMENTED errors - * reported to the guest. + * Provides a {@link MetricsHandler} implementation for the plugin to use. + * This handler manages metric definition, recording, and retrieval requested by the WASM module + * via the relevant {@code proxy_*} ABI calls (e.g., {@code proxy_define_metric}). + * If no handler is provided, {@link MetricsHandler#DEFAULT} (which returns UNIMPLEMENTED) + * might be used implicitly. * - * @param metricsHandler the metrics handler of the plugin - * @return this builder + * @param metricsHandler the {@link MetricsHandler} implementation to manage plugin metrics. + * @return this {@code Builder} instance for method chaining. + * @see MetricsHandler */ public Builder withMetricsHandler(MetricsHandler metricsHandler) { this.metricsHandler = metricsHandler; @@ -146,12 +185,15 @@ public Builder withMetricsHandler(MetricsHandler metricsHandler) { } /** - * Set the shared queue handler of the plugin. If the sahred queue handler is not set, - * then calls by the guest to define/use shared queues will result in UNIMPLEMENTED errors - * reported to the guest. + * Provides a {@link SharedQueueHandler} implementation for the plugin to use. + * This handler manages operations on shared message queues requested by the WASM module + * via the relevant {@code proxy_*} ABI calls (e.g., {@code proxy_register_shared_queue}). + * If no handler is provided, {@link SharedQueueHandler#DEFAULT} (which returns UNIMPLEMENTED) + * might be used implicitly. * - * @param sharedQueueHandler the shared queue handler of the plugin - * @return this builder + * @param sharedQueueHandler the {@link SharedQueueHandler} implementation to manage shared queues. + * @return this {@code Builder} instance for method chaining. + * @see SharedQueueHandler */ public Builder withSharedQueueHandler(SharedQueueHandler sharedQueueHandler) { this.sharedQueueHandler = sharedQueueHandler; @@ -159,12 +201,15 @@ public Builder withSharedQueueHandler(SharedQueueHandler sharedQueueHandler) { } /** - * Set the shared data handler of the plugin. If the shared data handler is not set, - * then calls by the guest to define/use shared data will result in UNIMPLEMENTED errors - * reported to the guest. + * Provides a {@link SharedDataHandler} implementation for the plugin to use. + * This handler manages operations on shared key-value data requested by the WASM module + * via the relevant {@code proxy_*} ABI calls (e.g., {@code proxy_get_shared_data}). + * If no handler is provided, {@link SharedDataHandler#DEFAULT} (which returns UNIMPLEMENTED) + * might be used implicitly. * - * @param sharedDataHandler the shared data handler of the plugin - * @return this builder + * @param sharedDataHandler the {@link SharedDataHandler} implementation to manage shared data. + * @return this {@code Builder} instance for method chaining. + * @see SharedDataHandler */ public Builder withSharedDataHandler(SharedDataHandler sharedDataHandler) { this.sharedDataHandler = sharedDataHandler; @@ -172,12 +217,19 @@ public Builder withSharedDataHandler(SharedDataHandler sharedDataHandler) { } /** - * Set whether the plugin is shared between host requests. If the plugin is shared, - * then the plugin will be created once and reused for each host request. If the plugin - * is not shared, then a new plugin MAY be use for each concurrent host request. + * Configures whether the plugin instance should be shared across multiple host requests or contexts. + * + *

If {@code shared} is {@code true}, a single WASM instance will be created and reused. + * across multiple concurrent requests. Since Proxy-Wasm plugins are not thread-safe, the requests will + * contend on an access lock for the plugin. Using a shared plugin allows the plugin to maintain state + * between the requests. It will use less memory but will have a performance impact due to the contention. * - * @param shared whether the plugin is shared - * @return this builder + *

If {@code shared} is {@code false} (the default), the host will create a new, separate WASM instance for each + * request or context (depending on the host implementation and threading model). This provides better + * isolation, eliminates contention, but consumes more memory. + * + * @param shared {@code true} to indicate the plugin instance can be shared, {@code false} otherwise. + * @return this {@code Builder} instance for method chaining. */ public Builder withShared(boolean shared) { this.shared = shared; @@ -185,10 +237,12 @@ public Builder withShared(boolean shared) { } /** - * Set the VM config of the plugin. + * Sets the Virtual Machine (VM) configuration data for the plugin. + * This configuration is typically provided once when the VM (and the plugin) is initialized. + * It's accessible to the plugin via the {@code proxy_get_vm_configuration} ABI call. * - * @param vmConfig the VM config of the plugin - * @return this builder + * @param vmConfig A byte array containing the VM configuration data. + * @return this {@code Builder} instance for method chaining. */ public Builder withVmConfig(byte[] vmConfig) { this.vmConfig = vmConfig; @@ -196,10 +250,13 @@ public Builder withVmConfig(byte[] vmConfig) { } /** - * Set the VM config of the plugin. + * Sets the Virtual Machine (VM) configuration data for the plugin using a String. + * The string will be converted to bytes using the platform's default charset. + * This configuration is accessible via the {@code proxy_get_vm_configuration} ABI call. * - * @param vmConfig the VM config of the plugin - * @return this builder + * @param vmConfig A String containing the VM configuration data. + * @return this {@code Builder} instance for method chaining. + * @see #withVmConfig(byte[]) */ public Builder withVmConfig(String vmConfig) { this.vmConfig = bytes(vmConfig); @@ -207,10 +264,13 @@ public Builder withVmConfig(String vmConfig) { } /** - * Set the plugin config of the plugin. + * Sets the specific configuration data for this plugin instance. + * This configuration is provided during the plugin's initialization phase + * (via {@code proxy_on_configure}) and allows tailoring the plugin's behavior. + * It's accessible to the plugin via the {@code proxy_get_plugin_configuration} ABI call. * - * @param pluginConfig the plugin config of the plugin - * @return this builder + * @param pluginConfig A byte array containing the plugin-specific configuration data. + * @return this {@code Builder} instance for method chaining. */ public Builder withPluginConfig(byte[] pluginConfig) { this.pluginConfig = pluginConfig; @@ -218,10 +278,13 @@ public Builder withPluginConfig(byte[] pluginConfig) { } /** - * Set the plugin config of the plugin. + * Sets the specific configuration data for this plugin instance using a String. + * The string will be converted to bytes using the platform's default charset. + * This configuration is accessible via the {@code proxy_get_plugin_configuration} ABI call. * - * @param pluginConfig the plugin config of the plugin - * @return this builder + * @param pluginConfig A String containing the plugin-specific configuration data. + * @return this {@code Builder} instance for method chaining. + * @see #withPluginConfig(byte[]) */ public Builder withPluginConfig(String pluginConfig) { this.pluginConfig = bytes(pluginConfig); @@ -229,10 +292,10 @@ public Builder withPluginConfig(String pluginConfig) { } /** - * Set the import memory of the plugin. + * Provides an explicit memory instance to be used by the WASM module. * - * @param memory the import memory of the plugin - * @return this builder + * @param memory The {@link ImportMemory} instance to be used by the WASM module. + * @return this {@code Builder} instance for method chaining. */ public Builder withImportMemory(ImportMemory memory) { proxyWasmBuilder.withImportMemory(memory); @@ -240,14 +303,16 @@ public Builder withImportMemory(ImportMemory memory) { } /** - * Set the machine factory of the plugin. The machine factory is used to control - * how instructions are executed. By default instructions are executed in a - * by an interpreter. To increase performance, you can use compile the - * was instructions to bytecode at runtime or at build time. For more information - * see https://chicory.dev/docs/experimental/aot + * Configures a custom factory for creating the {@link Machine} used to execute the WASM code. + * The {@link Machine} controls the low-level execution of WASM instructions. + * By default, an interpreter-based machine is used. + * Providing a custom factory allows using alternative execution strategies, such as + * Ahead-Of-Time (AOT) compilation to improve execution performance. + * + *

See the Chicory documentation (https://chicory.dev/docs/experimental/aot) for more details on Aot compilation. * - * @param machineFactory the machine factory of the plugin - * @return this builder + * @param machineFactory A function that takes a WASM {@link Instance} and returns a {@link Machine}. + * @return this {@code Builder} instance for method chaining. */ public Builder withMachineFactory(Function machineFactory) { proxyWasmBuilder.withMachineFactory(machineFactory); @@ -255,12 +320,13 @@ public Builder withMachineFactory(Function machineFactory) { } /** - * Set the WASI options of the plugin. A default WASI enviroment will be provided - * to the pluign. You can use this method to customize the WASI environment, - * for example to provide it access to some file system resources. + * Configures WebAssembly System Interface (WASI) options for the plugin instance. + * WASI provides a standard interface for WASM modules to interact with the underlying operating system + * for tasks like file system access, environment variables, etc. While Proxy-WASM defines its own ABI, + * some modules might also utilize WASI features. * - * @param options the WASI options of the plugin - * @return this builder + * @param options The {@link WasiOptions} to configure for the WASI environment. + * @return this {@code Builder} instance for method chaining. */ public Builder withWasiOptions(WasiOptions options) { proxyWasmBuilder.withWasiOptions(options); @@ -268,12 +334,18 @@ public Builder withWasiOptions(WasiOptions options) { } /** - * Build the plugin. + * Constructs and initializes the {@link Plugin} instance based on the configuration + * provided to this builder. + * + *

This involves setting up the WASM environment, linking host functions, applying configurations, + * and calling the necessary Proxy-WASM lifecycle functions (like {@code _start} and + * {@code proxy_on_vm_start}). * - * @return the plugin - * @throws StartException if the plugin fails to start + * @return The fully configured and initialized {@link Plugin} instance. + * @throws StartException If any error occurs during the plugin initialization process + * (e.g., WASM instantiation failure, error during {@code proxy_on_vm_start}). */ - public io.roastedroot.proxywasm.internal.Plugin build() throws StartException { + public Plugin build() throws StartException { return new io.roastedroot.proxywasm.internal.Plugin( proxyWasmBuilder.build(module), shared, diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/PluginFactory.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/PluginFactory.java index 395fc1c..879b131 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/PluginFactory.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/PluginFactory.java @@ -1,5 +1,23 @@ package io.roastedroot.proxywasm; +/** + * A functional interface representing a factory for creating {@link Plugin} instances. + * + *

This is typically used in scenarios where plugin instantiation needs to be deferred + * or customized, potentially based on configuration or context available at runtime. + * Implementations might handle loading WASM modules, configuring builders, and returning + * the ready-to-use plugin. + */ +@FunctionalInterface public interface PluginFactory { + /** + * Creates and returns a new {@link Plugin} instance. + * Implementations are responsible for all necessary setup, including potentially + * loading the WASM module and configuring it using {@link Plugin#builder(com.dylibso.chicory.wasm.WasmModule)}. + * + * @return A newly created {@link Plugin} instance. + * @throws Exception If any error occurs during plugin creation (e.g., file loading, WASM instantiation, + * initialization errors within the plugin's start lifecycle). + */ Plugin create() throws Exception; } diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/QueueName.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/QueueName.java index 03c0508..2547298 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/QueueName.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/QueueName.java @@ -2,13 +2,34 @@ import java.util.Objects; +/** + * Represents the identifier for a shared queue within the Proxy-WASM environment. + * A queue name is typically composed of a Virtual Machine (VM) identifier and a + * queue-specific name string. + * + *

The VM ID provides a namespace, allowing different VMs (potentially running + * different WASM modules or configurations) to define queues with the same name string + * without collision. + * + *

Instances of this class are immutable. + */ public class QueueName { + private final String vmId; private final String name; + /** + * Constructs a new QueueName. + * + * @param vmId The identifier of the VM context associated with this queue. + * Cannot be null. + * @param name The specific name of the queue within the VM context. + * Cannot be null. + * @throws NullPointerException if either {@code vmId} or {@code name} is null. + */ public QueueName(String vmId, String name) { - this.vmId = vmId; - this.name = name; + this.vmId = Objects.requireNonNull(vmId, "vmId cannot be null"); + this.name = Objects.requireNonNull(name, "name cannot be null"); } @Override @@ -25,10 +46,20 @@ public int hashCode() { return Objects.hash(vmId, name); } + /** + * Gets the VM identifier part of the queue name. + * + * @return The non-null VM ID string. + */ public String vmId() { return vmId; } + /** + * Gets the specific name string part of the queue name. + * + * @return The non-null queue name string. + */ public String name() { return name; } diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SharedData.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SharedData.java index 791b623..7478876 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SharedData.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SharedData.java @@ -1,18 +1,49 @@ package io.roastedroot.proxywasm; +/** + * Represents a unit of shared data retrieved from the host environment via + * {@link SharedDataHandler#getSharedData(String)}. + * + *

This class encapsulates the data itself (as a byte array) and its associated + * Compare-And-Swap (CAS) value. The CAS value acts as a version identifier, enabling + * optimistic concurrency control when updating shared data using + * {@link SharedDataHandler#setSharedData(String, byte[], int)}. + */ public class SharedData { private final byte[] data; private final int cas; + /** + * Constructs a new SharedData instance. + * + * @param data The raw byte data retrieved from the shared data store. + * This might be {@code null} if the key was found but had no associated value, + * depending on the {@link SharedDataHandler} implementation. + * @param cas The Compare-And-Swap (version) value associated with this data. + * A value of 0 typically indicates the key did not exist or CAS is not supported/applicable. + */ public SharedData(byte[] data, int cas) { this.data = data; this.cas = cas; } + /** + * Gets the raw data bytes. + * + * @return The byte array representing the shared data's value. + * May be {@code null}. + */ public byte[] data() { return data; } + /** + * Gets the Compare-And-Swap (CAS) value associated with this data. + * This value should be passed back to {@link SharedDataHandler#setSharedData(String, byte[], int)} + * when performing a conditional update. + * + * @return The integer CAS value. + */ public int cas() { return cas; } diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SharedDataHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SharedDataHandler.java index deef39a..2a6209a 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SharedDataHandler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SharedDataHandler.java @@ -2,14 +2,59 @@ import io.roastedroot.proxywasm.internal.WasmResult; +/** + * Defines the contract for handling shared key-value data accessible by Proxy-WASM modules. + * Implementations of this interface manage the storage, retrieval, and conditional update + * (using CAS - Compare-And-Swap) of data that can be shared across different WASM module + * instances or even different VMs, depending on the host environment's implementation. + * + *

Shared data provides a mechanism for state sharing, caching, or coordination between plugins. + */ public interface SharedDataHandler { + /** + * A default, non-functional instance of {@code SharedDataHandler}. + * This instance throws {@link WasmException} with {@link WasmResult#UNIMPLEMENTED} + * for {@link #getSharedData(String)} and returns {@link WasmResult#UNIMPLEMENTED} + * for {@link #setSharedData(String, byte[], int)}. + * Useful as a placeholder or base when shared data functionality is not supported or needed. + */ SharedDataHandler DEFAULT = new SharedDataHandler() {}; + /** + * Retrieves the shared data associated with the given key. + * The result includes the data itself and a CAS (Compare-And-Swap) value, which represents + * the version of the data. The CAS value is used for optimistic concurrency control + * during updates via {@link #setSharedData(String, byte[], int)}. + * + * @param key The key identifying the shared data item. + * @return A {@link SharedData} object containing the value and its CAS. + * @throws WasmException If the key is not found ({@link WasmResult#NOT_FOUND}), + * or if the operation is unimplemented by the host. + */ default SharedData getSharedData(String key) throws WasmException { throw new WasmException(WasmResult.UNIMPLEMENTED); } + /** + * Sets or updates the shared data associated with the given key. + * This operation can be conditional based on the provided CAS value. + * + *

The {@code cas} parameter enables Compare-And-Swap: + *

    + *
  • If {@code cas} is 0, the operation is unconditional (a blind write/overwrite).
  • + *
  • If {@code cas} is non-zero, the operation only succeeds if the current CAS value + * stored in the host for the given {@code key} matches the provided {@code cas}. + * If they don't match, it means the data was modified by another actor since it was + * last read, and the operation fails with {@link WasmResult#CAS_MISMATCH}.
  • + *
+ * + * @param key The key identifying the shared data item. + * @param value The new data value to store (can be null or empty, depending on implementation). A null value might signify deletion. + * @param cas The Compare-And-Swap value expected for a conditional update, or 0 for an unconditional update. + * @return A {@link WasmResult} indicating the outcome (e.g., {@link WasmResult#OK}, + * {@link WasmResult#CAS_MISMATCH}, {@link WasmResult#UNIMPLEMENTED}). + */ default WasmResult setSharedData(String key, byte[] value, int cas) { return WasmResult.UNIMPLEMENTED; } diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SharedQueueHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SharedQueueHandler.java index 3c10fab..abb1efb 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SharedQueueHandler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SharedQueueHandler.java @@ -2,22 +2,72 @@ import io.roastedroot.proxywasm.internal.WasmResult; +/** + * Defines the contract for handling shared message queues accessible by Proxy-WASM modules. + * Implementations of this interface manage the registration, resolution, and data manipulation + * (enqueue/dequeue) of queues that can potentially be accessed by multiple WASM modules + * or different instances of the same module, depending on the host environment's implementation. + * + *

Shared queues provide a mechanism for inter-plugin communication or for passing data + * between different processing stages involving WASM modules. + */ public interface SharedQueueHandler { + /** + * A default, non-functional instance of {@code SharedQueueHandler}. + * This instance throws {@link WasmException} with {@link WasmResult#UNIMPLEMENTED} + * for methods that register, resolve, or dequeue from queues, and returns + * {@link WasmResult#UNIMPLEMENTED} for enqueue operations. + * Useful as a placeholder or base when shared queue functionality is not supported or needed. + */ SharedQueueHandler DEFAULT = new SharedQueueHandler() {}; + /** + * Registers a shared queue with the given name, creating it if it doesn't exist, + * and returns its unique identifier (queue ID). + * + * @param name The {@link QueueName} uniquely identifying the queue (VM ID + queue name string). + * @return The unique integer ID assigned to the queue. + * @throws WasmException If the queue cannot be registered (e.g., resource limits, invalid name) + * or if the operation is unimplemented by the host. + */ default int registerSharedQueue(QueueName name) throws WasmException { throw new WasmException(WasmResult.UNIMPLEMENTED); } + /** + * Resolves the unique identifier (queue ID) for a shared queue given its name. + * This does not create the queue if it doesn't exist. + * + * @param name The {@link QueueName} uniquely identifying the queue (VM ID + queue name string). + * @return The unique integer ID of the existing queue. + * @throws WasmException If the queue cannot be found ({@link WasmResult#NOT_FOUND}), + * or if the operation is unimplemented by the host. + */ default int resolveSharedQueue(QueueName name) throws WasmException { throw new WasmException(WasmResult.UNIMPLEMENTED); } + /** + * Removes and returns the next available message (byte array) from the front of the specified queue. + * + * @param queueId The unique integer ID of the target queue. + * @return The dequeued message data as a byte array. + * @throws WasmException If the queue is empty ({@link WasmResult#EMPTY}), the queue ID is invalid + * ({@link WasmResult#NOT_FOUND}), or the operation is unimplemented by the host. + */ default byte[] dequeueSharedQueue(int queueId) throws WasmException { throw new WasmException(WasmResult.UNIMPLEMENTED); } + /** + * Adds a message (byte array) to the end of the specified shared queue. + * + * @param queueId The unique integer ID of the target queue. + * @param value The message data to enqueue. + * @return A {@link WasmResult} indicating the outcome (e.g., {@link WasmResult#OK}, + * {@link WasmResult#NOT_FOUND}, {@link WasmResult#UNIMPLEMENTED}). + */ default WasmResult enqueueSharedQueue(int queueId, byte[] value) { return WasmResult.UNIMPLEMENTED; } diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SimpleMetricsHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SimpleMetricsHandler.java index bbd2274..8cd2961 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SimpleMetricsHandler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SimpleMetricsHandler.java @@ -5,48 +5,96 @@ import java.util.concurrent.atomic.AtomicInteger; /** - * A simple implementation of the MetricsHandler interface that keeps track of metrics in memory. + * A basic, in-memory implementation of the {@link MetricsHandler} interface. + * + *

This handler manages metrics entirely within the host's memory using standard Java collections. + * It supports Counter, Gauge, and Histogram types in a simplified manner (e.g., Histograms are + * treated like Gauges for storage). It is suitable for single-process environments or testing + * scenarios where integration with a real metrics backend (like Prometheus, StatsD) is not required. + * + *

All operations on this handler are synchronized to ensure thread safety within a single JVM. */ public class SimpleMetricsHandler implements MetricsHandler { + /** + * Represents an individual metric managed by {@link SimpleMetricsHandler}. + * Stores the metric's ID, type, name, and its current value. + * Note: Histograms are stored as a single value (like a Gauge) in this simple implementation. + */ public static class Metric { private final int id; private final MetricType type; private final String name; - private long value; - + private long value; // Represents counter/gauge value, or potentially sum for histograms + + /** + * Constructs a new Metric instance. + * + * @param id The unique integer ID assigned by the handler. + * @param type The {@link MetricType} (Counter, Gauge, Histogram). + * @param name The name of the metric. + */ public Metric(int id, MetricType type, String name) { this.id = id; this.type = type; this.name = name; } + /** + * @return The unique integer ID of the metric. + */ public int id() { return id; } + /** + * @return The {@link MetricType} of the metric. + */ public MetricType type() { return type; } + /** + * @return The name of the metric. + */ public String name() { return name; } + /** + * @return The current value of the metric. + */ public long getValue() { return value; } + /** + * Sets the current value of the metric. + * Used internally by {@link SimpleMetricsHandler#recordMetric(int, long)} and + * {@link SimpleMetricsHandler#incrementMetric(int, long)}. + * + * @param value The new value for the metric. + */ public void setValue(long value) { this.value = value; } } private final AtomicInteger lastMetricId = new AtomicInteger(0); - private HashMap metrics = new HashMap<>(); - private HashMap metricsByName = new HashMap<>(); - + private final HashMap metrics = new HashMap<>(); + private final HashMap metricsByName = new HashMap<>(); + + /** + * Defines a new metric with the specified type and name. + * Assigns a new unique ID to the metric and stores it in memory. + * If a metric with the same name already exists, it is overwritten (behavior may vary in other handlers). + * + * @param type The {@link MetricType} of the new metric. + * @param name The name for the new metric. + * @return The unique integer ID assigned to the newly defined metric. + * @throws WasmException (Not currently thrown by this implementation, but part of the interface contract). + */ @Override public synchronized int defineMetric(MetricType type, String name) throws WasmException { var id = lastMetricId.incrementAndGet(); @@ -56,6 +104,13 @@ public synchronized int defineMetric(MetricType type, String name) throws WasmEx return id; } + /** + * Retrieves the current value of the specified metric. + * + * @param metricId The unique ID of the metric to query. + * @return The current value of the metric. + * @throws WasmException with {@link WasmResult#NOT_FOUND} if no metric exists with the given ID. + */ @Override public synchronized long getMetric(int metricId) throws WasmException { var metric = metrics.get(metricId); @@ -65,6 +120,14 @@ public synchronized long getMetric(int metricId) throws WasmException { return metric.getValue(); } + /** + * Increments the value of the specified metric by the given amount. + * Applicable primarily to Counters, but this implementation applies it additively to any metric type. + * + * @param metricId The unique ID of the metric to increment. + * @param value The amount to add to the metric's current value. + * @return {@link WasmResult#OK} if successful, or {@link WasmResult#NOT_FOUND} if the metric ID is invalid. + */ @Override public synchronized WasmResult incrementMetric(int metricId, long value) { var metric = metrics.get(metricId); @@ -75,6 +138,14 @@ public synchronized WasmResult incrementMetric(int metricId, long value) { return WasmResult.OK; } + /** + * Sets the value of the specified metric to the given value. + * Applicable primarily to Gauges, but this implementation applies it directly to any metric type. + * + * @param metricId The unique ID of the metric to record. + * @param value The value to set for the metric. + * @return {@link WasmResult#OK} if successful, or {@link WasmResult#NOT_FOUND} if the metric ID is invalid. + */ @Override public synchronized WasmResult recordMetric(int metricId, long value) { var metric = metrics.get(metricId); @@ -85,6 +156,12 @@ public synchronized WasmResult recordMetric(int metricId, long value) { return WasmResult.OK; } + /** + * Removes the metric definition and its associated value. + * + * @param metricId The unique ID of the metric to remove. + * @return {@link WasmResult#OK} if the metric was successfully removed, or {@link WasmResult#NOT_FOUND} if no metric exists with the given ID. + */ @Override public synchronized WasmResult removeMetric(int metricId) { Metric metric = metrics.remove(metricId); diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SimpleSharedDataHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SimpleSharedDataHandler.java index a167b1f..f775650 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SimpleSharedDataHandler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SimpleSharedDataHandler.java @@ -4,32 +4,80 @@ import java.util.HashMap; /** - * A simple implementation of the SharedDataHandler interface that keeps the shared data in memory. + * A basic, in-memory implementation of the {@link SharedDataHandler} interface. + * + *

This handler manages shared key-value data entirely within the host's memory using a + * {@link HashMap}. It supports Compare-And-Swap (CAS) operations for optimistic concurrency control. + * It is suitable for single-process environments or testing scenarios where data persistence or + * cross-process sharing is not required. + * + *

All operations on this handler are synchronized to ensure thread safety within a single JVM. */ public class SimpleSharedDataHandler implements SharedDataHandler { private final HashMap sharedData = new HashMap<>(); + /** + * Retrieves the shared data associated with the given key from the in-memory store. + * + * @param key The key identifying the shared data item. + * @return A {@link SharedData} object containing the value and its current CAS value, + * or {@code null} if the key is not found in the map. + * @throws WasmException (Not currently thrown by this implementation, but part of the interface contract). + */ @Override public synchronized SharedData getSharedData(String key) throws WasmException { + // Note: The interface contract might expect a NOT_FOUND exception, but returning null + // is also common for map lookups. The caller (ProxyWasm runtime) should handle null. return sharedData.get(key); } + /** + * Sets or updates the shared data associated with the given key in the in-memory store, + * potentially performing a Compare-And-Swap (CAS) check. + * + *

CAS behavior: + *

    + *
  • If the key does not exist: The operation succeeds only if {@code cas} is 0. A new entry + * is created with the given value and a CAS value of 0 (or 1, depending on interpretation, + * this implementation uses 0 initially, then increments).
  • + *
  • If the key exists: The operation succeeds only if {@code cas} is 0 (unconditional update) + * or if {@code cas} matches the current CAS value stored for the key. On successful update, + * the CAS value is incremented.
  • + *
+ * + * Setting {@code value} to {@code null} effectively removes the key if the CAS check passes + * (or if cas is 0), as {@code HashMap} allows null values. + * + * @param key The key identifying the shared data item. + * @param value The new data value to store (can be null). + * @param cas The Compare-And-Swap value for conditional update, or 0 for unconditional update. + * @return {@link WasmResult#OK} if the update was successful, or + * {@link WasmResult#CAS_MISMATCH} if the CAS check failed. + */ @Override public synchronized WasmResult setSharedData(String key, byte[] value, int cas) { - SharedData prev = sharedData.get(key); - if (prev == null) { + SharedData current = sharedData.get(key); + int nextCas = (current == null) ? 1 : current.cas() + 1; // Simple incrementing CAS + + if (current == null) { + // Key does not exist if (cas == 0) { - sharedData.put(key, new SharedData(value, 0)); + // Unconditional set/create + sharedData.put(key, new SharedData(value, nextCas)); return WasmResult.OK; } else { - return WasmResult.CAS_MISMATCH; + // CAS specified for a non-existent key + return WasmResult.CAS_MISMATCH; // Or NOT_FOUND, depending on desired semantics } } else { - if (cas == 0 || prev.cas() == cas) { - sharedData.put(key, new SharedData(value, prev.cas() + 1)); + // Key exists + if (cas == 0 || current.cas() == cas) { + // Unconditional update OR CAS matches + sharedData.put(key, new SharedData(value, nextCas)); return WasmResult.OK; } else { + // CAS mismatch return WasmResult.CAS_MISMATCH; } } diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SimpleSharedQueueHandler.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SimpleSharedQueueHandler.java index 9b5c1d3..e846bdf 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SimpleSharedQueueHandler.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/SimpleSharedQueueHandler.java @@ -6,15 +6,37 @@ import java.util.concurrent.atomic.AtomicInteger; /** - * Simple implementation of SharedQueueHandler that keep the queues in memory. + * A basic, in-memory implementation of the {@link SharedQueueHandler} interface. + * + *

This handler manages shared queues entirely within the host's memory using standard Java collections. + * It is suitable for single-process environments or testing scenarios where queue persistence or + * cross-process sharing is not required. + * + *

All operations on this handler are synchronized to ensure thread safety within a single JVM. */ public class SimpleSharedQueueHandler implements SharedQueueHandler { + /** + * Represents an individual shared queue managed by {@link SimpleSharedQueueHandler}. + * Each queue has a unique name (within its VM), a host-assigned ID, and holds its data + * in an in-memory list. + */ public static class SharedQueue { + /** The name identifying this queue, composed of a VM ID and queue name string. */ public final QueueName queueName; + + /** The underlying list holding the byte array messages in the queue (FIFO order). */ public final LinkedList data = new LinkedList<>(); + + /** The unique integer ID assigned to this queue by the handler. */ public final int id; + /** + * Constructs a new SharedQueue instance. + * + * @param queueName The {@link QueueName} identifying this queue. + * @param id The unique integer ID assigned by the handler. + */ public SharedQueue(QueueName queueName, int id) { this.queueName = queueName; this.id = id; @@ -24,10 +46,24 @@ public SharedQueue(QueueName queueName, int id) { private final AtomicInteger lastSharedQueueId = new AtomicInteger(0); private final HashMap sharedQueues = new HashMap<>(); + /** + * Retrieves the internal {@link SharedQueue} object associated with the given ID. + * This is primarily for internal use or testing/inspection. + * + * @param queueId The ID of the queue to retrieve. + * @return The {@link SharedQueue} instance, or {@code null} if no queue exists with that ID. + */ public synchronized SharedQueue getSharedQueue(int queueId) { return sharedQueues.get(queueId); } + /** + * Adds a message (byte array) to the end of the specified queue. + * + * @param queueId The ID of the target queue. + * @param value The message data to enqueue. + * @return {@link WasmResult#OK} if successful, or {@link WasmResult#NOT_FOUND} if the queue ID is invalid. + */ @Override public synchronized WasmResult enqueueSharedQueue(int queueId, byte[] value) { SharedQueue queue = sharedQueues.get(queueId); @@ -38,6 +74,13 @@ public synchronized WasmResult enqueueSharedQueue(int queueId, byte[] value) { return WasmResult.OK; } + /** + * Removes and returns the message at the front of the specified queue. + * + * @param queueId The ID of the target queue. + * @return The dequeued message data as a byte array, or {@code null} if the queue is empty. + * @throws WasmException with {@link WasmResult#NOT_FOUND} if the queue ID is invalid. + */ @Override public synchronized byte[] dequeueSharedQueue(int queueId) throws WasmException { SharedQueue queue = sharedQueues.get(queueId); @@ -47,6 +90,13 @@ public synchronized byte[] dequeueSharedQueue(int queueId) throws WasmException return queue.data.poll(); } + /** + * Finds the ID of an existing queue based on its name. + * + * @param queueName The {@link QueueName} (VM ID and name string) to look up. + * @return The integer ID of the existing queue. + * @throws WasmException with {@link WasmResult#NOT_FOUND} if no queue with the specified name exists. + */ @Override public synchronized int resolveSharedQueue(QueueName queueName) throws WasmException { var existing = @@ -60,6 +110,15 @@ public synchronized int resolveSharedQueue(QueueName queueName) throws WasmExcep } } + /** + * Registers a new shared queue with the given name or returns the ID if it already exists. + * If the queue does not exist, a new unique ID is generated and associated with the name. + * + * @param queueName The {@link QueueName} (VM ID and name string) to register or resolve. + * @return The integer ID of the (potentially newly created) queue. + * @throws WasmException (Although not declared, could potentially occur in subclass implementations + * or future versions, e.g., due to resource limits. Current implementation does not throw.) + */ @Override public synchronized int registerSharedQueue(QueueName queueName) throws WasmException { var existing = diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/StartException.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/StartException.java index 86fccb1..77b8315 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/StartException.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/StartException.java @@ -1,13 +1,30 @@ package io.roastedroot.proxywasm; /** - * StartException is thrown when there is an error during the start of a plugin. + * Exception thrown when an error occurs during the initialization or startup phase of a Proxy-WASM plugin. + * + *

This typically happens during the execution of the {@link Plugin.Builder#build()} method, + * encompassing issues such as WASM module instantiation failures, errors within the WASM + * {@code _start} function, or failures during the initial {@code proxy_on_vm_start} or + * {@code proxy_on_configure} calls within the plugin. */ public class StartException extends Exception { + /** + * Constructs a new StartException with the specified detail message. + * + * @param message the detail message. + */ public StartException(String message) { super(message); } + /** + * Constructs a new StartException with the specified detail message and cause. + * + * @param message the detail message. + * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method). + * (A {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.) + */ public StartException(String message, Throwable cause) { super(message, cause); } diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/WasmException.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/WasmException.java index f37fd13..7a02a19 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/WasmException.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/WasmException.java @@ -3,18 +3,40 @@ import io.roastedroot.proxywasm.internal.WasmResult; /** - * Represents WebAssembly result codes. - * Converted from Go's WasmResult type. + * An exception used to signal specific outcomes or errors from host functions + * back to the calling Proxy-WASM module. + * + *

This exception wraps a {@link WasmResult} enum value, which corresponds to the + * standard result codes defined in the Proxy-WASM ABI specification. When a host function + * (like those defined in {@link MetricsHandler}, {@link SharedQueueHandler}, etc.) needs + * to return a status other than simple success (which is often indicated by a void return + * or a non-exceptional return value), it throws a {@code WasmException} containing the + * appropriate {@code WasmResult}. + * + *

The runtime catches this exception and translates its contained {@code WasmResult} + * into the integer value expected by the WASM module according to the ABI. */ public class WasmException extends Exception { + /** The specific Proxy-WASM result code associated with this exception. */ private final WasmResult result; + /** + * Constructs a new WasmException with the specified result code. + * The exception message is automatically set to the description of the {@link WasmResult}. + * + * @param result The {@link WasmResult} representing the outcome or error condition. + */ public WasmException(WasmResult result) { super(result.description()); this.result = result; } + /** + * Gets the underlying {@link WasmResult} code associated with this exception. + * + * @return The non-null {@link WasmResult} enum value. + */ public WasmResult result() { return result; } diff --git a/proxy-wasm-jaxrs-jersey/src/test/java/io/roastedroot/proxywasm/jaxrs/example/App.java b/proxy-wasm-jaxrs-jersey/src/test/java/io/roastedroot/proxywasm/jaxrs/example/App.java index 8382e9b..c5c53dc 100644 --- a/proxy-wasm-jaxrs-jersey/src/test/java/io/roastedroot/proxywasm/jaxrs/example/App.java +++ b/proxy-wasm-jaxrs-jersey/src/test/java/io/roastedroot/proxywasm/jaxrs/example/App.java @@ -24,6 +24,7 @@ public static PluginFactory headerTests() throws StartException { return () -> Plugin.builder(parseTestModule("/go-examples/unit_tester/main.wasm")) .withName("headerTests") + .withShared(true) .withLogger(new MockLogger("headerTests")) .withPluginConfig(gson.toJson(Map.of("type", "headerTests"))) .withMachineFactory(AotMachine::new) @@ -34,7 +35,6 @@ public static PluginFactory headerTestsNotShared() throws StartException { return () -> Plugin.builder(parseTestModule("/go-examples/unit_tester/main.wasm")) .withName("headerTestsNotShared") - .withShared(false) .withLogger(new MockLogger("headerTestsNotShared")) .withPluginConfig(gson.toJson(Map.of("type", "headerTests"))) .withMachineFactory(AotMachine::new) @@ -45,6 +45,7 @@ public static PluginFactory tickTests() throws StartException { return () -> Plugin.builder(parseTestModule("/go-examples/unit_tester/main.wasm")) .withName("tickTests") + .withShared(true) .withLogger(new MockLogger("tickTests")) .withPluginConfig(gson.toJson(Map.of("type", "tickTests"))) .withMachineFactory(AotMachine::new) diff --git a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPlugin.java b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPlugin.java index db0d390..26ebb48 100644 --- a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPlugin.java +++ b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPlugin.java @@ -8,7 +8,7 @@ /** * This annotation is used to mark a JAX-RS resource class or method - * as being filters by one or more Wasm-Plugins. + * as being filters by one or more Proxy-Wasm plugins. */ @NameBinding @Retention(RetentionPolicy.RUNTIME) diff --git a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPluginFeature.java b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPluginFeature.java index 8a0d44c..7ac60ea 100644 --- a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPluginFeature.java +++ b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPluginFeature.java @@ -7,7 +7,7 @@ import java.util.Arrays; /** - * WasmPluginFeature is a JAX-RS feature that allows the use of Wasm-Plugins to filter JAX-RS + * WasmPluginFeature is a JAX-RS feature that allows the use of Proxy-Wasm plugins to filter JAX-RS * requests. */ public class WasmPluginFeature extends AbstractWasmPluginFeature { diff --git a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPluginFilter.java b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPluginFilter.java index 240ae7b..f97b57c 100644 --- a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPluginFilter.java +++ b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPluginFilter.java @@ -23,7 +23,7 @@ /** * This class implements the JAX-RS filters to intercept requests and responses by - * one or more Wasm-Plugins. + * one or more Proxy-Wasm plugins. */ public class WasmPluginFilter implements ContainerRequestFilter, WriterInterceptor, ContainerResponseFilter { diff --git a/quarkus-proxy-wasm-example/src/main/java/io/roastedroot/proxywasm/jaxrs/example/App.java b/quarkus-proxy-wasm-example/src/main/java/io/roastedroot/proxywasm/jaxrs/example/App.java index 37feec0..d612ad3 100644 --- a/quarkus-proxy-wasm-example/src/main/java/io/roastedroot/proxywasm/jaxrs/example/App.java +++ b/quarkus-proxy-wasm-example/src/main/java/io/roastedroot/proxywasm/jaxrs/example/App.java @@ -23,6 +23,7 @@ public PluginFactory example() throws StartException { return () -> Plugin.builder(module) .withName("example") + .withShared(true) .withPluginConfig("{ \"type\": \"headerTests\" }") .withMachineFactory(AotMachine::new) .build(); diff --git a/quarkus-proxy-wasm/integration-tests/src/main/java/io/quarkiverse/proxywasm/it/App.java b/quarkus-proxy-wasm/integration-tests/src/main/java/io/quarkiverse/proxywasm/it/App.java index 8ae3262..e93f724 100644 --- a/quarkus-proxy-wasm/integration-tests/src/main/java/io/quarkiverse/proxywasm/it/App.java +++ b/quarkus-proxy-wasm/integration-tests/src/main/java/io/quarkiverse/proxywasm/it/App.java @@ -28,6 +28,7 @@ public PluginFactory headerTests() throws StartException { return () -> Plugin.builder(parseTestModule("/go-examples/unit_tester/main.wasm")) .withName("headerTests") + .withShared(true) .withLogger(new MockLogger("headerTests")) .withPluginConfig(gson.toJson(Map.of("type", "headerTests"))) .build(); @@ -38,7 +39,6 @@ public PluginFactory headerTestsNotShared() throws StartException { return () -> Plugin.builder(parseTestModule("/go-examples/unit_tester/main.wasm")) .withName("headerTestsNotShared") - .withShared(false) .withLogger(new MockLogger("headerTestsNotShared")) .withPluginConfig(gson.toJson(Map.of("type", "headerTests"))) .withMachineFactory(AotMachine::new) @@ -50,6 +50,7 @@ public PluginFactory tickTests() throws StartException { return () -> Plugin.builder(parseTestModule("/go-examples/unit_tester/main.wasm")) .withName("tickTests") + .withShared(true) .withLogger(new MockLogger("tickTests")) .withPluginConfig(gson.toJson(Map.of("type", "tickTests"))) .withMachineFactory(AotMachine::new) diff --git a/quarkus-x-corazawaf-example/src/main/java/io/roastedroot/proxywasm/corazawaf/example/App.java b/quarkus-x-corazawaf-example/src/main/java/io/roastedroot/proxywasm/corazawaf/example/App.java index baff31e..a6d5dec 100644 --- a/quarkus-x-corazawaf-example/src/main/java/io/roastedroot/proxywasm/corazawaf/example/App.java +++ b/quarkus-x-corazawaf-example/src/main/java/io/roastedroot/proxywasm/corazawaf/example/App.java @@ -37,6 +37,7 @@ public PluginFactory waf() throws StartException { return () -> Plugin.builder(module) .withName("waf") + .withShared(true) .withLogger(DEBUG ? LogHandler.SYSTEM : null) .withPluginConfig(CONFIG) .withMetricsHandler(new SimpleMetricsHandler())