Allows to change the default ErrorHandler response type (#11522)

Allows to set the default mime type for the error response in `ErrorHandler`.
This commit is contained in:
Dennis Hoersch 2024-03-19 19:17:12 +01:00 committed by GitHub
parent c8ea569e5d
commit d39dde5213
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 1 deletions

View File

@ -74,6 +74,7 @@ public class ErrorHandler implements Request.Handler
boolean _showStacks = true;
boolean _showMessageInTitle = true;
String _defaultResponseMimeType = Type.TEXT_HTML.asString();
HttpField _cacheControl = new PreEncodedHttpField(HttpHeader.CACHE_CONTROL, "must-revalidate,no-cache,no-store");
public ErrorHandler()
@ -127,7 +128,7 @@ public class ErrorHandler implements Request.Handler
callback.succeeded();
return;
}
acceptable = Collections.singletonList(Type.TEXT_HTML.asString());
acceptable = Collections.singletonList(_defaultResponseMimeType);
}
List<Charset> charsets = request.getHeaders().getQualityCSV(HttpHeader.ACCEPT_CHARSET).stream()
.map(s ->
@ -477,6 +478,23 @@ public class ErrorHandler implements Request.Handler
_showMessageInTitle = showMessageInTitle;
}
/**
* @return The mime type to be used when a client does not specify an Accept header, or the request did not fully parse
*/
@ManagedAttribute("Mime type to be used when a client does not specify an Accept header, or the request did not fully parse")
public String getDefaultResponseMimeType()
{
return _defaultResponseMimeType;
}
/**
* @param defaultResponseMimeType The mime type to be used when a client does not specify an Accept header, or the request did not fully parse
*/
public void setDefaultResponseMimeType(String defaultResponseMimeType)
{
_defaultResponseMimeType = Objects.requireNonNull(defaultResponseMimeType);;
}
protected void write(Writer writer, String string) throws IOException
{
if (string == null)

View File

@ -26,6 +26,7 @@ import org.eclipse.jetty.http.BadMessageException;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.io.Content;
import org.eclipse.jetty.io.QuietException;
import org.eclipse.jetty.logging.StacklessLogging;
@ -159,6 +160,31 @@ public class ErrorHandlerTest
assertContent(response);
}
@Test
public void test404NoAcceptButSpecifiedDefaultResponseMimeType() throws Exception
{
ErrorHandler errorHandler = new ErrorHandler();
errorHandler.setDefaultResponseMimeType(MimeTypes.Type.APPLICATION_JSON.asString());
server.setErrorHandler(errorHandler);
String rawResponse = connector.getResponse("""
GET / HTTP/1.1
Host: Localhost
""");
HttpTester.Response response = HttpTester.parseResponse(rawResponse);
assertThat("Response status code", response.getStatus(), is(404));
assertThat("Response Content-Length", response.getField(HttpHeader.CONTENT_LENGTH).getIntValue(), greaterThan(0));
assertThat("Response Content-Type", response.get(HttpHeader.CONTENT_TYPE), containsString("application/json"));
assertThat(response.get(HttpHeader.DATE), notNullValue());
assertThat(response.getContent(), containsString("\"status\":\"404\""));
assertThat(response.getContent(), containsString("\"message\":\"Not Found\""));
assertContent(response);
}
@Test
public void test404EmptyAccept() throws Exception
{