Skip to content

Commit 0a3f0c4

Browse files
committed
Refactoring to clean up things.
Move non-jaxrs specific bits back to proxy-wasm-java-host. Move the jaxrs unit tests back to proxy-wasm-jaxrs-quarkus Make the proxy-wasm-jaxrs-quarkus-example simpler so that we can point user’s to it as a starting point. Signed-off-by: Hiram Chirino <hiram@hiramchirino.com>
1 parent b6adc85 commit 0a3f0c4

34 files changed

Lines changed: 808 additions & 774 deletions

File tree

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
package io.roastedroot.proxywasm.plugin;
2+
3+
import io.roastedroot.proxywasm.Action;
4+
import io.roastedroot.proxywasm.ChainedHandler;
5+
import io.roastedroot.proxywasm.Handler;
6+
import io.roastedroot.proxywasm.Helpers;
7+
import io.roastedroot.proxywasm.ProxyMap;
8+
import io.roastedroot.proxywasm.StreamType;
9+
import io.roastedroot.proxywasm.WasmException;
10+
import io.roastedroot.proxywasm.WasmResult;
11+
import java.util.HashMap;
12+
import java.util.List;
13+
import java.util.concurrent.CountDownLatch;
14+
15+
public class HttpContext {
16+
17+
private final Plugin plugin;
18+
private final io.roastedroot.proxywasm.HttpContext context;
19+
private final HttpRequestAdaptor requestAdaptor;
20+
private final long startedAt = System.currentTimeMillis();
21+
22+
final HashMap<List<String>, byte[]> properties = new HashMap<>();
23+
private byte[] httpRequestBody = new byte[0];
24+
private byte[] grpcReceiveBuffer = new byte[0];
25+
private byte[] upstreamData = new byte[0];
26+
private byte[] downStreamData = new byte[0];
27+
private byte[] httpResponseBody = new byte[0];
28+
private SendResponse sendResponse;
29+
private Action action;
30+
private CountDownLatch resumeLatch;
31+
32+
HttpContext(Plugin plugin, HttpRequestAdaptor requestAdaptor) {
33+
this.plugin = plugin;
34+
this.requestAdaptor = requestAdaptor;
35+
this.context = plugin.wasm.createHttpContext(new HandlerImpl());
36+
}
37+
38+
public Plugin plugin() {
39+
return plugin;
40+
}
41+
42+
public io.roastedroot.proxywasm.HttpContext context() {
43+
return context;
44+
}
45+
46+
public HttpRequestAdaptor requestAdaptor() {
47+
return requestAdaptor;
48+
}
49+
50+
public Action getAction() {
51+
return action;
52+
}
53+
54+
public void maybePause() {
55+
// don't pause if plugin wants us to continue
56+
if (action == Action.CONTINUE) {
57+
return;
58+
}
59+
// don't pause if the plugin wants to respond to the request
60+
if (sendResponse != null) {
61+
return;
62+
}
63+
64+
// ok, lets pause the request processing. A tick or http call response event will
65+
// need to resume the processing
66+
resumeLatch = new CountDownLatch(1);
67+
plugin.unlock();
68+
try {
69+
resumeLatch.await();
70+
} catch (InterruptedException ignore) {
71+
return;
72+
} finally {
73+
plugin.lock();
74+
resumeLatch = null;
75+
}
76+
}
77+
78+
public byte[] getHttpRequestBody() {
79+
return httpRequestBody;
80+
}
81+
82+
public byte[] getHttpResponseBody() {
83+
return httpResponseBody;
84+
}
85+
86+
public void setHttpRequestBody(byte[] httpRequestBody) {
87+
this.httpRequestBody = httpRequestBody;
88+
}
89+
90+
public byte[] getGrpcReceiveBuffer() {
91+
return grpcReceiveBuffer;
92+
}
93+
94+
public void setGrpcReceiveBuffer(byte[] grpcReceiveBuffer) {
95+
this.grpcReceiveBuffer = grpcReceiveBuffer;
96+
}
97+
98+
public byte[] getUpstreamData() {
99+
return upstreamData;
100+
}
101+
102+
public void setUpstreamData(byte[] upstreamData) {
103+
this.upstreamData = upstreamData;
104+
}
105+
106+
public byte[] getDownStreamData() {
107+
return downStreamData;
108+
}
109+
110+
public void setDownStreamData(byte[] downStreamData) {
111+
this.downStreamData = downStreamData;
112+
}
113+
114+
public void setHttpResponseBody(byte[] httpResponseBody) {
115+
this.httpResponseBody = httpResponseBody;
116+
}
117+
118+
public SendResponse getSendResponse() {
119+
return sendResponse;
120+
}
121+
122+
public SendResponse consumeSentHttpResponse() {
123+
var result = sendResponse;
124+
sendResponse = null;
125+
return result;
126+
}
127+
128+
class HandlerImpl extends ChainedHandler {
129+
130+
@Override
131+
protected Handler next() {
132+
return plugin.handler;
133+
}
134+
135+
public ProxyMap getHttpRequestHeaders() {
136+
return requestAdaptor.getHttpRequestHeaders();
137+
}
138+
139+
public ProxyMap getHttpRequestTrailers() {
140+
return requestAdaptor.getHttpRequestTrailers();
141+
}
142+
143+
public ProxyMap getHttpResponseHeaders() {
144+
return requestAdaptor.getHttpResponseHeaders();
145+
}
146+
147+
public ProxyMap getHttpResponseTrailers() {
148+
return requestAdaptor.getHttpResponseTrailers();
149+
}
150+
151+
public ProxyMap getGrpcReceiveInitialMetaData() {
152+
return requestAdaptor.getGrpcReceiveInitialMetaData();
153+
}
154+
155+
public ProxyMap getGrpcReceiveTrailerMetaData() {
156+
return requestAdaptor.getGrpcReceiveTrailerMetaData();
157+
}
158+
159+
// //////////////////////////////////////////////////////////////////////
160+
// Buffers
161+
// //////////////////////////////////////////////////////////////////////
162+
163+
@Override
164+
public byte[] getHttpRequestBody() {
165+
return httpRequestBody;
166+
}
167+
168+
@Override
169+
public WasmResult setHttpRequestBody(byte[] body) {
170+
httpRequestBody = body;
171+
return WasmResult.OK;
172+
}
173+
174+
public void appendHttpRequestBody(byte[] body) {
175+
httpRequestBody = Helpers.append(httpRequestBody, body);
176+
}
177+
178+
@Override
179+
public byte[] getGrpcReceiveBuffer() {
180+
return grpcReceiveBuffer;
181+
}
182+
183+
@Override
184+
public WasmResult setGrpcReceiveBuffer(byte[] buffer) {
185+
grpcReceiveBuffer = buffer;
186+
return WasmResult.OK;
187+
}
188+
189+
@Override
190+
public byte[] getUpstreamData() {
191+
return upstreamData;
192+
}
193+
194+
@Override
195+
public WasmResult setUpstreamData(byte[] data) {
196+
upstreamData = data;
197+
return WasmResult.OK;
198+
}
199+
200+
@Override
201+
public byte[] getDownStreamData() {
202+
return downStreamData;
203+
}
204+
205+
@Override
206+
public WasmResult setDownStreamData(byte[] data) {
207+
downStreamData = data;
208+
return WasmResult.OK;
209+
}
210+
211+
@Override
212+
public byte[] getHttpResponseBody() {
213+
return httpResponseBody;
214+
}
215+
216+
@Override
217+
public WasmResult setHttpResponseBody(byte[] body) {
218+
httpResponseBody = body;
219+
return WasmResult.OK;
220+
}
221+
222+
public void appendHttpResponseBody(byte[] body) {
223+
httpResponseBody = Helpers.append(httpResponseBody, body);
224+
}
225+
226+
// //////////////////////////////////////////////////////////////////////
227+
// HTTP streams
228+
// //////////////////////////////////////////////////////////////////////
229+
230+
@Override
231+
public WasmResult sendHttpResponse(
232+
int responseCode,
233+
byte[] responseCodeDetails,
234+
byte[] responseBody,
235+
ProxyMap additionalHeaders,
236+
int grpcStatus) {
237+
sendResponse =
238+
new SendResponse(
239+
responseCode,
240+
responseCodeDetails,
241+
responseBody,
242+
additionalHeaders,
243+
grpcStatus);
244+
245+
if (resumeLatch != null) {
246+
resumeLatch.countDown();
247+
}
248+
return WasmResult.OK;
249+
}
250+
251+
@Override
252+
public WasmResult setAction(StreamType streamType, Action actionValue) {
253+
action = actionValue;
254+
if (action == Action.CONTINUE && resumeLatch != null) {
255+
resumeLatch.countDown();
256+
}
257+
return WasmResult.OK;
258+
}
259+
260+
// //////////////////////////////////////////////////////////////////////
261+
// Properties
262+
// //////////////////////////////////////////////////////////////////////
263+
@Override
264+
public byte[] getProperty(List<String> path) throws WasmException {
265+
byte[] result = requestAdaptor.getProperty(HttpContext.this, path);
266+
if (result == null) {
267+
result = properties.get(path);
268+
}
269+
return result;
270+
}
271+
272+
@Override
273+
public WasmResult setProperty(List<String> path, byte[] value) {
274+
var result = requestAdaptor.setProperty(HttpContext.this, path, value);
275+
if (result == WasmResult.NOT_FOUND) {
276+
properties.put(path, value);
277+
}
278+
return WasmResult.OK;
279+
}
280+
}
281+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.roastedroot.proxywasm.plugin;
2+
3+
import io.roastedroot.proxywasm.ProxyMap;
4+
import io.roastedroot.proxywasm.WasmException;
5+
import io.roastedroot.proxywasm.WasmResult;
6+
import java.util.List;
7+
8+
/**
9+
* This interface will help us deal with differences in the http server impl.
10+
*/
11+
public interface HttpRequestAdaptor {
12+
13+
String remoteAddress();
14+
15+
String remotePort();
16+
17+
String localAddress();
18+
19+
String localPort();
20+
21+
ProxyMap getHttpRequestHeaders();
22+
23+
ProxyMap getHttpRequestTrailers();
24+
25+
ProxyMap getHttpResponseHeaders();
26+
27+
ProxyMap getHttpResponseTrailers();
28+
29+
ProxyMap getGrpcReceiveInitialMetaData();
30+
31+
ProxyMap getGrpcReceiveTrailerMetaData();
32+
33+
byte[] getProperty(HttpContext pluginRequest, List<String> path) throws WasmException;
34+
35+
WasmResult setProperty(HttpContext pluginRequest, List<String> path, byte[] value);
36+
}

proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/Logger.java renamed to proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/plugin/Logger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.roastedroot.proxywasm.jaxrs;
1+
package io.roastedroot.proxywasm.plugin;
22

33
import io.roastedroot.proxywasm.LogLevel;
44

0 commit comments

Comments
 (0)