Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 97 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,97 @@
# Proxy WASM Java SDK
# Proxy WASM Java Host

The Java implementation for proxy-wasm, enabling developer to run proxy-wasm extensions in Java.

## Building

To build the project, you need to have Maven installed. You can build the project using the following command:

```bash
mvn clean install
```

## Quarkus Example

The `quarkus-proxy-wasm-example` directory contains a simple example of how to use the proxy-wasm Java host with Quarkus.

It creates a simple JAX-RS application that uses ta proxy-wasm plugin to filter the http request before it reaches the application resource:

```java
package io.roastedroot.proxywasm.jaxrs.example;

import io.roastedroot.proxywasm.jaxrs.WasmPlugin;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

@Path("/")
public class Resources {

@Path("/test")
@GET
@WasmPlugin("example") // filter with example wasm plugin
public String ffiTests() {
return "Hello World";
}
}
```

The `WasmPlugin` annotation is used to specify the name of the plugin to be used for filtering.

```java
package io.roastedroot.proxywasm.jaxrs.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 jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import java.nio.file.Path;

@ApplicationScoped
public class App {

private static WasmModule module =
Parser.parse(
Path.of("../proxy-wasm-java-host/src/test/go-examples/unit_tester/main.wasm"));

// configure the the example wasm plugin
@Produces
public PluginFactory example() throws StartException {
return () ->
Plugin.builder()
.withName("example")
.withPluginConfig("{ \"type\": \"headerTests\" }")
.build(module);
}
}
```

The method `example()` is give CDI a `PluginFactory` that can create instances of the `example` plugin. An instance of that plugin will be used to filter the request on resources that have the `@WasmPlugin("example")` annotation.

### Running the Example

Once the project is [built](#building), you can run the example using the following command:

```bash
cd quarkus-proxy-wasm-example
java -jar target/quarkus-app/quarkus-run.jar
```

Then test it using curl command:

```bash
curl -i http://localhost:8080/test
```

The example uses a simple wasm plugin which just adds an `x-response-counter` header to the response. The value of the header is incremented for each request. So you should see output like this:

```
HTTP/1.1 200 OK
Content-Type: text/plain;charset=UTF-8
x-response-counter: 1
content-length: 11

Hello World
```
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<compiler-plugin.version>${maven.compiler.version}</compiler-plugin.version>
<surefire-plugin.version>3.5.2</surefire-plugin.version>
<failsafe-plugin.version>${surefire-plugin.version}</failsafe-plugin.version>
<jandex.version>3.2.7</jandex.version>

<!-- test time versions -->
<junit.version>5.12.0</junit.version>
Expand Down Expand Up @@ -232,7 +233,7 @@
</activation>
<modules>
<module>quarkus-proxy-wasm</module>
<module>proxy-wasm-quarkus-example</module>
<module>quarkus-proxy-wasm-example</module>
</modules>
</profile>
</profiles>
Expand Down
38 changes: 3 additions & 35 deletions proxy-wasm-jaxrs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,50 +26,16 @@
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>aot-experimental</artifactId>
<version>${chicory.version}</version>
</dependency>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>host-module-annotations-experimental</artifactId>
<version>${chicory.version}</version>
</dependency>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>runtime</artifactId>
<version>${chicory.version}</version>
</dependency>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>wasi</artifactId>
<version>${chicory.version}</version>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.roastedroot</groupId>
<artifactId>proxy-wasm-java-host</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<optional>true</optional>
</dependency>

<!-- Depend on a minimal set of jakarta/jaxrs apis -->
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
Expand All @@ -80,6 +46,8 @@
<artifactId>jakarta.inject-api</artifactId>
<scope>provided</scope>
</dependency>

<!-- Depend on a minimal set of jakarta/jaxrs apis -->
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
Expand All @@ -93,7 +61,7 @@
<plugin>
<groupId>io.smallrye</groupId>
<artifactId>jandex-maven-plugin</artifactId>
<version>3.2.7</version>
<version>${jandex.version}</version>
<executions>
<execution>
<id>make-index</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
import io.roastedroot.proxywasm.plugin.HttpCallResponse;
import io.roastedroot.proxywasm.plugin.HttpCallResponseHandler;
import io.roastedroot.proxywasm.plugin.HttpRequestAdaptor;
import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Alternative;
import jakarta.ws.rs.core.UriBuilder;
import java.net.URI;
import java.net.http.HttpClient;
Expand All @@ -19,9 +16,6 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Alternative
@Priority(100)
@ApplicationScoped
public class ServerAdaptor implements io.roastedroot.proxywasm.plugin.ServerAdaptor {

ScheduledExecutorService tickExecutorService = Executors.newScheduledThreadPool(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.roastedroot.proxywasm.jaxrs.cdi;

import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Alternative;

@Alternative
@Priority(100)
@ApplicationScoped
public class ServerAdaptor extends io.roastedroot.proxywasm.jaxrs.ServerAdaptor {}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public class WasmPluginFeature extends io.roastedroot.proxywasm.jaxrs.AbstractWa

@Inject Instance<PluginFactory> factories;

@Inject @Any ServerAdaptor httpServer;
@Inject @Any ServerAdaptor serverAdaptor;

@Inject
@PostConstruct
public void init() throws StartException {
init(factories, httpServer);
init(factories, serverAdaptor);
}

@PreDestroy
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>proxy-wasm-quarkus-example</artifactId>
<artifactId>quarkus-proxy-wasm-example</artifactId>
<packaging>jar</packaging>
<name>proxy-wasm-quarkus-example</name>
<name>quarkus-proxy-wasm-example</name>

<dependencyManagement>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.roastedroot.proxywasm.jaxrs.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 jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import java.nio.file.Path;

@ApplicationScoped
public class App {

private static WasmModule module =
Parser.parse(
Path.of("../proxy-wasm-java-host/src/test/go-examples/unit_tester/main.wasm"));

// configure the the example wasm plugin
@Produces
public PluginFactory example() throws StartException {
return () ->
Plugin.builder()
.withName("example")
.withPluginConfig("{ \"type\": \"headerTests\" }")
.build(module);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.roastedroot.proxywasm.jaxrs.example;

import io.roastedroot.proxywasm.jaxrs.WasmPlugin;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

@Path("/")
public class Resources {

@Path("/test")
@GET
@WasmPlugin("example") // filter with example wasm plugin
public String ffiTests() {
return "Hello World";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ public class FFITest {
@Test
public void reverse() throws InterruptedException {

given().body("My Test")
.when()
.post("/ffiTests/reverse")
given().when()
.get("/test")
.then()
.statusCode(200)
.body(equalTo("tseT yM"));
.header("x-response-counter", "1")
.body(equalTo("Hello World"));

given().when()
.get("/test")
.then()
.statusCode(200)
.header("x-response-counter", "2")
.body(equalTo("Hello World"));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkiverse.proxywasm.deployment;

import io.quarkiverse.proxywasm.runtime.VertxHttpRequestAdaptor;
import io.quarkiverse.proxywasm.runtime.VertxServerAdaptor;
import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.FeatureBuildItem;
Expand All @@ -20,7 +22,8 @@ FeatureBuildItem feature() {

@BuildStep
AdditionalBeanBuildItem resources() {
return new AdditionalBeanBuildItem(WasmPluginFeature.class);
return new AdditionalBeanBuildItem(
WasmPluginFeature.class, VertxServerAdaptor.class, VertxHttpRequestAdaptor.class);
}

@BuildStep
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.roastedroot.proxywasm.jaxrs.vertx;
package io.quarkiverse.proxywasm.runtime;

import io.roastedroot.proxywasm.jaxrs.JaxrsHttpRequestAdaptor;
import jakarta.annotation.Priority;
Expand Down
Loading
Loading