|
45 | 45 | import java.util.Map; |
46 | 46 | import java.util.concurrent.CountDownLatch; |
47 | 47 | import java.util.concurrent.TimeUnit; |
| 48 | +import java.util.function.BiConsumer; |
48 | 49 | import java.util.function.Supplier; |
49 | 50 | import java.util.logging.Logger; |
50 | 51 | import javax.annotation.Nullable; |
@@ -123,9 +124,13 @@ private void writeHeadersToServletResponse(Metadata metadata) { |
123 | 124 | resp.setStatus(HttpServletResponse.SC_OK); |
124 | 125 | resp.setContentType(CONTENT_TYPE_GRPC); |
125 | 126 |
|
| 127 | + serializeHeaders(metadata, resp::addHeader); |
| 128 | + } |
| 129 | + |
| 130 | + private static void serializeHeaders(Metadata metadata, BiConsumer<String, String> consumer) { |
126 | 131 | byte[][] serializedHeaders = TransportFrameUtil.toHttp2Headers(metadata); |
127 | 132 | for (int i = 0; i < serializedHeaders.length; i += 2) { |
128 | | - resp.addHeader( |
| 133 | + consumer.accept( |
129 | 134 | new String(serializedHeaders[i], StandardCharsets.US_ASCII), |
130 | 135 | new String(serializedHeaders[i + 1], StandardCharsets.US_ASCII)); |
131 | 136 | } |
@@ -278,13 +283,8 @@ public void writeTrailers(Metadata trailers, boolean headersSent, Status status) |
278 | 283 | if (!headersSent) { |
279 | 284 | writeHeadersToServletResponse(trailers); |
280 | 285 | } else { |
281 | | - byte[][] serializedHeaders = TransportFrameUtil.toHttp2Headers(trailers); |
282 | | - for (int i = 0; i < serializedHeaders.length; i += 2) { |
283 | | - String key = new String(serializedHeaders[i], StandardCharsets.US_ASCII); |
284 | | - String newValue = new String(serializedHeaders[i + 1], StandardCharsets.US_ASCII); |
285 | | - trailerSupplier.get().computeIfPresent(key, (k, v) -> v + "," + newValue); |
286 | | - trailerSupplier.get().putIfAbsent(key, newValue); |
287 | | - } |
| 286 | + serializeHeaders(trailers, (key, value) -> |
| 287 | + trailerSupplier.get().merge(key, value, TrailerSupplier::joinValues)); |
288 | 288 | } |
289 | 289 |
|
290 | 290 | writer.complete(); |
@@ -322,6 +322,10 @@ private static final class TrailerSupplier implements Supplier<Map<String, Strin |
322 | 322 | public Map<String, String> get() { |
323 | 323 | return trailers; |
324 | 324 | } |
| 325 | + |
| 326 | + static String joinValues(String oldValue, String newValue) { |
| 327 | + return oldValue + "," + newValue; |
| 328 | + } |
325 | 329 | } |
326 | 330 |
|
327 | 331 | static String toHexString(byte[] bytes, int length) { |
|
0 commit comments