Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 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.servlet;
 
 import java.util.List;
 import java.util.Map;
 
 
 
 /* ------------------------------------------------------------ */
Error Page Error Handler An ErrorHandler that maps exceptions and status codes to URIs for dispatch using the internal ERROR style of dispatch.
 
 public class ErrorPageErrorHandler extends ErrorHandler
 {
     private static final Logger LOG = Log.getLogger(ErrorPageErrorHandler.class);
 
     public final static String ERROR_PAGE="org.eclipse.jetty.server.error_page";
     public final static String GLOBAL_ERROR_PAGE = "org.eclipse.jetty.server.error_page.global";
 
     protected ServletContext _servletContext;
     private final Map<String,String_errorPagesnew HashMap<String,String>(); // code or exception to URL
     private final List<ErrorCodeRange_errorPageList=new ArrayList<ErrorCodeRange>(); // list of ErrorCode by range
 
     /* ------------------------------------------------------------ */
     public ErrorPageErrorHandler()
     {}
 
     /* ------------------------------------------------------------ */
    
 
     @Override
     public void handle(String targetRequest baseRequestHttpServletRequest requestHttpServletResponse responsethrows IOException
     {
         String method = request.getMethod();
         if(!method.equals(.) && !method.equals(.) && !method.equals(.))
         {
             AbstractHttpConnection.getCurrentConnection().getRequest().setHandled(true);
             return;
         }
         if (!=null)
         {
             String error_pagenull;
             Class<?> exClass= (Class<?>)request.getAttribute(.);
 
             if (ServletException.class.equals(exClass))
             {
                 error_page= (String).get(exClass.getName());
                 if (error_page == null)
                 {
                     Throwable th= (Throwable)request.getAttribute(.);
                     while (th instanceof ServletException)
                         th= ((ServletException)th).getRootCause();
                     if (th != null)
                         exClassth.getClass();
                 }
             }
 
             while (error_page == null && exClass != null )
             {
                 error_page= (String).get(exClass.getName());
                 exClassexClass.getSuperclass();
             }
 
            if (error_page == null)
            {
                // look for an exact code match
                Integer code=(Integer)request.getAttribute(.);
                if (code!=null)
                {
                    error_page= (String).get(Integer.toString(code));
                    // if still not found
                    if ((error_page == null) && ( != null))
                    {
                        // look for an error code range match.
                        for (int i = 0; i < .size(); i++)
                        {
                            ErrorCodeRange errCode = (ErrorCodeRange.get(i);
                            if (errCode.isInRange(code))
                            {
                                error_page = errCode.getUri();
                                break;
                            }
                        }
                    }
                }
            }
            
            //try new servlet 3.0 global error page
            if (error_page == null)
            {
                error_page = .get();
            }
            if (error_page!=null)
            {
                String old_error_page=(String)request.getAttribute();
                if (old_error_page==null || !old_error_page.equals(error_page))
                {
                    request.setAttribute(error_page);
                    Dispatcher dispatcher = (Dispatcher.getRequestDispatcher(error_page);
                    try
                    {
                        if(dispatcher!=null)
                        {
                            dispatcher.error(requestresponse);
                            return;
                        }
                        else
                        {
                            .warn("No error page "+error_page);
                        }
                    }
                    catch (ServletException e)
                    {
                        .warn(.e);
                        return;
                    }
                }
            }
        }
        super.handle(targetbaseRequestrequestresponse);
    }
    /* ------------------------------------------------------------ */
    

Returns:
Returns the errorPages.
    public Map<String,StringgetErrorPages()
    {
        return ;
    }
    /* ------------------------------------------------------------ */
    

Parameters:
errorPages The errorPages to set. A map of Exception class name or error code as a string to URI string
    public void setErrorPages(Map<String,StringerrorPages)
    {
        .clear();
        if (errorPages!=null)
            .putAll(errorPages);
    }
    /* ------------------------------------------------------------ */
    
Add Error Page mapping for an exception class This method is called as a result of an exception-type element in a web.xml file or may be called directly

Parameters:
exception The exception
uri The URI of the error page.
    public void addErrorPage(Class<? extends Throwableexception,String uri)
    {
        .put(exception.getName(),uri);
    }
    
    /* ------------------------------------------------------------ */
    
Add Error Page mapping for an exception class This method is called as a result of an exception-type element in a web.xml file or may be called directly

Parameters:
exceptionClassName The exception
uri The URI of the error page.
    public void addErrorPage(String exceptionClassName,String uri)
    {
        .put(exceptionClassName,uri);
    }
    /* ------------------------------------------------------------ */
    
Add Error Page mapping for a status code. This method is called as a result of an error-code element in a web.xml file or may be called directly

Parameters:
code The HTTP status code to match
uri The URI of the error page.
    public void addErrorPage(int code,String uri)
    {
        .put(Integer.toString(code),uri);
    }
    /* ------------------------------------------------------------ */
    
Add Error Page mapping for a status code range. This method is not available from web.xml and must be called directly.

Parameters:
from The lowest matching status code
to The highest matching status code
uri The URI of the error page.
    public void addErrorPage(int fromint toString uri)
    {
        .add(new ErrorCodeRange(fromtouri));
    }
    /* ------------------------------------------------------------ */
    @Override
    protected void doStart() throws Exception
    {
        super.doStart();
        =ContextHandler.getCurrentContext();
    }
    /* ------------------------------------------------------------ */
    @Override
    protected void doStop() throws Exception
    {
        // TODO Auto-generated method stub
        super.doStop();
    }
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    private class ErrorCodeRange
    {
        private int _from;
        private int _to;
        private String _uri;
        ErrorCodeRange(int fromint toString uri)
            throws IllegalArgumentException
        {
            if (from > to)
                throw new IllegalArgumentException("from>to");
             = from;
             = to;
             = uri;
        }
        boolean isInRange(int value)
        {
            if ((value >= ) && (value <= ))
            {
                return true;
            }
            return false;
        }
        String getUri()
        {
            return ;
        }
        @Override
        public String toString()
        {
            return "from: " +  + ",to: " +  + ",uri: " + ;
        }
    }
New to GrepCode? Check out our FAQ X