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
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<modules>
<module>proxy-wasm-java-host</module>
<module>proxy-wasm-jaxrs</module>
<module>proxy-wasm-jaxrs-jersey</module>
</modules>

<properties>
Expand Down Expand Up @@ -53,6 +54,8 @@
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
<quarkus.platform.version>3.19.3</quarkus.platform.version>
<quarkus.version>3.21.0</quarkus.version>
<jersey.version>3.1.10</jersey.version>
<jetty.version>11.0.25</jetty.version>

</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public final class Plugin {

private final ReentrantLock lock = new ReentrantLock();
final ProxyWasm wasm;
ServerAdaptor httpServer;
ServerAdaptor serverAdaptor;
private final boolean shared;
private final String name;

Expand Down Expand Up @@ -76,8 +76,12 @@ public boolean isShared() {
return shared;
}

public void setHttpServer(ServerAdaptor httpServer) {
this.httpServer = httpServer;
public ServerAdaptor getServerAdaptor() {
return serverAdaptor;
}

public void setServerAdaptor(ServerAdaptor serverAdaptor) {
this.serverAdaptor = serverAdaptor;
}

public Logger logger() {
Expand Down Expand Up @@ -318,7 +322,7 @@ public WasmResult setTickPeriodMilliseconds(int tickMs) {

// schedule the new tick
cancelTick =
httpServer.scheduleTick(
serverAdaptor.scheduleTick(
Math.max(minTickPeriodMilliseconds, tickPeriodMilliseconds),
() -> {
lock();
Expand Down Expand Up @@ -423,7 +427,7 @@ public int httpCall(
try {
var id = lastCallId.incrementAndGet();
var future =
httpServer.scheduleHttpCall(
serverAdaptor.scheduleHttpCall(
method,
connectHost,
connectPort,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ default void close() {}
class SharedPlugin implements Pool {
private final Plugin plugin;

public SharedPlugin(Plugin plugin) throws StartException {
public SharedPlugin(ServerAdaptor serverAdaptor, Plugin plugin) throws StartException {
this.plugin = plugin;
this.plugin.setServerAdaptor(serverAdaptor);
}

public void close() {
Expand Down Expand Up @@ -50,10 +51,12 @@ public Plugin borrow() throws StartException {

class PluginPerRequest implements Pool {

private final ServerAdaptor serverAdaptor;
final PluginFactory factory;
private final String name;

public PluginPerRequest(PluginFactory factory, Plugin plugin) {
public PluginPerRequest(ServerAdaptor serverAdaptor, PluginFactory factory, Plugin plugin) {
this.serverAdaptor = serverAdaptor;
this.factory = factory;
this.name = plugin.name();
release(plugin);
Expand All @@ -67,6 +70,7 @@ public String name() {
@Override
public Plugin borrow() throws StartException {
Plugin plugin = factory.create();
plugin.setServerAdaptor(serverAdaptor);
plugin.wasm.start();
return plugin;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public interface ServerAdaptor {

Runnable scheduleTick(long delay, Runnable task);

HttpRequestAdaptor httpRequestAdaptor(Object context);

Runnable scheduleHttpCall(
String method,
String host,
Expand Down
109 changes: 109 additions & 0 deletions proxy-wasm-jaxrs-jersey/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.roastedroot</groupId>
<artifactId>proxy-wasm-java-host-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>proxy-wasm-jaxrs-jersey</artifactId>
<packaging>jar</packaging>
<name>proxy-wasm-jaxrs-jersey</name>

<dependencies>
<dependency>
<groupId>io.roastedroot</groupId>
<artifactId>proxy-wasm-jaxrs</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>

<!-- Jersey -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>${jersey.version}</version>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.12.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>

<!-- Tests -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>2.0.12</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<resources>
<resource>
<directory>src/test/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<configuration>
<annotationProcessorPathsUseDepMgmt>true</annotationProcessorPathsUseDepMgmt>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package io.roastedroot.proxywasm.jaxrs.example;

import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.wasm.WasmModule;
import com.google.gson.Gson;
import io.roastedroot.proxywasm.StartException;
import io.roastedroot.proxywasm.plugin.Plugin;
import io.roastedroot.proxywasm.plugin.PluginFactory;
import java.nio.file.Path;
import java.util.Map;

public class App {

public static final String EXAMPLES_DIR = "../proxy-wasm-java-host/src/test";
private static final Gson gson = new Gson();

public static WasmModule parseTestModule(String file) {
return Parser.parse(Path.of(EXAMPLES_DIR + file));
}

public static PluginFactory headerTests() throws StartException {
return () ->
Plugin.builder()
.withName("headerTests")
.withLogger(new MockLogger("headerTests"))
.withPluginConfig(gson.toJson(Map.of("type", "headerTests")))
.build(parseTestModule("/go-examples/unit_tester/main.wasm"));
}

public static PluginFactory headerTestsNotShared() throws StartException {
return () ->
Plugin.builder()
.withName("headerTestsNotShared")
.withShared(false)
.withLogger(new MockLogger("headerTestsNotShared"))
.withPluginConfig(gson.toJson(Map.of("type", "headerTests")))
.build(parseTestModule("/go-examples/unit_tester/main.wasm"));
}

public static PluginFactory tickTests() throws StartException {
return () ->
Plugin.builder()
.withName("tickTests")
.withLogger(new MockLogger("tickTests"))
.withPluginConfig(gson.toJson(Map.of("type", "tickTests")))
.build(parseTestModule("/go-examples/unit_tester/main.wasm"));
}

public static PluginFactory ffiTests() throws StartException {
return () ->
Plugin.builder()
.withName("ffiTests")
.withLogger(new MockLogger("ffiTests"))
.withPluginConfig(gson.toJson(Map.of("type", "ffiTests")))
.withForeignFunctions(Map.of("reverse", App::reverse))
.build(parseTestModule("/go-examples/unit_tester/main.wasm"));
}

public static byte[] reverse(byte[] data) {
byte[] reversed = new byte[data.length];
for (int i = 0; i < data.length; i++) {
reversed[i] = data[data.length - 1 - i];
}
return reversed;
}

public static PluginFactory httpCallTests() throws StartException {
return () ->
Plugin.builder()
.withName("httpCallTests")
.withLogger(new MockLogger("httpCallTests"))
.withPluginConfig(
gson.toJson(
Map.of(
"type", "httpCallTests",
"upstream", "web_service",
"path", "/ok")))
.withUpstreams(Map.of("web_service", "localhost:8081"))
.build(parseTestModule("/go-examples/unit_tester/main.wasm"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.roastedroot.proxywasm.jaxrs.example;

import java.util.ArrayList;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.junit.jupiter.api.Assertions;

public class Helpers {
private Helpers() {}

public static void assertLogsContain(ArrayList<String> loggedMessages, String... message) {
for (String m : message) {
Assertions.assertTrue(
loggedMessages.contains(m), "logged messages does not contain: " + m);
}
}

public static <T> TypeSafeMatcher<T> isTrue(IsTrueMatcher.Predicate<T> predicate) {
return new IsTrueMatcher<T>(predicate);
}

public static class IsTrueMatcher<T> extends TypeSafeMatcher<T> {

public interface Predicate<T> {
boolean matchesSafely(T value);
}

Predicate<T> predicate;

public IsTrueMatcher(Predicate<T> predicate) {
this.predicate = predicate;
}

@Override
protected boolean matchesSafely(T item) {
return predicate.matchesSafely(item);
}

@Override
public void describeTo(Description description) {
description.appendText("is not true");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.roastedroot.proxywasm.jaxrs.example;

import io.roastedroot.proxywasm.LogLevel;
import io.roastedroot.proxywasm.plugin.Logger;
import java.util.ArrayList;

public class MockLogger implements Logger {

static final boolean DEBUG = "true".equals(System.getenv("DEBUG"));

final ArrayList<String> loggedMessages = new ArrayList<>();
private final String name;

public MockLogger(String name) {
this.name = name;
}

@Override
public synchronized void log(LogLevel level, String message) {
if (DEBUG) {
System.out.printf("%s: [%s] %s\n", level, name, message);
}
loggedMessages.add(message);
}

@Override
public synchronized LogLevel getLogLevel() {
return LogLevel.TRACE;
}

public synchronized ArrayList<String> loggedMessages() {
return new ArrayList<>(loggedMessages);
}
}
Loading
Loading