Top Inners Fields Constructors Methods
org.eclipse.jetty.ee10.servlet

public Class ErrorHandler

extends Object
implements Handler
Class Inheritance
All Implemented Interfaces
org.eclipse.jetty.server.Request.Handler, org.eclipse.jetty.util.thread.Invocable
Known Direct Subclasses
org.eclipse.jetty.ee10.servlet.ErrorPageErrorHandler
Imports
java.io.IOException, .OutputStreamWriter, .PrintWriter, .StringWriter, .Writer, java.nio.BufferOverflowException, .ByteBuffer, java.nio.charset.Charset, .StandardCharsets, java.util.HashMap, .List, .Map, java.util.stream.Collectors, jakarta.servlet.RequestDispatcher, .ServletException, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, org.eclipse.jetty.http.HttpField, .HttpFields, .HttpHeader, .HttpStatus, .MimeTypes, .QuotedQualityCSV, org.eclipse.jetty.io.ByteBufferOutputStream, org.eclipse.jetty.server.Request, .Response, .Server, org.eclipse.jetty.server.handler.ContextHandler, org.eclipse.jetty.util.BufferUtil, .Callback, .StringUtil, org.slf4j.Logger, .LoggerFactory

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface

Field Summary

Modifier and TypeField and Description
pack-priv String
pack-priv boolean
pack-priv boolean
pack-priv boolean
pack-priv boolean
public static final String
public static final String
public static final String
private static final Logger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public ByteBuffer

Returns:

The content as a ByteBuffer, or null for no body.
badMessageError
(int
The error code that will be sent
status
,
String
The reason for the error code (may be null)
reason
,
HttpFields.Mutable
The header fields that will be sent with the response.
fields
)

Bad Message Error body

Generate an error response body to be sent for a bad message.

public boolean
protected void
generateAcceptableResponse(ServletContextRequest
The base request
baseRequest
,
HttpServletRequest
The servlet request (may be wrapped)
request
,
HttpServletResponse
The response (may be wrapped)
response
,
int
the http error code
code
,
String
the http error message
message
)

Generate an acceptable error response.

protected void
generateAcceptableResponse(ServletContextRequest
The base request
baseRequest
,
HttpServletRequest
The servlet request (may be wrapped)
request
,
HttpServletResponse
The response (may be wrapped)
response
,
int
the http error code
code
,
String
the http error message
message
,
String
The mimetype to generate (may be */*or other wildcard)
contentType
)

Generate an acceptable error response for a mime type.

protected Writer

Returns:

A Writer if there is a known acceptable charset or null
getAcceptableWriter
(Request
The base request
baseRequest
,
HttpServletRequest
The servlet request (may be wrapped)
request
,
HttpServletResponse
The response (may be wrapped)
response
)
Deprecated

Returns an acceptable writer for an error page.

public String

Returns:

the cacheControl header to set on error responses.
getCacheControl
()

Get the cacheControl.

public static Request.Handler
public boolean
public boolean
handle(Request
the HTTP request to handle
request
,
Response
the HTTP response to handle
response
,
Callback
the callback to complete when the handling is complete
callback
)

Implements org.eclipse.jetty.server.Request.Handler.handle.

Invoked to handle the passed HTTP request and response.

protected void
handleErrorPage(HttpServletRequest request, Writer writer, int code, String message)

private void
htmlRow(Writer writer, String tag, Object value)

public boolean
isShowServlet()

Returns: True if the error page will show the Servlet that generated the error

public boolean
isShowStacks()

Returns: True if stack traces are shown in the error pages

public void
setCacheControl(String
the cacheControl header to set on error responses.
cacheControl
)

Set the cacheControl.

public void
setShowMessageInTitle(boolean
if true, the error message appears in page title
showMessageInTitle
)

Set if true, the error message appears in page title.

public void
setShowServlet(boolean
True if the error page will show the Servlet that generated the error
showServlet
)

public void
setShowStacks(boolean
True if stack traces are shown in the error pages
showStacks
)

protected void
write(Writer writer, String string)

protected void
writeErrorJson(HttpServletRequest request, PrintWriter writer, int code, String message)

protected void
writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)

protected void
writeErrorPageBody(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)

protected void
writeErrorPageHead(HttpServletRequest request, Writer writer, int code, String message)

protected void
writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message, String uri)

protected void
protected void
writeErrorPlain(HttpServletRequest request, PrintWriter writer, int code, String message)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

_cacheControlback to summary
pack-priv String _cacheControl
_disableStacksback to summary
pack-priv boolean _disableStacks
_showMessageInTitleback to summary
pack-priv boolean _showMessageInTitle
_showServletback to summary
pack-priv boolean _showServlet
_showStacksback to summary
pack-priv boolean _showStacks
ERROR_CHARSETback to summary
public static final String ERROR_CHARSET
ERROR_CONTEXTback to summary
public static final String ERROR_CONTEXT
ERROR_PAGEback to summary
public static final String ERROR_PAGE
LOGback to summary
private static final Logger LOG

Constructor Detail

ErrorHandlerback to summary
public ErrorHandler()

Method Detail

badMessageErrorback to summary
public ByteBuffer badMessageError(int status, String reason, HttpFields.Mutable fields)

Bad Message Error body

Generate an error response body to be sent for a bad message. In this case there is something wrong with the request, so either a request cannot be built, or it is not safe to build a request. This method allows for a simple error page body to be returned and some response headers to be set.

Parameters
status:int

The error code that will be sent

reason:String

The reason for the error code (may be null)

fields:HttpFields.Mutable

The header fields that will be sent with the response.

Returns:ByteBuffer

The content as a ByteBuffer, or null for no body.

errorPageForMethodback to summary
public boolean errorPageForMethod(String method)
generateAcceptableResponseback to summary
protected void generateAcceptableResponse(ServletContextRequest baseRequest, HttpServletRequest request, HttpServletResponse response, int code, String message) throws IOException

Generate an acceptable error response.

This method is called to generate an Error page of a mime type that is acceptable to the user-agent. The Accept header is evaluated in quality order and the method generateAcceptableResponse(ServletContextRequest, HttpServletRequest, HttpServletResponse, int, String, String) is called for each mimetype until the response is written to or committed.

Parameters
baseRequest:ServletContextRequest

The base request

request:HttpServletRequest

The servlet request (may be wrapped)

response:HttpServletResponse

The response (may be wrapped)

code:int

the http error code

message:String

the http error message

Exceptions
IOException:
if the response cannot be generated
generateAcceptableResponseback to summary
protected void generateAcceptableResponse(ServletContextRequest baseRequest, HttpServletRequest request, HttpServletResponse response, int code, String message, String contentType) throws IOException

Generate an acceptable error response for a mime type.

This method is called for each mime type in the users agent's Accept header, a response of the appropriate type is generated.

The default implementation handles "text/html", "text/*" and "*/*". The method can be overridden to handle other types. Implementations must immediate produce a response and may not be async.

Parameters
baseRequest:ServletContextRequest

The base request

request:HttpServletRequest

The servlet request (may be wrapped)

response:HttpServletResponse

The response (may be wrapped)

code:int

the http error code

message:String

the http error message

contentType:String

The mimetype to generate (may be */*or other wildcard)

Exceptions
IOException:
if a response cannot be generated
getAcceptableWriterback to summary
protected Writer getAcceptableWriter(Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException

Deprecated

Returns an acceptable writer for an error page.

Uses the user-agent's Accept-Charset to get response Writer. The acceptable charsets are tested in quality order if they are known to the JVM and the first known is set on HttpServletResponse#setCharacterEncoding(String) and the HttpServletResponse#getWriter() method used to return a writer. If there is no Accept-Charset header then ISO-8859-1 is used. If '*' is the highest quality known charset, then utf-8 is used.

Parameters
baseRequest:Request

The base request

request:HttpServletRequest

The servlet request (may be wrapped)

response:HttpServletResponse

The response (may be wrapped)

Returns:Writer

A Writer if there is a known acceptable charset or null

Annotations
@Deprecated
Exceptions
IOException:
if a Writer cannot be returned
getCacheControlback to summary
public String getCacheControl()

Get the cacheControl.

Returns:String

the cacheControl header to set on error responses.

getErrorHandlerback to summary
public static Request.Handler getErrorHandler(Server server, ContextHandler context)
getShowMessageInTitleback to summary
public boolean getShowMessageInTitle()
handleback to summary
public boolean handle(Request request, Response response, Callback callback) throws Exception

Implements org.eclipse.jetty.server.Request.Handler.handle.

Doc from org.eclipse.jetty.server.Request.Handler.handle.

Invoked to handle the passed HTTP request and response.

The request is accepted by returning true, then handling must be concluded by completing the passed callback. The handling may be asynchronous, i.e. this method may return true and complete the given callback later, possibly from a different thread. If this method returns false, then the callback must not be invoked and any mutation on the response reversed.

Exceptions thrown by this method may be subsequently handled by an error Request.Handler, if present, otherwise a default HTTP 500 error is generated and the callback completed while writing the error response.

The simplest implementation is:

public boolean handle(Request request, Response response, Callback callback)
{
    callback.succeeded();
    return true;
}

A HelloWorld implementation is:

public boolean handle(Request request, Response response, Callback callback)
{
    response.write(true, ByteBuffer.wrap("Hello World\n".getBytes(StandardCharsets.UTF_8)), callback);
    return true;
}
Parameters
request:Request

the HTTP request to handle

response:Response

the HTTP response to handle

callback:Callback

the callback to complete when the handling is complete

Returns:boolean

True if and only if the request will be handled, a response generated and the callback eventually called. This may occur within the scope of the call to this method, or asynchronously some time later. If false is returned, then this method must not generate a response, nor complete the callback.

Annotations
@Override
Exceptions
Exception:
if there is a failure during the handling. Catchers cannot assume that the callback will be called and thus should attempt to complete the request as if a false had been returned.
handleErrorPageback to summary
protected void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message) throws IOException
htmlRowback to summary
private void htmlRow(Writer writer, String tag, Object value) throws IOException
isShowServletback to summary
public boolean isShowServlet()
Returns:boolean

True if the error page will show the Servlet that generated the error

isShowStacksback to summary
public boolean isShowStacks()
Returns:boolean

True if stack traces are shown in the error pages

setCacheControlback to summary
public void setCacheControl(String cacheControl)

Set the cacheControl.

Parameters
cacheControl:String

the cacheControl header to set on error responses.

setShowMessageInTitleback to summary
public void setShowMessageInTitle(boolean showMessageInTitle)

Set if true, the error message appears in page title.

Parameters
showMessageInTitle:boolean

if true, the error message appears in page title

setShowServletback to summary
public void setShowServlet(boolean showServlet)
Parameters
showServlet:boolean

True if the error page will show the Servlet that generated the error

setShowStacksback to summary
public void setShowStacks(boolean showStacks)
Parameters
showStacks:boolean

True if stack traces are shown in the error pages

writeback to summary
protected void write(Writer writer, String string) throws IOException
writeErrorJsonback to summary
protected void writeErrorJson(HttpServletRequest request, PrintWriter writer, int code, String message)
writeErrorPageback to summary
protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) throws IOException
writeErrorPageBodyback to summary
protected void writeErrorPageBody(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) throws IOException
writeErrorPageHeadback to summary
protected void writeErrorPageHead(HttpServletRequest request, Writer writer, int code, String message) throws IOException
writeErrorPageMessageback to summary
protected void writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message, String uri) throws IOException
writeErrorPageStacksback to summary
protected void writeErrorPageStacks(HttpServletRequest request, Writer writer) throws IOException
writeErrorPlainback to summary
protected void writeErrorPlain(HttpServletRequest request, PrintWriter writer, int code, String message)
org.eclipse.jetty.ee10.servlet back to summary

public Interface ErrorHandler.ErrorPageMapper

Known Direct Implementers
org.eclipse.jetty.ee10.servlet.ErrorPageErrorHandler

Method Summary

Modifier and TypeMethod and Description
public String

Method Detail

getErrorPageback to summary
public String getErrorPage(HttpServletRequest request)