tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From craig...@locus.apache.org
Subject cvs commit: jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/deploy ErrorPage.java
Date Sat, 24 Jun 2000 01:09:05 GMT
craigmcc    00/06/23 18:09:03

  Modified:    proposals/catalina/src/share/org/apache/tomcat/core
                        Constants.java StandardWrapperValve.java
               proposals/catalina/src/share/org/apache/tomcat/deploy
                        ErrorPage.java
  Log:
  Partially implement the <error-page> directive in the "web.xml" file.
  For the purposes of this directive, the <location> element is assumed
  to be a context-relative path starting with a "/" character, similar
  to the way that the <taglib-location> is evaluated.
  
  The current implementation has the following restrictions:
  
  * The custom error page will not be rendered if the originating servlet
    has already committed the response.
  
  * Custom error pages for HTTP status codes are only honored when a
    servlet or JSP page itself sets the status code.  Errors caught by
    the container cause standard responses to be generated.
  
  Revision  Changes    Path
  1.4       +27 -3     jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/core/Constants.java
  
  Index: Constants.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/core/Constants.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Constants.java	2000/05/12 00:47:24	1.3
  +++ Constants.java	2000/06/24 01:09:00	1.4
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/core/Constants.java,v
1.3 2000/05/12 00:47:24 craigmcc Exp $
  - * $Revision: 1.3 $
  - * $Date: 2000/05/12 00:47:24 $
  + * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/core/Constants.java,v
1.4 2000/06/24 01:09:00 craigmcc Exp $
  + * $Revision: 1.4 $
  + * $Date: 2000/06/24 01:09:00 $
    *
    * ====================================================================
    *
  @@ -89,6 +89,30 @@
        */
       public static final String CLASSPATH_ATTR =
   	"org.apache.tomcat.jsp_classpath";
  +
  +
  +    /**
  +     * The request attribute under which we forward a Java exception type
  +     * (as an object of type Class) to an error page.
  +     */
  +    public static final String EXCEPTION_TYPE =
  +	"javax.servlet.error.exception_type";
  +
  +
  +    /**
  +     * The request attribute under which we forward an HTTP status message
  +     * (as an object of type String) to an error page.
  +     */
  +    public static final String MESSAGE =
  +	"javax.servlet.error.message";
  +
  +
  +    /**
  +     * The request attribute under which we forward an HTTP status code
  +     * (as an object of type Integer) to an error page.
  +     */
  +    public static final String STATUS_CODE =
  +	"javax.servlet.error.status_code";
   
   
       /**
  
  
  
  1.15      +136 -27   jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/core/StandardWrapperValve.java
  
  Index: StandardWrapperValve.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/core/StandardWrapperValve.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- StandardWrapperValve.java	2000/06/23 00:58:49	1.14
  +++ StandardWrapperValve.java	2000/06/24 01:09:00	1.15
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/core/StandardWrapperValve.java,v
1.14 2000/06/23 00:58:49 craigmcc Exp $
  - * $Revision: 1.14 $
  - * $Date: 2000/06/23 00:58:49 $
  + * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/core/StandardWrapperValve.java,v
1.15 2000/06/24 01:09:00 craigmcc Exp $
  + * $Revision: 1.15 $
  + * $Date: 2000/06/24 01:09:00 $
    *
    * ====================================================================
    *
  @@ -67,7 +67,9 @@
   
   import java.io.IOException;
   import java.io.PrintWriter;
  +import javax.servlet.RequestDispatcher;
   import javax.servlet.Servlet;
  +import javax.servlet.ServletContext;
   import javax.servlet.ServletException;
   import javax.servlet.ServletOutputStream;
   import javax.servlet.ServletRequest;
  @@ -77,11 +79,14 @@
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;
   import org.apache.tomcat.Container;
  +import org.apache.tomcat.Context;
  +import org.apache.tomcat.HttpRequest;
   import org.apache.tomcat.HttpResponse;
   import org.apache.tomcat.Logger;
   import org.apache.tomcat.Request;
   import org.apache.tomcat.Response;
   import org.apache.tomcat.Wrapper;
  +import org.apache.tomcat.deploy.ErrorPage;
   import org.apache.tomcat.util.StringManager;
   import org.apache.tomcat.valves.ValveBase;
   
  @@ -91,7 +96,7 @@
    * <code>StandardWrapper</code> container implementation.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.14 $ $Date: 2000/06/23 00:58:49 $
  + * @version $Revision: 1.15 $ $Date: 2000/06/24 01:09:00 $
    */
   
   final class StandardWrapperValve
  @@ -102,6 +107,12 @@
   
   
       /**
  +     * The debugging detail level for this component.
  +     */
  +    private int debug = 0;
  +
  +
  +    /**
        * The string manager for this package.
        */
       private static final StringManager sm =
  @@ -169,10 +180,8 @@
   	} catch (Throwable e) {
   	    log(sm.getString("standardWrapper.allocateException",
   			     wrapper.getName()), e);
  -	    throwable = new ServletException
  -		(sm.getString("standardWrapper.allocateException",
  -			      wrapper.getName()), e);
  -	    exception(request, response, (ServletException) throwable);
  +	    throwable = e;
  +	    exception(request, response, e);
   	    servlet = null;
   	}
   
  @@ -200,14 +209,13 @@
   	} catch (ServletException e) {
   	    log(sm.getString("standardWrapper.serviceException",
   			     wrapper.getName()), e);
  +	    throwable = e;
   	    exception(request, response, e);
   	} catch (Throwable e) {
   	    log(sm.getString("standardWrapper.serviceException",
   			     wrapper.getName()), e);
  -	    throwable = new ServletException
  -		(sm.getString("standardWrapper.serviceException",
  -			      wrapper.getName()), e);
  -	    exception(request, response, (ServletException) throwable);
  +	    throwable = e;
  +	    exception(request, response, e);
   	}
   
   	// Deallocate the allocated servlet instance
  @@ -223,10 +231,8 @@
   	} catch (Throwable e) {
   	    log(sm.getString("standardWrapper.deallocateException",
   			     wrapper.getName()), e);
  -	    throwable = new ServletException
  -		(sm.getString("standardWrapper.deallocateException",
  -			      wrapper.getName()), e);
  -	    exception(request, response, (ServletException) throwable);
  +	    throwable = e;
  +	    exception(request, response, e);
   	}
   
   	// Generate a response for the generated HTTP status and message
  @@ -241,6 +247,66 @@
   
   
       /**
  +     * Handle an HTTP status code or Java exception by forwarding control
  +     * to the location included in the specified errorPage object.  It is
  +     * assumed that the caller has already recorded any request attributes
  +     * that are to be forwarded to this page.  Return <code>true</code> if
  +     * we successfully utilized the specified error page location, or
  +     * <code>false</code> if the default error report should be rendered.
  +     *
  +     * @param request The request being processed
  +     * @param response The response being generated
  +     * @param errorPage The errorPage directive we are obeying
  +     */
  +    private boolean custom(Request request, Response response,
  +			   ErrorPage errorPage) {
  +
  +	if (debug >= 1)
  +	    log("Processing " + errorPage);
  +
  +	// Validate our current environment
  +	if (!(request instanceof HttpRequest)) {
  +	    if (debug >= 1)
  +		log(" Not processing an HTTP request --> default handling");
  +	    return (false);	// NOTE - Nothing we can do generically
  +	}
  +	HttpServletRequest hreq =
  +	    (HttpServletRequest) request.getRequest();
  +	if (!(response instanceof HttpResponse)) {
  +	    if (debug >= 1)
  +		log("Not processing an HTTP response --> default handling");
  +	    return (false);	// NOTE - Nothing we can do generically
  +	}
  +	HttpServletResponse hres =
  +	    (HttpServletResponse) response.getResponse();
  +
  +	try {
  +
  +	    // Reset the response if possible (else IllegalStateException)
  +	    hres.reset();
  +
  +	    // Forward control to the specified location
  +	    ServletContext servletContext =
  +		((Context) container.getParent()).getServletContext();
  +	    RequestDispatcher rd =
  +		servletContext.getRequestDispatcher(errorPage.getLocation());
  +	    rd.forward(hreq, hres);
  +
  +	    // Indicate that we have successfully processed this custom page
  +	    return (true);
  +
  +	} catch (Throwable t) {
  +
  +	    // Report our failure to process this custom page
  +	    log("Exception Processing " + errorPage, t);
  +	    return (false);
  +
  +	}
  +
  +    }
  +
  +
  +    /**
        * Handle the specified ServletException encountered while processing
        * the specified Request to produce the specified Response.  Any
        * exceptions that occur during generation of the exception report are
  @@ -252,7 +318,25 @@
        *  a root cause exception
        */
       private void exception(Request request, Response response,
  -			   ServletException exception) {
  +			   Throwable exception) {
  +
  +	// Handle a custom error page for this status code
  +	Context context = (Context) container.getParent();
  +	Throwable realError = exception;
  +	if (exception instanceof ServletException) {
  +	    Throwable rootCause =
  +		((ServletException) exception).getRootCause();
  +	    if (rootCause != null)
  +		realError = rootCause;
  +	}
  +        ErrorPage errorPage =
  +	    context.findErrorPage(realError.getClass().getName());
  +	if (errorPage != null) {
  +	    request.getRequest().setAttribute(Constants.EXCEPTION_TYPE,
  +					      realError.getClass());
  +	    if (custom(request, response, errorPage))
  +		return;
  +	}
   
   	// Reset the response (if possible)
   	try {
  @@ -273,9 +357,16 @@
   	    ;
   	}
   
  -	// Render an HTML error page (FIXME - per web.xml descriptor)
  -	Throwable rootCause = exception.getRootCause();
  +	// Render a default HTML exception report page
  +	Throwable rootCause = null;
  +	if (exception instanceof ServletException)
  +	    rootCause = ((ServletException) exception).getRootCause();
   	try {
  +	    try {
  +		response.getResponse().setContentType("text/html");
  +	    } catch (Throwable t) {
  +		;
  +	    }
   	    PrintWriter writer = response.getReporter();
   	    try {
   		response.getResponse().flushBuffer();
  @@ -354,7 +445,7 @@
   	if (container != null)
   	    logger = container.getLogger();
   	if (logger != null)
  -	    logger.log("StandardWrapperValve[" + container.getName() + "] "
  +	    logger.log("StandardWrapperValve[" + container.getName() + "]: "
   		       + message, throwable);
   	else {
   	    String containerName = null;
  @@ -388,19 +479,33 @@
   	    return;
   	HttpServletResponse hres =
   	    (HttpServletResponse) response.getResponse();
  -	int status = hresponse.getStatus();
  +	int statusCode = hresponse.getStatus();
   	String message = hresponse.getMessage();
   	if (message == null)
   	    message = "";
  -	if (status == HttpServletResponse.SC_OK)
  +
  +	// Handle a custom error page for this status code
  +	Context context = (Context) container.getParent();
  +	ErrorPage errorPage = context.findErrorPage(statusCode);
  +	if (errorPage != null) {
  +	    request.getRequest().setAttribute(Constants.STATUS_CODE,
  +					      new Integer(statusCode));
  +	    request.getRequest().setAttribute(Constants.MESSAGE,
  +					      message);
  +	    if (custom(request, response, errorPage))
  +		return;
  +	}
  +
  +	// Do nothing on an OK status
  +	if (statusCode == HttpServletResponse.SC_OK)
   	    return;
  -        if (status < 300)
  +        if (statusCode < 300)
               return;
   
   	// Do nothing if there is no report for the specified status code
   	String report = null;
   	try {
  -	    report = sm.getString("http." + status, message);
  +	    report = sm.getString("http." + statusCode, message);
   	} catch (Throwable t) {
   	    ;
   	}
  @@ -414,9 +519,13 @@
   	    ;
   	}
   
  -	// Render an HTML status report page (FIXME - per web.xml descriptor)
  +	// Render a default HTML status report page
   	try {
  -	    hres.setContentType("text/html");
  +	    try {
  +		hres.setContentType("text/html");
  +	    } catch (Throwable t) {
  +		;
  +	    }
   	    PrintWriter writer = response.getReporter();
   	    try {
   		hres.flushBuffer();
  @@ -433,7 +542,7 @@
   	    writer.println("<br><br>");
   	    writer.println("<h1>" +
   			   sm.getString("standardWrapper.statusHeader",
  -					"" + status, message) +
  +					"" + statusCode, message) +
   			   "</h1>");
   	    writer.println(report);
   	    writer.println("</body>");
  
  
  
  1.2       +43 -3     jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/deploy/ErrorPage.java
  
  Index: ErrorPage.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/deploy/ErrorPage.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ErrorPage.java	2000/06/23 22:25:02	1.1
  +++ ErrorPage.java	2000/06/24 01:09:01	1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/deploy/ErrorPage.java,v
1.1 2000/06/23 22:25:02 craigmcc Exp $
  - * $Revision: 1.1 $
  - * $Date: 2000/06/23 22:25:02 $
  + * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/deploy/ErrorPage.java,v
1.2 2000/06/24 01:09:01 craigmcc Exp $
  + * $Revision: 1.2 $
  + * $Date: 2000/06/24 01:09:01 $
    *
    * ====================================================================
    *
  @@ -71,7 +71,7 @@
    * deployment descriptor.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.1 $ $Date: 2000/06/23 22:25:02 $
  + * @version $Revision: 1.2 $ $Date: 2000/06/24 01:09:01 $
    */
   
   public final class ErrorPage {
  @@ -124,6 +124,22 @@
   
   
       /**
  +     * Set the error code (hack for default XmlMapper data type).
  +     *
  +     * @param errorCode The new error code
  +     */
  +    public void setErrorCode(String errorCode) {
  +
  +	try {
  +	    this.errorCode = Integer.parseInt(errorCode);
  +	} catch (Throwable t) {
  +	    this.errorCode = 0;
  +	}
  +
  +    }
  +
  +
  +    /**
        * Return the exception type.
        */
       public String getExceptionType() {
  @@ -163,6 +179,30 @@
       public void setLocation(String location) {
   
   	this.location = location;
  +
  +    }
  +
  +
  +    // --------------------------------------------------------- Public Methods
  +
  +
  +    /**
  +     * Render a String representation of this object.
  +     */
  +    public String toString() {
  +
  +	StringBuffer sb = new StringBuffer("ErrorPage[");
  +	if (exceptionType == null) {
  +	    sb.append("errorCode=");
  +	    sb.append(errorCode);
  +	} else {
  +	    sb.append("exceptionType=");
  +	    sb.append(exceptionType);
  +	}
  +	sb.append(", location=");
  +	sb.append(location);
  +	sb.append("]");
  +	return (sb.toString());
   
       }
   
  
  
  

Mime
View raw message