|
1 | 1 | package io.vertx.grpc.transcoding.impl; |
2 | 2 |
|
3 | 3 | import io.vertx.core.buffer.Buffer; |
4 | | -import io.vertx.core.internal.buffer.BufferInternal; |
5 | 4 | import io.vertx.core.json.DecodeException; |
6 | 5 | import io.vertx.core.json.JsonObject; |
7 | 6 | import io.vertx.grpc.transcoding.impl.config.HttpVariableBinding; |
8 | 7 |
|
9 | 8 | import java.util.List; |
| 9 | +import java.util.Objects; |
10 | 10 |
|
11 | | -public class MessageWeaver { |
| 11 | +/** |
| 12 | + * The MessageWeaver class handles the merging and transformation of gRPC messages during HTTP-to-gRPC transcoding operations. |
| 13 | + * |
| 14 | + * @see HttpVariableBinding |
| 15 | + */ |
| 16 | +public final class MessageWeaver { |
12 | 17 |
|
| 18 | + private static final String ROOT_LEVEL = "*"; |
| 19 | + |
| 20 | + private MessageWeaver() { |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Weaves HTTP variable bindings and request body into a gRPC message. |
| 25 | + * |
| 26 | + * @param message The original message buffer |
| 27 | + * @param bindings The HTTP variable bindings |
| 28 | + * @param transcodingRequestBody The transcoding request body path |
| 29 | + * @return The modified buffer with weaved content |
| 30 | + * @throws DecodeException If JSON decoding fails |
| 31 | + */ |
13 | 32 | public static Buffer weaveRequestMessage(Buffer message, List<HttpVariableBinding> bindings, String transcodingRequestBody) throws DecodeException { |
14 | | - if (bindings.isEmpty() && transcodingRequestBody == null) { |
| 33 | + if ((bindings == null || bindings.isEmpty()) && (transcodingRequestBody == null || transcodingRequestBody.isEmpty())) { |
15 | 34 | return message; |
16 | 35 | } |
17 | | - JsonObject result = weaveRequestMessage2(message, bindings, transcodingRequestBody); |
18 | | - BufferInternal buffer = BufferInternal.buffer(); |
19 | | - buffer.appendString(result.encode()); |
20 | | - return buffer; |
21 | | - } |
22 | | - |
23 | | - public static JsonObject weaveRequestMessage2(Buffer message, List<HttpVariableBinding> bindings, String transcodingRequestBody) throws DecodeException { |
24 | 36 |
|
25 | 37 | JsonObject result = new JsonObject(); |
26 | 38 |
|
27 | | - // First handle the bindings |
| 39 | + if (bindings != null && !bindings.isEmpty()) { |
| 40 | + applyBindings(result, bindings); |
| 41 | + } |
| 42 | + |
| 43 | + JsonObject messageJson = null; |
| 44 | + if (message != null && !message.toString().isBlank()) { |
| 45 | + messageJson = message.toJsonObject(); |
| 46 | + } |
| 47 | + |
| 48 | + if (messageJson != null && !messageJson.isEmpty()) { |
| 49 | + if (transcodingRequestBody == null || transcodingRequestBody.isEmpty()) { |
| 50 | + // No specific path, merge at root level with deep copy |
| 51 | + result.mergeIn(messageJson, true); |
| 52 | + } else if (ROOT_LEVEL.equals(transcodingRequestBody)) { |
| 53 | + // Wildcard, merge at root level without overwriting bindings |
| 54 | + result.mergeIn(messageJson); |
| 55 | + } else { |
| 56 | + applyAtPath(result, transcodingRequestBody.split("\\."), messageJson); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return result.toBuffer(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Applies HTTP variable bindings to the result object. |
| 65 | + */ |
| 66 | + private static void applyBindings(JsonObject result, List<HttpVariableBinding> bindings) { |
28 | 67 | for (HttpVariableBinding binding : bindings) { |
29 | | - JsonObject current = result; |
30 | 68 | List<String> fieldPath = binding.getFieldPath(); |
| 69 | + if (fieldPath == null || fieldPath.isEmpty()) { |
| 70 | + continue; |
| 71 | + } |
31 | 72 |
|
32 | | - // Navigate to parent object, creating path if needed |
| 73 | + // Navigate to parent object, creating nested structure as needed |
| 74 | + JsonObject current = result; |
33 | 75 | for (int i = 0; i < fieldPath.size() - 1; i++) { |
34 | 76 | String fieldName = fieldPath.get(i); |
35 | | - if (!current.containsKey(fieldName)) { |
36 | | - current.put(fieldName, new JsonObject()); |
37 | | - } |
38 | | - Object fieldValue = current.getValue(fieldName); |
39 | | - if (!(fieldValue instanceof JsonObject)) { |
40 | | - // If the field exists but is not an object, overwrite it with an empty object |
41 | | - current.put(fieldName, new JsonObject()); |
| 77 | + JsonObject next = current.getJsonObject(fieldName); |
| 78 | + if (next == null) { |
| 79 | + next = new JsonObject(); |
| 80 | + current.put(fieldName, next); |
42 | 81 | } |
43 | | - current = current.getJsonObject(fieldName); |
| 82 | + current = next; |
44 | 83 | } |
45 | 84 |
|
46 | 85 | // Set the value at the final path position |
47 | | - current.put(fieldPath.get(fieldPath.size() - 1), binding.getValue()); |
| 86 | + String lastField = fieldPath.get(fieldPath.size() - 1); |
| 87 | + current.put(lastField, binding.getValue()); |
48 | 88 | } |
| 89 | + } |
49 | 90 |
|
50 | | - // Then handle the transcoding request body |
51 | | - if (transcodingRequestBody != null && !transcodingRequestBody.isEmpty()) { |
52 | | - JsonObject messageJson = message.toString().isBlank() ? new JsonObject() : new JsonObject(message.toString()); |
53 | | - if (!messageJson.isEmpty()) { |
54 | | - if (transcodingRequestBody.equals("*")) { |
55 | | - result.mergeIn(messageJson); |
56 | | - } else { |
57 | | - JsonObject current = result; |
58 | | - String[] path = transcodingRequestBody.split("\\."); |
59 | | - for (int i = 0; i < path.length - 1; i++) { |
60 | | - String fieldName = path[i]; |
61 | | - if (!current.containsKey(fieldName)) { |
62 | | - current.put(fieldName, new JsonObject()); |
63 | | - } |
64 | | - current = current.getJsonObject(fieldName); |
65 | | - } |
66 | | - current.put(path[path.length - 1], messageJson); |
67 | | - } |
68 | | - } |
69 | | - } else { |
70 | | - JsonObject messageJson = message.toString().isBlank() ? new JsonObject() : message.toJsonObject(); |
71 | | - if (!messageJson.isEmpty()) { |
72 | | - result.mergeIn(messageJson, true); |
| 91 | + /** |
| 92 | + * Applies an object at a specific path in the JSON structure. |
| 93 | + */ |
| 94 | + private static void applyAtPath(JsonObject root, String[] path, Object value) { |
| 95 | + JsonObject current = root; |
| 96 | + for (int i = 0; i < path.length - 1; i++) { |
| 97 | + String fieldName = path[i]; |
| 98 | + JsonObject next = current.getJsonObject(fieldName); |
| 99 | + if (next == null) { |
| 100 | + next = new JsonObject(); |
| 101 | + current.put(fieldName, next); |
73 | 102 | } |
| 103 | + current = next; |
74 | 104 | } |
75 | | - |
76 | | - return result; |
| 105 | + current.put(path[path.length - 1], value); |
77 | 106 | } |
78 | 107 |
|
79 | | - public static Buffer weaveResponseMessage(Buffer message, String transcodingResponseBody) { |
80 | | - if (transcodingResponseBody == null || transcodingResponseBody.isEmpty()) { |
| 108 | + /** |
| 109 | + * Extracts a response message portion based on the transcoding path. |
| 110 | + * |
| 111 | + * @param message The original message buffer |
| 112 | + * @param transcodingResponseBody The path to extract from the response |
| 113 | + * @return The modified buffer with the extracted content |
| 114 | + */ |
| 115 | + public static Buffer weaveResponseMessage(Buffer message, String transcodingResponseBody) throws DecodeException { |
| 116 | + Objects.requireNonNull(message, "Message cannot be null"); |
| 117 | + |
| 118 | + if (transcodingResponseBody == null || transcodingResponseBody.isEmpty() || transcodingResponseBody.equals(ROOT_LEVEL)) { |
81 | 119 | return message; |
82 | 120 | } |
83 | 121 |
|
84 | 122 | JsonObject json = message.toJsonObject(); |
85 | | - |
86 | | - if (transcodingResponseBody.equals("*")) { |
87 | | - return message; |
88 | | - } |
89 | | - |
90 | 123 | String[] path = transcodingResponseBody.split("\\."); |
91 | | - JsonObject current = json; |
92 | 124 |
|
93 | | - // Navigate to the specified path |
| 125 | + JsonObject current = json; |
94 | 126 | for (String field : path) { |
95 | | - if (current.containsKey(field)) { |
96 | | - Object value = current.getValue(field); |
97 | | - if (value instanceof JsonObject) { |
98 | | - current = (JsonObject) value; |
99 | | - } else { |
100 | | - throw new IllegalStateException("Invalid transcodingResponseBody path: " + transcodingResponseBody); |
101 | | - } |
| 127 | + Object value = current.getValue(field); |
| 128 | + if (value instanceof JsonObject) { |
| 129 | + current = (JsonObject) value; |
102 | 130 | } else { |
103 | | - throw new IllegalStateException("Invalid transcodingResponseBody path: " + transcodingResponseBody); |
| 131 | + throw new IllegalArgumentException("Path segment '" + field + "' in transcodingResponseBody does not refer to a JSON object"); |
104 | 132 | } |
105 | 133 | } |
106 | 134 |
|
107 | | - // Return just the object at the specified path |
108 | | - return Buffer.buffer(current.encode()); |
| 135 | + return current.toBuffer(); |
109 | 136 | } |
110 | 137 | } |
0 commit comments