[cvs] expresso commit by rimovm: Missing PrintException tag.

JCorporate Ltd jcorp at jcorporate.com
Fri Mar 18 23:21:48 UTC 2005


Log Message:
-----------
Missing PrintException tag.

Added Files:
-----------
    expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/services/taglib:
        PrintExceptionTag.java

Revision Data
-------------
--- /dev/null
+++ expresso-web/WEB-INF/src/com/jcorporate/expresso/services/taglib/PrintExceptionTag.java
@@ -0,0 +1,259 @@
+package com.jcorporate.expresso.services.taglib;
+
+/* ====================================================================
+ * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
+ *
+ * Copyright (c) 1995-2003 Jcorporate Ltd. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ *    if any, must include the following acknowledgment:
+ *       "This product includes software developed by Jcorporate Ltd.
+ *        (http://www.jcorporate.com/)."
+ *    Alternately, this acknowledgment may appear in the software itself,
+ *    if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. "Jcorporate" and product names such as "Expresso" must
+ *    not be used to endorse or promote products derived from this
+ *    software without prior written permission. For written permission,
+ *    please contact info at jcorporate.com.
+ *
+ * 5. Products derived from this software may not be called "Expresso",
+ *    or other Jcorporate product names; nor may "Expresso" or other
+ *    Jcorporate product names appear in their name, without prior
+ *    written permission of Jcorporate Ltd.
+ *
+ * 6. No product derived from this software may compete in the same
+ *    market space, i.e. framework, without prior written permission
+ *    of Jcorporate Ltd. For written permission, please contact
+ *    partners at jcorporate.com.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED.  IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Jcorporate Ltd. Contributions back
+ * to the project(s) are encouraged when you make modifications.
+ * Please send them to support at jcorporate.com. For more information
+ * on Jcorporate Ltd. and its products, please see
+ * <http://www.jcorporate.com/>.
+ *
+ * Portions of this software are based upon other open source
+ * products and are subject to their respective licenses.
+ */
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+import javax.servlet.ServletRequest;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+import javax.servlet.jsp.JspException;
+import javax.servlet.jsp.tagext.TagSupport;
+import com.jcorporate.expresso.core.misc.ConfigManager;
+import com.jcorporate.expresso.core.misc.ConfigurationException;
+import com.jcorporate.expresso.core.misc.CurrentLogin;
+import com.jcorporate.expresso.core.misc.StringUtil;
+import com.jcorporate.expresso.kernel.util.FastStringBuffer;
+import org.apache.log4j.Logger;
+
+/**
+ * This tag prints the exception to the Expresso log and if StackTrace
+ * printing is defined in the expresso configuration files, print it to
+ * the JSP as well.  Use it in your error pages.
+ * <p>Simply use it like so:
+ * <blockquote><pre>
+ * <expresso:exception/>
+ * </pre><blockquote>
+ * </p>
+ */
+public class PrintExceptionTag
+    extends TagSupport
+{
+
+    /**
+     * The one and only logger instance.
+     */
+    private static final Logger LOG = Logger.getLogger("com.jcorporate.expresso.jsp");
+
+    /**
+     * Default Constructor.
+     */
+    public PrintExceptionTag()
+    {
+        super();
+    }
+
+
+    /**
+     * Process the end tag for this instance.
+     *
+     * @return indication of whether to continue evaluating the JSP page.
+     * @throws JspException if an error occurred while processing this tag
+     */
+    public int doEndTag()
+        throws JspException
+    {
+        Throwable exception = this.pageContext.getException();
+
+        //We allocate instead of relying on the pool since there is always
+        //a chance that the pool was corrupted and that's why we're here.
+        FastStringBuffer stringBuffer = new FastStringBuffer(256);
+        try {
+            if (shouldPrintStackTrace()) {
+                stringBuffer.append("<li>");
+                stringBuffer.append("You can examine the 'stack trace'"
+                    + "(programmer information) below for more information about this problem. This stack"
+                    +" trace has been logged.");
+                stringBuffer.append("</li>");
+
+                stringBuffer.append("<blockquote>");
+                if (exception != null) {
+                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
+                    exception.printStackTrace(new PrintStream(bos));
+                    stringBuffer.append((bos.toString()));
+                } else {
+                    stringBuffer.append("<i>[No exception available in session]</i>");
+                }
+                stringBuffer.append("</blockquote>");
+
+            } else {
+                stringBuffer.append("<p>");
+                stringBuffer.append("Your error has been logged. ");
+                stringBuffer.append("</p>");
+            }
+
+            try {
+                this.pageContext.getOut().println(stringBuffer.toString());
+            } catch (IOException ex) {
+                throw new JspException("I/O Error writing output stream to client");
+            }
+
+        } finally {
+            stringBuffer.release();
+        }
+
+        return EVAL_PAGE;
+    }
+
+    /**
+     * Returns true if the stack trace should be printed.  If there is an error,
+     * then stack trace should only be logged to system logs and not printed
+     * for the user. (Secure Default Value)
+     * @return boolean true if everything succeeds and showStackTrace is
+     * defined as true/y in the config file for the current context.
+     */
+    private boolean shouldPrintStackTrace()
+    {
+        try {
+            HttpSession session = pageContext.getSession();
+            CurrentLogin cl = (CurrentLogin) session.getAttribute(CurrentLogin.LOGIN_KEY);
+
+            String databaseContext = null;
+            if (cl != null) {
+                databaseContext = cl.getDBName();
+            }
+
+            if (databaseContext == null) {
+                databaseContext = "default";
+            }
+
+            boolean returnValue;
+            //First try getting database context the normal way.
+            try {
+                returnValue = ConfigManager.getContext(databaseContext).showStackTrace();
+            } catch (ConfigurationException ex) {
+
+                //If that fails, try to grab the 'default' context.
+                try {
+                    returnValue = ConfigManager.getContext("default").showStackTrace();
+                } catch (ConfigurationException ex2) {
+
+                    //If that fails, log, and return false.
+                    LOG.error("Error getting database context " + databaseContext
+                        + "we are not printing a stack trace on the JSP", ex2);
+                    returnValue = false;
+                }
+            }
+
+            return returnValue;
+        } catch (Throwable t) {
+            //Catch throwables because we don't want the original error lost in
+            //translation :)
+            LOG.error("Error grabbing 'should print stack trace info' defaulting to secure 'not print stack trace'"
+                , t);
+            return false;
+        }
+    }
+
+    /**
+     * Process the start tag for this instance.  Prints the exception to the
+     * log first thing.
+     *
+     * @return  SKIP_BODY
+     * @throws JspException if an error occurred while processing this tag
+     */
+    public int doStartTag()
+        throws JspException
+    {
+        ServletRequest request = this.pageContext.getRequest();
+        HttpServletRequest httpRequest = null;
+        if (request instanceof HttpServletRequest) {
+            httpRequest = (HttpServletRequest) request;
+        }
+        Exception exception = this.pageContext.getException();
+
+        try {
+            FastStringBuffer fsb = new FastStringBuffer(256);
+            if (httpRequest != null) {
+                fsb.append("JSP Page Exception: user requested '");
+                fsb.append(StringUtil.notNull(httpRequest.getRequestURI()));
+                String queryString = StringUtil.notNull(httpRequest.getQueryString());
+                if (queryString.length() > 0) {
+                    fsb.append("?");
+                    fsb.append(queryString);
+                }
+                fsb.append("'");
+            } else {
+                fsb.append("Servlet Exception");
+            }
+
+            if (exception != null) {
+                LOG.error(fsb.toString(), exception);
+            } else {
+                LOG.error(fsb.toString());
+            }
+        } catch (Throwable e) {
+            LOG.error("Error printing error information for JSP page", e);
+            LOG.error("Original exception: ", this.pageContext.getException());
+            System.err.println("Error attempting to form basic logging information: " + e.getMessage());
+
+        }
+
+        return SKIP_BODY;
+    }
+
+
+}


More information about the cvs mailing list