37
37
38
38
import org .springframework .http .HttpHeaders ;
39
39
import org .springframework .http .HttpMethod ;
40
+ import org .springframework .http .InvalidMediaTypeException ;
40
41
import org .springframework .http .MediaType ;
41
42
import org .springframework .util .Assert ;
42
43
import org .springframework .util .LinkedCaseInsensitiveMap ;
@@ -104,6 +105,7 @@ public URI getURI() {
104
105
public HttpHeaders getHeaders () {
105
106
if (this .headers == null ) {
106
107
this .headers = new HttpHeaders ();
108
+
107
109
for (Enumeration <?> headerNames = this .servletRequest .getHeaderNames (); headerNames .hasMoreElements ();) {
108
110
String headerName = (String ) headerNames .nextElement ();
109
111
for (Enumeration <?> headerValues = this .servletRequest .getHeaders (headerName );
@@ -112,33 +114,41 @@ public HttpHeaders getHeaders() {
112
114
this .headers .add (headerName , headerValue );
113
115
}
114
116
}
117
+
115
118
// HttpServletRequest exposes some headers as properties: we should include those if not already present
116
- MediaType contentType = this .headers .getContentType ();
117
- if (contentType == null ) {
118
- String requestContentType = this .servletRequest .getContentType ();
119
- if (StringUtils .hasLength (requestContentType )) {
120
- contentType = MediaType .parseMediaType (requestContentType );
121
- this .headers .setContentType (contentType );
119
+ try {
120
+ MediaType contentType = this .headers .getContentType ();
121
+ if (contentType == null ) {
122
+ String requestContentType = this .servletRequest .getContentType ();
123
+ if (StringUtils .hasLength (requestContentType )) {
124
+ contentType = MediaType .parseMediaType (requestContentType );
125
+ this .headers .setContentType (contentType );
126
+ }
122
127
}
123
- }
124
- if ( contentType != null && contentType . getCharset () == null ) {
125
- String requestEncoding = this . servletRequest . getCharacterEncoding ();
126
- if ( StringUtils . hasLength (requestEncoding )) {
127
- Charset charSet = Charset . forName ( requestEncoding );
128
- Map < String , String > params = new LinkedCaseInsensitiveMap < String >( );
129
- params .putAll ( contentType . getParameters ());
130
- params . put ( "charset" , charSet . toString () );
131
- MediaType newContentType = new MediaType ( contentType . getType (), contentType . getSubtype (), params );
132
- this . headers . setContentType ( newContentType );
128
+ if ( contentType != null && contentType . getCharset () == null ) {
129
+ String requestEncoding = this . servletRequest . getCharacterEncoding ();
130
+ if ( StringUtils . hasLength ( requestEncoding )) {
131
+ Charset charSet = Charset . forName (requestEncoding );
132
+ Map < String , String > params = new LinkedCaseInsensitiveMap < String >( );
133
+ params . putAll ( contentType . getParameters () );
134
+ params .put ( "charset" , charSet . toString ());
135
+ MediaType newContentType = new MediaType ( contentType . getType (), contentType . getSubtype (), params );
136
+ this . headers . setContentType ( newContentType );
137
+ }
133
138
}
134
139
}
140
+ catch (InvalidMediaTypeException ex ) {
141
+ // Ignore: simply not exposing an invalid content type in HttpHeaders...
142
+ }
143
+
135
144
if (this .headers .getContentLength () < 0 ) {
136
145
int requestContentLength = this .servletRequest .getContentLength ();
137
146
if (requestContentLength != -1 ) {
138
147
this .headers .setContentLength (requestContentLength );
139
148
}
140
149
}
141
150
}
151
+
142
152
return this .headers ;
143
153
}
144
154
0 commit comments