1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2019 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.
22
22
import java .util .ArrayList ;
23
23
import java .util .List ;
24
24
25
+ import org .springframework .http .HttpHeaders ;
25
26
import org .springframework .http .HttpInputMessage ;
26
27
import org .springframework .http .HttpOutputMessage ;
27
28
import org .springframework .http .MediaType ;
@@ -72,7 +73,9 @@ public StringHttpMessageConverter(Charset defaultCharset) {
72
73
73
74
74
75
/**
75
- * Indicates whether the {@code Accept-Charset} should be written to any outgoing request.
76
+ * Whether the {@code Accept-Charset} header should be written to any outgoing
77
+ * request sourced from the value of {@link Charset#availableCharsets()}.
78
+ * The behavior is suppressed if the header has already been set.
76
79
* <p>Default is {@code true}.
77
80
*/
78
81
public void setWriteAcceptCharset (boolean writeAcceptCharset ) {
@@ -99,10 +102,11 @@ protected Long getContentLength(String str, @Nullable MediaType contentType) {
99
102
100
103
@ Override
101
104
protected void writeInternal (String str , HttpOutputMessage outputMessage ) throws IOException {
102
- if (this .writeAcceptCharset ) {
103
- outputMessage .getHeaders ().setAcceptCharset (getAcceptedCharsets ());
105
+ HttpHeaders headers = outputMessage .getHeaders ();
106
+ if (this .writeAcceptCharset && headers .get (HttpHeaders .ACCEPT_CHARSET ) == null ) {
107
+ headers .setAcceptCharset (getAcceptedCharsets ());
104
108
}
105
- Charset charset = getContentTypeCharset (outputMessage . getHeaders () .getContentType ());
109
+ Charset charset = getContentTypeCharset (headers .getContentType ());
106
110
StreamUtils .copy (str , charset , outputMessage .getBody ());
107
111
}
108
112
0 commit comments