All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.jetty.server.handler.ErrorHandler Maven / Gradle / Ivy

The newest version!
//
//  ========================================================================
//  Copyright (c) 1995-2012 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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.util.ByteArrayISO8859Writer;

/* ------------------------------------------------------------ */
/** 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
{
    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)
     */
    @Override
    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        baseRequest.setHandled(true);
        String method = request.getMethod();
        if(!HttpMethod.GET.is(method) && !HttpMethod.POST.is(method) && !HttpMethod.HEAD.is(method))
            return;
        response.setContentType(MimeTypes.Type.TEXT_HTML_8859_1.asString());
        if (_cacheControl!=null)
            response.setHeader(HttpHeader.CACHE_CONTROL.asString(), _cacheControl);
        ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(4096);
        String reason=(response instanceof Response)?((Response)response).getReason():null;
        handleErrorPage(request, writer, response.getStatus(), reason);
        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("\n\n");
        writeErrorPageHead(request,writer,code,message);
        writer.write("\n");
        writeErrorPageBody(request,writer,code,message,showStacks);
        writer.write("\n\n\n");
    }

    /* ------------------------------------------------------------ */
    protected void writeErrorPageHead(HttpServletRequest request, Writer writer, int code, String message)
        throws IOException
        {
        writer.write("\n");
        writer.write("Error ");
        writer.write(Integer.toString(code));

        if (_showMessageInTitle)
        {
            writer.write(' ');
            write(writer,message);
        }
        writer.write("\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("
Powered by Jetty://
\n"); } /* ------------------------------------------------------------ */ protected void writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message,String uri) throws IOException { writer.write("

HTTP ERROR "); writer.write(Integer.toString(code)); writer.write("

\n

Problem accessing "); write(writer,uri); writer.write(". Reason:\n

    ");
        write(writer,message);
        writer.write("

"); } /* ------------------------------------------------------------ */ protected void writeErrorPageStacks(HttpServletRequest request, Writer writer) throws IOException { Throwable th = (Throwable)request.getAttribute("javax.servlet.error.exception"); while(th!=null) { writer.write("

Caused by:

");
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            th.printStackTrace(pw);
            pw.flush();
            write(writer,sw.getBuffer().toString());
            writer.write("
\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' : writer.write(">"); break; default: if (Character.isISOControl(c) && !Character.isWhitespace(c)) writer.write('?'); else writer.write(c); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy