001: /*
002: * $Id: DefaultExceptionResponseStrategy.java,v 1.1 2005/11/27 09:20:16 eelco12
003: * Exp $ $Revision: 461835 $ $Date: 2006-08-15 17:45:42 +0200 (Tue, 15 Aug 2006) $
004: *
005: * ==============================================================================
006: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007: * use this file except in compliance with the License. You may obtain a copy of
008: * the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package wicket.request.compound;
019:
020: import wicket.Application;
021: import wicket.Page;
022: import wicket.RequestCycle;
023: import wicket.RestartResponseAtInterceptPageException;
024: import wicket.RestartResponseException;
025: import wicket.Session;
026: import wicket.WicketRuntimeException;
027: import wicket.authorization.AuthorizationException;
028: import wicket.markup.html.pages.ExceptionErrorPage;
029: import wicket.settings.IExceptionSettings;
030:
031: /**
032: * Default implementation of
033: * {@link wicket.request.compound.IExceptionResponseStrategy}. Depending on the
034: * setting it returns a (pluggable) exception page or a blank page, and it
035: * passes the exception through to the current request cycle by calling
036: * {@link wicket.RequestCycle#onRuntimeException(Page, RuntimeException)}.
037: *
038: * @author Eelco Hillenius
039: * @author Johan Compagner
040: * @author Igor Vaynberg (ivaynberg)
041: */
042: public class DefaultExceptionResponseStrategy implements
043: IExceptionResponseStrategy {
044:
045: /**
046: * Construct.
047: */
048: public DefaultExceptionResponseStrategy() {
049: }
050:
051: /**
052: * @see wicket.request.compound.IExceptionResponseStrategy#respond(wicket.RequestCycle,
053: * java.lang.RuntimeException)
054: */
055: public final void respond(final RequestCycle requestCycle,
056: final RuntimeException e) {
057: // If application doesn't want debug info showing up for users
058: final Session session = requestCycle.getSession();
059: final Application application = session.getApplication();
060: final IExceptionSettings settings = application
061: .getExceptionSettings();
062: final Page responsePage = requestCycle.getResponsePage();
063:
064: Page override = onRuntimeException(responsePage, e);
065: if (override != null) {
066: throw new RestartResponseException(override);
067: } else if (e instanceof AuthorizationException) {
068: // are authorization exceptions always thrown before the real render?
069: // else we need to make a page (see below) or set it hard to a redirect.
070: Class accessDeniedPageClass = application
071: .getApplicationSettings().getAccessDeniedPage();
072:
073: throw new RestartResponseAtInterceptPageException(
074: accessDeniedPageClass);
075: } else if (settings.getUnexpectedExceptionDisplay() != IExceptionSettings.SHOW_NO_EXCEPTION_PAGE) {
076: // we do not want to redirect - we want to inline the error output
077: // and preserve the url so when the refresh button is pressed we
078: // rerun the code that caused the error
079: requestCycle.setRedirect(false);
080:
081: // figure out which error page to show
082: Class internalErrorPageClass = application
083: .getApplicationSettings().getInternalErrorPage();
084: Class responseClass = responsePage != null ? responsePage
085: .getClass() : null;
086:
087: if (responseClass != internalErrorPageClass
088: && settings.getUnexpectedExceptionDisplay() == IExceptionSettings.SHOW_INTERNAL_ERROR_PAGE) {
089: throw new RestartResponseException(
090: internalErrorPageClass);
091: } else if (responseClass != ExceptionErrorPage.class) {
092: // Show full details
093: throw new RestartResponseException(
094: new ExceptionErrorPage(e, responsePage));
095: } else {
096: // give up while we're ahead!
097: throw new WicketRuntimeException(
098: "Internal Error: Could not render error page "
099: + internalErrorPageClass, e);
100: }
101: }
102: }
103:
104: /**
105: * This method is called when a runtime exception is thrown, just before the
106: * actual handling of the runtime exception. This implemention passes the
107: * call through to
108: * {@link RequestCycle#onRuntimeException(Page, RuntimeException)}. Note
109: * that if you override this method or provide a whole new implementation of
110: * {@link IExceptionResponseStrategy} alltogether,
111: * {@link RequestCycle#onRuntimeException(Page, RuntimeException)} will not
112: * be supported.
113: *
114: * @param page
115: * Any page context where the exception was thrown
116: * @param e
117: * The exception
118: * @return Any error page to redirect to
119: */
120: protected Page onRuntimeException(final Page page,
121: final RuntimeException e) {
122: return RequestCycle.get().onRuntimeException(page, e);
123: }
124: }
|