Skip to content

Commit bd27781

Browse files
committed
Merge branch '5.2.x' into master
2 parents 74f64c4 + 16d125c commit bd27781

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

spring-core/src/main/java/org/springframework/core/codec/StringDecoder.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ public final class StringDecoder extends AbstractDataBufferDecoder<String> {
7272

7373
private final boolean stripDelimiter;
7474

75+
private Charset defaultCharset = DEFAULT_CHARSET;
76+
7577
private final ConcurrentMap<Charset, byte[][]> delimitersCache = new ConcurrentHashMap<>();
7678

7779

@@ -83,6 +85,24 @@ private StringDecoder(List<String> delimiters, boolean stripDelimiter, MimeType.
8385
}
8486

8587

88+
/**
89+
* Set the default character set to fall back on if the MimeType does not specify any.
90+
* <p>By default this is {@code UTF-8}.
91+
* @param defaultCharset the charset to fall back on
92+
* @since 5.2.9
93+
*/
94+
public void setDefaultCharset(Charset defaultCharset) {
95+
this.defaultCharset = defaultCharset;
96+
}
97+
98+
/**
99+
* Return the configured {@link #setDefaultCharset(Charset) defaultCharset}.
100+
* @since 5.2.9
101+
*/
102+
public Charset getDefaultCharset() {
103+
return this.defaultCharset;
104+
}
105+
86106
@Override
87107
public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType) {
88108
return (elementType.resolve() == String.class && super.canDecode(elementType, mimeType));
@@ -136,12 +156,12 @@ public String decode(DataBuffer dataBuffer, ResolvableType elementType,
136156
return value;
137157
}
138158

139-
private static Charset getCharset(@Nullable MimeType mimeType) {
159+
private Charset getCharset(@Nullable MimeType mimeType) {
140160
if (mimeType != null && mimeType.getCharset() != null) {
141161
return mimeType.getCharset();
142162
}
143163
else {
144-
return DEFAULT_CHARSET;
164+
return getDefaultCharset();
145165
}
146166
}
147167

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ private void addContentDispositionHeader(ServletServerHttpRequest request, Servl
411411

412412
try {
413413
int status = response.getServletResponse().getStatus();
414-
if (status < 200 || status > 299) {
414+
if (status < 200 || (status > 299 && status < 400)) {
415415
return;
416416
}
417417
}

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,25 @@ public void addContentDispositionHeader() throws Exception {
400400
this.servletRequest.removeAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE);
401401
}
402402

403+
@Test
404+
public void addContentDispositionHeaderToErrorResponse() throws Exception {
405+
ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
406+
factory.addMediaType("pdf", new MediaType("application", "pdf"));
407+
factory.afterPropertiesSet();
408+
409+
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(
410+
Collections.singletonList(new StringHttpMessageConverter()),
411+
factory.getObject());
412+
413+
this.servletRequest.setRequestURI("/hello.dataless");
414+
this.servletResponse.setStatus(400);
415+
416+
processor.handleReturnValue("body", this.returnTypeString, this.container, this.request);
417+
418+
String header = servletResponse.getHeader("Content-Disposition");
419+
assertThat(header).isEqualTo("inline;filename=f.txt");
420+
}
421+
403422
@Test
404423
public void supportsReturnTypeResponseBodyOnType() throws Exception {
405424
Method method = ResponseBodyController.class.getMethod("handle");
@@ -724,10 +743,14 @@ private void assertContentDisposition(RequestResponseBodyMethodProcessor process
724743

725744
String header = servletResponse.getHeader("Content-Disposition");
726745
if (expectContentDisposition) {
727-
assertThat(header).as("Expected 'Content-Disposition' header. Use case: '" + comment + "'").isEqualTo("inline;filename=f.txt");
746+
assertThat(header)
747+
.as("Expected 'Content-Disposition' header. Use case: '" + comment + "'")
748+
.isEqualTo("inline;filename=f.txt");
728749
}
729750
else {
730-
assertThat(header).as("Did not expect 'Content-Disposition' header. Use case: '" + comment + "'").isNull();
751+
assertThat(header)
752+
.as("Did not expect 'Content-Disposition' header. Use case: '" + comment + "'")
753+
.isNull();
731754
}
732755

733756
this.servletRequest = new MockHttpServletRequest();

0 commit comments

Comments
 (0)