Class Session

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

public class Session extends Object implements Runnable, MessageContext
The thread that handles a connection. This class passes most of it's responsibilities off to the CommandHandler.
Author:
Jon Stevens, Jeff Schnitzer
  • Constructor Details

    • Session

      public Session(SMTPServer server, org.subethamail.smtp.server.ServerThread serverThread, Socket socket) throws IOException
      Creates the Runnable Session object.
      Parameters:
      server - a link to our parent
      socket - is the socket to the client
      Throws:
      IOException
  • Method Details

    • 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 quitting member goes true.
      Specified by:
      run in interface Runnable
    • setSocket

      public void setSocket(Socket socket) throws 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:
      IOException
    • getSocket

      public Socket getSocket()
      This method is only used by the start tls command
      Returns:
      the current socket to the client
    • closeSocket

      public void closeSocket() throws IOException
      Close the client socket if it is open
      Throws:
      IOException
    • getRawInput

      public 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(String response) throws IOException
      Sends the response to the client
      Throws:
      IOException
    • getSessionId

      public String getSessionId()
      Returns an identifier of the session which is reasonably unique within an extended time period.
    • getRemoteAddress

      public 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 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(String value)
    • getHasMailFrom

      @Deprecated public boolean getHasMailFrom()
    • addRecipient

      public void addRecipient(String recipientAddress)
    • getRecipientCount

      public int getRecipientCount()
    • getSingleRecipient

      public String getSingleRecipient()
      Returns the first accepted recipient if there is exactly one accepted recipient, otherwise it returns null.
    • 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
    • startMailTransaction

      public void startMailTransaction() throws IllegalStateException
      Starts a mail transaction by creating a new message handler.
      Throws:
      IllegalStateException - if a mail transaction is already in progress
    • isMailTransactionInProgress

      public boolean isMailTransactionInProgress()
      Returns true if a mail transaction is started, i.e. a MAIL command is received, and the transaction is not yet completed or aborted. A transaction is successfully completed after the message content is received and accepted at the end of the DATA command.
    • resetMailTransaction

      public void resetMailTransaction()
      Stops the mail transaction if it in progress and resets all state related to mail transactions.

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

    • resetMessageState

      @Deprecated public void resetMessageState()
      Deprecated.
    • resetSmtpProtocol

      public void resetSmtpProtocol()
      Reset the SMTP protocol to the initial state, which is the state after a server issues a 220 service ready greeting.
    • 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(Certificate[] tlsPeerCertificates)
    • getTlsPeerCertificates

      public 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: