|
1 | 1 | package io.vertx.httpproxy; |
2 | 2 |
|
| 3 | +import io.vertx.codegen.annotations.GenIgnore; |
3 | 4 | import io.vertx.codegen.annotations.Unstable; |
4 | 5 | import io.vertx.codegen.annotations.VertxGen; |
5 | 6 | import io.vertx.core.buffer.Buffer; |
6 | 7 | import io.vertx.core.json.JsonArray; |
7 | 8 | import io.vertx.core.json.JsonObject; |
8 | 9 | import io.vertx.httpproxy.impl.BodyTransformerImpl; |
9 | 10 |
|
| 11 | +import java.util.Collections; |
| 12 | +import java.util.List; |
10 | 13 | import java.util.function.Function; |
11 | 14 |
|
12 | 15 | /** |
13 | 16 | * A synchronous function that transforms an HTTP body entity. |
14 | 17 | */ |
15 | 18 | @VertxGen |
16 | 19 | @Unstable |
17 | | -public interface BodyTransformer extends Function<Buffer, Buffer> { |
| 20 | +public interface BodyTransformer { |
18 | 21 |
|
19 | 22 | /** |
20 | | - * Create a callback for transform JsonObject. |
| 23 | + * @return whether this transformer consumes the {@code mediaType}, {@code mediaType} can be {@code null} |
| 24 | + * when the HTTP head does not present a body, the default implementation returns {@code false} |
| 25 | + */ |
| 26 | + default boolean consumes(MediaType mediaType) { |
| 27 | + return false; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @return the media type produced by this transformer, the default implementation returns {@code application/octet-stream} |
| 32 | + */ |
| 33 | + default MediaType produces(MediaType mediaType) { |
| 34 | + return mediaType; |
| 35 | + } |
| 36 | + |
| 37 | + @GenIgnore |
| 38 | + Function<Buffer, Buffer> transformer(MediaType mediaType); |
| 39 | + |
| 40 | + @GenIgnore(GenIgnore.PERMITTED_TYPE) |
| 41 | + static BodyTransformer transformer(MediaType consumedMediaType, MediaType producedMediaType, Function<Buffer, Buffer> transformer) { |
| 42 | + return new BodyTransformerImpl(transformer, consumedMediaType, producedMediaType); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Create a transformer that transforms JSON object to JSON object, the transformer accepts and produces {@code application/json}. |
21 | 47 | * |
22 | | - * @param transformer the operation to transform data |
23 | | - * @return the built callback |
| 48 | + * @param fn the operation to transform data |
| 49 | + * @return the transformer instance |
24 | 50 | */ |
25 | | - static BodyTransformer transformJsonObject(Function<JsonObject, JsonObject> transformer) { |
26 | | - return BodyTransformerImpl.transformJsonObject(transformer); |
| 51 | + static BodyTransformer transformJsonObject(Function<JsonObject, JsonObject> fn) { |
| 52 | + return BodyTransformerImpl.transformJsonObject(fn); |
27 | 53 | } |
28 | 54 |
|
29 | 55 | /** |
30 | | - * Create a callback for transform JsonArray. |
| 56 | + * Create a transformer that transforms JSON array to JSON array, the transformer accepts and produces {@code application/json}. |
31 | 57 | * |
32 | | - * @param transformer the operation to transform data |
33 | | - * @return the built callback |
| 58 | + * @param fn the operation to transform data |
| 59 | + * @return the transformer instance |
34 | 60 | */ |
35 | | - static BodyTransformer transformJsonArray(Function<JsonArray, JsonArray> transformer) { |
36 | | - return BodyTransformerImpl.transformJsonArray(transformer); |
| 61 | + static BodyTransformer transformJsonArray(Function<JsonArray, JsonArray> fn) { |
| 62 | + return BodyTransformerImpl.transformJsonArray(fn); |
37 | 63 | } |
38 | 64 |
|
39 | 65 | /** |
40 | | - * Create a callback for transform json with unknown shape. |
| 66 | + * Create a transformer that transforms JSON value to JSON value, the transformer accepts and produces {@code application/json}. |
41 | 67 | * |
42 | | - * @param transformer the operation to transform data |
43 | | - * @return the built callback |
| 68 | + * @param fn the operation to transform data |
| 69 | + * @return the transformer instance |
44 | 70 | */ |
45 | | - static BodyTransformer transformJson(Function<Object, Object> transformer) { |
46 | | - return BodyTransformerImpl.transformJson(transformer); |
| 71 | + static BodyTransformer transformJson(Function<Object, Object> fn) { |
| 72 | + return BodyTransformerImpl.transformJson(fn); |
47 | 73 | } |
48 | 74 |
|
49 | 75 | /** |
50 | | - * Create a callback for transform texts. |
| 76 | + * Create a transformer that transforms text to text, the transformer accepts and produces {@code text/plain}. |
51 | 77 | * |
52 | | - * @param transformer the operation to transform data |
53 | | - * @return the built callback |
| 78 | + * @param fn the operation to transform data |
| 79 | + * @return the transformer instance |
54 | 80 | */ |
55 | | - static BodyTransformer transformText(Function<String, String> transformer, String encoding) { |
56 | | - return BodyTransformerImpl.transformText(transformer, encoding); |
| 81 | + static BodyTransformer transformText(Function<String, String> fn, String encoding) { |
| 82 | + return BodyTransformerImpl.transformText(fn, encoding); |
57 | 83 | } |
58 | 84 |
|
59 | 85 | /** |
60 | | - * Create a callback to discard the body. |
| 86 | + * Create a transformer that discards the body, the transformer accepts any media type. |
61 | 87 | * |
62 | | - * @return the built callback |
| 88 | + * @return the transformer instance |
63 | 89 | */ |
64 | 90 | static BodyTransformer discard() { |
65 | 91 | return BodyTransformerImpl.discard(); |
|
0 commit comments