|
21 | 21 | import java.lang.annotation.Annotation;
|
22 | 22 | import java.util.ArrayList;
|
23 | 23 | import java.util.Collections;
|
| 24 | +import java.util.HashMap; |
24 | 25 | import java.util.List;
|
25 | 26 | import java.util.Map;
|
26 | 27 |
|
|
58 | 59 | */
|
59 | 60 | public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport implements HttpMessageEncoder<Object> {
|
60 | 61 |
|
61 |
| - protected final List<MediaType> streamingMediaTypes = new ArrayList<>(1); |
| 62 | + private static final byte[] NEWLINE_SEPARATOR = {'\n'}; |
62 | 63 |
|
63 |
| - protected boolean streamingLineSeparator = true; |
| 64 | + private static final Map<MediaType, byte[]> STREAM_SEPARATORS; |
| 65 | + |
| 66 | + static { |
| 67 | + STREAM_SEPARATORS = new HashMap<>(); |
| 68 | + STREAM_SEPARATORS.put(MediaType.APPLICATION_STREAM_JSON, NEWLINE_SEPARATOR); |
| 69 | + STREAM_SEPARATORS.put(MediaType.parseMediaType("application/stream+x-jackson-smile"), new byte[0]); |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + private final List<MediaType> streamingMediaTypes = new ArrayList<>(1); |
64 | 74 |
|
65 | 75 |
|
66 | 76 | /**
|
@@ -103,20 +113,23 @@ public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory buffe
|
103 | 113 | return Flux.from(inputStream).map(value ->
|
104 | 114 | encodeValue(value, mimeType, bufferFactory, elementType, hints));
|
105 | 115 | }
|
106 |
| - else if (this.streamingMediaTypes.stream().anyMatch(mediaType -> mediaType.isCompatibleWith(mimeType))) { |
107 |
| - return Flux.from(inputStream).map(value -> { |
108 |
| - DataBuffer buffer = encodeValue(value, mimeType, bufferFactory, elementType, hints); |
109 |
| - if (streamingLineSeparator) { |
110 |
| - buffer.write(new byte[]{'\n'}); |
111 |
| - } |
112 |
| - return buffer; |
113 |
| - }); |
114 |
| - } |
115 |
| - else { |
116 |
| - ResolvableType listType = ResolvableType.forClassWithGenerics(List.class, elementType); |
117 |
| - return Flux.from(inputStream).collectList().map(list -> |
118 |
| - encodeValue(list, mimeType, bufferFactory, listType, hints)).flux(); |
| 116 | + |
| 117 | + for (MediaType streamingMediaType : this.streamingMediaTypes) { |
| 118 | + if (streamingMediaType.isCompatibleWith(mimeType)) { |
| 119 | + byte[] separator = STREAM_SEPARATORS.getOrDefault(streamingMediaType, NEWLINE_SEPARATOR); |
| 120 | + return Flux.from(inputStream).map(value -> { |
| 121 | + DataBuffer buffer = encodeValue(value, mimeType, bufferFactory, elementType, hints); |
| 122 | + if (separator != null) { |
| 123 | + buffer.write(separator); |
| 124 | + } |
| 125 | + return buffer; |
| 126 | + }); |
| 127 | + } |
119 | 128 | }
|
| 129 | + |
| 130 | + ResolvableType listType = ResolvableType.forClassWithGenerics(List.class, elementType); |
| 131 | + return Flux.from(inputStream).collectList().map(list -> |
| 132 | + encodeValue(list, mimeType, bufferFactory, listType, hints)).flux(); |
120 | 133 | }
|
121 | 134 |
|
122 | 135 | private DataBuffer encodeValue(Object value, @Nullable MimeType mimeType, DataBufferFactory bufferFactory,
|
|
0 commit comments