org.apache.geronimo.javamail.util
Class MailConnection

java.lang.Object
  extended by org.apache.geronimo.javamail.util.MailConnection
Direct Known Subclasses:
IMAPConnection, NNTPConnection, POP3Connection, SMTPConnection

public class MailConnection
extends Object

Base class for all mail Store/Transport connection. Centralizes management of a lot of common connection handling. Actual protcol-specific functions are handled at the subclass level.


Field Summary
protected static String AUTHENTICATION_CRAMMD5
           
protected static String AUTHENTICATION_DIGESTMD5
           
protected static String AUTHENTICATION_LOGIN
           
protected static String AUTHENTICATION_PLAIN
           
protected  List authentications
           
protected  String authid
           
protected  Map capabilities
           
protected static String CAPABILITY_STARTTLS
           
protected static char CR
          constants for EOL termination
protected  boolean debug
           
protected  PrintStream debugStream
           
protected static String DEFAULT_MAIL_HOST
           
protected  int defaultPort
           
protected  InputStream inputStream
           
protected static char LF
           
protected  InetAddress localAddress
           
protected  String localHost
           
protected  int localPort
           
protected static String MAIL_AUTH
          property keys for protocol properties.
protected static String MAIL_AUTHORIZATIONID
           
protected static String MAIL_ENCODE_TRACE
           
protected static String MAIL_FACTORY_CLASS
           
protected static String MAIL_FACTORY_FALLBACK
           
protected static String MAIL_FACTORY_PORT
           
protected static String MAIL_LOCALADDRESS
           
protected static String MAIL_LOCALHOST
           
protected static String MAIL_LOCALPORT
           
protected static String MAIL_LOGIN_DISABLE
           
protected static String MAIL_PLAIN_DISABLE
           
protected static String MAIL_PORT
           
protected static String MAIL_SASL_ENABLE
           
protected static String MAIL_SASL_MECHANISMS
           
protected static String MAIL_SASL_REALM
           
protected static String MAIL_SSL_CIPHERSUITES
           
protected static String MAIL_SSL_ENABLE
           
protected static String MAIL_SSL_PROTOCOLS
           
protected static String MAIL_STARTTLS_ENABLE
           
protected static String MAIL_TIMEOUT
           
protected  List mechanisms
           
protected static int MIN_MILLIS
           
protected  OutputStream outputStream
           
protected  String password
           
protected  ProtocolProperties props
           
protected  String protocol
           
protected  String realm
           
protected  String serverHost
           
protected  int serverPort
           
protected  Session session
           
protected  Socket socket
           
protected  boolean sslConnection
           
protected  int timeout
           
protected static int TIMEOUT
           
protected  String username
           
 
Constructor Summary
protected MailConnection(ProtocolProperties props)
           
 
Method Summary
protected  void checkConnected()
          Verify that we have a good connection before attempting to send a command.
 void closeServerConnection()
          Close the server connection at termination.
 void connect(Socket s)
          Establish a connection using an existing socket.
protected  void debugOut(String message)
          Internal debug output routine.
protected  void debugOut(String message, Throwable e)
          Internal debugging routine for reporting exceptions.
 Map getCapabilities()
          Get the capabilities map.
protected  void getConnectedSocket()
          Creates a connected socket
protected  void getConnectedSSLSocket()
          Creates a connected SSL socket for an initial SSL connection.
protected  void getConnectedTLSSocket()
          Switch the connection to using TLS level security, switching to an SSL socket.
protected  void getConnection()
          Create a transport connection object and connect it to the target server.
protected  void getConnectionProperties()
          Get common connection properties before creating a connection socket.
protected  void getConnectionStreams()
          Set up the input and output streams for server communications once the socket connection has been made.
 String getHost()
          Retrieve the connection host.
 String getLocalHost()
          Retrieve the local client host name.
protected  ClientAuthenticator getLoginAuthenticator()
          Process SASL-type authentication.
protected  List getSaslMechanisms()
          Get a list of the SASL mechanisms we're configured to accept.
 String getSASLRealm()
          Retrieve the SASL realm used for DIGEST-MD5 authentication.
protected  List getServerMechanisms()
          Get the list of authentication mechanisms the server is supposed to support.
 boolean hasCapability(String capability)
          Test if this connection has a given capability.
 boolean protocolConnect(String host, int port, String username, String password)
          Connect to the server and do the initial handshaking.
protected  List selectSaslMechanisms()
          Merge the configured SASL mechanisms with the capabilities that the server has indicated it supports, returning a merged list that can be used for selecting a mechanism.
 void setLocalHost(String localHost)
          Explicitly set the local host information.
 void setSASLRealm(String name)
          Explicitly set the SASL realm used for DIGEST-MD5 authenticaiton.
 boolean supportsMechanism(String mech)
          Test if the server supports a given mechanism.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CR

protected static final char CR
constants for EOL termination

See Also:
Constant Field Values

LF

protected static final char LF
See Also:
Constant Field Values

MAIL_AUTH

protected static final String MAIL_AUTH
property keys for protocol properties.

See Also:
Constant Field Values

MAIL_PORT

protected static final String MAIL_PORT
See Also:
Constant Field Values

MAIL_LOCALHOST

protected static final String MAIL_LOCALHOST
See Also:
Constant Field Values

MAIL_STARTTLS_ENABLE

protected static final String MAIL_STARTTLS_ENABLE
See Also:
Constant Field Values

MAIL_SSL_ENABLE

protected static final String MAIL_SSL_ENABLE
See Also:
Constant Field Values

MAIL_TIMEOUT

protected static final String MAIL_TIMEOUT
See Also:
Constant Field Values

MAIL_SASL_ENABLE

protected static final String MAIL_SASL_ENABLE
See Also:
Constant Field Values

MAIL_SASL_REALM

protected static final String MAIL_SASL_REALM
See Also:
Constant Field Values

MAIL_AUTHORIZATIONID

protected static final String MAIL_AUTHORIZATIONID
See Also:
Constant Field Values

MAIL_SASL_MECHANISMS

protected static final String MAIL_SASL_MECHANISMS
See Also:
Constant Field Values

MAIL_PLAIN_DISABLE

protected static final String MAIL_PLAIN_DISABLE
See Also:
Constant Field Values

MAIL_LOGIN_DISABLE

protected static final String MAIL_LOGIN_DISABLE
See Also:
Constant Field Values

MAIL_FACTORY_CLASS

protected static final String MAIL_FACTORY_CLASS
See Also:
Constant Field Values

MAIL_FACTORY_FALLBACK

protected static final String MAIL_FACTORY_FALLBACK
See Also:
Constant Field Values

MAIL_FACTORY_PORT

protected static final String MAIL_FACTORY_PORT
See Also:
Constant Field Values

MAIL_SSL_PROTOCOLS

protected static final String MAIL_SSL_PROTOCOLS
See Also:
Constant Field Values

MAIL_SSL_CIPHERSUITES

protected static final String MAIL_SSL_CIPHERSUITES
See Also:
Constant Field Values

MAIL_LOCALADDRESS

protected static final String MAIL_LOCALADDRESS
See Also:
Constant Field Values

MAIL_LOCALPORT

protected static final String MAIL_LOCALPORT
See Also:
Constant Field Values

MAIL_ENCODE_TRACE

protected static final String MAIL_ENCODE_TRACE
See Also:
Constant Field Values

MIN_MILLIS

protected static final int MIN_MILLIS
See Also:
Constant Field Values

TIMEOUT

protected static final int TIMEOUT
See Also:
Constant Field Values

DEFAULT_MAIL_HOST

protected static final String DEFAULT_MAIL_HOST
See Also:
Constant Field Values

CAPABILITY_STARTTLS

protected static final String CAPABILITY_STARTTLS
See Also:
Constant Field Values

AUTHENTICATION_PLAIN

protected static final String AUTHENTICATION_PLAIN
See Also:
Constant Field Values

AUTHENTICATION_LOGIN

protected static final String AUTHENTICATION_LOGIN
See Also:
Constant Field Values

AUTHENTICATION_CRAMMD5

protected static final String AUTHENTICATION_CRAMMD5
See Also:
Constant Field Values

AUTHENTICATION_DIGESTMD5

protected static final String AUTHENTICATION_DIGESTMD5
See Also:
Constant Field Values

session

protected Session session

protocol

protected String protocol

sslConnection

protected boolean sslConnection

defaultPort

protected int defaultPort

props

protected ProtocolProperties props

serverHost

protected String serverHost

serverPort

protected int serverPort

socket

protected Socket socket

localAddress

protected InetAddress localAddress

localPort

protected int localPort

localHost

protected String localHost

timeout

protected int timeout

username

protected String username

password

protected String password

realm

protected String realm

authid

protected String authid

inputStream

protected InputStream inputStream

outputStream

protected OutputStream outputStream

debugStream

protected PrintStream debugStream

debug

protected boolean debug

authentications

protected List authentications

capabilities

protected Map capabilities

mechanisms

protected List mechanisms
Constructor Detail

MailConnection

protected MailConnection(ProtocolProperties props)
Method Detail

protocolConnect

public boolean protocolConnect(String host,
                               int port,
                               String username,
                               String password)
                        throws MessagingException
Connect to the server and do the initial handshaking.

Parameters:
host - The target host name.
port - The target port
username - The connection username (can be null)
password - The authentication password (can be null).
Returns:
true if we were able to obtain a connection and authenticate.
Throws:
MessagingException

connect

public void connect(Socket s)
Establish a connection using an existing socket.

Parameters:
s - The socket to use.

getConnection

protected void getConnection()
                      throws IOException,
                             MessagingException
Create a transport connection object and connect it to the target server.

Throws:
MessagingException
IOException

getConnectionProperties

protected void getConnectionProperties()
Get common connection properties before creating a connection socket.


getConnectedSocket

protected void getConnectedSocket()
                           throws IOException
Creates a connected socket

Throws:
MessagingException
IOException

getConnectedSSLSocket

protected void getConnectedSSLSocket()
                              throws IOException
Creates a connected SSL socket for an initial SSL connection.

Throws:
MessagingException
IOException

getConnectedTLSSocket

protected void getConnectedTLSSocket()
                              throws MessagingException
Switch the connection to using TLS level security, switching to an SSL socket.

Throws:
MessagingException

getConnectionStreams

protected void getConnectionStreams()
                             throws MessagingException,
                                    IOException
Set up the input and output streams for server communications once the socket connection has been made.

Throws:
MessagingException
IOException

closeServerConnection

public void closeServerConnection()
Close the server connection at termination.


checkConnected

protected void checkConnected()
                       throws MessagingException
Verify that we have a good connection before attempting to send a command.

Throws:
MessagingException

getSASLRealm

public String getSASLRealm()
Retrieve the SASL realm used for DIGEST-MD5 authentication. This will either be explicitly set, or retrieved using the mail.imap.sasl.realm session property.

Returns:
The current realm information (which can be null).

setSASLRealm

public void setSASLRealm(String name)
Explicitly set the SASL realm used for DIGEST-MD5 authenticaiton.

Parameters:
name - The new realm name.

getSaslMechanisms

protected List getSaslMechanisms()
Get a list of the SASL mechanisms we're configured to accept.

Returns:
A list of mechanisms we're allowed to use.

getServerMechanisms

protected List getServerMechanisms()
Get the list of authentication mechanisms the server is supposed to support.

Returns:
A list of the server supported authentication mechanisms.

selectSaslMechanisms

protected List selectSaslMechanisms()
Merge the configured SASL mechanisms with the capabilities that the server has indicated it supports, returning a merged list that can be used for selecting a mechanism.

Returns:
A List representing the intersection of the configured list and the capabilities list.

getLoginAuthenticator

protected ClientAuthenticator getLoginAuthenticator()
                                             throws MessagingException
Process SASL-type authentication.

Returns:
An authenticator to process the login challenge/response handling.
Throws:
MessagingException

debugOut

protected void debugOut(String message)
Internal debug output routine.

Parameters:
value - The string value to output.

debugOut

protected void debugOut(String message,
                        Throwable e)
Internal debugging routine for reporting exceptions.

Parameters:
message - A message associated with the exception context.
e - The received exception.

hasCapability

public boolean hasCapability(String capability)
Test if this connection has a given capability.

Parameters:
capability - The capability name.
Returns:
true if this capability is in the list, false for a mismatch.

getCapabilities

public Map getCapabilities()
Get the capabilities map.

Returns:
The capabilities map for the connection.

supportsMechanism

public boolean supportsMechanism(String mech)
Test if the server supports a given mechanism.

Parameters:
mech - The mechanism name.
Returns:
true if the server has asserted support for the named mechanism.

getHost

public String getHost()
Retrieve the connection host.

Returns:
The host name.

getLocalHost

public String getLocalHost()
                    throws MessagingException
Retrieve the local client host name.

Returns:
The string version of the local host name.
Throws:
SMTPTransportException
MessagingException

setLocalHost

public void setLocalHost(String localHost)
Explicitly set the local host information.

Parameters:
localHost - The new localHost name.


Copyright © 2003-2009 The Apache Software Foundation. All Rights Reserved.