org.subethamail.smtp.server
Class Session

java.lang.Object
  extended by java.lang.Thread
      extended by org.subethamail.smtp.server.Session
All Implemented Interfaces:
java.lang.Runnable, MessageContext

public class Session
extends java.lang.Thread
implements MessageContext

The thread that handles a connection. This class passes most of it's responsibilities off to the CommandHandler.

Author:
Jon Stevens, Jeff Schnitzer

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
Session(SMTPServer server, java.net.Socket socket)
          Creates (but does not start) the thread object.
 
Method Summary
 void addRecipient()
           
 void closeSocket()
          Close the client socket if it is open
protected  void endMessageHandler()
          Safely calls done() on a message hander, if one exists
 AuthenticationHandler getAuthenticationHandler()
           
 int getDeclaredMessageSize()
           
 boolean getHasMailFrom()
           
 java.lang.String getHelo()
          Simple state
 MessageHandler getMessageHandler()
           
 java.io.InputStream getRawInput()
           
 CRLFTerminatedReader getReader()
           
 int getRecipientCount()
           
 java.net.InetSocketAddress getRemoteAddress()
           
 SMTPServer getServer()
           
 SMTPServer getSMTPServer()
           
 java.net.Socket getSocket()
          This method is only used by the start tls command
 java.security.cert.Certificate[] getTlsPeerCertificates()
          Returns the identity of the peer which was established as part of the TLS handshake as defined by SSLSession.getPeerCertificates().
 boolean isAuthenticated()
           
 boolean isTLSStarted()
           
 void quit()
          Triggers the shutdown of the thread and the closing of the connection.
 void resetMessageState()
          Some state is associated with each particular message (senders, recipients, the message handler).
 void run()
          The thread for each session runs on this and shuts down when the shutdown member goes true.
 void sendResponse(java.lang.String response)
          Sends the response to the client
 void setAuthenticationHandler(AuthenticationHandler handler)
          This is called by the AuthCommand when a session is successfully authenticated.
 void setDeclaredMessageSize(int declaredMessageSize)
           
 void setHasMailFrom(boolean value)
           
 void setHelo(java.lang.String value)
           
 void setSocket(java.net.Socket socket)
          Initializes our reader, writer, and the i/o filter chains based on the specified socket.
 void setTlsPeerCertificates(java.security.cert.Certificate[] tlsPeerCertificates)
           
 void setTlsStarted(boolean tlsStarted)
           
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Session

public Session(SMTPServer server,
               java.net.Socket socket)
        throws java.io.IOException
Creates (but does not start) the thread object.

Parameters:
server - a link to our parent
socket - is the socket to the client
Throws:
java.io.IOException
Method Detail

getServer

public SMTPServer getServer()
Returns:
a reference to the master server object

run

public void run()
The thread for each session runs on this and shuts down when the shutdown member goes true.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

setSocket

public void setSocket(java.net.Socket socket)
               throws java.io.IOException
Initializes our reader, writer, and the i/o filter chains based on the specified socket. This is called internally when we startup and when (if) SSL is started.

Throws:
java.io.IOException

getSocket

public java.net.Socket getSocket()
This method is only used by the start tls command

Returns:
the current socket to the client

closeSocket

public void closeSocket()
                 throws java.io.IOException
Close the client socket if it is open

Throws:
java.io.IOException

getRawInput

public java.io.InputStream getRawInput()
Returns:
the raw input stream from the client

getReader

public CRLFTerminatedReader getReader()
Returns:
the cooked CRLF-terminated reader from the client

sendResponse

public void sendResponse(java.lang.String response)
                  throws java.io.IOException
Sends the response to the client

Throws:
java.io.IOException

getRemoteAddress

public java.net.InetSocketAddress getRemoteAddress()
Specified by:
getRemoteAddress in interface MessageContext
Returns:
the IP address of the remote server.

getSMTPServer

public SMTPServer getSMTPServer()
Specified by:
getSMTPServer in interface MessageContext
Returns:
the SMTPServer object.

getMessageHandler

public MessageHandler getMessageHandler()
Returns:
the current message handler

getHelo

public java.lang.String getHelo()
Simple state

Specified by:
getHelo in interface MessageContext
Returns:
the host name or address literal the client supplied in the HELO or EHLO command, or null if neither of these commands were received yet. Note that SubEthaSMTP (along with some MTAs, but contrary to RFC 5321) accept mail transactions without these commands.

setHelo

public void setHelo(java.lang.String value)

getHasMailFrom

public boolean getHasMailFrom()

setHasMailFrom

public void setHasMailFrom(boolean value)

addRecipient

public void addRecipient()

getRecipientCount

public int getRecipientCount()

isAuthenticated

public boolean isAuthenticated()

getAuthenticationHandler

public AuthenticationHandler getAuthenticationHandler()
Specified by:
getAuthenticationHandler in interface MessageContext
Returns:
the handler instance that was used to authenticate.

setAuthenticationHandler

public void setAuthenticationHandler(AuthenticationHandler handler)
This is called by the AuthCommand when a session is successfully authenticated. The handler will be an object created by the AuthenticationHandlerFactory.


getDeclaredMessageSize

public int getDeclaredMessageSize()
Returns:
the maxMessageSize

setDeclaredMessageSize

public void setDeclaredMessageSize(int declaredMessageSize)
Parameters:
declaredMessageSize - the size that the client says the message will be

resetMessageState

public void resetMessageState()
Some state is associated with each particular message (senders, recipients, the message handler). Some state is not; seeing hello, TLS, authentication.


endMessageHandler

protected void endMessageHandler()
Safely calls done() on a message hander, if one exists


quit

public void quit()
Triggers the shutdown of the thread and the closing of the connection.


isTLSStarted

public boolean isTLSStarted()
Returns:
true when the TLS handshake was completed, false otherwise

setTlsStarted

public void setTlsStarted(boolean tlsStarted)
Parameters:
tlsStarted - true when the TLS handshake was completed, false otherwise

setTlsPeerCertificates

public void setTlsPeerCertificates(java.security.cert.Certificate[] tlsPeerCertificates)

getTlsPeerCertificates

public java.security.cert.Certificate[] getTlsPeerCertificates()
Returns the identity of the peer which was established as part of the TLS handshake as defined by SSLSession.getPeerCertificates().

In order to get this information, override SMTPServer.createSSLSocket(java.net.Socket) and call setNeedClientAuth(true) on the created socket.

Specified by:
getTlsPeerCertificates in interface MessageContext
Returns:
an ordered array of peer certificates, with the peer's own certificate first followed by any certificate authorities, or null when no such information is available
See Also:
SSLSession.getPeerCertificates()


Copyright © 2006-2011. All Rights Reserved.