You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ri...@apache.org on 2008/06/24 17:39:53 UTC

svn commit: r671217 - in /geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail: transport/smtp/SMTPTransport.java util/CountingOutputStream.java

Author: rickmcguire
Date: Tue Jun 24 08:39:52 2008
New Revision: 671217

URL: http://svn.apache.org/viewvc?rev=671217&view=rev
Log:
GERONIMO-4156 Add javamail SMTP support for the SIZE= extension on the MAIL FROM command.


Added:
    geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/util/CountingOutputStream.java   (with props)
Modified:
    geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/transport/smtp/SMTPTransport.java

Modified: geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/transport/smtp/SMTPTransport.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/transport/smtp/SMTPTransport.java?rev=671217&r1=671216&r2=671217&view=diff
==============================================================================
--- geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/transport/smtp/SMTPTransport.java (original)
+++ geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/transport/smtp/SMTPTransport.java Tue Jun 24 08:39:52 2008
@@ -54,6 +54,7 @@
 import org.apache.geronimo.javamail.authentication.DigestMD5Authenticator;
 import org.apache.geronimo.javamail.authentication.LoginAuthenticator;
 import org.apache.geronimo.javamail.authentication.PlainAuthenticator;
+import org.apache.geronimo.javamail.util.CountingOutputStream;
 import org.apache.geronimo.javamail.util.MIMEOutputStream;
 import org.apache.geronimo.javamail.util.TraceInputStream;
 import org.apache.geronimo.javamail.util.TraceOutputStream;
@@ -1407,6 +1408,53 @@
         // response information
         return !line.isError();
     }
+    
+    /**
+     * Get an estimate of the transmission size for this 
+     * message.  This size is the complete message as it is 
+     * encoded and transmitted on the DATA command, not counting 
+     * the terminating ".CRLF". 
+     * 
+     * @param msg    The message we're sending.
+     * 
+     * @return The count of bytes, if it can be calculated. 
+     */
+    protected int getSizeEstimate(Message msg) {
+        // now the data... I could look at the type, but
+        try {
+            CountingOutputStream outputStream = new CountingOutputStream(); 
+            
+            // 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.
+            MIMEOutputStream mimeOut = new MIMEOutputStream(outputStream);
+
+            msg.writeTo(mimeOut);
+
+            // now to finish, we make sure there's a line break at the end.  
+            mimeOut.forceTerminatingLineBreak();   
+            // and flush the data to send it along 
+            mimeOut.flush();   
+            
+            return outputStream.getCount();   
+        } catch (IOException e) {
+            return 0;     // can't get an estimate 
+        } catch (MessagingException e) {
+            return 0;     // can't get an estimate 
+        }
+    }
+    
 
     /**
      * Sends the data in the message down the socket. This presumes the server
@@ -1604,13 +1652,19 @@
         StringBuffer command = new StringBuffer();
 
         // start building up the command
-        if (sendAs8bit) {
-        }
         command.append("MAIL FROM: ");
         command.append(fixEmailAddress(from));
         if (sendAs8bit) {
             command.append(" BODY=8BITMIME"); 
         }
+        
+        // some servers ask for a size estimate on the initial send 
+        if (supportsExtension("SIZE")) {
+            int estimate = getSizeEstimate(message); 
+            if (estimate > 0) {
+                command.append(" SIZE=" + estimate); 
+            }
+        }
 
         // does this server support Delivery Status Notification? Then we may
         // need to add some extra to the command.

Added: geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/util/CountingOutputStream.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/util/CountingOutputStream.java?rev=671217&view=auto
==============================================================================
--- geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/util/CountingOutputStream.java (added)
+++ geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/util/CountingOutputStream.java Tue Jun 24 08:39:52 2008
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 just counts
+ * the number of bytes written to the stream. 
+ * @version $Rev$ $Date$
+ */
+public class CountingOutputStream extends OutputStream {
+    // the counting accumulator 
+    int count = 0; 
+
+    // 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 {
+        // just increment the count 
+        count++; 
+    }
+    
+    
+    /**
+     * Get the current accumulator total for this stream. 
+     * 
+     * @return The current count. 
+     */
+    public int getCount() {
+        return count; 
+    }
+    
+    
+    /**
+     * Reset the counter to zero. 
+     */
+    public void reset() {
+        count = 0; 
+    }
+}
+

Propchange: geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/util/CountingOutputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/util/CountingOutputStream.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/javamail/trunk/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/util/CountingOutputStream.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain