|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2022 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
17 | 17 | package org.springframework.http.codec;
|
18 | 18 |
|
19 | 19 | import java.nio.charset.StandardCharsets;
|
20 |
| -import java.time.Duration; |
21 | 20 | import java.util.Collections;
|
22 | 21 | import java.util.List;
|
23 | 22 | import java.util.Map;
|
@@ -124,65 +123,42 @@ private Flux<Publisher<DataBuffer>> encode(Publisher<?> input, ResolvableType el
|
124 | 123 | ServerSentEvent<?> sse = (element instanceof ServerSentEvent<?> serverSentEvent ?
|
125 | 124 | serverSentEvent : ServerSentEvent.builder().data(element).build());
|
126 | 125 |
|
127 |
| - StringBuilder sb = new StringBuilder(); |
128 |
| - String id = sse.id(); |
129 |
| - String event = sse.event(); |
130 |
| - Duration retry = sse.retry(); |
131 |
| - String comment = sse.comment(); |
| 126 | + String sseText = sse.format(); |
132 | 127 | Object data = sse.data();
|
133 |
| - if (id != null) { |
134 |
| - writeField("id", id, sb); |
135 |
| - } |
136 |
| - if (event != null) { |
137 |
| - writeField("event", event, sb); |
138 |
| - } |
139 |
| - if (retry != null) { |
140 |
| - writeField("retry", retry.toMillis(), sb); |
141 |
| - } |
142 |
| - if (comment != null) { |
143 |
| - sb.append(':').append(StringUtils.replace(comment, "\n", "\n:")).append('\n'); |
144 |
| - } |
145 |
| - if (data != null) { |
146 |
| - sb.append("data:"); |
147 |
| - } |
148 | 128 |
|
149 | 129 | Flux<DataBuffer> result;
|
150 | 130 | if (data == null) {
|
151 |
| - result = Flux.just(encodeText(sb + "\n", mediaType, factory)); |
| 131 | + result = Flux.just(encodeText(sseText + "\n", mediaType, factory)); |
152 | 132 | }
|
153 | 133 | else if (data instanceof String text) {
|
154 | 134 | text = StringUtils.replace(text, "\n", "\ndata:");
|
155 |
| - result = Flux.just(encodeText(sb + text + "\n\n", mediaType, factory)); |
| 135 | + result = Flux.just(encodeText(sseText + text + "\n\n", mediaType, factory)); |
156 | 136 | }
|
157 | 137 | else {
|
158 |
| - result = encodeEvent(sb, data, dataType, mediaType, factory, hints); |
| 138 | + result = encodeEvent(sseText, data, dataType, mediaType, factory, hints); |
159 | 139 | }
|
160 | 140 |
|
161 | 141 | return result.doOnDiscard(DataBuffer.class, DataBufferUtils::release);
|
162 | 142 | });
|
163 | 143 | }
|
164 | 144 |
|
165 | 145 | @SuppressWarnings("unchecked")
|
166 |
| - private <T> Flux<DataBuffer> encodeEvent(StringBuilder eventContent, T data, ResolvableType dataType, |
| 146 | + private <T> Flux<DataBuffer> encodeEvent(CharSequence sseText, T data, ResolvableType dataType, |
167 | 147 | MediaType mediaType, DataBufferFactory factory, Map<String, Object> hints) {
|
168 | 148 |
|
169 | 149 | if (this.encoder == null) {
|
170 | 150 | throw new CodecException("No SSE encoder configured and the data is not String.");
|
171 | 151 | }
|
172 | 152 |
|
173 | 153 | return Flux.defer(() -> {
|
174 |
| - DataBuffer startBuffer = encodeText(eventContent, mediaType, factory); |
| 154 | + DataBuffer startBuffer = encodeText(sseText, mediaType, factory); |
175 | 155 | DataBuffer endBuffer = encodeText("\n\n", mediaType, factory);
|
176 | 156 | DataBuffer dataBuffer = ((Encoder<T>) this.encoder).encodeValue(data, factory, dataType, mediaType, hints);
|
177 | 157 | Hints.touchDataBuffer(dataBuffer, hints, logger);
|
178 | 158 | return Flux.just(startBuffer, dataBuffer, endBuffer);
|
179 | 159 | });
|
180 | 160 | }
|
181 | 161 |
|
182 |
| - private void writeField(String fieldName, Object fieldValue, StringBuilder sb) { |
183 |
| - sb.append(fieldName).append(':').append(fieldValue).append('\n'); |
184 |
| - } |
185 |
| - |
186 | 162 | private DataBuffer encodeText(CharSequence text, MediaType mediaType, DataBufferFactory bufferFactory) {
|
187 | 163 | Assert.notNull(mediaType.getCharset(), "Expected MediaType with charset");
|
188 | 164 | byte[] bytes = text.toString().getBytes(mediaType.getCharset());
|
|
0 commit comments