001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.tags.html;
020:
021: import org.apache.beehive.netui.util.internal.InternalStringBuilder;
022:
023: import org.apache.beehive.netui.pageflow.internal.AdapterManager;
024: import org.apache.beehive.netui.tags.AbstractSimpleTag;
025: import org.apache.beehive.netui.util.HtmlExceptionFormatter;
026: import org.apache.struts.Globals;
027: import org.apache.struts.util.ResponseUtils;
028:
029: import javax.servlet.ServletRequest;
030: import javax.servlet.jsp.JspException;
031: import javax.servlet.jsp.PageContext;
032:
033: /**
034: * Renders formatted exception data, as found in the Request with the key:
035: * <code>org.apache.struts.action.Action.EXCEPTION_KEY</code>. Exceptions ignores its
036: * body content.
037: * @jsptagref.tagdescription Renders exception messages and stack traces inline on the JSP page.
038: * @example In this sample, the <netui:exceptions> tag will output the exception title and message,
039: * but not the stacktraces.
040: * <pre><netui:exceptions showMessage="true" showStackTrace="false" /></pre>
041: * @netui:tag name="exceptions" body-content="empty" description="Displays formatted exception messages."
042: */
043: public class Exceptions extends AbstractSimpleTag {
044: private boolean _showMessage = true;
045: private boolean _showStackTrace = false;
046: private boolean _showDevModeStackTrace = false;
047:
048: /**
049: * Return the name of the Tag.
050: */
051: public String getTagName() {
052: return "Exceptions";
053: }
054:
055: /**
056: * Set whether or not the exception message is being shown.
057: * @param showMessage true or false depending on the setting desired
058: * @jsptagref.attributedescription Boolean. Determines whether or not the exception message is shown. Defaults to <code>true</code>
059: * @jsptagref.databindable false
060: * @jsptagref.attributesyntaxvalue <i>boolean_showMessage</i>
061: * @netui:attribute required="false" rtexprvalue="true" type="boolean"
062: * description="Determines whether or not the exception message is shown. Defaults to true."
063: */
064: public void setShowMessage(boolean showMessage) {
065: _showMessage = showMessage;
066: }
067:
068: /**
069: * Set whether or not the stack trace is being shown.
070: * @param showStackTrace true or false depending on the setting desired
071: * @jsptagref.attributedescription Boolean. Determines whether or not the stack trace is shown. Defaults to <code>false</code>
072: * @jsptagref.databindable false
073: * @jsptagref.attributesyntaxvalue <i>boolean_showStackTrace</i>
074: * @netui:attribute required="false" rtexprvalue="true" type="boolean"
075: * description="Determines whether or not the stack trace is shown. Defaults to false."
076: */
077: public void setShowStackTrace(boolean showStackTrace) {
078: _showStackTrace = showStackTrace;
079: }
080:
081: /**
082: * Set whether or not the stack trace is being shown.
083: * @param showStackTrace true or false depending on the setting desired
084: * @jsptagref.attributedescription Boolean. Determine if the stack trace is display only when dev mode
085: * is on. Default is <code>true</code>.
086: * @jsptagref.databindable false
087: * @jsptagref.attributesyntaxvalue <i>boolean_showDevModeStackTrace</i>
088: * @netui:attribute required="false" rtexprvalue="true" type="boolean"
089: * description="Determine if the stack trace is display only when dev mode is on. Default is true."
090: */
091: public void setShowDevModeStackTrace(boolean showStackTrace) {
092: _showDevModeStackTrace = showStackTrace;
093: }
094:
095: /**
096: * Render the exception text based on the display attributes.
097: * @throws JspException if a JSP exception has occurred
098: */
099: public void doTag() throws JspException {
100: // First look for the exception in the pageflow/struts request attribute. If it's not there,
101: // look for it in the request attribute the container provides for web.xml-configured error
102: // pages.
103: InternalStringBuilder results = new InternalStringBuilder(128);
104: PageContext pageContext = getPageContext();
105: Throwable e = (Throwable) pageContext.getAttribute(
106: Globals.EXCEPTION_KEY, PageContext.REQUEST_SCOPE);
107:
108: if (e == null) {
109: ServletRequest req = pageContext.getRequest();
110: e = (Throwable) req
111: .getAttribute("javax.servlet.error.exception");
112: if (e == null) {
113: e = (Throwable) req
114: .getAttribute("javax.servlet.jsp.jspException");
115: }
116: }
117:
118: if (!_showStackTrace && _showDevModeStackTrace) {
119: boolean devMode = !AdapterManager
120: .getServletContainerAdapter(
121: pageContext.getServletContext())
122: .isInProductionMode();
123: if (devMode)
124: _showStackTrace = true;
125: }
126:
127: if (e != null) {
128: if (_showMessage) {
129: String msg = e.getMessage();
130: // if we have message lets output the exception name and the name of the
131: if ((msg != null) && (msg.length() > 0)) {
132: if (!_showStackTrace)
133: msg = e.getClass().getName() + ": " + msg;
134: results.append(HtmlExceptionFormatter.format(msg,
135: e, _showStackTrace));
136: } else {
137: results.append(HtmlExceptionFormatter.format(e
138: .getClass().getName(), e, _showStackTrace));
139: }
140: } else {
141: results.append(HtmlExceptionFormatter.format(null, e,
142: _showStackTrace));
143: }
144: ResponseUtils.write(pageContext, results.toString());
145: }
146: }
147: }
|