[logback-dev] svn commit: r2110 - in logback/trunk: logback-core/src/main/java/ch/qos/logback/core/net logback-core/src/main/java/ch/qos/logback/core/util logback-core/src/test/java/ch/qos/logback/core/util logback-site/src/site/pages/manual

noreply.ceki at qos.ch noreply.ceki at qos.ch
Mon Dec 29 15:07:56 CET 2008


Author: ceki
Date: Mon Dec 29 15:07:56 2008
New Revision: 2110

Added:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/ContentTypeUtil.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/ContentTypeUtilTest.java
Modified:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SMTPAppenderBase.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/PackageTest.java
   logback/trunk/logback-site/src/site/pages/manual/appenders.html

Log:
- add charset encoding support for SMTPAppenderBase. This fixes LBCORE-69.

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SMTPAppenderBase.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SMTPAppenderBase.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SMTPAppenderBase.java	Mon Dec 29 15:07:56 2008
@@ -30,8 +30,12 @@
 import ch.qos.logback.core.Layout;
 import ch.qos.logback.core.boolex.EvaluationException;
 import ch.qos.logback.core.boolex.EventEvaluator;
+import ch.qos.logback.core.util.ContentTypeUtil;
 import ch.qos.logback.core.util.OptionHelper;
 
+// Contributors:
+// Andrey Rybin charset encoding support http://jira.qos.ch/browse/LBCORE-69
+
 /**
  * An abstract class that provides support for sending events to an email
  * address.
@@ -41,7 +45,6 @@
  * 
  * @author Ceki Gülcü
  * @author Sébastien Pennec
- * 
  */
 public abstract class SMTPAppenderBase<E> extends AppenderBase<E> {
 
@@ -59,7 +62,9 @@
   String username;
   String password;
 
-  protected Message msg;
+  private String charsetEncoding = "UTF-8";
+
+  protected MimeMessage mimeMsg;
 
   protected EventEvaluator<E> eventEvaluator;
 
@@ -109,16 +114,16 @@
     // props.put("mail.debug", "true");
 
     Session session = Session.getInstance(props, loginAuthenticator);
-    msg = new MimeMessage(session);
+    mimeMsg = new MimeMessage(session);
 
     try {
       if (from != null) {
-        msg.setFrom(getAddress(from));
+        mimeMsg.setFrom(getAddress(from));
       } else {
-        msg.setFrom();
+        mimeMsg.setFrom();
       }
 
-      msg.setRecipients(Message.RecipientType.TO, parseAddress(to));
+      mimeMsg.setRecipients(Message.RecipientType.TO, parseAddress(to));
 
       subjectLayout = makeSubjectLayout(subjectStr);
 
@@ -166,7 +171,7 @@
       return false;
     }
 
-    if (this.msg == null) {
+    if (this.mimeMsg == null) {
       addError("Message object not configured.");
       return false;
     }
@@ -254,17 +259,25 @@
       }
 
       if (subjectLayout != null) {
-        msg.setSubject(subjectLayout.doLayout(lastEventObject));
+        mimeMsg.setSubject(subjectLayout.doLayout(lastEventObject),
+            charsetEncoding);
       }
 
-      part.setContent(sbuf.toString(), layout.getContentType());
+      String contentType = layout.getContentType();
+
+      if (ContentTypeUtil.isTextual(contentType)) {
+        part.setText(sbuf.toString(), charsetEncoding, ContentTypeUtil
+            .getSubType(contentType));
+      } else {
+        part.setContent(sbuf.toString(), layout.getContentType());
+      }
 
       Multipart mp = new MimeMultipart();
       mp.addBodyPart(part);
-      msg.setContent(mp);
+      mimeMsg.setContent(mp);
 
-      msg.setSentDate(new Date());
-      Transport.send(msg);
+      mimeMsg.setSentDate(new Date());
+      Transport.send(mimeMsg);
     } catch (Exception e) {
       addError("Error occured while sending e-mail notification.", e);
     }
@@ -344,12 +357,12 @@
 
   // for testing purpose only
   public Message getMessage() {
-    return msg;
+    return mimeMsg;
   }
 
   // for testing purpose only
-  public void setMessage(Message msg) {
-    this.msg = msg;
+  public void setMessage(MimeMessage msg) {
+    this.mimeMsg = msg;
   }
 
   public boolean isSTARTTLS() {
@@ -402,4 +415,22 @@
     this.password = password;
   }
 
+  /**
+   * @see #setCharsetEncoding(String)
+   * @return the charset encoding value
+   */
+  String getCharsetEncoding() {
+    return charsetEncoding;
+  }
+
+  /**
+   * Set the character set encoding of the outgoing email messages. The default
+   * encoding is "UTF-8" which usually works well for most purposes.
+   * 
+   * @param charsetEncoding
+   */
+  void setCharsetEncoding(String charsetEncoding) {
+    this.charsetEncoding = charsetEncoding;
+  }
+
 }

Added: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/ContentTypeUtil.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/ContentTypeUtil.java	Mon Dec 29 15:07:56 2008
@@ -0,0 +1,43 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
+ * Copyright (C) 2000-2008, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package ch.qos.logback.core.util;
+
+/**
+ * Various utility methods for processing strings representing context types.
+ * 
+ * @author Ceki Gulcu
+ * 
+ */
+public class ContentTypeUtil {
+
+  public static boolean isTextual(String contextType) {
+    if (contextType == null) {
+      return false;
+    }
+    return contextType.startsWith("text");
+  }
+
+  public static String getSubType(String contextType) {
+    if (contextType == null) {
+      return null;
+    }
+    int index = contextType.indexOf('/');
+    if (index == -1) {
+      return null;
+    } else {
+      int subTypeStartIndex = index + 1;
+      if (subTypeStartIndex < contextType.length()) {
+        return contextType.substring(subTypeStartIndex);
+      } else {
+        return null;
+      }
+    }
+  }
+}

Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/ContentTypeUtilTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/ContentTypeUtilTest.java	Mon Dec 29 15:07:56 2008
@@ -0,0 +1,40 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
+ * Copyright (C) 2000-2008, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package ch.qos.logback.core.util;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+
+public class ContentTypeUtilTest {
+
+  
+  @Test
+  public void smoke() {
+    String contextType = "text/html";
+    assertTrue(ContentTypeUtil.isTextual(contextType));
+    assertEquals("html", ContentTypeUtil.getSubType(contextType));
+  }
+  
+  @Test
+  public void nullContext() {
+    String contextType = null;
+    assertFalse(ContentTypeUtil.isTextual(contextType));
+    assertNull(ContentTypeUtil.getSubType(contextType));
+  }
+  
+  @Test
+  public void emptySubtype() {
+    String contextType = "text/";
+    assertTrue(ContentTypeUtil.isTextual(contextType));
+    assertNull(ContentTypeUtil.getSubType(contextType));
+  }
+}

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/PackageTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/PackageTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/PackageTest.java	Mon Dec 29 15:07:56 2008
@@ -20,6 +20,7 @@
   FileUtilTest.class,
   OptionHelperTest.class,
   StatusPrinterTest.class,
-  TimeUtilTest.class})
+  TimeUtilTest.class,
+  ContentTypeUtilTest.class})
 public class PackageTest {
 }

Modified: logback/trunk/logback-site/src/site/pages/manual/appenders.html
==============================================================================
--- logback/trunk/logback-site/src/site/pages/manual/appenders.html	(original)
+++ logback/trunk/logback-site/src/site/pages/manual/appenders.html	Mon Dec 29 15:07:56 2008
@@ -1994,6 +1994,7 @@
         <th>Type</th>
         <th>Description</th>
       </tr>
+
       <tr>
         <td><b><span class="option">SMTPHost</span></b></td>
         <td><code>String</code></td>
@@ -2124,6 +2125,18 @@
         </td>
       </tr>
 
+      <tr  class="alt">
+        <td><b><span class="option">CharsetEncoding</span></b></td>
+        <td><code>String</code></td>
+        <td>The outgoing email message will be encoded in the
+        designated <a
+        href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html">charset</a>. The
+        default charset encoding is "UTF-8" which works well for most
+        purposes.
+        </td>
+      </tr>
+
+
 		</table>		
 		
 		<p>The SMTPAppender keeps only the last <span


More information about the logback-dev mailing list