Skip to content

Commit

Permalink
Allows to change the default ErrorHandler response type (#11522)
Browse files Browse the repository at this point in the history
Allows to set the default mime type for the error response in `ErrorHandler`.
  • Loading branch information
dhs3000 authored Mar 19, 2024
1 parent c8ea569 commit d39dde5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -127,7 +128,7 @@ protected void generateResponse(Request request, Response response, int code, St
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 ->
Expand Down Expand Up @@ -477,6 +478,23 @@ public void setShowMessageInTitle(boolean showMessageInTitle)
_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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand Down Expand Up @@ -159,6 +160,31 @@ public void test404NoAccept() throws Exception
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
{
Expand Down

0 comments on commit d39dde5

Please sign in to comment.