From b203b2b1196bfd5507c83a4fe81d362de840ec0a Mon Sep 17 00:00:00 2001 From: Marc Blank Date: Tue, 18 Sep 2012 15:07:27 -0700 Subject: [PATCH] Remove useless class and crazy UIDL logging * The logging makes it very difficult to debug POP3 issues Change-Id: Ic3ffb9e5b3240918dff3e713fc2a7c49976efe84 --- .../android/emailcommon/mail/Transport.java | 141 ------------------ src/com/android/email/mail/Store.java | 4 +- .../android/email/mail/store/Pop3Store.java | 11 +- .../email/mail/transport/MailTransport.java | 34 +---- .../email/mail/transport/SmtpSender.java | 10 +- 5 files changed, 18 insertions(+), 182 deletions(-) delete mode 100644 emailcommon/src/com/android/emailcommon/mail/Transport.java diff --git a/emailcommon/src/com/android/emailcommon/mail/Transport.java b/emailcommon/src/com/android/emailcommon/mail/Transport.java deleted file mode 100644 index 97aff151c..000000000 --- a/emailcommon/src/com/android/emailcommon/mail/Transport.java +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * - * 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 com.android.emailcommon.mail; - - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.InetAddress; -import java.net.SocketException; - -/** - * This interface defines a "transport", which is defined here as being one layer below the - * specific wire protocols such as POP3, IMAP, or SMTP. - * - * Practically speaking, it provides a definition of the common functionality between them - * (dealing with sockets & streams, SSL, logging, and so forth), and provides a seam just below - * the individual protocols to enable better testing. - * - * The following features are supported and presumed to be common: - * - * Interpretation of URI - * Support for SSL and TLS wireline security - */ -public interface Transport extends Cloneable { - - /** - * Connection security options for transport that supports SSL and/or TLS - */ - public static final int CONNECTION_SECURITY_NONE = 0; - public static final int CONNECTION_SECURITY_SSL = 1; - public static final int CONNECTION_SECURITY_TLS = 2; - - /** - * Get a new transport, using an existing one as a model. The new transport is configured as if - * setUri() and setSecurity() have been called, but not opened or connected in any way. - * @return a new Transport ready to open() - */ - public Transport clone(); - - /** - * Returns the host or {@code null} if none was specified. - */ - public String getHost(); - - /** - * Returns the port or {@code 0} if none was specified. - */ - public int getPort(); - - /** - * @return true if the security mode indicates that SSL is possible - */ - public boolean canTrySslSecurity(); - - /** - * @return true if the security mode indicates that TLS is possible - */ - public boolean canTryTlsSecurity(); - - /** - * @return true if the security mode indicates that all certificates can be trusted - */ - public boolean canTrustAllCertificates(); - - /** - * Set the socket timeout. - * @param timeoutMilliseconds the read timeout value if greater than {@code 0}, or - * {@code 0} for an infinite timeout. - */ - public void setSoTimeout(int timeoutMilliseconds) throws SocketException; - - /** - * Attempts to open the connection using the supplied parameters, and using SSL if indicated. - */ - public void open() throws MessagingException, CertificateValidationException; - - /** - * Attempts to reopen the connection using TLS. - */ - public void reopenTls() throws MessagingException; - - /** - * @return true if the connection is open - */ - public boolean isOpen(); - - /** - * Closes the connection. Does not send any closure messages, simply closes the socket and the - * associated streams. Best effort only. Catches all exceptions and always returns. - * - * MUST NOT throw any exceptions. - */ - public void close(); - - /** - * @return returns the active input stream - */ - public InputStream getInputStream(); - - /** - * @return returns the active output stream - */ - public OutputStream getOutputStream(); - - /** - * Write a single line to the server, and may generate a log entry (if enabled). - * @param s The text to send to the server. - * @param sensitiveReplacement If the command includes sensitive data (e.g. authentication) - * please pass a replacement string here (for logging). Most callers simply pass null, - */ - void writeLine(String s, String sensitiveReplacement) throws IOException; - - /** - * Reads a single line from the server. Any delimiter characters will not be included in the - * result. May generate a log entry, if enabled. - * @return Returns the string from the server. - * @throws IOException - */ - String readLine() throws IOException; - - /** - * @return The local address. If we have an open socket, get the local address from this. - * Otherwise simply use {@link InetAddress#getLocalHost}. - */ - InetAddress getLocalAddress() throws IOException; -} diff --git a/src/com/android/email/mail/Store.java b/src/com/android/email/mail/Store.java index ec8113493..fcbf2833a 100644 --- a/src/com/android/email/mail/Store.java +++ b/src/com/android/email/mail/Store.java @@ -23,11 +23,11 @@ import android.util.Log; import com.android.email.R; import com.android.email.mail.store.Pop3Store; import com.android.email.mail.store.ServiceStore; +import com.android.email.mail.transport.MailTransport; import com.android.email2.ui.MailActivityEmail; import com.android.emailcommon.Logging; import com.android.emailcommon.mail.Folder; import com.android.emailcommon.mail.MessagingException; -import com.android.emailcommon.mail.Transport; import com.android.emailcommon.provider.Account; import com.android.emailcommon.provider.EmailContent; import com.android.emailcommon.provider.HostAuth; @@ -51,7 +51,7 @@ public abstract class Store { static final HashMap sStores = new HashMap(); protected Context mContext; protected Account mAccount; - protected Transport mTransport; + protected MailTransport mTransport; protected String mUsername; protected String mPassword; diff --git a/src/com/android/email/mail/store/Pop3Store.java b/src/com/android/email/mail/store/Pop3Store.java index 4cbea9dc8..f46c8451b 100644 --- a/src/com/android/email/mail/store/Pop3Store.java +++ b/src/com/android/email/mail/store/Pop3Store.java @@ -33,7 +33,6 @@ import com.android.emailcommon.mail.Folder; import com.android.emailcommon.mail.Folder.OpenMode; import com.android.emailcommon.mail.Message; import com.android.emailcommon.mail.MessagingException; -import com.android.emailcommon.mail.Transport; import com.android.emailcommon.provider.Account; import com.android.emailcommon.provider.HostAuth; import com.android.emailcommon.provider.Mailbox; @@ -90,7 +89,7 @@ public class Pop3Store extends Store { * up and ready to use. Do not use for real code. * @param testTransport The Transport to inject and use for all future communication. */ - /* package */ void setTransport(Transport testTransport) { + /* package */ void setTransport(MailTransport testTransport) { mTransport = testTransport; } @@ -225,7 +224,7 @@ public class Pop3Store extends Store { executeSimpleCommand("UIDL"); // drain the entire output, so additional communications don't get confused. String response; - while ((response = mTransport.readLine()) != null) { + while ((response = mTransport.readLine(false)) != null) { parser.parseMultiLine(response); if (parser.mEndOfMessage) { break; @@ -446,7 +445,7 @@ public class Pop3Store extends Store { } } else { String response = executeSimpleCommand("UIDL"); - while ((response = mTransport.readLine()) != null) { + while ((response = mTransport.readLine(false)) != null) { if (!parser.parseMultiLine(response)) { throw new IOException(); } @@ -722,7 +721,7 @@ public class Pop3Store extends Store { Pop3Capabilities capabilities = new Pop3Capabilities(); try { String response = executeSimpleCommand("CAPA"); - while ((response = mTransport.readLine()) != null) { + while ((response = mTransport.readLine(true)) != null) { if (response.equals(".")) { break; } else if (response.equalsIgnoreCase("STLS")){ @@ -767,7 +766,7 @@ public class Pop3Store extends Store { mTransport.writeLine(command, sensitiveReplacement); } - String response = mTransport.readLine(); + String response = mTransport.readLine(true); if (response.length() > 1 && response.charAt(0) == '-') { throw new MessagingException(response); diff --git a/src/com/android/email/mail/transport/MailTransport.java b/src/com/android/email/mail/transport/MailTransport.java index 785d647e6..692eef177 100644 --- a/src/com/android/email/mail/transport/MailTransport.java +++ b/src/com/android/email/mail/transport/MailTransport.java @@ -16,17 +16,16 @@ package com.android.email.mail.transport; +import android.content.Context; +import android.util.Log; + import com.android.email2.ui.MailActivityEmail; import com.android.emailcommon.Logging; import com.android.emailcommon.mail.CertificateValidationException; import com.android.emailcommon.mail.MessagingException; -import com.android.emailcommon.mail.Transport; import com.android.emailcommon.provider.HostAuth; import com.android.emailcommon.utility.SSLUtils; -import android.content.Context; -import android.util.Log; - import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.IOException; @@ -45,11 +44,7 @@ import javax.net.ssl.SSLPeerUnverifiedException; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocket; -/** - * This class implements the common aspects of "transport", one layer below the - * specific wire protocols such as POP3, IMAP, or SMTP. - */ -public class MailTransport implements Transport { +public class MailTransport { // TODO protected eventually /*protected*/ public static final int SOCKET_CONNECT_TIMEOUT = 10000; @@ -79,31 +74,26 @@ public class MailTransport implements Transport { * and {@link #setHost(String)} were invoked), but not opened or connected in any way. */ @Override - public Transport clone() { + public MailTransport clone() { return new MailTransport(mContext, mDebugLabel, mHostAuth); } - @Override public String getHost() { return mHostAuth.mAddress; } - @Override public int getPort() { return mHostAuth.mPort; } - @Override public boolean canTrySslSecurity() { return (mHostAuth.mFlags & HostAuth.FLAG_SSL) != 0; } - @Override public boolean canTryTlsSecurity() { return (mHostAuth.mFlags & HostAuth.FLAG_TLS) != 0; } - @Override public boolean canTrustAllCertificates() { return (mHostAuth.mFlags & HostAuth.FLAG_TRUST_ALL) != 0; } @@ -112,7 +102,6 @@ public class MailTransport implements Transport { * Attempts to open a connection using the Uri supplied for connection parameters. Will attempt * an SSL connection if indicated. */ - @Override public void open() throws MessagingException, CertificateValidationException { if (MailActivityEmail.DEBUG) { Log.d(Logging.LOG_TAG, "*** " + mDebugLabel + " open " + @@ -161,7 +150,6 @@ public class MailTransport implements Transport { * * TODO should we explicitly close the old socket? This seems funky to abandon it. */ - @Override public void reopenTls() throws MessagingException { try { mSocket = SSLUtils.getSSLSocketFactory(mContext, mHostAuth, canTrustAllCertificates()) @@ -226,12 +214,10 @@ public class MailTransport implements Transport { * @param timeoutMilliseconds the read timeout value if greater than {@code 0}, or * {@code 0} for an infinite timeout. */ - @Override public void setSoTimeout(int timeoutMilliseconds) throws SocketException { mSocket.setSoTimeout(timeoutMilliseconds); } - @Override public boolean isOpen() { return (mIn != null && mOut != null && mSocket != null && mSocket.isConnected() && !mSocket.isClosed()); @@ -240,7 +226,6 @@ public class MailTransport implements Transport { /** * Close the connection. MUST NOT return any exceptions - must be "best effort" and safe. */ - @Override public void close() { try { mIn.close(); @@ -262,12 +247,10 @@ public class MailTransport implements Transport { mSocket = null; } - @Override public InputStream getInputStream() { return mIn; } - @Override public OutputStream getOutputStream() { return mOut; } @@ -275,7 +258,6 @@ public class MailTransport implements Transport { /** * Writes a single line to the server using \r\n termination. */ - @Override public void writeLine(String s, String sensitiveReplacement) throws IOException { if (MailActivityEmail.DEBUG) { if (sensitiveReplacement != null && !Logging.DEBUG_SENSITIVE) { @@ -296,8 +278,7 @@ public class MailTransport implements Transport { * Reads a single line from the server, using either \r\n or \n as the delimiter. The * delimiter char(s) are not included in the result. */ - @Override - public String readLine() throws IOException { + public String readLine(boolean loggable) throws IOException { StringBuffer sb = new StringBuffer(); InputStream in = getInputStream(); int d; @@ -314,13 +295,12 @@ public class MailTransport implements Transport { Log.d(Logging.LOG_TAG, "End of stream reached while trying to read line."); } String ret = sb.toString(); - if (MailActivityEmail.DEBUG) { + if (loggable && MailActivityEmail.DEBUG) { Log.d(Logging.LOG_TAG, "<<< " + ret); } return ret; } - @Override public InetAddress getLocalAddress() { if (isOpen()) { return mSocket.getLocalAddress(); diff --git a/src/com/android/email/mail/transport/SmtpSender.java b/src/com/android/email/mail/transport/SmtpSender.java index ce4a8f844..b318a59d2 100644 --- a/src/com/android/email/mail/transport/SmtpSender.java +++ b/src/com/android/email/mail/transport/SmtpSender.java @@ -28,7 +28,6 @@ import com.android.emailcommon.mail.Address; import com.android.emailcommon.mail.AuthenticationFailedException; import com.android.emailcommon.mail.CertificateValidationException; import com.android.emailcommon.mail.MessagingException; -import com.android.emailcommon.mail.Transport; import com.android.emailcommon.provider.Account; import com.android.emailcommon.provider.EmailContent.Message; import com.android.emailcommon.provider.HostAuth; @@ -42,12 +41,11 @@ import javax.net.ssl.SSLException; /** * This class handles all of the protocol-level aspects of sending messages via SMTP. - * TODO Remove dependence upon URI; there's no reason why we need it here */ public class SmtpSender extends Sender { private final Context mContext; - private Transport mTransport; + private MailTransport mTransport; private String mUsername; private String mPassword; @@ -77,7 +75,7 @@ public class SmtpSender extends Sender { * up and ready to use. Do not use for real code. * @param testTransport The Transport to inject and use for all future communication. */ - /* package */ void setTransport(Transport testTransport) { + /* package */ void setTransport(MailTransport testTransport) { mTransport = testTransport; } @@ -239,12 +237,12 @@ public class SmtpSender extends Sender { mTransport.writeLine(command, sensitiveReplacement); } - String line = mTransport.readLine(); + String line = mTransport.readLine(true); String result = line; while (line.length() >= 4 && line.charAt(3) == '-') { - line = mTransport.readLine(); + line = mTransport.readLine(true); result += line.substring(3); }