cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vgritse...@apache.org
Subject svn commit: rev 36582 - cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/notification
Date Wed, 18 Aug 2004 22:48:29 GMT
Author: vgritsenko
Date: Wed Aug 18 15:48:28 2004
New Revision: 36582

Modified:
   cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/notification/DefaultNotifyingBuilder.java
   cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/notification/Notifier.java
   cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/notification/SimpleNotifyingBean.java
Log:
Escape notifying object fields when printing into html.
Full chain stack trace now prints full chain stacktrace in case of jdk13
(under jdk14, nested exception stacktrace is printed by Exception).
Use jakarta-commons-lang utils.


Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/notification/DefaultNotifyingBuilder.java
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/notification/DefaultNotifyingBuilder.java
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/notification/DefaultNotifyingBuilder.java
Wed Aug 18 15:48:28 2004
@@ -1,12 +1,12 @@
 /*
  * Copyright 1999-2004 The Apache Software Foundation.
- * 
+ *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -15,18 +15,17 @@
  */
 package org.apache.cocoon.components.notification;
 
-import org.apache.avalon.framework.CascadingThrowable;
 import org.apache.avalon.framework.component.Component;
 
-import org.xml.sax.SAXException;
+import org.apache.commons.lang.SystemUtils;
+import org.apache.commons.lang.exception.ExceptionUtils;
 import org.xml.sax.SAXParseException;
 
-import javax.xml.transform.TransformerException;
 import javax.xml.transform.SourceLocator;
-
+import javax.xml.transform.TransformerException;
 import java.io.PrintWriter;
 import java.io.StringWriter;
-
+import java.io.Writer;
 import java.util.Map;
 
 /**
@@ -34,13 +33,14 @@
  *
  * @author <a href="mailto:nicolaken@apache.org">Nicola Ken Barozzi</a>
  * @author Marc Liyanage (futureLAB AG)
- * @version CVS $Id: DefaultNotifyingBuilder.java,v 1.3 2004/03/05 13:02:49 bdelacretaz Exp
$
+ * @version CVS $Id$
  */
 public class DefaultNotifyingBuilder implements NotifyingBuilder, Component {
 
     /**
      * Builds a Notifying object (SimpleNotifyingBean in this case)
      * that tries to explain what the Object o can reveal.
+     *
      * @param sender who sent this Object.
      * @param o the object to use when building the SimpleNotifyingBean
      * @return the  Notifying Object that was build
@@ -54,25 +54,34 @@
             SimpleNotifyingBean n = new SimpleNotifyingBean(sender);
             n.setType(Notifying.ERROR_NOTIFICATION);
             n.setTitle("An Error Occurred");
+
             if (t != null) {
+                Throwable rootCause = getRootCause(t);
+
                 n.setSource(t.getClass().getName());
 
-                Throwable rootCauseThrowable = getRootCause(t);
-                n.addExtraDescription(Notifying.EXTRA_CAUSE, rootCauseThrowable.toString());
+                // NullPointerException usually does not have a message
+                if (rootCause.getMessage() != null) {
+                    n.setMessage(rootCause.getMessage());
+                } else {
+                    n.setMessage(t.getMessage());
+                }
+
+                n.setDescription(t.toString());
+                n.addExtraDescription(Notifying.EXTRA_CAUSE, rootCause.toString());
+
+                if (rootCause instanceof SAXParseException) {
+                    SAXParseException saxParseException = (SAXParseException) rootCause;
 
-                if (rootCauseThrowable instanceof SAXParseException) {
-                    SAXParseException saxParseException = (SAXParseException) rootCauseThrowable;
-                    n.setMessage         (                           saxParseException.getMessage()
     );
                     n.addExtraDescription(Notifying.EXTRA_LOCATION,
                                           String.valueOf(saxParseException.getSystemId()));
                     n.addExtraDescription(Notifying.EXTRA_LINE,
                                           String.valueOf(saxParseException.getLineNumber()));
                     n.addExtraDescription(Notifying.EXTRA_COLUMN,
                                           String.valueOf(saxParseException.getColumnNumber()));
-                } else if (rootCauseThrowable instanceof TransformerException) {
-                    TransformerException transformerException = (TransformerException) rootCauseThrowable;
+                } else if (rootCause instanceof TransformerException) {
+                    TransformerException transformerException = (TransformerException) rootCause;
                     SourceLocator sourceLocator = transformerException.getLocator();
-                    n.setMessage         (                           transformerException.getMessage()
  );
 
                     if (null != sourceLocator) {
                         n.addExtraDescription(Notifying.EXTRA_LOCATION,
@@ -82,33 +91,19 @@
                         n.addExtraDescription(Notifying.EXTRA_COLUMN,
                                               String.valueOf(sourceLocator.getColumnNumber()));
                     }
-                } else {
-                    n.setMessage(t.getMessage());
                 }
 
-                n.setDescription(t.toString());
-
-                // Get the stacktrace: if the exception is a SAXException,
-                // the stacktrace of the embedded exception is used as the
-                // SAXException does not append it automatically
-                Throwable stackTraceException;
-                if (t instanceof SAXException && ((SAXException) t).getException()
!= null) {
-                    stackTraceException = ((SAXException) t).getException();
-                } else {
-                    stackTraceException = t;
-                }
-                // org.apache.avalon.framework.ExceptionUtil.captureStackTrace();
+                // Add root cause exception stacktrace
                 StringWriter sw = new StringWriter();
-                stackTraceException.printStackTrace(new PrintWriter(sw));
+                rootCause.printStackTrace(new PrintWriter(sw));
                 n.addExtraDescription(Notifying.EXTRA_STACKTRACE, sw.toString());
-                // Add nested throwables description
+
+                // Add full exception chain
                 sw = new StringWriter();
-                appendCauses(new PrintWriter(sw), stackTraceException);
-                String causes = sw.toString();
-                if (causes != null && causes.length() != 0) {
-                    n.addExtraDescription(Notifying.EXTRA_FULLTRACE, causes);
-                }
+                appendTraceChain(sw, t);
+                n.addExtraDescription(Notifying.EXTRA_FULLTRACE, sw.toString());
             }
+
             return n;
         } else {
             SimpleNotifyingBean n = new SimpleNotifyingBean(sender);
@@ -123,6 +118,7 @@
     /**
      * Builds a Notifying object (SimpleNotifyingBean in this case)
      * that explains a notification.
+     *
      * @param sender who sent this Object.
      * @param o the object to use when building the SimpleNotifyingBean
      * @param type see the Notifying apidocs
@@ -157,43 +153,26 @@
 
 
     /**
-     * Print recursively all nested causes of a Throwable in a PrintWriter.
+     * Print stacktrace of the Throwable and stacktraces of its all nested causes into a
Writer.
      */
-    private static void appendCauses (PrintWriter out, Throwable t) {
-        Throwable cause = null;
-        if (t instanceof CascadingThrowable) {
-            cause = ((CascadingThrowable) t).getCause();
-        } else if (t instanceof SAXException) {
-            cause = ((SAXException) t).getException();
-        } else if (t instanceof java.sql.SQLException) {
-            cause = ((java.sql.SQLException) t).getNextException();
-        }
-        if (cause != null) {
-            out.print("Original Exception: ");
-            cause.printStackTrace(out);
-            out.println();
-            // Recurse
-            appendCauses(out, cause);
+    private static void appendTraceChain(Writer out, Throwable t) {
+        PrintWriter pw = new PrintWriter(out);
+        if (SystemUtils.isJavaVersionAtLeast(140)) {
+            t.printStackTrace(pw);
+        } else {
+            for (Throwable cause = t; cause != null; cause = ExceptionUtils.getCause(cause))
{
+                if (cause != t) {
+                    pw.println();
+                }
+                cause.printStackTrace(pw);
+            }
         }
     }
 
     /**
-     * Get root Exception.
+     * Get root cause Throwable.
      */
     public static Throwable getRootCause (Throwable t) {
-        Throwable cause = null;
-        if (t instanceof CascadingThrowable) {
-            cause = ((CascadingThrowable) t).getCause();
-        } else if (t instanceof SAXException) {
-            cause = ((SAXException) t).getException();
-        } else if (t instanceof java.sql.SQLException) {
-            cause = ((java.sql.SQLException) t).getNextException();
-        }
-        if (cause == null) {
-            return t;
-        } else {
-            // Recurse
-            return getRootCause(cause);
-        }
+        return ExceptionUtils.getRootCause(t);
     }
 }

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/notification/Notifier.java
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/notification/Notifier.java
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/notification/Notifier.java
Wed Aug 18 15:48:28 2004
@@ -1,12 +1,12 @@
 /*
  * Copyright 1999-2004 The Apache Software Foundation.
- * 
+ *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -17,39 +17,25 @@
 
 import org.apache.cocoon.Constants;
 
+import org.apache.commons.lang.StringEscapeUtils;
 import org.xml.sax.ContentHandler;
 import org.xml.sax.SAXException;
 import org.xml.sax.helpers.AttributesImpl;
 
 import java.io.IOException;
 import java.io.OutputStream;
-import java.util.Map;
 import java.util.Iterator;
+import java.util.Map;
 
 /**
  * Generates a representations of the specified Notifying Object.
  *
  * @author <a href="mailto:nicolaken@supereva.it">Nicola Ken Barozzi</a>
  * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
- * @version CVS $Id: Notifier.java,v 1.4 2004/03/05 13:02:49 bdelacretaz Exp $
+ * @version CVS $Id$
  */
 public class Notifier {
 
-    /*
-     * Generate notification information as a response.
-     * The notification is directly written to the OutputStream.
-     * @param  n The <code>Notifying</code> object
-     * @param outputStream The output stream the notification is written to
-     *        This could be <code>null</code>.
-     * @deprecated There is no way in which this method could understand what mime/type to
use. Instead use void notify(Notifying n, OutputStream outputStream, String mimetype), where
the mime/type is requested.
-     * @see #notify(Notifying n, OutputStream, String)
-
-    public static String notify(Notifying n, OutputStream outputStream) throws IOException
{
-      notify(n, outputStream, "text/html") ;
-      return "text/html";
-    }
-     */
-
     /**
      * Generate notification information as a response.
      * The notification is directly written to the OutputStream.
@@ -71,6 +57,10 @@
      *        This could be <code>null</code>.
      */
     private static void notifyHTML(Notifying n, OutputStream outputStream) throws IOException
{
+        if (outputStream == null) {
+            return;
+        }
+
         StringBuffer sb = new StringBuffer();
 
         sb.append("<html><head><title>").append(n.getTitle()).append("</title>");
@@ -86,38 +76,33 @@
         sb.append("a:active {color: #006666;}");
         sb.append("--></style>");
         sb.append("</head><body>");
-        sb.append("<h1>").append(n.getTitle()).append("</h1>");
-        sb.append("<p><span>Message:</span> ").append(n.getMessage()).append("</p>");
-        sb.append("<p><span>Description:</span> ").append(n.getDescription()).append("</p>");
-        sb.append("<p><span>Sender:</span> ").append(n.getSender()).append("</p>");
-        sb.append("<p><span>Source:</span> ").append(n.getSource()).append("</p>");
-
-        Map extraDescriptions = n.getExtraDescriptions();
-        Iterator keyIter = extraDescriptions.keySet().iterator();
-
-        while (keyIter.hasNext()) {
-            String key = (String) keyIter.next();
-
-            sb.append("<p><span>").append(key).append("</span><pre>").append(
-                    extraDescriptions.get(key)).append("</pre></p>");
+        sb.append("<h1>")
+          .append(StringEscapeUtils.escapeXml(n.getTitle())).append("</h1>");
+        sb.append("<p><span>Message:</span> ")
+          .append(StringEscapeUtils.escapeXml(n.getMessage())).append("</p>");
+        sb.append("<p><span>Description:</span> ")
+          .append(StringEscapeUtils.escapeXml(n.getDescription())).append("</p>");
+        sb.append("<p><span>Sender:</span> ")
+          .append(StringEscapeUtils.escapeXml(n.getSender())).append("</p>");
+        sb.append("<p><span>Source:</span> ")
+          .append(StringEscapeUtils.escapeXml(n.getSource())).append("</p>");
+
+        Map extras = n.getExtraDescriptions();
+        Iterator i = extras.keySet().iterator();
+        while (i.hasNext()) {
+            final String key = (String) i.next();
+
+            sb.append("<p><span>")
+              .append(key).append("</span><pre>")
+              .append(StringEscapeUtils.escapeXml(String.valueOf(extras.get(key))))
+              .append("</pre></p>");
         }
 
         sb.append("<p class='footer'><a href='http://cocoon.apache.org/'>").append(Constants.COMPLETE_NAME).append("</p>");
         sb.append("</body></html>");
 
-        if (outputStream != null)
-            outputStream.write(sb.toString().getBytes());
+        outputStream.write(sb.toString().getBytes());
     }
-
-    /*
-     * Generate notification information in XML format.
-     * @deprecated Using a ContentHandler doesn't mean that a mimetype cannot be specified;
it could be svg or
-     * @see #notify(Notifying, ContentHandler, String)
-
-    public static void notify(Notifying n, ContentHandler ch) throws SAXException {
-      notify(n, ch, "text/xml");
-    }
-     */
 
     /**
      * Generate notification information in XML format.

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/notification/SimpleNotifyingBean.java
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/notification/SimpleNotifyingBean.java
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/notification/SimpleNotifyingBean.java
Wed Aug 18 15:48:28 2004
@@ -1,12 +1,12 @@
 /*
  * Copyright 1999-2004 The Apache Software Foundation.
- * 
+ *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -22,7 +22,7 @@
  *  A simple bean implementation of Notifying.
  *
  * @author <a href="mailto:barozzi@nicolaken.com">Nicola Ken Barozzi</a>
- * @version CVS $Id: SimpleNotifyingBean.java,v 1.2 2004/03/05 13:02:49 bdelacretaz Exp $
+ * @version CVS $Id$
  */
 public class SimpleNotifyingBean implements Notifying {
 
@@ -34,17 +34,17 @@
     /**
      * The title of the notification.
      */
-    private String title = "";
+    private String title = "Generic notification";
 
     /**
      * The source that generates the notification.
      */
-    private String source = "";
+    private String source = getClass().getName();
 
     /**
      * The sender of the notification.
      */
-    private String sender = "";
+    private String sender = "unknown";
 
     /**
      * The notification itself.
@@ -54,7 +54,7 @@
     /**
      * A more detailed description of the notification.
      */
-    private String description = "No details available.";
+    private String description = "";
 
     /**
      * A HashMap containing extra notifications
@@ -63,15 +63,10 @@
 
 
     public SimpleNotifyingBean() {
-        this.sender = "unknown";
-        setSource(this.getClass().getName());
-        setTitle("Generic notification");
     }
 
     public SimpleNotifyingBean(Object sender) {
         this.sender = sender.getClass().getName();
-        setSource(this.getClass().getName());
-        setTitle("Generic notification");
     }
 
     /**
@@ -100,24 +95,6 @@
     public void setSource(String source) {
         this.source = source;
     }
-
-    /**
-     * Sets the Sender of the SimpleNotifyingBean object
-     *
-     * @param  sender  The new sender value
-    private void setSender(Object sender) {
-        this.sender = sender.getClass().getName();
-    }
-     */
-
-    /**
-     * Sets the Sender of the SimpleNotifyingBean object
-     *
-     * @param  sender  The new sender value
-    private void setSender(String sender) {
-        this.sender = sender;
-    }
-     */
 
     /**
      * Sets the Message of the SimpleNotifyingBean object

Mime
View raw message