Merge remote-tracking branch 'origin/jetty-9.3.x'
This commit is contained in:
commit
779317dc30
|
@ -35,9 +35,7 @@ import org.eclipse.jetty.http.HttpHeader;
|
||||||
import org.eclipse.jetty.http.HttpMethod;
|
import org.eclipse.jetty.http.HttpMethod;
|
||||||
import org.eclipse.jetty.http.HttpStatus;
|
import org.eclipse.jetty.http.HttpStatus;
|
||||||
import org.eclipse.jetty.http.MimeTypes;
|
import org.eclipse.jetty.http.MimeTypes;
|
||||||
import org.eclipse.jetty.server.AsyncContextEvent;
|
|
||||||
import org.eclipse.jetty.server.Dispatcher;
|
import org.eclipse.jetty.server.Dispatcher;
|
||||||
import org.eclipse.jetty.server.HttpOutput;
|
|
||||||
import org.eclipse.jetty.server.Request;
|
import org.eclipse.jetty.server.Request;
|
||||||
import org.eclipse.jetty.server.Response;
|
import org.eclipse.jetty.server.Response;
|
||||||
import org.eclipse.jetty.server.Server;
|
import org.eclipse.jetty.server.Server;
|
||||||
|
@ -47,23 +45,27 @@ import org.eclipse.jetty.util.StringUtil;
|
||||||
import org.eclipse.jetty.util.log.Log;
|
import org.eclipse.jetty.util.log.Log;
|
||||||
import org.eclipse.jetty.util.log.Logger;
|
import org.eclipse.jetty.util.log.Logger;
|
||||||
|
|
||||||
/**
|
/* ------------------------------------------------------------ */
|
||||||
* <p>Component that handles Error Pages.</p>
|
/** Handler for Error pages
|
||||||
* <p>An ErrorHandler is registered with {@link ContextHandler#setErrorHandler(ErrorHandler)} or
|
* An ErrorHandler is registered with {@link ContextHandler#setErrorHandler(ErrorHandler)} or
|
||||||
* {@link org.eclipse.jetty.server.Server#addBean(Object)}.</p>
|
* {@link org.eclipse.jetty.server.Server#addBean(Object)}.
|
||||||
* <p>It is called by {@link HttpServletResponse#sendError(int)} to write an error page via
|
* It is called by the HttpResponse.sendError method to write a error page via {@link #handle(String, Request, HttpServletRequest, HttpServletResponse)}
|
||||||
* {@link #handle(String, Request, HttpServletRequest, HttpServletResponse)}
|
* or via {@link #badMessageError(int, String, HttpFields)} for bad requests for which a dispatch cannot be done.
|
||||||
* or via {@link #badMessageError(int, String, HttpFields)} for bad requests for which a
|
*
|
||||||
* dispatch cannot be done.</p>
|
|
||||||
*/
|
*/
|
||||||
public class ErrorHandler extends AbstractHandler
|
public class ErrorHandler extends AbstractHandler
|
||||||
{
|
{
|
||||||
private static final Logger LOG = Log.getLogger(ErrorHandler.class);
|
private static final Logger LOG = Log.getLogger(ErrorHandler.class);
|
||||||
|
public final static String ERROR_PAGE="org.eclipse.jetty.server.error_page";
|
||||||
|
|
||||||
|
boolean _showStacks=true;
|
||||||
|
boolean _showMessageInTitle=true;
|
||||||
|
String _cacheControl="must-revalidate,no-cache,no-store";
|
||||||
|
|
||||||
private boolean _showStacks = true;
|
/* ------------------------------------------------------------ */
|
||||||
private boolean _showMessageInTitle = true;
|
/*
|
||||||
private String _cacheControl = "must-revalidate,no-cache,no-store";
|
* @see org.eclipse.jetty.server.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
|
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
|
||||||
{
|
{
|
||||||
|
@ -73,31 +75,31 @@ public class ErrorHandler extends AbstractHandler
|
||||||
baseRequest.setHandled(true);
|
baseRequest.setHandled(true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this instanceof ErrorPageMapper)
|
if (this instanceof ErrorPageMapper)
|
||||||
{
|
{
|
||||||
String error_page = ((ErrorPageMapper)this).getErrorPage(request);
|
String error_page=((ErrorPageMapper)this).getErrorPage(request);
|
||||||
|
if (error_page!=null && request.getServletContext()!=null)
|
||||||
ServletContext context = request.getServletContext();
|
|
||||||
if (context == null)
|
|
||||||
{
|
{
|
||||||
AsyncContextEvent event = baseRequest.getHttpChannelState().getAsyncContextEvent();
|
String old_error_page=(String)request.getAttribute(ERROR_PAGE);
|
||||||
context = event == null ? null : event.getServletContext();
|
if (old_error_page==null || !old_error_page.equals(error_page))
|
||||||
}
|
|
||||||
|
|
||||||
if (error_page != null && context != null)
|
|
||||||
{
|
|
||||||
Dispatcher dispatcher = (Dispatcher)context.getRequestDispatcher(error_page);
|
|
||||||
if (dispatcher != null)
|
|
||||||
{
|
{
|
||||||
|
request.setAttribute(ERROR_PAGE, error_page);
|
||||||
|
|
||||||
|
Dispatcher dispatcher = (Dispatcher) request.getServletContext().getRequestDispatcher(error_page);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
dispatcher.error(request, response);
|
if(dispatcher!=null)
|
||||||
return;
|
{
|
||||||
|
dispatcher.error(request, response);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
LOG.warn("No error page "+error_page);
|
||||||
}
|
}
|
||||||
catch (ServletException x)
|
catch (ServletException e)
|
||||||
{
|
{
|
||||||
throw new IOException(x);
|
LOG.warn(Log.EXCEPTION, e);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -105,124 +107,112 @@ public class ErrorHandler extends AbstractHandler
|
||||||
{
|
{
|
||||||
LOG.debug("No Error Page mapping for request({} {}) (using default)",request.getMethod(),request.getRequestURI());
|
LOG.debug("No Error Page mapping for request({} {}) (using default)",request.getMethod(),request.getRequestURI());
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
LOG.warn("Could not dispatch to error page: {}", error_page);
|
|
||||||
// Fall through to provide the default error page.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
baseRequest.setHandled(true);
|
|
||||||
|
|
||||||
HttpOutput out = baseRequest.getResponse().getHttpOutput();
|
baseRequest.setHandled(true);
|
||||||
if (!out.isAsync())
|
response.setContentType(MimeTypes.Type.TEXT_HTML_8859_1.asString());
|
||||||
{
|
if (_cacheControl!=null)
|
||||||
response.setContentType(MimeTypes.Type.TEXT_HTML_8859_1.asString());
|
response.setHeader(HttpHeader.CACHE_CONTROL.asString(), _cacheControl);
|
||||||
String cacheHeader = getCacheControl();
|
ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(4096);
|
||||||
if (cacheHeader != null)
|
String reason=(response instanceof Response)?((Response)response).getReason():null;
|
||||||
response.setHeader(HttpHeader.CACHE_CONTROL.asString(), cacheHeader);
|
handleErrorPage(request, writer, response.getStatus(), reason);
|
||||||
ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(4096);
|
writer.flush();
|
||||||
String reason = (response instanceof Response) ? ((Response)response).getReason() : null;
|
response.setContentLength(writer.size());
|
||||||
handleErrorPage(request, writer, response.getStatus(), reason);
|
writer.writeTo(response.getOutputStream());
|
||||||
writer.flush();
|
writer.destroy();
|
||||||
response.setContentLength(writer.size());
|
|
||||||
writer.writeTo(response.getOutputStream());
|
|
||||||
writer.destroy();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
protected void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message)
|
protected void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
writeErrorPage(request, writer, code, message, isShowStacks());
|
writeErrorPage(request, writer, code, message, _showStacks);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)
|
protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
if (message == null)
|
if (message == null)
|
||||||
message = HttpStatus.getMessage(code);
|
message=HttpStatus.getMessage(code);
|
||||||
|
|
||||||
writer.write("<html>\n<head>\n");
|
writer.write("<html>\n<head>\n");
|
||||||
writeErrorPageHead(request, writer, code, message);
|
writeErrorPageHead(request,writer,code,message);
|
||||||
writer.write("</head>\n<body>");
|
writer.write("</head>\n<body>");
|
||||||
writeErrorPageBody(request, writer, code, message, showStacks);
|
writeErrorPageBody(request,writer,code,message,showStacks);
|
||||||
writer.write("\n</body>\n</html>\n");
|
writer.write("\n</body>\n</html>\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
protected void writeErrorPageHead(HttpServletRequest request, Writer writer, int code, String message)
|
protected void writeErrorPageHead(HttpServletRequest request, Writer writer, int code, String message)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
writer.write("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/>\n");
|
writer.write("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/>\n");
|
||||||
writer.write("<title>Error ");
|
writer.write("<title>Error ");
|
||||||
writer.write(Integer.toString(code));
|
writer.write(Integer.toString(code));
|
||||||
|
|
||||||
if (getShowMessageInTitle())
|
if (_showMessageInTitle)
|
||||||
{
|
{
|
||||||
writer.write(' ');
|
writer.write(' ');
|
||||||
write(writer, message);
|
write(writer,message);
|
||||||
}
|
}
|
||||||
writer.write("</title>\n");
|
writer.write("</title>\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
protected void writeErrorPageBody(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)
|
protected void writeErrorPageBody(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
String uri = request.getRequestURI();
|
String uri= request.getRequestURI();
|
||||||
|
|
||||||
writeErrorPageMessage(request, writer, code, message, uri);
|
writeErrorPageMessage(request,writer,code,message,uri);
|
||||||
if (showStacks)
|
if (showStacks)
|
||||||
writeErrorPageStacks(request, writer);
|
writeErrorPageStacks(request,writer);
|
||||||
|
|
||||||
Request.getBaseRequest(request).getHttpChannel().getHttpConfiguration()
|
Request.getBaseRequest(request).getHttpChannel().getHttpConfiguration()
|
||||||
.writePoweredBy(writer, "<hr>", "<hr/>\n");
|
.writePoweredBy(writer,"<hr>","<hr/>\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
protected void writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message, String uri)
|
protected void writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message,String uri)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
writer.write("<h2>HTTP ERROR ");
|
writer.write("<h2>HTTP ERROR ");
|
||||||
writer.write(Integer.toString(code));
|
writer.write(Integer.toString(code));
|
||||||
writer.write("</h2>\n<p>Problem accessing ");
|
writer.write("</h2>\n<p>Problem accessing ");
|
||||||
write(writer, uri);
|
write(writer,uri);
|
||||||
writer.write(". Reason:\n<pre> ");
|
writer.write(". Reason:\n<pre> ");
|
||||||
write(writer, message);
|
write(writer,message);
|
||||||
writer.write("</pre></p>");
|
writer.write("</pre></p>");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
protected void writeErrorPageStacks(HttpServletRequest request, Writer writer)
|
protected void writeErrorPageStacks(HttpServletRequest request, Writer writer)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
Throwable th = (Throwable)request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
|
Throwable th = (Throwable)request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
|
||||||
while (th != null)
|
while(th!=null)
|
||||||
{
|
{
|
||||||
writer.write("<h3>Caused by:</h3><pre>");
|
writer.write("<h3>Caused by:</h3><pre>");
|
||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
PrintWriter pw = new PrintWriter(sw);
|
PrintWriter pw = new PrintWriter(sw);
|
||||||
th.printStackTrace(pw);
|
th.printStackTrace(pw);
|
||||||
pw.flush();
|
pw.flush();
|
||||||
write(writer, sw.getBuffer().toString());
|
write(writer,sw.getBuffer().toString());
|
||||||
writer.write("</pre>\n");
|
writer.write("</pre>\n");
|
||||||
|
|
||||||
th = th.getCause();
|
th =th.getCause();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
/**
|
/** Bad Message Error body
|
||||||
* <p>Generate a error response body to be sent for a bad message.</p>
|
* <p>Generate a error response body to be sent for a bad message.
|
||||||
* <p>In this case there is something wrong with the request, so either
|
* 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.
|
* 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
|
* This method allows for a simple error page body to be returned
|
||||||
* and some response headers to be set.</p>
|
* and some response headers to be set.
|
||||||
*
|
|
||||||
* @param status The error code that will be sent
|
* @param status The error code that will be sent
|
||||||
* @param reason The reason for the error code (may be null)
|
* @param reason The reason for the error code (may be null)
|
||||||
* @param fields The header fields that will be sent with the response.
|
* @param fields The header fields that will be sent with the response.
|
||||||
|
@ -230,14 +220,14 @@ public class ErrorHandler extends AbstractHandler
|
||||||
*/
|
*/
|
||||||
public ByteBuffer badMessageError(int status, String reason, HttpFields fields)
|
public ByteBuffer badMessageError(int status, String reason, HttpFields fields)
|
||||||
{
|
{
|
||||||
if (reason == null)
|
if (reason==null)
|
||||||
reason = HttpStatus.getMessage(status);
|
reason=HttpStatus.getMessage(status);
|
||||||
fields.put(HttpHeader.CONTENT_TYPE, MimeTypes.Type.TEXT_HTML_8859_1.asString());
|
fields.put(HttpHeader.CONTENT_TYPE,MimeTypes.Type.TEXT_HTML_8859_1.asString());
|
||||||
return BufferUtil.toBuffer("<h1>Bad Message " + status + "</h1><pre>reason: " + reason + "</pre>");
|
return BufferUtil.toBuffer("<h1>Bad Message " + status + "</h1><pre>reason: " + reason + "</pre>");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
/**
|
/** Get the cacheControl.
|
||||||
* @return the cacheControl header to set on error responses.
|
* @return the cacheControl header to set on error responses.
|
||||||
*/
|
*/
|
||||||
public String getCacheControl()
|
public String getCacheControl()
|
||||||
|
@ -246,7 +236,7 @@ public class ErrorHandler extends AbstractHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
/**
|
/** Set the cacheControl.
|
||||||
* @param cacheControl the cacheControl header to set on error responses.
|
* @param cacheControl the cacheControl header to set on error responses.
|
||||||
*/
|
*/
|
||||||
public void setCacheControl(String cacheControl)
|
public void setCacheControl(String cacheControl)
|
||||||
|
@ -256,7 +246,7 @@ public class ErrorHandler extends AbstractHandler
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
/**
|
/**
|
||||||
* @return whether stack traces are shown in the error pages
|
* @return True if stack traces are shown in the error pages
|
||||||
*/
|
*/
|
||||||
public boolean isShowStacks()
|
public boolean isShowStacks()
|
||||||
{
|
{
|
||||||
|
@ -265,7 +255,7 @@ public class ErrorHandler extends AbstractHandler
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
/**
|
/**
|
||||||
* @param showStacks whether stack traces are shown in the error pages
|
* @param showStacks True if stack traces are shown in the error pages
|
||||||
*/
|
*/
|
||||||
public void setShowStacks(boolean showStacks)
|
public void setShowStacks(boolean showStacks)
|
||||||
{
|
{
|
||||||
|
@ -274,27 +264,25 @@ public class ErrorHandler extends AbstractHandler
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
/**
|
/**
|
||||||
* @return whether the error message appears in page title
|
* @param showMessageInTitle if true, the error message appears in page title
|
||||||
*/
|
|
||||||
public boolean getShowMessageInTitle()
|
|
||||||
{
|
|
||||||
return _showMessageInTitle;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
|
||||||
/**
|
|
||||||
* @param showMessageInTitle whether the error message appears in page title
|
|
||||||
*/
|
*/
|
||||||
public void setShowMessageInTitle(boolean showMessageInTitle)
|
public void setShowMessageInTitle(boolean showMessageInTitle)
|
||||||
{
|
{
|
||||||
_showMessageInTitle = showMessageInTitle;
|
_showMessageInTitle = showMessageInTitle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
protected void write(Writer writer, String string)
|
public boolean getShowMessageInTitle()
|
||||||
throws IOException
|
|
||||||
{
|
{
|
||||||
if (string == null)
|
return _showMessageInTitle;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------ */
|
||||||
|
protected void write(Writer writer,String string)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
if (string==null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
writer.write(StringUtil.sanitizeXmlString(string));
|
writer.write(StringUtil.sanitizeXmlString(string));
|
||||||
|
@ -309,22 +297,11 @@ public class ErrorHandler extends AbstractHandler
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
public static ErrorHandler getErrorHandler(Server server, ContextHandler context)
|
public static ErrorHandler getErrorHandler(Server server, ContextHandler context)
|
||||||
{
|
{
|
||||||
ErrorHandler error_handler = null;
|
ErrorHandler error_handler=null;
|
||||||
if (context != null)
|
if (context!=null)
|
||||||
error_handler = context.getErrorHandler();
|
error_handler=context.getErrorHandler();
|
||||||
if (error_handler == null)
|
if (error_handler==null && server!=null)
|
||||||
{
|
error_handler = server.getBean(ErrorHandler.class);
|
||||||
synchronized (ErrorHandler.class)
|
|
||||||
{
|
|
||||||
error_handler = server.getBean(ErrorHandler.class);
|
|
||||||
if (error_handler == null)
|
|
||||||
{
|
|
||||||
error_handler = new ErrorHandler();
|
|
||||||
error_handler.setServer(server);
|
|
||||||
server.addManaged(error_handler);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return error_handler;
|
return error_handler;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,6 +43,7 @@ import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainer
|
||||||
import org.eclipse.jetty.websocket.server.WebSocketServerFactory;
|
import org.eclipse.jetty.websocket.server.WebSocketServerFactory;
|
||||||
import org.junit.AfterClass;
|
import org.junit.AfterClass;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class SessionTrackingTest
|
public class SessionTrackingTest
|
||||||
|
@ -129,6 +130,7 @@ public class SessionTrackingTest
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Ignore
|
||||||
public void testAddRemoveSessions() throws Exception
|
public void testAddRemoveSessions() throws Exception
|
||||||
{
|
{
|
||||||
// Create Client
|
// Create Client
|
||||||
|
|
Loading…
Reference in New Issue