Author: pedro Date: Fri Dec 17 20:00:51 2010 New Revision: 1050482 URL: http://svn.apache.org/viewvc?rev=1050482&view=rev Log: Enabling other frameworks to plug in the application a custom response for their exceptions Issue: WICKET-3256
Added: wicket/trunk/wicket/src/test/java/org/apache/wicket/ExceptionMapperTest.java Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/Application.java Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/Application.java URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/Application.java?rev=1050482&r1=1050481&r2=1050482&view=diff ============================================================================== --- wicket/trunk/wicket/src/main/java/org/apache/wicket/Application.java (original) +++ wicket/trunk/wicket/src/main/java/org/apache/wicket/Application.java Fri Dec 17 20:00:51 2010 @@ -192,6 +192,9 @@ public abstract class Application implem /** request cycle provider */ private IRequestCycleProvider requestCycleProvider; + /** exception mapper provider */ + private IProvider<IExceptionMapper> exceptionMapperProvider; + /** session store provider */ private IProvider<ISessionStore> sessionStoreProvider; @@ -704,9 +707,26 @@ public abstract class Application implem pageAccessSynchronizer = new PageAccessSynchronizer(getRequestCycleSettings().getTimeout()); requestCycleProvider = new DefaultRequestCycleProvider(); + exceptionMapperProvider = new DefaultExceptionMapperProvider(); } - /** + /** + * @return the exception mapper provider + */ + public IProvider<IExceptionMapper> getExceptionMapperProvider() + { + return exceptionMapperProvider; + } + + /** + * @param exceptionMapperProvider the new exception mapper provider + */ + public void setExceptionMapperProvider(IProvider<IExceptionMapper> exceptionMapperProvider) + { + this.exceptionMapperProvider = exceptionMapperProvider; + } + + /** * * @return Session state provider */ @@ -1449,6 +1469,14 @@ public abstract class Application implem this.requestCycleProvider = requestCycleProvider; } + private static class DefaultExceptionMapperProvider implements IProvider<IExceptionMapper> + { + public IExceptionMapper get() + { + return new DefaultExceptionMapper(); + } + } + /** * */ @@ -1469,7 +1497,7 @@ public abstract class Application implem public final RequestCycle createRequestCycle(final Request request, final Response response) { RequestCycleContext context = new RequestCycleContext(request, response, - getRootRequestMapper(), newExceptionMapper()); + getRootRequestMapper(), getExceptionMapperProvider().get()); RequestCycle requestCycle = getRequestCycleProvider().get(context); requestCycle.getListeners().add(requestCycleListeners); @@ -1485,15 +1513,6 @@ public abstract class Application implem } /** - * @return a mapper that knows what kind of error page to show when an exception occurs during - * page rendering - */ - protected IExceptionMapper newExceptionMapper() - { - return new DefaultExceptionMapper(); - } - - /** * Sets an {...@link IHeaderResponseDecorator} that you want your application to use to decorate * header responses. * Added: wicket/trunk/wicket/src/test/java/org/apache/wicket/ExceptionMapperTest.java URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/ExceptionMapperTest.java?rev=1050482&view=auto ============================================================================== --- wicket/trunk/wicket/src/test/java/org/apache/wicket/ExceptionMapperTest.java (added) +++ wicket/trunk/wicket/src/test/java/org/apache/wicket/ExceptionMapperTest.java Fri Dec 17 20:00:51 2010 @@ -0,0 +1,140 @@ +/* + * 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.wicket; + +import org.apache.wicket.markup.IMarkupResourceStreamProvider; +import org.apache.wicket.markup.html.WebPage; +import org.apache.wicket.markup.html.link.Link; +import org.apache.wicket.request.IExceptionMapper; +import org.apache.wicket.request.IRequestHandler; +import org.apache.wicket.request.handler.PageProvider; +import org.apache.wicket.request.handler.RenderPageRequestHandler; +import org.apache.wicket.util.IProvider; +import org.apache.wicket.util.lang.Exceptions; +import org.apache.wicket.util.resource.IResourceStream; +import org.apache.wicket.util.resource.StringResourceStream; + +/** + */ +public class ExceptionMapperTest extends WicketTestCase +{ + /** + * Testing an custom exception mapper provider that return an wrapped exception mapper in order + * to catch special exceptions. It is important to enable other frameworks to plug in the + * application a custom response for their exceptions. + * + * @see <a href="https://issues.apache.org/jira/browse/WICKET-3256">WICKET-3256</a> + */ + public void testExceptionMapper() + { + Application app = tester.getApplication(); + WrapperProvider wrapper = new WrapperProvider(app.getExceptionMapperProvider()); + app.setExceptionMapperProvider(wrapper); + tester.setExposeExceptions(false); + tester.startPage(TestPage.class); + tester.clickLink(MockPageWithLink.LINK_ID); + tester.assertRenderedPage(TestExceptionPage.class); + } + + /** + */ + public static class WrapperProvider implements IProvider<IExceptionMapper> + { + private IProvider<IExceptionMapper> wrapped; + WrapperExceptionMapper wrapperExceptionMapper; + + /** + * @param wrapped + * exception mapper provider + */ + public WrapperProvider(IProvider<IExceptionMapper> wrapped) + { + this.wrapped = wrapped; + } + + public IExceptionMapper get() + { + return wrapperExceptionMapper = new WrapperExceptionMapper(wrapped.get()); + } + } + + /** + */ + public static class WrapperExceptionMapper implements IExceptionMapper + { + private IExceptionMapper wrapped; + + /** + * @param wrapped + * IExceptionMapper + */ + public WrapperExceptionMapper(IExceptionMapper wrapped) + { + this.wrapped = wrapped; + } + + public IRequestHandler map(Exception e) + { + if (Exceptions.findCause(e, TestException.class) != null) + { + return new RenderPageRequestHandler(new PageProvider(TestExceptionPage.class)); + } + return wrapped.map(e); + } + } + + + /** + */ + public static class TestException extends RuntimeException + { + private static final long serialVersionUID = 1L; + + } + + /** + */ + public static class TestExceptionPage extends WebPage implements IMarkupResourceStreamProvider + { + public IResourceStream getMarkupResourceStream(MarkupContainer container, + Class<?> containerClass) + { + return new StringResourceStream("<html><body></body></html>"); + } + } + + /** + */ + public static class TestPage extends MockPageWithLink + { + /** + */ + public TestPage() + { + add(new Link<Void>(LINK_ID) + { + private static final long serialVersionUID = 1L; + + @Override + public void onClick() + { + throw new TestException(); + } + }); + } + } +} \ No newline at end of file