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

org.openqa.jetty.http.handler.RootNotFoundHandler Maven / Gradle / Ivy

There is a newer version: 4.0.0-alpha-2
Show newest version
// ========================================================================
// $Id: RootNotFoundHandler.java,v 1.11 2005/08/13 00:01:26 gregwilkins Exp $
// Copyright 2002-2004 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.openqa.jetty.http.handler;

import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.logging.Log;
import org.openqa.jetty.log.LogFactory;
import org.openqa.jetty.http.HttpContext;
import org.openqa.jetty.http.HttpException;
import org.openqa.jetty.http.HttpFields;
import org.openqa.jetty.http.HttpRequest;
import org.openqa.jetty.http.HttpResponse;
import org.openqa.jetty.util.ByteArrayISO8859Writer;
import org.openqa.jetty.util.StringUtil;

/* ------------------------------------------------------------ */
/** 
 * @version $Id: RootNotFoundHandler.java,v 1.11 2005/08/13 00:01:26 gregwilkins Exp $
 * @author Greg Wilkins (gregw)
 */
public class RootNotFoundHandler extends NotFoundHandler
{
    private static Log log = LogFactory.getLog(RootNotFoundHandler.class);

    
    /* ------------------------------------------------------------ */
    public void handle(String pathInContext,
                       String pathParams,
                       HttpRequest request,
                       HttpResponse response)
        throws HttpException, IOException
    {
        log.debug("Root Not Found");
        String method=request.getMethod();
        
        if (!method.equals(HttpRequest.__GET) ||
            !request.getPath().equals("/"))
        {
            // don't bother with fancy format.
            super.handle(pathInContext,pathParams,request,response);
            return;
        }

        response.setStatus(404);
        request.setHandled(true);
        response.setReason("Not Found");
        response.setContentType(HttpFields.__TextHtml);
        
        ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(1500);

        String uri=request.getPath();
        uri=StringUtil.replace(uri,"<","<");
        uri=StringUtil.replace(uri,">",">");
        
        writer.write("\n\nError 404 - Not Found");
        writer.write("\n\n

Error 404 - Not Found.

\n"); writer.write("No context on this server matched or handled this request.
"); writer.write("Contexts known to this server are: The links above may not work if a virtual host is configured"); for (int i=0;i<10;i++) writer.write("\n"); writer.write("\n\n\n"); writer.flush(); response.setContentLength(writer.size()); OutputStream out=response.getOutputStream(); writer.writeTo(out); out.close(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy