Handle invalid MediaType in Jetty/Tomcat adapters
See: gh-23553
This commit is contained in:
parent
b7eaab4c5d
commit
23be5dfb0e
|
@ -262,8 +262,13 @@ public abstract class AbstractServerHttpResponse implements ServerHttpResponse {
|
|||
protected abstract void applyStatusCode();
|
||||
|
||||
/**
|
||||
* Apply header changes from {@link #getHeaders()} to the underlying response.
|
||||
* This method is called once only.
|
||||
* Invoked when the response is getting committed allowing sub-classes to
|
||||
* make apply header values to the underlying response.
|
||||
* <p>Note that most sub-classes use an {@link HttpHeaders} instance that
|
||||
* wraps an adapter to the native response headers such that changes are
|
||||
* propagated to the underlying response on the go. That means this callback
|
||||
* is typically not used other than for specialized updates such as setting
|
||||
* the contentType or characterEncoding fields in a Servlet response.
|
||||
*/
|
||||
protected abstract void applyHeaders();
|
||||
|
||||
|
|
|
@ -101,8 +101,15 @@ public class JettyHttpHandlerAdapter extends ServletHttpHandlerAdapter {
|
|||
|
||||
@Override
|
||||
protected void applyHeaders() {
|
||||
MediaType contentType = getHeaders().getContentType();
|
||||
HttpServletResponse response = getNativeResponse();
|
||||
MediaType contentType = null;
|
||||
try {
|
||||
contentType = getHeaders().getContentType();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
String rawContentType = getHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
|
||||
response.setContentType(rawContentType);
|
||||
}
|
||||
if (response.getContentType() == null && contentType != null) {
|
||||
response.setContentType(contentType.toString());
|
||||
}
|
||||
|
|
|
@ -118,7 +118,14 @@ class ServletServerHttpResponse extends AbstractListenerServerHttpResponse {
|
|||
this.response.addHeader(headerName, headerValue);
|
||||
}
|
||||
});
|
||||
MediaType contentType = getHeaders().getContentType();
|
||||
MediaType contentType = null;
|
||||
try {
|
||||
contentType = getHeaders().getContentType();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
String rawContentType = getHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
|
||||
this.response.setContentType(rawContentType);
|
||||
}
|
||||
if (this.response.getContentType() == null && contentType != null) {
|
||||
this.response.setContentType(contentType.toString());
|
||||
}
|
||||
|
@ -126,6 +133,10 @@ class ServletServerHttpResponse extends AbstractListenerServerHttpResponse {
|
|||
if (this.response.getCharacterEncoding() == null && charset != null) {
|
||||
this.response.setCharacterEncoding(charset.name());
|
||||
}
|
||||
long contentLength = getHeaders().getContentLength();
|
||||
if (contentLength != -1) {
|
||||
this.response.setContentLengthLong(contentLength);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -204,7 +204,14 @@ public class TomcatHttpHandlerAdapter extends ServletHttpHandlerAdapter {
|
|||
@Override
|
||||
protected void applyHeaders() {
|
||||
HttpServletResponse response = getNativeResponse();
|
||||
MediaType contentType = getHeaders().getContentType();
|
||||
MediaType contentType = null;
|
||||
try {
|
||||
contentType = getHeaders().getContentType();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
String rawContentType = getHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
|
||||
response.setContentType(rawContentType);
|
||||
}
|
||||
if (response.getContentType() == null && contentType != null) {
|
||||
response.setContentType(contentType.toString());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue