Top Description Inners Fields Constructors Methods
org.eclipse.jetty.server.handler

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.server.handler.ReHandlingErrorHandler, org.eclipse.jetty.server.Server.DynamicErrorHandler
Annotations
@ManagedObject
Imports
java.io.IOException, .OutputStreamWriter, .PrintWriter, .StringWriter, .Writer, java.nio.BufferOverflowException, .ByteBuffer, java.nio.charset.Charset, .StandardCharsets, java.util.Collections, .HashMap, .List, .Map, .Objects, .Set, java.util.stream.Collectors, org.eclipse.jetty.http.HttpException, .HttpField, .HttpFields, .HttpHeader, .HttpStatus, .MimeTypes.Type, .PreEncodedHttpField, .QuotedQualityCSV, org.eclipse.jetty.io.ByteBufferOutputStream, .Content, .Retainable, .RetainableByteBuffer, org.eclipse.jetty.server.Request, .Response, .Server, org.eclipse.jetty.util.Attributes, .BufferUtil, .Callback, .StringUtil, org.eclipse.jetty.util.annotation.ManagedAttribute, .ManagedObject, org.slf4j.Logger, .LoggerFactory

Handler for Error pages An ErrorHandler is registered with Server#setErrorHandler(Request.Handler). It is called by the Response#writeError(Request, Response, Callback, int, String) to generate an error page.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
private static class

Field Summary

Modifier and TypeField and Description
pack-priv HttpField
pack-priv boolean
pack-priv boolean
public static final HttpField
public static final String
public static final String
public static final String
public static final Set<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 boolean
generateAcceptableResponse(Request request, Response response, Callback callback, String contentType, List<Charset> charsets, int code, String message, Throwable cause)

protected void
generateResponse(Request request, Response response, int code, String message, Throwable cause, Callback callback)

public String

Returns:

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

Get the cacheControl.

public static Request.Handler
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.

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

public boolean
public boolean

Returns:

True if stack traces are shown in the error pages
isShowStacks
()

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
setShowStacks(boolean
True if stack traces are shown in the error pages
showStacks
)

protected void
write(Writer writer, String string)

protected void
writeErrorHtml(Request request, Writer writer, Charset charset, int code, String message, Throwable cause, boolean showStacks)

protected void
writeErrorHtmlBody(Request request, Writer writer, int code, String message, Throwable cause, boolean showStacks)

protected void
writeErrorHtmlHead(Request request, Writer writer, int code, String message)

protected void
writeErrorHtmlMessage(Request request, Writer writer, int code, String message, Throwable cause, String uri)

protected void
writeErrorHtmlMeta(Request request, Writer writer, Charset charset)

protected void
protected void
writeErrorJson(Request request, PrintWriter writer, int code, String message, Throwable cause, boolean showStacks)

protected void
writeErrorPlain(Request request, PrintWriter writer, int code, String message, Throwable cause, boolean showStacks)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

_cacheControlback to summary
pack-priv HttpField _cacheControl
_showMessageInTitleback to summary
pack-priv boolean _showMessageInTitle
_showStacksback to summary
pack-priv boolean _showStacks
ERROR_CACHE_CONTROLback to summary
public static final HttpField ERROR_CACHE_CONTROL
ERROR_CONTEXTback to summary
public static final String ERROR_CONTEXT
ERROR_EXCEPTIONback to summary
public static final String ERROR_EXCEPTION
ERROR_MESSAGEback to summary
public static final String ERROR_MESSAGE
ERROR_METHODSback to summary
public static final Set<String> ERROR_METHODS
ERROR_STATUSback to summary
public static final String ERROR_STATUS
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 boolean generateAcceptableResponse(Request request, Response response, Callback callback, String contentType, List<Charset> charsets, int code, String message, Throwable cause) throws IOException
generateResponseback to summary
protected void generateResponse(Request request, Response response, int code, String message, Throwable cause, Callback callback) throws IOException
getCacheControlback to summary
public String getCacheControl()

Get the cacheControl.

Returns:String

the cacheControl header to set on error responses.

Annotations
@ManagedAttribute:The value of the Cache-Control response header
getErrorHandlerback to summary
public static Request.Handler getErrorHandler(Server server, ContextHandler context)
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.
htmlRowback to summary
private void htmlRow(Writer writer, String tag, Object value) throws IOException
isShowMessageInTitleback to summary
public boolean isShowMessageInTitle()
Annotations
@ManagedAttribute:Whether the error message is shown in the error page title
isShowStacksback to summary
public boolean isShowStacks()
Returns:boolean

True if stack traces are shown in the error pages

Annotations
@ManagedAttribute:Whether the error page shows the stack trace
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

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
writeErrorHtmlback to summary
protected void writeErrorHtml(Request request, Writer writer, Charset charset, int code, String message, Throwable cause, boolean showStacks) throws IOException
writeErrorHtmlBodyback to summary
protected void writeErrorHtmlBody(Request request, Writer writer, int code, String message, Throwable cause, boolean showStacks) throws IOException
writeErrorHtmlHeadback to summary
protected void writeErrorHtmlHead(Request request, Writer writer, int code, String message) throws IOException
writeErrorHtmlMessageback to summary
protected void writeErrorHtmlMessage(Request request, Writer writer, int code, String message, Throwable cause, String uri) throws IOException
writeErrorHtmlMetaback to summary
protected void writeErrorHtmlMeta(Request request, Writer writer, Charset charset) throws IOException
writeErrorHtmlStacksback to summary
protected void writeErrorHtmlStacks(Request request, Writer writer) throws IOException
writeErrorJsonback to summary
protected void writeErrorJson(Request request, PrintWriter writer, int code, String message, Throwable cause, boolean showStacks)
writeErrorPlainback to summary
protected void writeErrorPlain(Request request, PrintWriter writer, int code, String message, Throwable cause, boolean showStacks)
org.eclipse.jetty.server.handler back to summary

public Class ErrorHandler.ErrorRequest

extends AttributesWrapper
Class Inheritance

Field Summary

Modifier and TypeField and Description
private static final Set<String>

Constructor Summary

AccessConstructor and Description
public
ErrorRequest(Request request, int status, String message, Throwable cause)

Method Summary

Modifier and TypeMethod and Description
public void
demand(Runnable
the demand callback to invoke when there is a content chunk available.
demandCallback
)

Overrides org.eclipse.jetty.server.Request.Wrapper.demand.

Implements org.eclipse.jetty.server.Request.demand.

Demands to invoke the given demand callback parameter when a chunk of content is available.

public Content.Chunk
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from org.eclipse.jetty.server.Request.AttributesWrapper:
asAttributeMapclearAttributesgetAttributegetAttributeNameSetremoveAttributesetAttribute

Field Detail

ATTRIBUTESback to summary
private static final Set<String> ATTRIBUTES

Constructor Detail

ErrorRequestback to summary
public ErrorRequest(Request request, int status, String message, Throwable cause)

Method Detail

demandback to summary
public void demand(Runnable demandCallback)

Overrides org.eclipse.jetty.server.Request.Wrapper.demand.

Implements org.eclipse.jetty.server.Request.demand.

Doc from org.eclipse.jetty.server.Request.demand.

Demands to invoke the given demand callback parameter when a chunk of content is available.

See how to use this method idiomatically.

Implementations guarantee that calls to this method are safely reentrant so that stack overflows are avoided in the case of mutual recursion between the execution of the Runnable callback and a call to this method. Invocations of the passed Runnable are serialized and a callback for demand call is not invoked until any previous demand callback has returned. Thus the Runnable should not block waiting for a callback of a future demand call.

The demand callback may be invoked spuriously: a subsequent call to read() may return null.

Calling this method establishes a pending demand, which is fulfilled when the demand callback is invoked.

Calling this method when there is already a pending demand results in an IllegalStateException to be thrown.

If the invocation of the demand callback throws an exception, then fail(Throwable) is called.

Parameters
demandCallback:Runnable

the demand callback to invoke when there is a content chunk available.

Annotations
@Override
readback to summary
public Content.Chunk read()

Overrides org.eclipse.jetty.server.Request.Wrapper.read.

Implements org.eclipse.jetty.server.Request.read.

Doc from org.eclipse.jetty.server.Request.read.

Reads a chunk of content.

See how to use this method idiomatically.

The returned chunk could be:

  • null, to signal that there isn't a chunk of content available
  • an Chunk instance with non null Chunk#getFailure(), to signal that there was a failure trying to produce a chunk of content, or that the content production has been failed externally
  • a Chunk instance, containing the chunk of content.

Once a read returns an Chunk instance with non-null Chunk#getFailure() then if the failure is last further reads will continue to return the same failure chunk instance, otherwise further read() operations may return different non-failure chunks.

Once a read returns a last chunk, further reads will continue to return a last chunk (although the instance may be different).

The content reader code must ultimately arrange for a call to Chunk#release() on the returned Chunk.

Additionally, prior to the ultimate call to Chunk#release(), the reader code may make additional calls to Chunk#retain(), that must ultimately be matched by a correspondent number of calls to Chunk#release().

Concurrent reads from different threads are not recommended, as they are inherently in a race condition.

Reads performed outside the invocation context of a demand callback are allowed. However, reads performed with a pending demand are inherently in a race condition (the thread that reads with the thread that invokes the demand callback).

In addition, the returned Content.Chunk may be a Trailers instance, in case of request content trailers.

Returns:Content.Chunk

Doc from org.eclipse.jetty.io.Content.Source.read.

a chunk of content, possibly a failure instance, or null

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
org.eclipse.jetty.server.handler back to summary

private Class ErrorHandler.WriteErrorCallback

extends Nested
Class Inheritance

The callback used by ErrorHandler#generateAcceptableResponse(Request, Response, Callback, String, List, int, String, Throwable) when calling Response#write(boolean, ByteBuffer, Callback) to wrap the passed in Callback so that the RetainableByteBuffer used can be released.

Field Summary

Modifier and TypeField and Description
private final Retainable

Constructor Summary

AccessConstructor and Description
public
WriteErrorCallback(Callback callback, Retainable retainable)

Method Summary

Modifier and TypeMethod and Description
public void
Inherited from org.eclipse.jetty.util.Callback.Nested:
failedgetCallbackgetInvocationTypesucceededtoString

Field Detail

_retainableback to summary
private final Retainable _retainable

Constructor Detail

WriteErrorCallbackback to summary
public WriteErrorCallback(Callback callback, Retainable retainable)

Method Detail

completedback to summary
public void completed()

Overrides org.eclipse.jetty.util.Callback.Nested.completed.

Implements org.eclipse.jetty.util.Callback.Completing.completed.

Annotations
@Override