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