Skip to content

Commit 1f7fdea

Browse files
committed
feat: The jaxrs filter is now working on Jersey/Jetty.
Signed-off-by: Hiram Chirino <hiram@hiramchirino.com>
1 parent 8c4265c commit 1f7fdea

File tree

24 files changed

+882
-135
lines changed

24 files changed

+882
-135
lines changed

pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<modules>
2323
<module>proxy-wasm-java-host</module>
2424
<module>proxy-wasm-jaxrs</module>
25+
<module>proxy-wasm-jaxrs-jersey</module>
2526
</modules>
2627

2728
<properties>
@@ -53,6 +54,8 @@
5354
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
5455
<quarkus.platform.version>3.19.3</quarkus.platform.version>
5556
<quarkus.version>3.21.0</quarkus.version>
57+
<jersey.version>3.1.10</jersey.version>
58+
<jetty.version>11.0.25</jetty.version>
5659

5760
</properties>
5861

proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/Plugin.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public final class Plugin {
3535

3636
private final ReentrantLock lock = new ReentrantLock();
3737
final ProxyWasm wasm;
38-
ServerAdaptor httpServer;
38+
ServerAdaptor serverAdaptor;
3939
private final boolean shared;
4040
private final String name;
4141

@@ -76,8 +76,12 @@ public boolean isShared() {
7676
return shared;
7777
}
7878

79-
public void setHttpServer(ServerAdaptor httpServer) {
80-
this.httpServer = httpServer;
79+
public ServerAdaptor getServerAdaptor() {
80+
return serverAdaptor;
81+
}
82+
83+
public void setServerAdaptor(ServerAdaptor serverAdaptor) {
84+
this.serverAdaptor = serverAdaptor;
8185
}
8286

8387
public Logger logger() {
@@ -318,7 +322,7 @@ public WasmResult setTickPeriodMilliseconds(int tickMs) {
318322

319323
// schedule the new tick
320324
cancelTick =
321-
httpServer.scheduleTick(
325+
serverAdaptor.scheduleTick(
322326
Math.max(minTickPeriodMilliseconds, tickPeriodMilliseconds),
323327
() -> {
324328
lock();
@@ -423,7 +427,7 @@ public int httpCall(
423427
try {
424428
var id = lastCallId.incrementAndGet();
425429
var future =
426-
httpServer.scheduleHttpCall(
430+
serverAdaptor.scheduleHttpCall(
427431
method,
428432
connectHost,
429433
connectPort,

proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/Pool.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ default void close() {}
1717
class SharedPlugin implements Pool {
1818
private final Plugin plugin;
1919

20-
public SharedPlugin(Plugin plugin) throws StartException {
20+
public SharedPlugin(ServerAdaptor serverAdaptor, Plugin plugin) throws StartException {
2121
this.plugin = plugin;
22+
this.plugin.setServerAdaptor(serverAdaptor);
2223
}
2324

2425
public void close() {
@@ -50,10 +51,12 @@ public Plugin borrow() throws StartException {
5051

5152
class PluginPerRequest implements Pool {
5253

54+
private final ServerAdaptor serverAdaptor;
5355
final PluginFactory factory;
5456
private final String name;
5557

56-
public PluginPerRequest(PluginFactory factory, Plugin plugin) {
58+
public PluginPerRequest(ServerAdaptor serverAdaptor, PluginFactory factory, Plugin plugin) {
59+
this.serverAdaptor = serverAdaptor;
5760
this.factory = factory;
5861
this.name = plugin.name();
5962
release(plugin);
@@ -67,6 +70,7 @@ public String name() {
6770
@Override
6871
public Plugin borrow() throws StartException {
6972
Plugin plugin = factory.create();
73+
plugin.setServerAdaptor(serverAdaptor);
7074
plugin.wasm.start();
7175
return plugin;
7276
}

proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/ServerAdaptor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public interface ServerAdaptor {
1010

1111
Runnable scheduleTick(long delay, Runnable task);
1212

13+
HttpRequestAdaptor httpRequestAdaptor(Object context);
14+
1315
Runnable scheduleHttpCall(
1416
String method,
1517
String host,

proxy-wasm-jaxrs-jersey/pom.xml

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>io.roastedroot</groupId>
7+
<artifactId>proxy-wasm-java-host-parent</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
12+
<artifactId>proxy-wasm-jaxrs-jersey</artifactId>
13+
<packaging>jar</packaging>
14+
<name>proxy-wasm-jaxrs-jersey</name>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>io.roastedroot</groupId>
19+
<artifactId>proxy-wasm-jaxrs</artifactId>
20+
<version>${project.version}</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.eclipse.jetty</groupId>
24+
<artifactId>jetty-server</artifactId>
25+
<version>${jetty.version}</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.eclipse.jetty</groupId>
29+
<artifactId>jetty-servlet</artifactId>
30+
<version>${jetty.version}</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.glassfish.jersey.containers</groupId>
34+
<artifactId>jersey-container-jetty-http</artifactId>
35+
<version>${jersey.version}</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.glassfish.jersey.containers</groupId>
39+
<artifactId>jersey-container-servlet</artifactId>
40+
<version>${jersey.version}</version>
41+
</dependency>
42+
43+
<!-- Jersey -->
44+
<dependency>
45+
<groupId>org.glassfish.jersey.core</groupId>
46+
<artifactId>jersey-server</artifactId>
47+
<version>${jersey.version}</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.glassfish.jersey.inject</groupId>
51+
<artifactId>jersey-hk2</artifactId>
52+
<version>${jersey.version}</version>
53+
</dependency>
54+
55+
<dependency>
56+
<groupId>com.google.code.gson</groupId>
57+
<artifactId>gson</artifactId>
58+
<version>2.12.1</version>
59+
<scope>test</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>io.rest-assured</groupId>
63+
<artifactId>rest-assured</artifactId>
64+
<version>5.3.1</version>
65+
<scope>test</scope>
66+
</dependency>
67+
68+
<!-- Tests -->
69+
<dependency>
70+
<groupId>org.junit.jupiter</groupId>
71+
<artifactId>junit-jupiter</artifactId>
72+
<version>${junit.version}</version>
73+
<scope>test</scope>
74+
</dependency>
75+
76+
<!-- Logging -->
77+
<dependency>
78+
<groupId>org.slf4j</groupId>
79+
<artifactId>slf4j-nop</artifactId>
80+
<version>2.0.12</version>
81+
<scope>test</scope>
82+
</dependency>
83+
</dependencies>
84+
85+
<build>
86+
<resources>
87+
<resource>
88+
<directory>src/test/resources</directory>
89+
</resource>
90+
</resources>
91+
<plugins>
92+
<plugin>
93+
<artifactId>maven-compiler-plugin</artifactId>
94+
<executions>
95+
<execution>
96+
<id>default-compile</id>
97+
<configuration>
98+
<annotationProcessorPathsUseDepMgmt>true</annotationProcessorPathsUseDepMgmt>
99+
</configuration>
100+
</execution>
101+
</executions>
102+
</plugin>
103+
<plugin>
104+
<artifactId>maven-surefire-plugin</artifactId>
105+
<version>${surefire-plugin.version}</version>
106+
</plugin>
107+
</plugins>
108+
</build>
109+
</project>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package io.roastedroot.proxywasm.jaxrs.example;
2+
3+
import com.dylibso.chicory.wasm.Parser;
4+
import com.dylibso.chicory.wasm.WasmModule;
5+
import com.google.gson.Gson;
6+
import io.roastedroot.proxywasm.StartException;
7+
import io.roastedroot.proxywasm.plugin.Plugin;
8+
import io.roastedroot.proxywasm.plugin.PluginFactory;
9+
import java.nio.file.Path;
10+
import java.util.Map;
11+
12+
public class App {
13+
14+
public static final String EXAMPLES_DIR = "../proxy-wasm-java-host/src/test";
15+
private static final Gson gson = new Gson();
16+
17+
public static WasmModule parseTestModule(String file) {
18+
return Parser.parse(Path.of(EXAMPLES_DIR + file));
19+
}
20+
21+
public static PluginFactory headerTests() throws StartException {
22+
return () ->
23+
Plugin.builder()
24+
.withName("headerTests")
25+
.withLogger(new MockLogger("headerTests"))
26+
.withPluginConfig(gson.toJson(Map.of("type", "headerTests")))
27+
.build(parseTestModule("/go-examples/unit_tester/main.wasm"));
28+
}
29+
30+
public static PluginFactory headerTestsNotShared() throws StartException {
31+
return () ->
32+
Plugin.builder()
33+
.withName("headerTestsNotShared")
34+
.withShared(false)
35+
.withLogger(new MockLogger("headerTestsNotShared"))
36+
.withPluginConfig(gson.toJson(Map.of("type", "headerTests")))
37+
.build(parseTestModule("/go-examples/unit_tester/main.wasm"));
38+
}
39+
40+
public static PluginFactory tickTests() throws StartException {
41+
return () ->
42+
Plugin.builder()
43+
.withName("tickTests")
44+
.withLogger(new MockLogger("tickTests"))
45+
.withPluginConfig(gson.toJson(Map.of("type", "tickTests")))
46+
.build(parseTestModule("/go-examples/unit_tester/main.wasm"));
47+
}
48+
49+
public static PluginFactory ffiTests() throws StartException {
50+
return () ->
51+
Plugin.builder()
52+
.withName("ffiTests")
53+
.withLogger(new MockLogger("ffiTests"))
54+
.withPluginConfig(gson.toJson(Map.of("type", "ffiTests")))
55+
.withForeignFunctions(Map.of("reverse", App::reverse))
56+
.build(parseTestModule("/go-examples/unit_tester/main.wasm"));
57+
}
58+
59+
public static byte[] reverse(byte[] data) {
60+
byte[] reversed = new byte[data.length];
61+
for (int i = 0; i < data.length; i++) {
62+
reversed[i] = data[data.length - 1 - i];
63+
}
64+
return reversed;
65+
}
66+
67+
public static PluginFactory httpCallTests() throws StartException {
68+
return () ->
69+
Plugin.builder()
70+
.withName("httpCallTests")
71+
.withLogger(new MockLogger("httpCallTests"))
72+
.withPluginConfig(
73+
gson.toJson(
74+
Map.of(
75+
"type", "httpCallTests",
76+
"upstream", "web_service",
77+
"path", "/ok")))
78+
.withUpstreams(Map.of("web_service", "localhost:8081"))
79+
.build(parseTestModule("/go-examples/unit_tester/main.wasm"));
80+
}
81+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.roastedroot.proxywasm.jaxrs.example;
2+
3+
import java.util.ArrayList;
4+
import org.hamcrest.Description;
5+
import org.hamcrest.TypeSafeMatcher;
6+
import org.junit.jupiter.api.Assertions;
7+
8+
public class Helpers {
9+
private Helpers() {}
10+
11+
public static void assertLogsContain(ArrayList<String> loggedMessages, String... message) {
12+
for (String m : message) {
13+
Assertions.assertTrue(
14+
loggedMessages.contains(m), "logged messages does not contain: " + m);
15+
}
16+
}
17+
18+
public static <T> TypeSafeMatcher<T> isTrue(IsTrueMatcher.Predicate<T> predicate) {
19+
return new IsTrueMatcher<T>(predicate);
20+
}
21+
22+
public static class IsTrueMatcher<T> extends TypeSafeMatcher<T> {
23+
24+
public interface Predicate<T> {
25+
boolean matchesSafely(T value);
26+
}
27+
28+
Predicate<T> predicate;
29+
30+
public IsTrueMatcher(Predicate<T> predicate) {
31+
this.predicate = predicate;
32+
}
33+
34+
@Override
35+
protected boolean matchesSafely(T item) {
36+
return predicate.matchesSafely(item);
37+
}
38+
39+
@Override
40+
public void describeTo(Description description) {
41+
description.appendText("is not true");
42+
}
43+
}
44+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.roastedroot.proxywasm.jaxrs.example;
2+
3+
import io.roastedroot.proxywasm.LogLevel;
4+
import io.roastedroot.proxywasm.plugin.Logger;
5+
import java.util.ArrayList;
6+
7+
public class MockLogger implements Logger {
8+
9+
static final boolean DEBUG = "true".equals(System.getenv("DEBUG"));
10+
11+
final ArrayList<String> loggedMessages = new ArrayList<>();
12+
private final String name;
13+
14+
public MockLogger(String name) {
15+
this.name = name;
16+
}
17+
18+
@Override
19+
public synchronized void log(LogLevel level, String message) {
20+
if (DEBUG) {
21+
System.out.printf("%s: [%s] %s\n", level, name, message);
22+
}
23+
loggedMessages.add(message);
24+
}
25+
26+
@Override
27+
public synchronized LogLevel getLogLevel() {
28+
return LogLevel.TRACE;
29+
}
30+
31+
public synchronized ArrayList<String> loggedMessages() {
32+
return new ArrayList<>(loggedMessages);
33+
}
34+
}

0 commit comments

Comments
 (0)