Skip to content

Commit 560d097

Browse files
committed
Consolidate documentation
1 parent ac16d4b commit 560d097

File tree

6 files changed

+130
-108
lines changed

6 files changed

+130
-108
lines changed

src/main/asciidoc/index.adoc

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,21 +181,40 @@ You can also refer to {@link io.vertx.httpproxy.ProxyInterceptorBuilder} for mor
181181

182182
==== Body interceptor
183183

184-
You can use body interceptor to create body transformations.
184+
You can use a {@link io.vertx.httpproxy.BodyTransformer} to create body transformations.
185+
186+
A set of predefined transformations facilitates the creation of a transformer.
185187

186188
[source,java]
187189
----
188-
{@link examples.HttpProxyExamples#bodyInterceptorJson}
190+
{@link examples.HttpProxyExamples#bodyTransformer}
191+
----
192+
193+
A body transformer is then turned into a proxy interceptor with the builder:
194+
195+
[source,java]
196+
----
197+
{@link examples.HttpProxyExamples#bodyInterceptorTransformer}
189198
----
190199

191-
{@link io.vertx.httpproxy.BodyTransformer} provides transformation for common data types, like {@link io.vertx.core.json.JsonObject}:
200+
{@link io.vertx.httpproxy.BodyTransformers} provides transformation for common data types, like {@link io.vertx.core.json.JsonObject}:
192201

193202
[source,java]
194203
----
195204
{@link examples.HttpProxyExamples#bodyInterceptorJson}
196205
----
197206

198-
Please check the {@link io.vertx.httpproxy.BodyTransformer} for other supported transformations.
207+
Most transformations provided in {@link io.vertx.httpproxy.BodyTransformers} are synchronous and buffer bytes. The default
208+
maximum amount of bytes is 256K bytes, you can provide a different amount:
209+
210+
[source,java]
211+
----
212+
{@link examples.HttpProxyExamples#bodyInterceptorJsonMaxBufferedSize}
213+
----
214+
215+
Please check the {@link io.vertx.httpproxy.BodyTransformers} for other supported transformations.
216+
217+
NOTE: you can also implement {@link io.vertx.httpproxy.BodyTransformer} contract to best adapt it to your needs.
199218

200219
==== Interception and WebSocket upgrades
201220

src/main/java/examples/HttpProxyExamples.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,26 +116,45 @@ public void queryInterceptorAdd(HttpProxy proxy, String key, String value) {
116116
ProxyInterceptor.builder().settingQueryParam(key, value).build());
117117
}
118118

119-
public void bodyInterceptorTransformer(HttpProxy proxy) {
119+
public void bodyTransformer() {
120+
BodyTransformer transformer = BodyTransformers.transform(
121+
MediaType.APPLICATION_JSON,
122+
MediaType.APPLICATION_OCTET_STREAM,
123+
buffer -> {
124+
// Apply some transformation
125+
return buffer;
126+
}
127+
);
128+
}
129+
130+
public void bodyInterceptorTransformer(HttpProxy proxy, BodyTransformer transformer) {
131+
proxy.addInterceptor(
132+
ProxyInterceptor
133+
.builder()
134+
.transformingResponseBody(transformer)
135+
.build()
136+
);
137+
}
138+
139+
public void bodyInterceptorJson(HttpProxy proxy) {
120140
proxy.addInterceptor(
121141
ProxyInterceptor
122142
.builder()
123143
.transformingResponseBody(
124-
MediaType.APPLICATION_JSON,
125-
MediaType.APPLICATION_OCTET_STREAM,
126-
buffer -> {
127-
// Apply some transformation
128-
return buffer;
129-
}
144+
BodyTransformers.jsonObject(
145+
jsonObject -> removeSomeFields(jsonObject)
146+
)
130147
).build());
131148
}
132149

133-
public void bodyInterceptorJson(HttpProxy proxy) {
150+
public void bodyInterceptorJsonMaxBufferedSize(HttpProxy proxy) {
134151
proxy.addInterceptor(
135152
ProxyInterceptor
136153
.builder()
137154
.transformingResponseBody(
138155
BodyTransformers.jsonObject(
156+
// Maximum amount of buffered bytes
157+
128 * 1024,
139158
jsonObject -> removeSomeFields(jsonObject)
140159
)
141160
).build());

src/main/java/io/vertx/httpproxy/BodyTransformers.java

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,58 +20,109 @@ public interface BodyTransformers {
2020
*/
2121
long DEFAULT_MAX_BUFFERED_SIZE = 256 * 1024;
2222

23+
/**
24+
* Like {@link #transform(MediaType, MediaType, long, Function)} with {@link #DEFAULT_MAX_BUFFERED_SIZE} maximum buffered bytes.
25+
*/
2326
@GenIgnore(GenIgnore.PERMITTED_TYPE)
24-
static BodyTransformer transform(MediaType consumedMediaType, MediaType producedMediaType, Function<Buffer, Buffer> transformer) {
25-
return transform(consumedMediaType, producedMediaType, DEFAULT_MAX_BUFFERED_SIZE, transformer);
27+
static BodyTransformer transform(MediaType consumes, MediaType produces, Function<Buffer, Buffer> transformer) {
28+
return transform(consumes, produces, DEFAULT_MAX_BUFFERED_SIZE, transformer);
2629
}
2730

31+
/**
32+
* <p>Create a body transformer that transforms {@code consumes} media type to the {@code produces} media type, by
33+
* applying the {@code transformer} function.</p>
34+
*
35+
* <p>The transformer buffers up to {@code maxBufferedBytes} and then apply the {@code transformer} function. When
36+
* the body exceeds the limit, the transformes signal an error and fail the transformation.</p>
37+
*
38+
* @param consumes the consumed media type
39+
* @param produces the produced media type
40+
* @param maxBufferedBytes the maximum amount of bytes buffered by the body transformer
41+
* @param transformer the transformer
42+
* @return the body transformer
43+
*/
2844
@GenIgnore(GenIgnore.PERMITTED_TYPE)
29-
static BodyTransformer transform(MediaType consumedMediaType, MediaType producedMediaType, long maxBufferedBytes, Function<Buffer, Buffer> transformer) {
30-
return new BodyTransformerImpl(transformer, maxBufferedBytes, consumedMediaType, producedMediaType);
45+
static BodyTransformer transform(MediaType consumes, MediaType produces, long maxBufferedBytes, Function<Buffer, Buffer> transformer) {
46+
return new BodyTransformerImpl(transformer, maxBufferedBytes, consumes, produces);
3147
}
3248

3349
/**
34-
* Create a transformer that transforms JSON object to JSON object, the transformer accepts and produces {@code application/json}.
50+
* Create a body transformer that transforms JSON object to JSON object, the transformer consumes and produces
51+
* {@code application/json}.
3552
*
36-
* @param fn the operation to transform data
53+
* @param maxBufferedBytes the maximum amount of bytes buffered by the body transformer
54+
* @param transformer the transformer function
3755
* @return the transformer instance
3856
*/
39-
static BodyTransformer jsonObject(Function<JsonObject, JsonObject> fn) {
40-
return BodyTransformerImpl.transformJsonObject(fn);
57+
static BodyTransformer jsonObject(long maxBufferedBytes, Function<JsonObject, JsonObject> transformer) {
58+
return BodyTransformerImpl.transformJsonObject(maxBufferedBytes, transformer);
4159
}
4260

4361
/**
44-
* Create a transformer that transforms JSON array to JSON array, the transformer accepts and produces {@code application/json}.
62+
* Like {@link #jsonObject(long, Function)} with {@link #DEFAULT_MAX_BUFFERED_SIZE} maximum buffered bytes.
63+
*/
64+
static BodyTransformer jsonObject(Function<JsonObject, JsonObject> transformer) {
65+
return jsonObject(DEFAULT_MAX_BUFFERED_SIZE, transformer);
66+
}
67+
68+
/**
69+
* Create a body transformer that transforms JSON array to JSON array, the transformer consumes and produces
70+
* {@code application/json}.
4571
*
46-
* @param fn the operation to transform data
72+
* @param maxBufferedBytes the maximum amount of bytes buffered by the body transformer
73+
* @param transformer the transformer function
4774
* @return the transformer instance
4875
*/
49-
static BodyTransformer jsonArray(Function<JsonArray, JsonArray> fn) {
50-
return BodyTransformerImpl.transformJsonArray(fn);
76+
static BodyTransformer jsonArray(long maxBufferedBytes, Function<JsonArray, JsonArray> transformer) {
77+
return BodyTransformerImpl.transformJsonArray(maxBufferedBytes, transformer);
78+
}
79+
80+
/**
81+
* Like {@link #jsonArray(long, Function)} with {@link #DEFAULT_MAX_BUFFERED_SIZE} maximum buffered bytes.
82+
*/
83+
static BodyTransformer jsonArray(Function<JsonArray, JsonArray> transformer) {
84+
return jsonArray(DEFAULT_MAX_BUFFERED_SIZE, transformer);
5185
}
5286

5387
/**
54-
* Create a transformer that transforms JSON value to JSON value, the transformer accepts and produces {@code application/json}.
88+
* Create a body transformer that transforms JSON value to JSON value, the transformer consumes and produces
89+
* {@code application/json}.
5590
*
56-
* @param fn the operation to transform data
91+
* @param maxBufferedBytes the maximum amount of bytes buffered by the body transformer
92+
* @param transformer the transformer function
5793
* @return the transformer instance
5894
*/
59-
static BodyTransformer jsonValue(Function<Object, Object> fn) {
60-
return BodyTransformerImpl.transformJson(fn);
95+
static BodyTransformer jsonValue(long maxBufferedBytes, Function<Object, Object> transformer) {
96+
return BodyTransformerImpl.transformJsonValue(maxBufferedBytes, transformer);
6197
}
6298

6399
/**
64-
* Create a transformer that transforms text to text, the transformer accepts and produces {@code text/plain}.
100+
* Like {@link #jsonValue(long, Function)} with {@link #DEFAULT_MAX_BUFFERED_SIZE} maximum buffered bytes.
101+
*/
102+
static BodyTransformer jsonValue(Function<Object, Object> transformer) {
103+
return jsonValue(DEFAULT_MAX_BUFFERED_SIZE, transformer);
104+
}
105+
106+
/**
107+
* Create a transformer that transforms text to text, the transformer consumes and produces {@code text/plain}.
65108
*
66-
* @param fn the operation to transform data
109+
* @param maxBufferedBytes the maximum amount of bytes buffered by the body transformer
110+
* @param transformer the transformer function
67111
* @return the transformer instance
68112
*/
69-
static BodyTransformer text(Function<String, String> fn, String encoding) {
70-
return BodyTransformerImpl.transformText(encoding, fn);
113+
static BodyTransformer text(long maxBufferedBytes, Function<String, String> transformer, String encoding) {
114+
return BodyTransformerImpl.transformText(maxBufferedBytes, encoding, transformer);
115+
}
116+
117+
/**
118+
* Like {@link #text(long, Function, String)} with {@link #DEFAULT_MAX_BUFFERED_SIZE} maximum buffered bytes.
119+
*/
120+
static BodyTransformer text(Function<String, String> transformer, String encoding) {
121+
return text(DEFAULT_MAX_BUFFERED_SIZE, transformer, encoding);
71122
}
72123

73124
/**
74-
* Create a transformer that discards the body, the transformer accepts any media type.
125+
* Create a transformer that discards the body, the transformer consumes any media type.
75126
*
76127
* @return the transformer instance
77128
*/

src/main/java/io/vertx/httpproxy/ProxyInterceptorBuilder.java

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
import io.vertx.codegen.annotations.VertxGen;
1818
import io.vertx.core.Handler;
1919
import io.vertx.core.MultiMap;
20-
import io.vertx.core.buffer.Buffer;
21-
import io.vertx.httpproxy.impl.BodyTransformerImpl;
2220

2321
import java.util.Set;
2422
import java.util.function.Function;
@@ -149,30 +147,6 @@ public interface ProxyInterceptorBuilder {
149147
@Fluent
150148
ProxyInterceptorBuilder transformingRequestBody(BodyTransformer requestTransformer);
151149

152-
/**
153-
* Like {@link #transformingRequestBody(MediaType, MediaType, Function, long)} with {@code maxBufferedSize} = {@link BodyTransformers#DEFAULT_MAX_BUFFERED_SIZE}
154-
*/
155-
@Fluent
156-
default ProxyInterceptorBuilder transformingRequestBody(MediaType consumedMediaType, MediaType producedMediaType, Function<Buffer, Buffer> requestTransformer) {
157-
return transformingRequestBody(consumedMediaType, producedMediaType, requestTransformer, BodyTransformers.DEFAULT_MAX_BUFFERED_SIZE);
158-
}
159-
160-
/**
161-
* <p>Apply a transformation to change the request body when the proxy receives it.</p>
162-
*
163-
* <p>The interceptor fully buffers the request body and then applies the transformation.</p>
164-
*
165-
* @param consumedMediaType the media type this transformer understand
166-
* @param producedMediaType the media type this transformer produces
167-
* @param requestTransformer the operation to apply to the request body
168-
* @param maxBufferedSize the maximum number of buffered bytes, when the buffered amount exceeds an HTTP error is sent
169-
* @return the created interceptor
170-
*/
171-
@Fluent
172-
default ProxyInterceptorBuilder transformingRequestBody(MediaType consumedMediaType, MediaType producedMediaType, Function<Buffer, Buffer> requestTransformer, long maxBufferedSize) {
173-
return transformingRequestBody(new BodyTransformerImpl(requestTransformer, maxBufferedSize, consumedMediaType, producedMediaType));
174-
}
175-
176150
/**
177151
* <p>Apply a transformation to change the response body when the proxy receives it.</p>
178152
*
@@ -184,28 +158,4 @@ default ProxyInterceptorBuilder transformingRequestBody(MediaType consumedMediaT
184158
@Fluent
185159
ProxyInterceptorBuilder transformingResponseBody(BodyTransformer responseTransformer);
186160

187-
/**
188-
* Like {@link #transformingResponseBody(MediaType, MediaType, Function, long)} with {@code maxBufferedSize} = {@link BodyTransformers#DEFAULT_MAX_BUFFERED_SIZE}
189-
*/
190-
@Fluent
191-
default ProxyInterceptorBuilder transformingResponseBody(MediaType consumedMediaType, MediaType producedMediaType, Function<Buffer, Buffer> responseTransformer) {
192-
return transformingResponseBody(consumedMediaType, producedMediaType, responseTransformer, BodyTransformers.DEFAULT_MAX_BUFFERED_SIZE);
193-
}
194-
195-
/**
196-
* <p>Apply a transformation to change the response body when the proxy receives it.</p>
197-
*
198-
* <p>The interceptor fully buffers the response body and then applies the transformation.</p>
199-
*
200-
* @param consumedMediaType the media type this transformer understand
201-
* @param producedMediaType the media type this transformer produces
202-
* @param responseTransformer the operation to apply to the response body
203-
* @param maxBufferedSize the maximum number of buffered bytes, when the buffered amount exceeds an HTTP error is sent
204-
* @return the created interceptor
205-
*/
206-
@Fluent
207-
default ProxyInterceptorBuilder transformingResponseBody(MediaType consumedMediaType, MediaType producedMediaType, Function<Buffer, Buffer> responseTransformer, long maxBufferedSize) {
208-
return transformingResponseBody(new BodyTransformerImpl(responseTransformer, maxBufferedSize, consumedMediaType, producedMediaType));
209-
}
210-
211161
}

src/main/java/io/vertx/httpproxy/impl/BodyTransformerImpl.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import io.vertx.core.streams.ReadStream;
1111
import io.vertx.httpproxy.Body;
1212
import io.vertx.httpproxy.BodyTransformer;
13-
import io.vertx.httpproxy.BodyTransformers;
1413
import io.vertx.httpproxy.MediaType;
1514

1615
import java.util.function.Function;
@@ -105,34 +104,18 @@ public MediaType produces(MediaType mediaType) {
105104
return produces;
106105
}
107106

108-
public static BodyTransformerImpl transformJsonObject(Function<JsonObject, JsonObject> transformer) {
109-
return transformJsonObject(BodyTransformers.DEFAULT_MAX_BUFFERED_SIZE, transformer);
110-
}
111-
112107
public static BodyTransformerImpl transformJsonObject(long maxBufferedBytes, Function<JsonObject, JsonObject> transformer) {
113108
return new BodyTransformerImpl(buffer -> transformer.apply(buffer.toJsonObject()).toBuffer(), maxBufferedBytes, MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON);
114109
}
115110

116-
public static BodyTransformerImpl transformJsonArray(Function<JsonArray, JsonArray> transformer) {
117-
return transformJsonArray(BodyTransformers.DEFAULT_MAX_BUFFERED_SIZE, transformer);
118-
}
119-
120111
public static BodyTransformerImpl transformJsonArray(long maxBufferedBytes, Function<JsonArray, JsonArray> transformer) {
121112
return new BodyTransformerImpl(buffer -> transformer.apply(buffer.toJsonArray()).toBuffer(), maxBufferedBytes, MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON);
122113
}
123114

124-
public static BodyTransformerImpl transformJson(Function<Object, Object> transformer) {
125-
return transformJson(BodyTransformers.DEFAULT_MAX_BUFFERED_SIZE, transformer);
126-
}
127-
128-
public static BodyTransformerImpl transformJson(long maxBufferedBytes, Function<Object, Object> transformer) {
115+
public static BodyTransformerImpl transformJsonValue(long maxBufferedBytes, Function<Object, Object> transformer) {
129116
return new BodyTransformerImpl(buffer -> Json.encodeToBuffer(transformer.apply(Json.decodeValue(buffer))), maxBufferedBytes, MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON);
130117
}
131118

132-
public static BodyTransformerImpl transformText(String encoding, Function<String, String> transformer) {
133-
return transformText(BodyTransformers.DEFAULT_MAX_BUFFERED_SIZE, encoding, transformer);
134-
}
135-
136119
public static BodyTransformerImpl transformText(long maxBufferedBytes, String encoding, Function<String, String> transformer) {
137120
return new BodyTransformerImpl(buffer -> Buffer.buffer(transformer.apply(buffer.toString(encoding))), maxBufferedBytes, MediaType.TEXT_PLAIN, MediaType.TEXT_PLAIN);
138121
}

src/test/java/io/vertx/tests/interceptors/BodyInterceptorTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public void testResponseMaxBufferedBytes(TestContext ctx) {
246246
});
247247

248248
startProxy(proxy -> proxy.origin(backend)
249-
.addInterceptor(ProxyInterceptor.builder().transformingResponseBody(null, MediaType.APPLICATION_JSON, buffer -> buffer, 512).build()));
249+
.addInterceptor(ProxyInterceptor.builder().transformingResponseBody(BodyTransformers.transform(null, MediaType.APPLICATION_JSON, 512, buffer -> buffer)).build()));
250250

251251
client.request(HttpMethod.POST, 8080, "localhost", "/")
252252
.compose(HttpClientRequest::send)
@@ -267,10 +267,10 @@ public void testRequestMaxBufferedBytes(TestContext ctx) {
267267
});
268268

269269
startProxy(proxy -> proxy.origin(backend)
270-
.addInterceptor(ProxyInterceptor.builder().transformingRequestBody(null, MediaType.APPLICATION_JSON, buffer -> {
270+
.addInterceptor(ProxyInterceptor.builder().transformingRequestBody(BodyTransformers.transform(null, MediaType.APPLICATION_JSON, 512, buffer -> {
271271
ctx.fail();
272272
return buffer;
273-
}, 512).build()));
273+
})).build()));
274274

275275
client.request(HttpMethod.POST, 8080, "localhost", "/")
276276
.compose(request -> request.send(Buffer.buffer("A".repeat(1024)))
@@ -285,9 +285,9 @@ public void testRequestMaxBufferedBytes(TestContext ctx) {
285285

286286
@Test
287287
public void testResponseBodyTransformationError1(TestContext ctx) {
288-
testResponseTransformationError(ctx, ProxyInterceptor.builder().transformingResponseBody(null, MediaType.APPLICATION_JSON, buffer -> {
288+
testResponseTransformationError(ctx, ProxyInterceptor.builder().transformingResponseBody(BodyTransformers.transform(null, MediaType.APPLICATION_JSON, buffer -> {
289289
throw new RuntimeException();
290-
}).build());
290+
})).build());
291291
}
292292

293293
@Test
@@ -349,9 +349,9 @@ private void testResponseTransformationError(TestContext ctx, ProxyInterceptor i
349349

350350
@Test
351351
public void testRequestBodyTransformationError(TestContext ctx) {
352-
testRequestTransformationError(ctx, ProxyInterceptor.builder().transformingRequestBody(null, MediaType.APPLICATION_JSON, buffer -> {
352+
testRequestTransformationError(ctx, ProxyInterceptor.builder().transformingRequestBody(BodyTransformers.transform(null, MediaType.APPLICATION_JSON, buffer -> {
353353
throw new RuntimeException();
354-
}).build());
354+
})).build());
355355
}
356356

357357
@Test

0 commit comments

Comments
 (0)