Class ErrorHandler
- java.lang.Object
-
- org.eclipse.jetty.util.component.AbstractLifeCycle
-
- org.eclipse.jetty.util.component.ContainerLifeCycle
-
- org.eclipse.jetty.server.handler.AbstractHandler
-
- org.eclipse.jetty.server.handler.ErrorHandler
-
- All Implemented Interfaces:
Handler
,Container
,Destroyable
,Dumpable
,Dumpable.DumpableContainer
,LifeCycle
- Direct Known Subclasses:
ErrorPageErrorHandler
public class ErrorHandler extends AbstractHandler
Handler for Error pages An ErrorHandler is registered withContextHandler.setErrorHandler(ErrorHandler)
orServer.setErrorHandler(ErrorHandler)
. It is called by the HttpResponse.sendError method to write an error page viahandle(String, Request, HttpServletRequest, HttpServletResponse)
or viabadMessageError(int, String, HttpFields)
for bad requests for which a dispatch cannot be done.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ErrorHandler.ErrorPageMapper
-
Nested classes/interfaces inherited from class org.eclipse.jetty.server.handler.AbstractHandler
AbstractHandler.ErrorDispatchHandler
-
Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListener
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Container
Container.InheritedListener, Container.Listener
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Dumpable
Dumpable.DumpableContainer
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.LifeCycle
LifeCycle.Listener
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
ERROR_CHARSET
static java.lang.String
ERROR_CONTEXT
static java.lang.String
ERROR_PAGE
-
Constructor Summary
Constructors Constructor Description ErrorHandler()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.nio.ByteBuffer
badMessageError(int status, java.lang.String reason, HttpFields fields)
Bad Message Error bodyvoid
doError(java.lang.String target, Request baseRequest, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
Deprecated error page generationboolean
errorPageForMethod(java.lang.String method)
protected void
generateAcceptableResponse(Request baseRequest, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, int code, java.lang.String message)
Generate an acceptable error response.protected void
generateAcceptableResponse(Request baseRequest, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, int code, java.lang.String message, java.lang.String contentType)
Generate an acceptable error response for a mime type.protected java.io.Writer
getAcceptableWriter(Request baseRequest, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
Deprecated.java.lang.String
getCacheControl()
Get the cacheControl.static ErrorHandler
getErrorHandler(Server server, ContextHandler context)
boolean
getShowMessageInTitle()
void
handle(java.lang.String target, Request baseRequest, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
Handle a request.protected void
handleErrorPage(javax.servlet.http.HttpServletRequest request, java.io.Writer writer, int code, java.lang.String message)
boolean
isShowServlet()
boolean
isShowStacks()
void
setCacheControl(java.lang.String cacheControl)
Set the cacheControl.void
setShowMessageInTitle(boolean showMessageInTitle)
void
setShowServlet(boolean showServlet)
void
setShowStacks(boolean showStacks)
protected void
write(java.io.Writer writer, java.lang.String string)
protected void
writeErrorPage(javax.servlet.http.HttpServletRequest request, java.io.Writer writer, int code, java.lang.String message, boolean showStacks)
protected void
writeErrorPageBody(javax.servlet.http.HttpServletRequest request, java.io.Writer writer, int code, java.lang.String message, boolean showStacks)
protected void
writeErrorPageHead(javax.servlet.http.HttpServletRequest request, java.io.Writer writer, int code, java.lang.String message)
protected void
writeErrorPageMessage(javax.servlet.http.HttpServletRequest request, java.io.Writer writer, int code, java.lang.String message, java.lang.String uri)
protected void
writeErrorPageStacks(javax.servlet.http.HttpServletRequest request, java.io.Writer writer)
-
Methods inherited from class org.eclipse.jetty.server.handler.AbstractHandler
destroy, doStart, doStop, getServer, setServer
-
Methods inherited from class org.eclipse.jetty.util.component.ContainerLifeCycle
addBean, addBean, addEventListener, addManaged, contains, dump, dump, dump, dump, dump, dumpBeans, dumpObject, dumpObjects, dumpStdErr, dumpThis, getBean, getBeans, getBeans, getContainedBeans, getContainedBeans, isAuto, isManaged, isUnmanaged, manage, removeBean, removeBeans, removeEventListener, setBeans, setStopTimeout, start, stop, unmanage, updateBean, updateBean, updateBeans
-
Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.eclipse.jetty.util.component.Dumpable.DumpableContainer
isDumpable
-
Methods inherited from interface org.eclipse.jetty.util.component.LifeCycle
addLifeCycleListener, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop
-
-
-
-
Field Detail
-
ERROR_PAGE
public static final java.lang.String ERROR_PAGE
- See Also:
- Constant Field Values
-
ERROR_CONTEXT
public static final java.lang.String ERROR_CONTEXT
- See Also:
- Constant Field Values
-
ERROR_CHARSET
public static final java.lang.String ERROR_CHARSET
- See Also:
- Constant Field Values
-
-
Method Detail
-
errorPageForMethod
public boolean errorPageForMethod(java.lang.String method)
-
handle
public void handle(java.lang.String target, Request baseRequest, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException
Description copied from interface:Handler
Handle a request.- Specified by:
handle
in interfaceHandler
- Specified by:
handle
in classAbstractHandler
- Parameters:
target
- The target of the request - either a URI or a name.baseRequest
- The original unwrapped request object.request
- The request either as theRequest
object or a wrapper of that request. The
method can be used access the Request object if required.HttpConnection.getCurrentConnection()
.getHttpChannel()
.getRequest()
response
- The response as theResponse
object or a wrapper of that request. The
method can be used access the Response object if required.HttpConnection.getCurrentConnection()
.getHttpChannel()
.getResponse()
- Throws:
java.io.IOException
- if unable to handle the request or response processingjavax.servlet.ServletException
- if unable to handle the request or response due to underlying servlet issue
-
doError
public void doError(java.lang.String target, Request baseRequest, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws java.io.IOException
Description copied from class:AbstractHandler
Deprecated error page generation- Overrides:
doError
in classAbstractHandler
- Parameters:
target
- The target of the request - either a URI or a name.baseRequest
- The original unwrapped request object.request
- The request either as theRequest
object or a wrapper of that request. The
method can be used access the Request object if required.HttpConnection.getCurrentConnection()
.getHttpChannel()
.getRequest()
response
- The response as theResponse
object or a wrapper of that request. The
method can be used access the Response object if required.HttpConnection.getCurrentConnection()
.getHttpChannel()
.getResponse()
- Throws:
java.io.IOException
- if unable to handle the request or response processing
-
generateAcceptableResponse
protected void generateAcceptableResponse(Request baseRequest, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, int code, java.lang.String message) throws java.io.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(Request, HttpServletRequest, HttpServletResponse, int, String, String)
is called for each mimetype until the response is written to or committed.- Parameters:
baseRequest
- The base requestrequest
- The servlet request (may be wrapped)response
- The response (may be wrapped)code
- the http error codemessage
- the http error message- Throws:
java.io.IOException
- if the response cannot be generated
-
getAcceptableWriter
@Deprecated protected java.io.Writer getAcceptableWriter(Request baseRequest, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws java.io.IOException
Deprecated.Returns an acceptable writer for an error page.Uses the user-agent's
Accept-Charset
to get responseWriter
. The acceptable charsets are tested in quality order if they are known to the JVM and the first known is set onServletResponse.setCharacterEncoding(String)
and theServletResponse.getWriter()
method used to return a writer. If there is noAccept-Charset
header thenISO-8859-1
is used. If '*' is the highest quality known charset, thenutf-8
is used.- Parameters:
baseRequest
- The base requestrequest
- The servlet request (may be wrapped)response
- The response (may be wrapped)- Returns:
- A
Writer
if there is a known acceptable charset or null - Throws:
java.io.IOException
- if a Writer cannot be returned
-
generateAcceptableResponse
protected void generateAcceptableResponse(Request baseRequest, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, int code, java.lang.String message, java.lang.String contentType) throws java.io.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, untilRequest.isHandled()
is true and 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
- The base requestrequest
- The servlet request (may be wrapped)response
- The response (may be wrapped)code
- the http error codemessage
- the http error messagecontentType
- The mimetype to generate (may be */*or other wildcard)- Throws:
java.io.IOException
- if a response cannot be generated
-
handleErrorPage
protected void handleErrorPage(javax.servlet.http.HttpServletRequest request, java.io.Writer writer, int code, java.lang.String message) throws java.io.IOException
- Throws:
java.io.IOException
-
writeErrorPage
protected void writeErrorPage(javax.servlet.http.HttpServletRequest request, java.io.Writer writer, int code, java.lang.String message, boolean showStacks) throws java.io.IOException
- Throws:
java.io.IOException
-
writeErrorPageHead
protected void writeErrorPageHead(javax.servlet.http.HttpServletRequest request, java.io.Writer writer, int code, java.lang.String message) throws java.io.IOException
- Throws:
java.io.IOException
-
writeErrorPageBody
protected void writeErrorPageBody(javax.servlet.http.HttpServletRequest request, java.io.Writer writer, int code, java.lang.String message, boolean showStacks) throws java.io.IOException
- Throws:
java.io.IOException
-
writeErrorPageMessage
protected void writeErrorPageMessage(javax.servlet.http.HttpServletRequest request, java.io.Writer writer, int code, java.lang.String message, java.lang.String uri) throws java.io.IOException
- Throws:
java.io.IOException
-
writeErrorPageStacks
protected void writeErrorPageStacks(javax.servlet.http.HttpServletRequest request, java.io.Writer writer) throws java.io.IOException
- Throws:
java.io.IOException
-
badMessageError
public java.nio.ByteBuffer badMessageError(int status, java.lang.String reason, HttpFields fields)
Bad Message Error bodyGenerate 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
- The error code that will be sentreason
- The reason for the error code (may be null)fields
- The header fields that will be sent with the response.- Returns:
- The content as a ByteBuffer, or null for no body.
-
getCacheControl
public java.lang.String getCacheControl()
Get the cacheControl.- Returns:
- the cacheControl header to set on error responses.
-
setCacheControl
public void setCacheControl(java.lang.String cacheControl)
Set the cacheControl.- Parameters:
cacheControl
- the cacheControl header to set on error responses.
-
isShowServlet
public boolean isShowServlet()
- Returns:
- True if the error page will show the Servlet that generated the error
-
setShowServlet
public void setShowServlet(boolean showServlet)
- Parameters:
showServlet
- True if the error page will show the Servlet that generated the error
-
isShowStacks
public boolean isShowStacks()
- Returns:
- True if stack traces are shown in the error pages
-
setShowStacks
public void setShowStacks(boolean showStacks)
- Parameters:
showStacks
- True if stack traces are shown in the error pages
-
setShowMessageInTitle
public void setShowMessageInTitle(boolean showMessageInTitle)
- Parameters:
showMessageInTitle
- if true, the error message appears in page title
-
getShowMessageInTitle
public boolean getShowMessageInTitle()
-
write
protected void write(java.io.Writer writer, java.lang.String string) throws java.io.IOException
- Throws:
java.io.IOException
-
getErrorHandler
public static ErrorHandler getErrorHandler(Server server, ContextHandler context)
-
-