3737
3838import org .springframework .http .HttpHeaders ;
3939import org .springframework .http .HttpMethod ;
40+ import org .springframework .http .InvalidMediaTypeException ;
4041import org .springframework .http .MediaType ;
4142import org .springframework .util .Assert ;
4243import org .springframework .util .LinkedCaseInsensitiveMap ;
@@ -104,6 +105,7 @@ public URI getURI() {
104105 public HttpHeaders getHeaders () {
105106 if (this .headers == null ) {
106107 this .headers = new HttpHeaders ();
108+
107109 for (Enumeration <?> headerNames = this .servletRequest .getHeaderNames (); headerNames .hasMoreElements ();) {
108110 String headerName = (String ) headerNames .nextElement ();
109111 for (Enumeration <?> headerValues = this .servletRequest .getHeaders (headerName );
@@ -112,33 +114,41 @@ public HttpHeaders getHeaders() {
112114 this .headers .add (headerName , headerValue );
113115 }
114116 }
117+
115118 // 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+ }
122127 }
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+ }
133138 }
134139 }
140+ catch (InvalidMediaTypeException ex ) {
141+ // Ignore: simply not exposing an invalid content type in HttpHeaders...
142+ }
143+
135144 if (this .headers .getContentLength () < 0 ) {
136145 int requestContentLength = this .servletRequest .getContentLength ();
137146 if (requestContentLength != -1 ) {
138147 this .headers .setContentLength (requestContentLength );
139148 }
140149 }
141150 }
151+
142152 return this .headers ;
143153 }
144154
0 commit comments