Skip to content

Commit c601ecf

Browse files
committed
Merge branch '5.1.x'
2 parents 0b50d1e + c8d49ed commit c601ecf

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java

+16-5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.http.server.reactive.ServerHttpResponse;
3939
import org.springframework.lang.Nullable;
4040
import org.springframework.util.Assert;
41+
import org.springframework.util.StringUtils;
4142

4243
/**
4344
* {@code HttpMessageWriter} that wraps and delegates to an {@link Encoder}.
@@ -166,19 +167,29 @@ private static MediaType addDefaultCharset(MediaType main, @Nullable MediaType d
166167
return main;
167168
}
168169

169-
private boolean isStreamingMediaType(@Nullable MediaType contentType) {
170-
if (contentType == null || !(this.encoder instanceof HttpMessageEncoder)) {
170+
private boolean isStreamingMediaType(@Nullable MediaType mediaType) {
171+
if (mediaType == null || !(this.encoder instanceof HttpMessageEncoder)) {
171172
return false;
172173
}
173-
for (MediaType mediaType : ((HttpMessageEncoder<?>) this.encoder).getStreamingMediaTypes()) {
174-
if (contentType.isCompatibleWith(mediaType) &&
175-
contentType.getParameters().keySet().containsAll(mediaType.getParameters().keySet())) {
174+
for (MediaType streamingMediaType : ((HttpMessageEncoder<?>) this.encoder).getStreamingMediaTypes()) {
175+
if (mediaType.isCompatibleWith(streamingMediaType) && matchParameters(mediaType, streamingMediaType)) {
176176
return true;
177177
}
178178
}
179179
return false;
180180
}
181181

182+
private boolean matchParameters(MediaType streamingMediaType, MediaType mediaType) {
183+
for (String name : streamingMediaType.getParameters().keySet()) {
184+
String s1 = streamingMediaType.getParameter(name);
185+
String s2 = mediaType.getParameter(name);
186+
if (StringUtils.hasText(s1) && StringUtils.hasText(s2) && !s1.equalsIgnoreCase(s2)) {
187+
return false;
188+
}
189+
}
190+
return true;
191+
}
192+
182193

183194
// Server side only...
184195

spring-web/src/test/java/org/springframework/http/codec/EncoderHttpMessageWriterTests.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.http.codec;
1818

19+
import java.lang.reflect.InvocationTargetException;
20+
import java.lang.reflect.Method;
1921
import java.nio.charset.StandardCharsets;
2022
import java.util.Arrays;
2123
import java.util.Collections;
@@ -31,13 +33,13 @@
3133
import reactor.core.publisher.Mono;
3234
import reactor.test.StepVerifier;
3335

34-
import org.springframework.core.codec.Encoder;
3536
import org.springframework.core.io.buffer.DataBuffer;
3637
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
3738
import org.springframework.http.MediaType;
3839
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
3940
import org.springframework.util.MimeType;
4041
import org.springframework.util.MimeTypeUtils;
42+
import org.springframework.util.ReflectionUtils;
4143

4244
import static java.nio.charset.StandardCharsets.ISO_8859_1;
4345
import static java.nio.charset.StandardCharsets.UTF_8;
@@ -64,7 +66,7 @@ public class EncoderHttpMessageWriterTests {
6466

6567

6668
@Mock
67-
private Encoder<String> encoder;
69+
private HttpMessageEncoder<String> encoder;
6870

6971
private ArgumentCaptor<MediaType> mediaTypeCaptor;
7072

@@ -177,6 +179,17 @@ public void emptyBodyWritten() {
177179
assertEquals(0, this.response.getHeaders().getContentLength());
178180
}
179181

182+
@Test // gh-22936
183+
public void isStreamingMediaType() throws InvocationTargetException, IllegalAccessException {
184+
HttpMessageWriter<String> writer = getWriter(TEXT_HTML);
185+
MediaType streamingMediaType = new MediaType(TEXT_PLAIN, Collections.singletonMap("streaming", "true"));
186+
when(this.encoder.getStreamingMediaTypes()).thenReturn(Arrays.asList(streamingMediaType));
187+
Method method = ReflectionUtils.findMethod(writer.getClass(), "isStreamingMediaType", MediaType.class);
188+
ReflectionUtils.makeAccessible(method);
189+
assertTrue((Boolean) method.invoke(writer, streamingMediaType));
190+
assertFalse((Boolean) method.invoke(writer, new MediaType(TEXT_PLAIN, Collections.singletonMap("streaming", "false"))));
191+
assertFalse((Boolean) method.invoke(writer, TEXT_HTML));
192+
}
180193

181194
private HttpMessageWriter<String> getWriter(MimeType... mimeTypes) {
182195
return getWriter(Flux.empty(), mimeTypes);

0 commit comments

Comments
 (0)