001: /*
002: * $Id: ExceptionErrorPage.java 459117 2006-02-09 13:16:04Z jcompagner $
003: * $Revision: 459117 $ $Date: 2006-02-09 14:16:04 +0100 (Thu, 09 Feb 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.markup.html.pages;
019:
020: import javax.servlet.http.HttpServletResponse;
021:
022: import wicket.Page;
023: import wicket.markup.MarkupException;
024: import wicket.markup.MarkupStream;
025: import wicket.markup.html.WebMarkupContainer;
026: import wicket.markup.html.WebPage;
027: import wicket.markup.html.basic.Label;
028: import wicket.markup.html.basic.MultiLineLabel;
029: import wicket.markup.html.debug.PageView;
030: import wicket.util.string.Strings;
031:
032: /**
033: * Shows a runtime exception on a nice HTML page.
034: *
035: * @author Jonathan Locke
036: */
037: public class ExceptionErrorPage extends WebPage {
038: private static final long serialVersionUID = 1L;
039:
040: /** Keep a reference to the root cause. WicketTester will use it */
041: private transient Throwable throwable;
042:
043: /**
044: * Constructor.
045: *
046: * @param throwable
047: * The exception to show
048: * @param page
049: * The page being rendered when the exception was thrown
050: */
051: public ExceptionErrorPage(final Throwable throwable, final Page page) {
052: this .throwable = throwable;
053:
054: // Add exception label
055: add(new MultiLineLabel("exception", Strings.toString(throwable)));
056:
057: // Get values
058: String resource = "";
059: String markup = "";
060: MarkupStream markupStream = null;
061:
062: if (throwable instanceof MarkupException) {
063: markupStream = ((MarkupException) throwable)
064: .getMarkupStream();
065:
066: if (markupStream != null) {
067: markup = markupStream.toHtmlDebugString();
068: resource = markupStream.getResource().toString();
069: }
070: }
071:
072: // Create markup label
073: final MultiLineLabel markupLabel = new MultiLineLabel("markup",
074: markup);
075:
076: markupLabel.setEscapeModelStrings(false);
077:
078: // Add container with markup highlighted
079: final WebMarkupContainer markupHighlight = new WebMarkupContainer(
080: "markupHighlight");
081:
082: markupHighlight.add(markupLabel);
083: markupHighlight.add(new Label("resource", resource));
084: add(markupHighlight);
085:
086: // Show container if markup stream is available
087: markupHighlight.setVisible(markupStream != null);
088:
089: // Show component tree of the page
090: if (page != null) {
091: add(new PageView("componentTree", page));
092: } else {
093: add(new Label("componentTree", ""));
094: }
095: }
096:
097: /**
098: * @see wicket.markup.html.WebPage#configureResponse()
099: */
100: protected void configureResponse() {
101: super .configureResponse();
102: getWebRequestCycle()
103: .getWebResponse()
104: .getHttpServletResponse()
105: .setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
106: }
107:
108: /**
109: * Get access to the exception
110: *
111: * @return The exception
112: */
113: public Throwable getThrowable() {
114: return throwable;
115: }
116:
117: /**
118: * @see wicket.Page#isErrorPage()
119: */
120: public boolean isErrorPage() {
121: return true;
122: }
123:
124: /**
125: * @see wicket.Component#isVersioned()
126: */
127: public boolean isVersioned() {
128: return false;
129: }
130: }
|