geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jlaskow...@apache.org
Subject svn commit: r383090 - in /geronimo/trunk/modules/javamail-transport/src/java/org/apache/geronimo/javamail: transport/smtp/SMTPTransport.java util/ util/MIMEOutputStream.java
Date Sat, 04 Mar 2006 11:24:23 GMT
Author: jlaskowski
Date: Sat Mar  4 03:24:20 2006
New Revision: 383090

URL: http://svn.apache.org/viewcvs?rev=383090&view=rev
Log:
GERONIMO-1673 SMTPTransport is not performing byte-stuffing and newline canonicalization on
message data

Thanks Rick!

Added:
    geronimo/trunk/modules/javamail-transport/src/java/org/apache/geronimo/javamail/util/
    geronimo/trunk/modules/javamail-transport/src/java/org/apache/geronimo/javamail/util/MIMEOutputStream.java
  (with props)
Modified:
    geronimo/trunk/modules/javamail-transport/src/java/org/apache/geronimo/javamail/transport/smtp/SMTPTransport.java

Modified: geronimo/trunk/modules/javamail-transport/src/java/org/apache/geronimo/javamail/transport/smtp/SMTPTransport.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/javamail-transport/src/java/org/apache/geronimo/javamail/transport/smtp/SMTPTransport.java?rev=383090&r1=383089&r2=383090&view=diff
==============================================================================
--- geronimo/trunk/modules/javamail-transport/src/java/org/apache/geronimo/javamail/transport/smtp/SMTPTransport.java
(original)
+++ geronimo/trunk/modules/javamail-transport/src/java/org/apache/geronimo/javamail/transport/smtp/SMTPTransport.java
Sat Mar  4 03:24:20 2006
@@ -52,6 +52,8 @@
 import org.apache.geronimo.mail.util.Base64;
 import org.apache.geronimo.mail.util.XText;
 
+import org.apache.geronimo.javamail.util.MIMEOutputStream;
+
 /**
  * Simple implementation of SMTP transport.  Just does plain RFC821-ish
  * delivery.
@@ -1262,8 +1264,19 @@
 
         // now the data...  I could look at the type, but
         try {
-            msg.writeTo(outputStream);
-            outputStream.flush();
+            // the data content has two requirements we need to meet by filtering the
+            // output stream.  Requirement 1 is to conicalize any line breaks.  All line
+            // breaks will be transformed into properly formed CRLF sequences.
+            //
+            // Requirement 2 is to perform byte-stuff for any line that begins with a "."
+            // so that data is not confused with the end-of-data marker (a "\r\n.\r\n" sequence.
+            //
+            // The MIME output stream performs those two functions on behalf of the content
+            // writer.
+            OutputStream mimeOut = new MIMEOutputStream(outputStream);
+
+            msg.writeTo(mimeOut);
+            mimeOut.flush();
         } catch (IOException e) {
             throw new MessagingException(e.toString());
         } catch (MessagingException e) {

Added: geronimo/trunk/modules/javamail-transport/src/java/org/apache/geronimo/javamail/util/MIMEOutputStream.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/javamail-transport/src/java/org/apache/geronimo/javamail/util/MIMEOutputStream.java?rev=383090&view=auto
==============================================================================
--- geronimo/trunk/modules/javamail-transport/src/java/org/apache/geronimo/javamail/util/MIMEOutputStream.java
(added)
+++ geronimo/trunk/modules/javamail-transport/src/java/org/apache/geronimo/javamail/util/MIMEOutputStream.java
Sat Mar  4 03:24:20 2006
@@ -0,0 +1,94 @@
+/**
+ *
+ * Copyright 2006 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.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.geronimo.javamail.util;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+/**
+ * An implementation of an OutputStream that performs MIME linebreak canonicalization
+ * and "byte-stuff" so that data content does not get mistaken for a message data-end
+ * marker (CRLF.CRLF)l
+ *
+ * @version $Rev$ $Date$
+ */
+public class MIMEOutputStream extends OutputStream {
+
+    // the wrappered output stream.
+    protected OutputStream out;
+
+    // last character we handled...used to recongnize line breaks.
+    protected int lastWrite = -1;
+
+    // a flag to indicate we've just processed a line break.  This is used for byte stuffing
purposes.  This
+    // is initially true, because if the first character of the content is a period, we need
to byte-stuff
+    // immediately.
+    protected boolean atLineBreak = true;
+
+    /**
+     * Create an output stream that writes to the target output stream.
+     *
+     * @param out    The wrapped output stream.
+     */
+    public MIMEOutputStream(OutputStream out) {
+        this.out = out;
+    }
+
+
+    // in order for this to work, we only need override the single character form, as the
others
+    // funnel through this one by default.
+    public void write(int ch) throws IOException {
+        // if this is a CR character, always write out a full sequence, and remember that
we just did this.
+        if (ch == '\r') {
+            out.write((byte)'\r');
+            out.write((byte)'\n');
+            // we've just taken a break;
+            atLineBreak = true;
+        }
+        // if this is a new line, then we need to determine if this is a loner or part of
a CRLF sequence.
+        else if (ch == '\n') {
+            // is this a lone ranger?
+            if (lastWrite != '\r') {
+                // write the full CRLF sequence.
+                out.write((byte)'\r');
+                out.write((byte)'\n');
+            }
+            // regardless of whether we wrote something or not, we're still at a line break.
+            atLineBreak = true;
+        }
+        // potential byte-stuffing situation?
+        else if (ch == '.') {
+            // ok, this is a potential stuff situation.  Did we just have a line break? 
Double up the character.
+            if (atLineBreak) {
+                out.write('.');
+            }
+            out.write('.');
+            atLineBreak = false;
+        }
+        else {
+            // just write this out and flip the linebreak flag.
+            out.write(ch);
+            atLineBreak = false;
+        }
+        // remember this last one for CRLF tracking purposes.
+        lastWrite = ch;
+    }
+}
+
+
+

Propchange: geronimo/trunk/modules/javamail-transport/src/java/org/apache/geronimo/javamail/util/MIMEOutputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/trunk/modules/javamail-transport/src/java/org/apache/geronimo/javamail/util/MIMEOutputStream.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



Mime
View raw message