GERONIMO-3427 STMP login fails if server sends multiple lines of response back from initial connection.

Improved version that always handles continuations. 



git-svn-id: https://svn.apache.org/repos/asf/geronimo/javamail/trunk@569336 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/geronimo-javamail_1.3.1/geronimo-javamail_1.3.1_provider/src/main/java/org/apache/geronimo/javamail/transport/smtp/SMTPReply.java b/geronimo-javamail_1.3.1/geronimo-javamail_1.3.1_provider/src/main/java/org/apache/geronimo/javamail/transport/smtp/SMTPReply.java
index dbee40e..f6877e9 100644
--- a/geronimo-javamail_1.3.1/geronimo-javamail_1.3.1_provider/src/main/java/org/apache/geronimo/javamail/transport/smtp/SMTPReply.java
+++ b/geronimo-javamail_1.3.1/geronimo-javamail_1.3.1_provider/src/main/java/org/apache/geronimo/javamail/transport/smtp/SMTPReply.java
@@ -19,6 +19,9 @@
 
 package org.apache.geronimo.javamail.transport.smtp;
 
+import java.util.ArrayList;
+import java.util.List;
+
 /**
  * Util class to represent a reply from a SMTP server
  * 
@@ -34,6 +37,9 @@
     // the returned message text
     private final String message;
 
+    // additional returned lines from a continued response 
+    private List lines; 
+
     // indicates that this is a continuation response
     private boolean continued;
 
@@ -74,6 +80,40 @@
     }
 
     /**
+     * Add a line to a continued response.  This will 
+     * update the continued status if the end of the 
+     * response is reached. 
+     * 
+     * @param line   The line to add.
+     */
+    public void addLine(String line) {
+        if (lines == null) {
+            lines = new ArrayList(); 
+            lines.add(message); 
+        }
+        // mark if we're still continued 
+        continued = line.charAt(3) == '-';
+        // add the line to the list 
+        lines.add(line.substring(4)); 
+    }
+    
+    /**
+     * Get the list of all of the lines associated with 
+     * this reply. 
+     * 
+     * @return A List containing all lines associated with this
+     *         reply.
+     */
+    public List getLines() {
+        if (lines == null) {
+            lines = new ArrayList(); 
+            lines.add(message); 
+        }
+        return lines;
+    }
+    
+
+    /**
      * Return the code value associated with the reply.
      * 
      * @return The integer code associated with the reply.
diff --git a/geronimo-javamail_1.3.1/geronimo-javamail_1.3.1_provider/src/main/java/org/apache/geronimo/javamail/transport/smtp/SMTPTransport.java b/geronimo-javamail_1.3.1/geronimo-javamail_1.3.1_provider/src/main/java/org/apache/geronimo/javamail/transport/smtp/SMTPTransport.java
index 44eb94d..429d32c 100644
--- a/geronimo-javamail_1.3.1/geronimo-javamail_1.3.1_provider/src/main/java/org/apache/geronimo/javamail/transport/smtp/SMTPTransport.java
+++ b/geronimo-javamail_1.3.1/geronimo-javamail_1.3.1_provider/src/main/java/org/apache/geronimo/javamail/transport/smtp/SMTPTransport.java
@@ -32,6 +32,7 @@
 import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.StringTokenizer;
 
 import javax.mail.Address;
@@ -1383,17 +1384,9 @@
      */
     protected boolean getWelcome() throws MessagingException {
         SMTPReply line = getReply();
-        // process any error now
-        if (line.isError()) {
-            return false; 
-        }
-        // Some servers send a multi-line welcome message.  Keep 
-        // reading lines until we hit the end of the response. 
-        while (line.isContinued()) {
-            line = getReply(); 
-        }
-        // this worked. 
-        return true; 
+        // just return the error status...we don't care about any of the 
+        // response information
+        return !line.isError();
     }
 
     /**
@@ -1733,6 +1726,11 @@
     protected SMTPReply getReply() throws MessagingException {
         try {
             lastServerResponse = new SMTPReply(receiveLine());
+            // if the first line we receive is a continuation, continue 
+            // reading lines until we reach the non-continued one. 
+            while (lastServerResponse.isContinued()) {
+                lastServerResponse.addLine(receiveLine()); 
+            }
         } catch (MalformedSMTPReplyException e) {
             throw new MessagingException(e.toString());
         } catch (MessagingException e) {
@@ -1881,29 +1879,24 @@
     protected boolean sendEhlo() throws MessagingException {
         sendLine("EHLO " + getLocalHost());
 
-        SMTPReply line = getReply();
+        SMTPReply reply = getReply();
 
         // we get a 250 code back. The first line is just a greeting, and
         // extensions are identifed on
         // continuations. If this fails, then we'll try once more with HELO to
         // establish bona fides.
-        if (line.getCode() != COMMAND_ACCEPTED) {
+        if (reply.getCode() != COMMAND_ACCEPTED) {
             return false;
         }
 
         // get a fresh extension mapping table.
         serverExtensionArgs = new HashMap();
 
+        List lines = reply.getLines(); 
         // process all of the continuation lines
-        while (line.isContinued()) {
-            // get the next line
-            line = getReply();
-            if (line.getCode() != COMMAND_ACCEPTED) {
-                // all EHLO failures go back to the HELO failback step.
-                return false;
-            }
+        for (int i = 1; i < lines.size(); i++) {
             // go process the extention
-            processExtension(line.getMessage());
+            processExtension((String)lines.get(i));
         }
         return true;
     }
diff --git a/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/transport/smtp/SMTPReply.java b/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/transport/smtp/SMTPReply.java
index dbee40e..ce564c7 100644
--- a/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/transport/smtp/SMTPReply.java
+++ b/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/transport/smtp/SMTPReply.java
@@ -18,6 +18,9 @@
  */
 
 package org.apache.geronimo.javamail.transport.smtp;
+ 
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * Util class to represent a reply from a SMTP server
@@ -33,6 +36,9 @@
 
     // the returned message text
     private final String message;
+    
+    // additional returned lines from a continued response 
+    private List lines; 
 
     // indicates that this is a continuation response
     private boolean continued;
@@ -72,6 +78,40 @@
             throw new MalformedSMTPReplyException("error in parsing code", e);
         }
     }
+    
+    /**
+     * Add a line to a continued response.  This will 
+     * update the continued status if the end of the 
+     * response is reached. 
+     * 
+     * @param line   The line to add.
+     */
+    public void addLine(String line) {
+        if (lines == null) {
+            lines = new ArrayList(); 
+            lines.add(message); 
+        }
+        // mark if we're still continued 
+        continued = line.charAt(3) == '-';
+        // add the line to the list 
+        lines.add(line.substring(4)); 
+    }
+    
+    /**
+     * Get the list of all of the lines associated with 
+     * this reply. 
+     * 
+     * @return A List containing all lines associated with this
+     *         reply.
+     */
+    public List getLines() {
+        if (lines == null) {
+            lines = new ArrayList(); 
+            lines.add(message); 
+        }
+        return lines;
+    }
+    
 
     /**
      * Return the code value associated with the reply.
diff --git a/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/transport/smtp/SMTPTransport.java b/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/transport/smtp/SMTPTransport.java
index ad63808..146dee9 100644
--- a/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/transport/smtp/SMTPTransport.java
+++ b/geronimo-javamail_1.4/geronimo-javamail_1.4_provider/src/main/java/org/apache/geronimo/javamail/transport/smtp/SMTPTransport.java
@@ -32,6 +32,7 @@
 import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.StringTokenizer;
 
 import javax.mail.Address;
@@ -1381,17 +1382,9 @@
      */
     protected boolean getWelcome() throws MessagingException {
         SMTPReply line = getReply();
-        // process any error now
-        if (line.isError()) {
-            return false; 
-        }
-        // Some servers send a multi-line welcome message.  Keep 
-        // reading lines until we hit the end of the response. 
-        while (line.isContinued()) {
-            line = getReply(); 
-        }
-        // this worked. 
-        return true; 
+        // just return the error status...we don't care about any of the 
+        // response information
+        return !line.isError();
     }
 
     /**
@@ -1731,6 +1724,11 @@
     protected SMTPReply getReply() throws MessagingException {
         try {
             lastServerResponse = new SMTPReply(receiveLine());
+            // if the first line we receive is a continuation, continue 
+            // reading lines until we reach the non-continued one. 
+            while (lastServerResponse.isContinued()) {
+                lastServerResponse.addLine(receiveLine()); 
+            }
         } catch (MalformedSMTPReplyException e) {
             throw new MessagingException(e.toString());
         } catch (MessagingException e) {
@@ -1879,29 +1877,24 @@
     protected boolean sendEhlo() throws MessagingException {
         sendLine("EHLO " + getLocalHost());
 
-        SMTPReply line = getReply();
+        SMTPReply reply = getReply();
 
         // we get a 250 code back. The first line is just a greeting, and
         // extensions are identifed on
         // continuations. If this fails, then we'll try once more with HELO to
         // establish bona fides.
-        if (line.getCode() != COMMAND_ACCEPTED) {
+        if (reply.getCode() != COMMAND_ACCEPTED) {
             return false;
         }
 
         // get a fresh extension mapping table.
         serverExtensionArgs = new HashMap();
 
+        List lines = reply.getLines(); 
         // process all of the continuation lines
-        while (line.isContinued()) {
-            // get the next line
-            line = getReply();
-            if (line.getCode() != COMMAND_ACCEPTED) {
-                // all EHLO failures go back to the HELO failback step.
-                return false;
-            }
+        for (int i = 1; i < lines.size(); i++) {
             // go process the extention
-            processExtension(line.getMessage());
+            processExtension((String)lines.get(i));
         }
         return true;
     }