home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Jetty example source code file (ErrorHandler.java)

This example Jetty source code file (ErrorHandler.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Jetty tags/keywords

bytearrayiso8859writer, content-type, errorhandler, http, httpconnection, io, ioexception, ioexception, jetty://, printwriter, printwriter, request, response, servlet, string, string, stringwriter, writer, writer

The Jetty ErrorHandler.java source code

// ========================================================================
// Copyright 1999-2005 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at 
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========================================================================
package org.mortbay.jetty.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.mortbay.jetty.HttpConnection;
import org.mortbay.jetty.HttpGenerator;
import org.mortbay.jetty.MimeTypes;
import org.mortbay.util.ByteArrayISO8859Writer;
import org.mortbay.util.StringUtil;


/* ------------------------------------------------------------ */
/** Handler for Error pages
 * A handler that is registered at the org.mortbay.http.ErrorHandler
 * context attributed and called by the HttpResponse.sendError method to write a
 * error page.
 * 
 * @author Greg Wilkins (gregw)
 */
public class ErrorHandler extends AbstractHandler
{
    boolean _showStacks=true;
    
    /* ------------------------------------------------------------ */
    /* 
     * @see org.mortbay.jetty.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
     */
    public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException
    {
        HttpConnection.getCurrentConnection().getRequest().setHandled(true);
        response.setContentType(MimeTypes.TEXT_HTML_8859_1);
        ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(4096);
        HttpConnection connection = HttpConnection.getCurrentConnection();
        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=HttpGenerator.getReason(code);
        else
        {
            message= StringUtil.replace(message, "&", "&");
            message= StringUtil.replace(message, "<", "<");
            message= StringUtil.replace(message, ">", ">");
        }

        writer.write("<html>\n\n");
        writeErrorPageHead(request,writer,code,message);
        writer.write("</head>\n");
        writeErrorPageBody(request,writer,code,message,showStacks);
        writer.write("\n</body>\n\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));
        writer.write(' ');
        if (message!=null)
            writer.write(message);
        writer.write("</title>\n");    
    }

    /* ------------------------------------------------------------ */
    protected void writeErrorPageBody(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)
        throws IOException
    {
        String uri= request.getRequestURI();
        if (uri!=null)
        {
            uri= StringUtil.replace(uri, "&", "&");
            uri= StringUtil.replace(uri, "<", "<");
            uri= StringUtil.replace(uri, ">", ">");
        }
        
        writeErrorPageMessage(request,writer,code,message,uri);
        if (showStacks)
            writeErrorPageStacks(request,writer);
        writer.write("<p>Powered by Jetty://

"); 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>
");
        writer.write(message);
        writer.write("</pre>\n

RequestURI="); writer.write(uri); writer.write("</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:

");
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            th.printStackTrace(pw);
            pw.flush();
            writer.write(sw.getBuffer().toString());
            writer.write("</pre>\n");

            th =th.getCause();
        }
    }
        

    /* ------------------------------------------------------------ */
    /**
     * @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;
    }

}

Other Jetty examples (source code examples)

Here is a short list of links related to this Jetty ErrorHandler.java source code file:



my book on functional programming

 

new blog posts

 

Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.