Java Code Examples for org.apache.catalina.deploy.ErrorPage#setErrorCode()
The following examples show how to use
org.apache.catalina.deploy.ErrorPage#setErrorCode() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DebugTomcat.java From kylin-on-parquet-v2 with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception { setupDebugEnv(); int port = 7070; if (args.length >= 1) { port = Integer.parseInt(args[0]); } File webBase = new File("../webapp/app"); File webInfDir = new File(webBase, "WEB-INF"); FileUtils.deleteDirectory(webInfDir); FileUtils.copyDirectoryToDirectory(new File("../server/src/main/webapp/WEB-INF"), webBase); Tomcat tomcat = new Tomcat(); tomcat.setPort(port); tomcat.setBaseDir("."); // Add AprLifecycleListener StandardServer server = (StandardServer) tomcat.getServer(); AprLifecycleListener listener = new AprLifecycleListener(); server.addLifecycleListener(listener); Context webContext = tomcat.addWebapp("/kylin", webBase.getAbsolutePath()); ErrorPage notFound = new ErrorPage(); notFound.setErrorCode(404); notFound.setLocation("/index.html"); webContext.addErrorPage(notFound); webContext.addWelcomeFile("index.html"); // tomcat start tomcat.start(); tomcat.getServer().await(); }
Example 2
Source File: TestStandardHostValve.java From Tomcat7.0.67 with Apache License 2.0 | 5 votes |
@Test public void testErrorPageHandling() throws Exception { // Set up a container Tomcat tomcat = getTomcatInstance(); // No file system docBase required Context ctx = tomcat.addContext("", null); // 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"); }
Example 3
Source File: TestStandardHostValve.java From tomcatsrc with Apache License 2.0 | 5 votes |
@Test public void testErrorPageHandling() throws Exception { // Set up a container Tomcat tomcat = getTomcatInstance(); // No file system docBase required Context ctx = tomcat.addContext("", null); // 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"); }
Example 4
Source File: TestStandardHostValve.java From tomcatsrc with Apache License 2.0 | 5 votes |
@Test(expected=IllegalArgumentException.class) public void testInvalidErrorPage() throws Exception { // Set up a container Tomcat tomcat = getTomcatInstance(); // No file system docBase required Context ctx = tomcat.addContext("", null); // Add a broken error page configuration ErrorPage errorPage500 = new ErrorPage(); errorPage500.setErrorCode("java.lang.Exception"); errorPage500.setLocation("/report/500"); ctx.addErrorPage(errorPage500); }
Example 5
Source File: DebugTomcat.java From kylin with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception { setupDebugEnv(); int port = 7070; if (args.length >= 1) { port = Integer.parseInt(args[0]); } File webBase = new File("../webapp/app"); File webInfDir = new File(webBase, "WEB-INF"); FileUtils.deleteDirectory(webInfDir); FileUtils.copyDirectoryToDirectory(new File("../server/src/main/webapp/WEB-INF"), webBase); Tomcat tomcat = new Tomcat(); tomcat.setPort(port); tomcat.setBaseDir("."); // Add AprLifecycleListener StandardServer server = (StandardServer) tomcat.getServer(); AprLifecycleListener listener = new AprLifecycleListener(); server.addLifecycleListener(listener); Context webContext = tomcat.addWebapp("/kylin", webBase.getAbsolutePath()); ErrorPage notFound = new ErrorPage(); notFound.setErrorCode(404); notFound.setLocation("/index.html"); webContext.addErrorPage(notFound); webContext.addWelcomeFile("index.html"); // tomcat start tomcat.start(); tomcat.getServer().await(); }
Example 6
Source File: TestStandardContextValve.java From Tomcat7.0.67 with Apache License 2.0 | 4 votes |
@Test public void testBug51653a() throws Exception { // Set up a container Tomcat tomcat = getTomcatInstance(); // No file system docBase required Context ctx = tomcat.addContext("", null); // Traces order of events across multiple components StringBuilder trace = new StringBuilder(); //Add the error page Tomcat.addServlet(ctx, "errorPage", new Bug51653ErrorPage(trace)); ctx.addServletMapping("/error", "errorPage"); // And the handling for 404 responses ErrorPage errorPage = new ErrorPage(); errorPage.setErrorCode(Response.SC_NOT_FOUND); errorPage.setLocation("/error"); ctx.addErrorPage(errorPage); // Add the request listener Bug51653RequestListener reqListener = new Bug51653RequestListener(trace); ((StandardContext) ctx).addApplicationEventListener(reqListener); tomcat.start(); // Request a page that does not exist int rc = getUrl("http://localhost:" + getPort() + "/invalid", new ByteChunk(), null); // Need to allow time (but not too long in case the test fails) for // ServletRequestListener to complete int i = 20; while (i > 0) { if (trace.toString().endsWith("Destroy")) { break; } Thread.sleep(250); i--; } assertEquals(Response.SC_NOT_FOUND, rc); assertEquals("InitErrorDestroy", trace.toString()); }
Example 7
Source File: TestStandardContextValve.java From Tomcat7.0.67 with Apache License 2.0 | 4 votes |
@Test public void testBug51653b() throws Exception { // Set up a container Tomcat tomcat = getTomcatInstance(); // No file system docBase required Context ctx = tomcat.addContext("", null); // Traces order of events across multiple components StringBuilder trace = new StringBuilder(); // Add the page that generates the error Tomcat.addServlet(ctx, "test", new Bug51653ErrorTrigger()); ctx.addServletMapping("/test", "test"); // Add the error page Tomcat.addServlet(ctx, "errorPage", new Bug51653ErrorPage(trace)); ctx.addServletMapping("/error", "errorPage"); // And the handling for 404 responses ErrorPage errorPage = new ErrorPage(); errorPage.setErrorCode(Response.SC_NOT_FOUND); errorPage.setLocation("/error"); ctx.addErrorPage(errorPage); // Add the request listener Bug51653RequestListener reqListener = new Bug51653RequestListener(trace); ((StandardContext) ctx).addApplicationEventListener(reqListener); tomcat.start(); // Request a page that does not exist int rc = getUrl("http://localhost:" + getPort() + "/test", new ByteChunk(), null); // Need to allow time (but not too long in case the test fails) for // ServletRequestListener to complete int i = 20; while (i > 0) { if (trace.toString().endsWith("Destroy")) { break; } Thread.sleep(250); i--; } assertEquals(Response.SC_NOT_FOUND, rc); assertEquals("InitErrorDestroy", trace.toString()); }
Example 8
Source File: TestAsyncContextImpl.java From Tomcat7.0.67 with Apache License 2.0 | 4 votes |
private void doTestTimeoutErrorDispatch(Boolean asyncError, ErrorPageAsyncMode mode) throws Exception { resetTracker(); // Setup Tomcat instance Tomcat tomcat = getTomcatInstance(); // No file system docBase required Context ctx = tomcat.addContext("", null); TimeoutServlet timeout = new TimeoutServlet(null, null); Wrapper w1 = Tomcat.addServlet(ctx, "time", timeout); w1.setAsyncSupported(true); ctx.addServletMapping("/async", "time"); NonAsyncServlet nonAsync = new NonAsyncServlet(); Wrapper w2 = Tomcat.addServlet(ctx, "nonAsync", nonAsync); w2.setAsyncSupported(true); ctx.addServletMapping("/error/nonasync", "nonAsync"); AsyncErrorPage asyncErrorPage = new AsyncErrorPage(mode); Wrapper w3 = Tomcat.addServlet(ctx, "asyncErrorPage", asyncErrorPage); w3.setAsyncSupported(true); ctx.addServletMapping("/error/async", "asyncErrorPage"); if (asyncError != null) { ErrorPage ep = new ErrorPage(); ep.setErrorCode(500); if (asyncError.booleanValue()) { ep.setLocation("/error/async"); } else { ep.setLocation("/error/nonasync"); } ctx.addErrorPage(ep); } ctx.addApplicationListener(TrackingRequestListener.class.getName()); TesterAccessLogValve alv = new TesterAccessLogValve(); ctx.getPipeline().addValve(alv); TesterAccessLogValve alvGlobal = new TesterAccessLogValve(); tomcat.getHost().getPipeline().addValve(alvGlobal); tomcat.start(); ByteChunk res = new ByteChunk(); try { getUrl("http://localhost:" + getPort() + "/async", res, null); } catch (IOException ioe) { // Ignore - expected for some error conditions } StringBuilder expected = new StringBuilder(); expected.append("requestInitialized-TimeoutServletGet-"); if (asyncError != null) { if (asyncError.booleanValue()) { expected.append("AsyncErrorPageGet-"); if (mode == ErrorPageAsyncMode.NO_COMPLETE){ expected.append("NoOp-"); } else if (mode == ErrorPageAsyncMode.COMPLETE) { expected.append("Complete-"); } else if (mode == ErrorPageAsyncMode.DISPATCH) { expected.append("Dispatch-NonAsyncServletGet-"); } } else { expected.append("NonAsyncServletGet-"); } } expected.append("requestDestroyed"); // Request may complete before listener has finished processing so wait // up to 5 seconds for the right response String expectedTrack = expected.toString(); int count = 0; while (!expectedTrack.equals(getTrack()) && count < 100) { Thread.sleep(50); count ++; } Assert.assertEquals(expectedTrack, getTrack()); // Check the access log alvGlobal.validateAccessLog(1, 500, TimeoutServlet.ASYNC_TIMEOUT, TimeoutServlet.ASYNC_TIMEOUT + TIMEOUT_MARGIN + REQUEST_TIME); alv.validateAccessLog(1, 500, TimeoutServlet.ASYNC_TIMEOUT, TimeoutServlet.ASYNC_TIMEOUT + TIMEOUT_MARGIN + REQUEST_TIME); }
Example 9
Source File: TestStandardContextValve.java From tomcatsrc with Apache License 2.0 | 4 votes |
@Test public void testBug51653a() throws Exception { // Set up a container Tomcat tomcat = getTomcatInstance(); // No file system docBase required Context ctx = tomcat.addContext("", null); // Traces order of events across multiple components StringBuilder trace = new StringBuilder(); //Add the error page Tomcat.addServlet(ctx, "errorPage", new Bug51653ErrorPage(trace)); ctx.addServletMapping("/error", "errorPage"); // And the handling for 404 responses ErrorPage errorPage = new ErrorPage(); errorPage.setErrorCode(Response.SC_NOT_FOUND); errorPage.setLocation("/error"); ctx.addErrorPage(errorPage); // Add the request listener Bug51653RequestListener reqListener = new Bug51653RequestListener(trace); ((StandardContext) ctx).addApplicationEventListener(reqListener); tomcat.start(); // Request a page that does not exist int rc = getUrl("http://localhost:" + getPort() + "/invalid", new ByteChunk(), null); // Need to allow time (but not too long in case the test fails) for // ServletRequestListener to complete int i = 20; while (i > 0) { if (trace.toString().endsWith("Destroy")) { break; } Thread.sleep(250); i--; } assertEquals(Response.SC_NOT_FOUND, rc); assertEquals("InitErrorDestroy", trace.toString()); }
Example 10
Source File: TestStandardContextValve.java From tomcatsrc with Apache License 2.0 | 4 votes |
@Test public void testBug51653b() throws Exception { // Set up a container Tomcat tomcat = getTomcatInstance(); // No file system docBase required Context ctx = tomcat.addContext("", null); // Traces order of events across multiple components StringBuilder trace = new StringBuilder(); // Add the page that generates the error Tomcat.addServlet(ctx, "test", new Bug51653ErrorTrigger()); ctx.addServletMapping("/test", "test"); // Add the error page Tomcat.addServlet(ctx, "errorPage", new Bug51653ErrorPage(trace)); ctx.addServletMapping("/error", "errorPage"); // And the handling for 404 responses ErrorPage errorPage = new ErrorPage(); errorPage.setErrorCode(Response.SC_NOT_FOUND); errorPage.setLocation("/error"); ctx.addErrorPage(errorPage); // Add the request listener Bug51653RequestListener reqListener = new Bug51653RequestListener(trace); ((StandardContext) ctx).addApplicationEventListener(reqListener); tomcat.start(); // Request a page that does not exist int rc = getUrl("http://localhost:" + getPort() + "/test", new ByteChunk(), null); // Need to allow time (but not too long in case the test fails) for // ServletRequestListener to complete int i = 20; while (i > 0) { if (trace.toString().endsWith("Destroy")) { break; } Thread.sleep(250); i--; } assertEquals(Response.SC_NOT_FOUND, rc); assertEquals("InitErrorDestroy", trace.toString()); }
Example 11
Source File: TestAsyncContextImpl.java From tomcatsrc with Apache License 2.0 | 4 votes |
private void doTestTimeoutErrorDispatch(Boolean asyncError, ErrorPageAsyncMode mode) throws Exception { resetTracker(); // Setup Tomcat instance Tomcat tomcat = getTomcatInstance(); // No file system docBase required Context ctx = tomcat.addContext("", null); TimeoutServlet timeout = new TimeoutServlet(null, null); Wrapper w1 = Tomcat.addServlet(ctx, "time", timeout); w1.setAsyncSupported(true); ctx.addServletMapping("/async", "time"); NonAsyncServlet nonAsync = new NonAsyncServlet(); Wrapper w2 = Tomcat.addServlet(ctx, "nonAsync", nonAsync); w2.setAsyncSupported(true); ctx.addServletMapping("/error/nonasync", "nonAsync"); AsyncErrorPage asyncErrorPage = new AsyncErrorPage(mode); Wrapper w3 = Tomcat.addServlet(ctx, "asyncErrorPage", asyncErrorPage); w3.setAsyncSupported(true); ctx.addServletMapping("/error/async", "asyncErrorPage"); if (asyncError != null) { ErrorPage ep = new ErrorPage(); ep.setErrorCode(500); if (asyncError.booleanValue()) { ep.setLocation("/error/async"); } else { ep.setLocation("/error/nonasync"); } ctx.addErrorPage(ep); } ctx.addApplicationListener(TrackingRequestListener.class.getName()); TesterAccessLogValve alv = new TesterAccessLogValve(); ctx.getPipeline().addValve(alv); TesterAccessLogValve alvGlobal = new TesterAccessLogValve(); tomcat.getHost().getPipeline().addValve(alvGlobal); tomcat.start(); ByteChunk res = new ByteChunk(); try { getUrl("http://localhost:" + getPort() + "/async", res, null); } catch (IOException ioe) { // Ignore - expected for some error conditions } StringBuilder expected = new StringBuilder(); expected.append("requestInitialized-TimeoutServletGet-"); if (asyncError != null) { if (asyncError.booleanValue()) { expected.append("AsyncErrorPageGet-"); if (mode == ErrorPageAsyncMode.NO_COMPLETE){ expected.append("NoOp-"); } else if (mode == ErrorPageAsyncMode.COMPLETE) { expected.append("Complete-"); } else if (mode == ErrorPageAsyncMode.DISPATCH) { expected.append("Dispatch-NonAsyncServletGet-"); } } else { expected.append("NonAsyncServletGet-"); } } expected.append("requestDestroyed"); // Request may complete before listener has finished processing so wait // up to 5 seconds for the right response String expectedTrack = expected.toString(); int count = 0; while (!expectedTrack.equals(getTrack()) && count < 100) { Thread.sleep(50); count ++; } Assert.assertEquals(expectedTrack, getTrack()); // Check the access log alvGlobal.validateAccessLog(1, 500, TimeoutServlet.ASYNC_TIMEOUT, TimeoutServlet.ASYNC_TIMEOUT + TIMEOUT_MARGIN + REQUEST_TIME); alv.validateAccessLog(1, 500, TimeoutServlet.ASYNC_TIMEOUT, TimeoutServlet.ASYNC_TIMEOUT + TIMEOUT_MARGIN + REQUEST_TIME); }