Mercurial Hosting > luan
changeset 911:cab5830e1ab0
remove ErrorHandler
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 08 Oct 2016 21:37:13 -0600 |
parents | 9b65e8064f90 |
children | 1d0c304e12b5 |
files | src/org/eclipse/jetty/server/AbstractHttpConnection.java src/org/eclipse/jetty/server/Response.java src/org/eclipse/jetty/server/handler/ContextHandler.java src/org/eclipse/jetty/server/handler/ErrorHandler.java |
diffstat | 4 files changed, 35 insertions(+), 343 deletions(-) [+] |
line wrap: on
line diff
--- a/src/org/eclipse/jetty/server/AbstractHttpConnection.java Sat Oct 08 21:30:32 2016 -0600 +++ b/src/org/eclipse/jetty/server/AbstractHttpConnection.java Sat Oct 08 21:37:13 2016 -0600 @@ -53,7 +53,6 @@ import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.io.EofException; import org.eclipse.jetty.io.RuntimeIOException; -import org.eclipse.jetty.server.handler.ErrorHandler; import org.eclipse.jetty.util.QuotedStringTokenizer; import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.util.URIUtil;
--- a/src/org/eclipse/jetty/server/Response.java Sat Oct 08 21:30:32 2016 -0600 +++ b/src/org/eclipse/jetty/server/Response.java Sat Oct 08 21:37:13 2016 -0600 @@ -46,7 +46,6 @@ import org.eclipse.jetty.http.MimeTypes; import org.eclipse.jetty.io.BufferCache.CachedBuffer; import org.eclipse.jetty.server.handler.ContextHandler; -import org.eclipse.jetty.server.handler.ErrorHandler; import org.eclipse.jetty.util.ByteArrayISO8859Writer; import org.eclipse.jetty.util.QuotedStringTokenizer; import org.eclipse.jetty.util.StringUtil; @@ -247,63 +246,47 @@ { Request request = _connection.getRequest(); - ErrorHandler error_handler = null; - ContextHandler.Context context = request.getContext(); - if (context!=null) - error_handler=context.getContextHandler().getErrorHandler(); - if (error_handler==null) - error_handler = _connection.getConnector().server.getBean(ErrorHandler.class); - if (error_handler!=null) + setHeader(HttpHeaders.CACHE_CONTROL, "must-revalidate,no-cache,no-store"); + setContentType(MimeTypes.TEXT_HTML_8859_1); + ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(2048); + if (message != null) { - request.setAttribute(RequestDispatcher.ERROR_STATUS_CODE,new Integer(code)); - request.setAttribute(RequestDispatcher.ERROR_MESSAGE, message); - request.setAttribute(RequestDispatcher.ERROR_REQUEST_URI, request.getRequestURI()); - error_handler.handle(null,_connection.getRequest(),_connection.getRequest(),this ); + message= StringUtil.replace(message, "&", "&"); + message= StringUtil.replace(message, "<", "<"); + message= StringUtil.replace(message, ">", ">"); } - else + String uri= request.getRequestURI(); + if (uri!=null) { - setHeader(HttpHeaders.CACHE_CONTROL, "must-revalidate,no-cache,no-store"); - setContentType(MimeTypes.TEXT_HTML_8859_1); - ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(2048); - if (message != null) - { - message= StringUtil.replace(message, "&", "&"); - message= StringUtil.replace(message, "<", "<"); - message= StringUtil.replace(message, ">", ">"); - } - String uri= request.getRequestURI(); - if (uri!=null) - { - uri= StringUtil.replace(uri, "&", "&"); - uri= StringUtil.replace(uri, "<", "<"); - uri= StringUtil.replace(uri, ">", ">"); - } + uri= StringUtil.replace(uri, "&", "&"); + uri= StringUtil.replace(uri, "<", "<"); + uri= StringUtil.replace(uri, ">", ">"); + } - writer.write("<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html;charset=ISO-8859-1\"/>\n"); - writer.write("<title>Error "); - writer.write(Integer.toString(code)); - writer.write(' '); - if (message==null) - message=HttpStatus.getMessage(code); - writer.write(message); - writer.write("</title>\n</head>\n<body>\n<h2>HTTP ERROR: "); - writer.write(Integer.toString(code)); - writer.write("</h2>\n<p>Problem accessing "); - writer.write(uri); - writer.write(". Reason:\n<pre> "); - writer.write(message); - writer.write("</pre>"); - writer.write("</p>\n<hr /><i><small>Powered by Jetty://</small></i>"); + writer.write("<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html;charset=ISO-8859-1\"/>\n"); + writer.write("<title>Error "); + writer.write(Integer.toString(code)); + writer.write(' '); + if (message==null) + message=HttpStatus.getMessage(code); + writer.write(message); + writer.write("</title>\n</head>\n<body>\n<h2>HTTP ERROR: "); + writer.write(Integer.toString(code)); + writer.write("</h2>\n<p>Problem accessing "); + writer.write(uri); + writer.write(". Reason:\n<pre> "); + writer.write(message); + writer.write("</pre>"); + writer.write("</p>\n<hr /><i><small>Powered by Jetty://</small></i>"); - for (int i= 0; i < 20; i++) - writer.write("\n "); - writer.write("\n</body>\n</html>\n"); + for (int i= 0; i < 20; i++) + writer.write("\n "); + writer.write("\n</body>\n</html>\n"); - writer.flush(); - setContentLength(writer.size()); - writer.writeTo(getOutputStream()); - writer.destroy(); - } + writer.flush(); + setContentLength(writer.size()); + writer.writeTo(getOutputStream()); + writer.destroy(); } else if (code!=SC_PARTIAL_CONTENT) {
--- a/src/org/eclipse/jetty/server/handler/ContextHandler.java Sat Oct 08 21:30:32 2016 -0600 +++ b/src/org/eclipse/jetty/server/handler/ContextHandler.java Sat Oct 08 21:37:13 2016 -0600 @@ -132,7 +132,6 @@ private MimeTypes _mimeTypes; private Map<String, String> _localeEncodingMap; private String[] _welcomeFiles; - private ErrorHandler _errorHandler; private String[] _vhosts; private Set<String> _connectors; private EventListener[] _eventListeners; @@ -245,19 +244,6 @@ } /* ------------------------------------------------------------ */ - @Override - public void setServer(Server server) - { - if (_errorHandler != null) - { - super.setServer(server); - _errorHandler.setServer(server); - } - else - super.setServer(server); - } - - /* ------------------------------------------------------------ */ /** * Set the virtual hosts for the context. Only requests that have a matching host header or fully qualified URL will be passed to that context with a * virtual host name. A context with no virtual host names or a null virtual host name is available to all requests that are not served by a context with a @@ -755,9 +741,6 @@ super.doStart(); - if (_errorHandler != null) - _errorHandler.start(); - // Context listeners if (_contextListeners != null) { @@ -821,9 +804,6 @@ setEventListeners((EventListener[])LazyList.toArray(_durableListeners, EventListener.class)); _durableListeners = null; - if (_errorHandler != null) - _errorHandler.stop(); - Enumeration e = _scontext.getAttributeNames(); while (e.hasMoreElements()) { @@ -1399,27 +1379,6 @@ } /* ------------------------------------------------------------ */ - /** - * @return Returns the errorHandler. - */ - public ErrorHandler getErrorHandler() - { - return _errorHandler; - } - - /* ------------------------------------------------------------ */ - /** - * @param errorHandler - * The errorHandler to set. - */ - public void setErrorHandler(ErrorHandler errorHandler) - { - if (errorHandler != null) - errorHandler.setServer(getServer()); - _errorHandler = errorHandler; - } - - /* ------------------------------------------------------------ */ public int getMaxFormContentSize() { return _maxFormContentSize;
--- a/src/org/eclipse/jetty/server/handler/ErrorHandler.java Sat Oct 08 21:30:32 2016 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,249 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.server.handler; - -import java.io.IOException; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.io.Writer; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.eclipse.jetty.http.HttpHeaders; -import org.eclipse.jetty.http.HttpMethods; -import org.eclipse.jetty.http.HttpStatus; -import org.eclipse.jetty.http.MimeTypes; -import org.eclipse.jetty.server.AbstractHttpConnection; -import org.eclipse.jetty.server.Request; -import org.eclipse.jetty.util.ByteArrayISO8859Writer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/* ------------------------------------------------------------ */ -/** Handler for Error pages - * An ErrorHandler is registered with {@link ContextHandler#setErrorHandler(ErrorHandler)} or - * {@link org.eclipse.jetty.server.Server#addBean(Object)}. - * It is called by the HttpResponse.sendError method to write a error page. - * - */ -public class ErrorHandler extends AbstractHandler -{ - private static final Logger LOG = LoggerFactory.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"; - - /* ------------------------------------------------------------ */ - /* - * @see org.eclipse.jetty.server.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int) - */ - public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException - { - AbstractHttpConnection connection = AbstractHttpConnection.getCurrentConnection(); - String method = request.getMethod(); - if(!method.equals(HttpMethods.GET) && !method.equals(HttpMethods.POST) && !method.equals(HttpMethods.HEAD)) - { - connection.getRequest().setHandled(true); - return; - } - - connection.getRequest().setHandled(true); - response.setContentType(MimeTypes.TEXT_HTML_8859_1); - if (_cacheControl!=null) - response.setHeader(HttpHeaders.CACHE_CONTROL, _cacheControl); - ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(4096); - handleErrorPage(request, writer, connection.getResponse().getStatus(), connection.getResponse().getReason()); - writer.flush(); - response.setContentLength(writer.size()); - writer.writeTo(response.getOutputStream()); - writer.destroy(); - } - - /* ------------------------------------------------------------ */ - protected void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message) - throws IOException - { - writeErrorPage(request, writer, code, message, _showStacks); - } - - /* ------------------------------------------------------------ */ - protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) - throws IOException - { - if (message == null) - message=HttpStatus.getMessage(code); - - writer.write("<html>\n<head>\n"); - writeErrorPageHead(request,writer,code,message); - writer.write("</head>\n<body>"); - writeErrorPageBody(request,writer,code,message,showStacks); - writer.write("\n</body>\n</html>\n"); - } - - /* ------------------------------------------------------------ */ - protected void writeErrorPageHead(HttpServletRequest request, Writer writer, int code, String message) - throws IOException - { - writer.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"/>\n"); - writer.write("<title>Error "); - writer.write(Integer.toString(code)); - - if (_showMessageInTitle) - { - writer.write(' '); - write(writer,message); - } - writer.write("</title>\n"); - } - - /* ------------------------------------------------------------ */ - protected void writeErrorPageBody(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) - throws IOException - { - String uri= request.getRequestURI(); - - writeErrorPageMessage(request,writer,code,message,uri); - if (showStacks) - writeErrorPageStacks(request,writer); - writer.write("<hr /><i><small>Powered by Jetty://</small></i>"); - for (int i= 0; i < 20; i++) - writer.write("<br/> \n"); - } - - /* ------------------------------------------------------------ */ - protected void writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message,String uri) - throws IOException - { - writer.write("<h2>HTTP ERROR "); - writer.write(Integer.toString(code)); - writer.write("</h2>\n<p>Problem accessing "); - write(writer,uri); - writer.write(". Reason:\n<pre> "); - write(writer,message); - writer.write("</pre></p>"); - } - - /* ------------------------------------------------------------ */ - protected void writeErrorPageStacks(HttpServletRequest request, Writer writer) - throws IOException - { - Throwable th = (Throwable)request.getAttribute("javax.servlet.error.exception"); - while(th!=null) - { - writer.write("<h3>Caused by:</h3><pre>"); - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - th.printStackTrace(pw); - pw.flush(); - write(writer,sw.getBuffer().toString()); - writer.write("</pre>\n"); - - th =th.getCause(); - } - } - - - /* ------------------------------------------------------------ */ - /** Get the cacheControl. - * @return the cacheControl header to set on error responses. - */ - public String getCacheControl() - { - return _cacheControl; - } - - /* ------------------------------------------------------------ */ - /** Set the cacheControl. - * @param cacheControl the cacheControl header to set on error responses. - */ - public void setCacheControl(String cacheControl) - { - _cacheControl = cacheControl; - } - - /* ------------------------------------------------------------ */ - /** - * @return True if stack traces are shown in the error pages - */ - public boolean isShowStacks() - { - return _showStacks; - } - - /* ------------------------------------------------------------ */ - /** - * @param showStacks True if stack traces are shown in the error pages - */ - public void setShowStacks(boolean showStacks) - { - _showStacks = showStacks; - } - - /* ------------------------------------------------------------ */ - /** - * @param showMessageInTitle if true, the error message appears in page title - */ - public void setShowMessageInTitle(boolean showMessageInTitle) - { - _showMessageInTitle = showMessageInTitle; - } - - - /* ------------------------------------------------------------ */ - public boolean getShowMessageInTitle() - { - return _showMessageInTitle; - } - - /* ------------------------------------------------------------ */ - protected void write(Writer writer,String string) - throws IOException - { - if (string==null) - return; - - for (int i=0;i<string.length();i++) - { - char c=string.charAt(i); - - switch(c) - { - case '&' : - writer.write("&"); - break; - case '<' : - writer.write("<"); - break; - case '>' : - writer.write(">"); - break; - - default: - if (Character.isISOControl(c) && !Character.isWhitespace(c)) - writer.write('?'); - else - writer.write(c); - } - } - } -}