Author: markt
Date: Sat Jun 30 13:06:27 2012
New Revision: 1355726

URL: http://svn.apache.org/viewvc?rev=1355726&view=rev
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=52135
Implement support for a default error page. It appears that this was meant to 
be in the 3.0 spec but got left out in error.

Added:
    tomcat/trunk/test/org/apache/catalina/core/TestStandardHostValve.java
Modified:
    tomcat/trunk/java/org/apache/catalina/core/StandardContext.java
    tomcat/trunk/java/org/apache/catalina/core/StandardHostValve.java
    tomcat/trunk/java/org/apache/catalina/deploy/ErrorPage.java

Modified: tomcat/trunk/java/org/apache/catalina/core/StandardContext.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/StandardContext.java?rev=1355726&r1=1355725&r2=1355726&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/StandardContext.java (original)
+++ tomcat/trunk/java/org/apache/catalina/core/StandardContext.java Sat Jun 30 
13:06:27 2012
@@ -556,7 +556,8 @@ public class StandardContext extends Con
 
     /**
      * The status code error pages for this web application, keyed by
-     * HTTP status code (as an Integer).
+     * HTTP status code (as an Integer). Note status code zero is used for the
+     * default error page.
      */
     private HashMap<Integer, ErrorPage> statusPages =
         new HashMap<Integer, ErrorPage>();

Modified: tomcat/trunk/java/org/apache/catalina/core/StandardHostValve.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/StandardHostValve.java?rev=1355726&r1=1355725&r2=1355726&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/StandardHostValve.java (original)
+++ tomcat/trunk/java/org/apache/catalina/core/StandardHostValve.java Sat Jun 
30 13:06:27 2012
@@ -279,6 +279,10 @@ final class StandardHostValve extends Va
         }
 
         ErrorPage errorPage = context.findErrorPage(statusCode);
+        if (errorPage == null) {
+            // Look for a default error page
+            errorPage = context.findErrorPage(0);
+        }
         if (errorPage != null) {
             response.setAppCommitted(false);
             request.setAttribute(RequestDispatcher.ERROR_STATUS_CODE,

Modified: tomcat/trunk/java/org/apache/catalina/deploy/ErrorPage.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/deploy/ErrorPage.java?rev=1355726&r1=1355725&r2=1355726&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/deploy/ErrorPage.java (original)
+++ tomcat/trunk/java/org/apache/catalina/deploy/ErrorPage.java Sat Jun 30 
13:06:27 2012
@@ -41,7 +41,8 @@ public class ErrorPage implements Serial
 
 
     /**
-     * The error (status) code for which this error page is active.
+     * The error (status) code for which this error page is active. Note that
+     * status code 0 is used for the default error page.
      */
     private int errorCode = 0;
 

Added: tomcat/trunk/test/org/apache/catalina/core/TestStandardHostValve.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/core/TestStandardHostValve.java?rev=1355726&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/core/TestStandardHostValve.java 
(added)
+++ tomcat/trunk/test/org/apache/catalina/core/TestStandardHostValve.java Sat 
Jun 30 13:06:27 2012
@@ -0,0 +1,110 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You 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.apache.catalina.core;
+
+import java.io.File;
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import org.apache.catalina.Context;
+import org.apache.catalina.connector.Response;
+import org.apache.catalina.deploy.ErrorPage;
+import org.apache.catalina.startup.Tomcat;
+import org.apache.catalina.startup.TomcatBaseTest;
+import org.apache.tomcat.util.buf.ByteChunk;
+
+public class TestStandardHostValve extends TomcatBaseTest {
+
+    @Test
+    public void testErrorPageHandling() throws Exception {
+        // Set up a container
+        Tomcat tomcat = getTomcatInstance();
+
+        // Must have a real docBase - just use temp
+        File docBase = new File(System.getProperty("java.io.tmpdir"));
+        Context ctx = tomcat.addContext("", docBase.getAbsolutePath());
+
+        // Add the error page
+        Tomcat.addServlet(ctx, "error", new ErrorServlet());
+        ctx.addServletMapping("/error", "error");
+
+        // Add the error handling page
+        Tomcat.addServlet(ctx, "report", new ReportServlet());
+        ctx.addServletMapping("/report/*", "report");
+
+        // And the handling for 500 responses
+        ErrorPage errorPage500 = new ErrorPage();
+        errorPage500.setErrorCode(Response.SC_INTERNAL_SERVER_ERROR);
+        errorPage500.setLocation("/report/500");
+        ctx.addErrorPage(errorPage500);
+
+        // And the default error handling
+        ErrorPage errorPageDefault = new ErrorPage();
+        errorPageDefault.setLocation("/report/default");
+        ctx.addErrorPage(errorPageDefault);
+
+        tomcat.start();
+
+        doTestErrorPageHandling(500, "/500");
+        doTestErrorPageHandling(501, "/default");
+    }
+
+    private void doTestErrorPageHandling(int error, String report)
+            throws Exception {
+
+        // Request a page that triggers an error
+        ByteChunk bc = new ByteChunk();
+        int rc = getUrl("http://localhost:"; + getPort() +
+                "/error?errorCode=" + error, bc, null);
+
+        Assert.assertEquals(error, rc);
+        Assert.assertEquals(report, bc.toString());
+    }
+
+    private static class ErrorServlet extends HttpServlet {
+
+        private static final long serialVersionUID = 1L;
+
+        @Override
+        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
+                throws ServletException, IOException {
+            int error =
+                    Integer.valueOf(req.getParameter("errorCode")).intValue();
+            resp.sendError(error);
+        }
+    }
+
+    private static class ReportServlet extends HttpServlet {
+
+        private static final long serialVersionUID = 1L;
+
+        @Override
+        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
+                throws ServletException, IOException {
+            String pathInfo = req.getPathInfo();
+            resp.setContentType("text/plain");
+            resp.getWriter().print(pathInfo);
+        }
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to