geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rickmcgu...@apache.org
Subject svn commit: r669902 - in /geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src: main/java/org/apache/geronimo/javamail/handlers/ test/java/org/apache/geronimo/javamail/handlers/
Date Fri, 20 Jun 2008 14:04:41 GMT
Author: rickmcguire
Date: Fri Jun 20 07:04:41 2008
New Revision: 669902

URL: http://svn.apache.org/viewvc?rev=669902&view=rev
Log:
GERONIMO-4136 Encoding coversion issues with geronimo javamail

Uncovered by the James test suite. 


Modified:
    geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/handlers/AbstractTextHandler.java
    geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/handlers/AbstractHandler.java
    geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/handlers/TextHtmlTest.java
    geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/handlers/TextPlainTest.java
    geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/handlers/TextXmlTest.java

Modified: geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/handlers/AbstractTextHandler.java
URL: http://svn.apache.org/viewvc/geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/handlers/AbstractTextHandler.java?rev=669902&r1=669901&r2=669902&view=diff
==============================================================================
--- geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/handlers/AbstractTextHandler.java
(original)
+++ geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/handlers/AbstractTextHandler.java
Fri Jun 20 07:04:41 2008
@@ -24,10 +24,14 @@
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.Reader;
+import java.io.UnsupportedEncodingException;
 
 import javax.activation.DataContentHandler;
 import javax.activation.DataSource;
 
+import javax.mail.internet.ContentType; 
+import javax.mail.internet.MimeUtility;
+
 /**
  * @version $Rev$ $Date$
  */
@@ -46,9 +50,26 @@
         return flavour.equals(dataFlavor) ? getContent(dataSource) : null;
     }
 
+    /**
+     * Read the content from the DataSource and transform 
+     * it into a text object (String). 
+     * 
+     * @param ds     The source DataSource.
+     * 
+     * @return The content object. 
+     * @exception IOException
+     */
     public Object getContent(DataSource ds) throws IOException {
         InputStream is = ds.getInputStream(); 
-        Reader reader = new InputStreamReader(is);
+        InputStreamReader reader;
+        // process any encoding to make sure the chars get transformed into the 
+        // correct byte types. 
+        try {
+            String charset = getCharSet(ds.getContentType());
+            reader = new InputStreamReader(is, charset);
+        } catch (Exception ex) {
+            throw new UnsupportedEncodingException(ex.toString());
+        }
         StringBuffer result = new StringBuffer(1024);
         char[] buffer = new char[32768];
         int count;
@@ -58,7 +79,21 @@
         return result.toString();
     }
 
-    public void writeTo(Object o, String mimeType, OutputStream os) throws IOException {
+    
+    /**
+     * Write an object of "our" type out to the provided 
+     * output stream.  The content type might modify the 
+     * result based on the content type parameters. 
+     * 
+     * @param object The object to write.
+     * @param contentType
+     *               The content mime type, including parameters.
+     * @param outputstream
+     *               The target output stream.
+     * 
+     * @throws IOException
+     */
+    public void writeTo(Object o, String contentType, OutputStream outputstream) throws IOException
{
         String s;
         if (o instanceof String) {
             s = (String) o;
@@ -67,9 +102,33 @@
         } else {
             return;
         }
-        // todo handle encoding
-        OutputStreamWriter writer = new OutputStreamWriter(os);
+        // process any encoding to make sure the chars get transformed into the 
+        // correct byte types. 
+        OutputStreamWriter writer;
+        try {
+            String charset = getCharSet(contentType);
+            writer = new OutputStreamWriter(outputstream, charset);
+        } catch (Exception ex) {
+            ex.printStackTrace(); 
+            throw new UnsupportedEncodingException(ex.toString());
+        }
         writer.write(s);
         writer.flush();
     }
+    
+
+    /**
+     * get the character set from content type
+     * @param contentType
+     * @return
+     * @throws ParseException
+     */
+    protected String getCharSet(String contentType) throws Exception {
+        ContentType type = new ContentType(contentType);
+        String charset = type.getParameter("charset");
+        if (charset == null) {
+            charset = "us-ascii";
+        }
+        return MimeUtility.javaCharset(charset);
+    }
 }

Modified: geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/handlers/AbstractHandler.java
URL: http://svn.apache.org/viewvc/geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/handlers/AbstractHandler.java?rev=669902&r1=669901&r2=669902&view=diff
==============================================================================
--- geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/handlers/AbstractHandler.java
(original)
+++ geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/handlers/AbstractHandler.java
Fri Jun 20 07:04:41 2008
@@ -44,7 +44,7 @@
             }
 
             public String getContentType() {
-                throw new UnsupportedOperationException();
+                return mimeType; 
             }
 
             public String getName() {

Modified: geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/handlers/TextHtmlTest.java
URL: http://svn.apache.org/viewvc/geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/handlers/TextHtmlTest.java?rev=669902&r1=669901&r2=669902&view=diff
==============================================================================
--- geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/handlers/TextHtmlTest.java
(original)
+++ geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/handlers/TextHtmlTest.java
Fri Jun 20 07:04:41 2008
@@ -35,5 +35,6 @@
         super.setUp();
         
         dch = new TextHtmlHandler();
+        mimeType = "text/html"; 
     }
 }

Modified: geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/handlers/TextPlainTest.java
URL: http://svn.apache.org/viewvc/geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/handlers/TextPlainTest.java?rev=669902&r1=669901&r2=669902&view=diff
==============================================================================
--- geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/handlers/TextPlainTest.java
(original)
+++ geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/handlers/TextPlainTest.java
Fri Jun 20 07:04:41 2008
@@ -35,5 +35,6 @@
         super.setUp();
         
         dch = new TextPlainHandler();
+        mimeType = "text/plain"; 
     }
 }

Modified: geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/handlers/TextXmlTest.java
URL: http://svn.apache.org/viewvc/geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/handlers/TextXmlTest.java?rev=669902&r1=669901&r2=669902&view=diff
==============================================================================
--- geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/handlers/TextXmlTest.java
(original)
+++ geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/test/java/org/apache/geronimo/javamail/handlers/TextXmlTest.java
Fri Jun 20 07:04:41 2008
@@ -35,5 +35,6 @@
         super.setUp();
         
         dch = new TextXmlHandler();
+        mimeType = "text/xml"; 
     }
 }



Mime
View raw message