public class Protocol
extends java.lang.Object
The Protocol object is multithread safe.
Modifier and Type | Field and Description |
---|---|
private static byte[] |
CRLF |
private java.util.List<ResponseHandler> |
handlers |
protected java.lang.String |
host |
private ResponseInputStream |
input |
private java.lang.String |
localHostName |
protected MailLogger |
logger |
private java.io.DataOutputStream |
output |
protected java.lang.String |
prefix |
protected java.util.Properties |
props |
protected boolean |
quote |
private java.net.Socket |
socket |
private int |
tagCounter |
(package private) static java.util.concurrent.atomic.AtomicInteger |
tagNum |
private java.lang.String |
tagPrefix |
private long |
timestamp |
private TraceInputStream |
traceInput |
protected MailLogger |
traceLogger |
private TraceOutputStream |
traceOutput |
Constructor and Description |
---|
Protocol(java.io.InputStream in,
java.io.PrintStream out,
java.util.Properties props,
boolean debug)
Constructor for debugging.
|
Protocol(java.lang.String host,
int port,
java.util.Properties props,
java.lang.String prefix,
boolean isSSL,
MailLogger logger)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
addResponseHandler(ResponseHandler h)
Adds a response handler.
|
Response[] |
command(java.lang.String command,
Argument args)
Send a command to the server.
|
private void |
commandEnd() |
private void |
commandStart(java.lang.String command) |
private java.lang.String |
computePrefix(java.util.Properties props,
java.lang.String prefix)
Compute the tag prefix to be used for this connection.
|
protected void |
disconnect()
Disconnect.
|
protected void |
finalize()
Finalizer.
|
java.nio.channels.SocketChannel |
getChannel()
Return the SocketChannel associated with this connection, if any.
|
java.net.InetAddress |
getInetAddress()
Return the address the socket connected to.
|
protected ResponseInputStream |
getInputStream()
Return the Protocol's InputStream.
|
protected java.lang.String |
getLocalHost()
Get the name of the local host.
|
protected java.io.OutputStream |
getOutputStream()
Return the Protocol's OutputStream
|
protected ByteArray |
getResponseBuffer()
Return a buffer to be used to read a response.
|
long |
getTimestamp()
Returns the timestamp.
|
void |
handleResult(Response response)
Convenience routine to handle OK, NO, BAD and BYE responses.
|
boolean |
hasResponse()
Is another response available in our buffer?
|
private void |
initStreams() |
boolean |
isSSL()
Is this connection using an SSL socket?
|
protected boolean |
isTracing()
Is protocol tracing enabled?
|
void |
notifyResponseHandlers(Response[] responses)
Notify response handlers
|
protected void |
processGreeting(Response r) |
Response |
readResponse() |
void |
removeResponseHandler(ResponseHandler h)
Removed the specified response handler.
|
protected void |
resumeTracing()
Resume protocol tracing, if it was enabled to begin with.
|
void |
simpleCommand(java.lang.String cmd,
Argument args)
Convenience routine to handle simple IAP commands
that do not have responses specific to that command.
|
void |
startCompression(java.lang.String cmd)
Start compression on the current connection.
|
void |
startTLS(java.lang.String cmd)
Start TLS on the current connection.
|
protected boolean |
supportsNonSyncLiterals()
Returns whether this Protocol supports non-synchronizing literals
Default is false.
|
boolean |
supportsUtf8()
Does the server support UTF-8?
This implementation returns false.
|
protected void |
suspendTracing()
Temporarily turn off protocol tracing, e.g., to prevent
tracing the authentication sequence, including the password.
|
java.lang.String |
writeCommand(java.lang.String command,
Argument args) |
protected java.lang.String host
private java.net.Socket socket
protected boolean quote
protected MailLogger logger
protected MailLogger traceLogger
protected java.util.Properties props
protected java.lang.String prefix
private TraceInputStream traceInput
private volatile ResponseInputStream input
private TraceOutputStream traceOutput
private volatile java.io.DataOutputStream output
private int tagCounter
private final java.lang.String tagPrefix
private java.lang.String localHostName
private final java.util.List<ResponseHandler> handlers
private volatile long timestamp
static final java.util.concurrent.atomic.AtomicInteger tagNum
private static final byte[] CRLF
public Protocol(java.lang.String host, int port, java.util.Properties props, java.lang.String prefix, boolean isSSL, MailLogger logger) throws java.io.IOException, ProtocolException
Opens a connection to the given host at given port.
host
- host to connect toport
- portnumber to connect toprops
- Properties object used by this protocolprefix
- Prefix to prepend to property keysisSSL
- use SSL?logger
- log messages herejava.io.IOException
- for I/O errorsProtocolException
- for protocol failurespublic Protocol(java.io.InputStream in, java.io.PrintStream out, java.util.Properties props, boolean debug) throws java.io.IOException
in
- the InputStream to read fromout
- the PrintStream to write toprops
- Properties object used by this protocoldebug
- true to enable debugging outputjava.io.IOException
- for I/O errorsprivate void initStreams() throws java.io.IOException
java.io.IOException
private java.lang.String computePrefix(java.util.Properties props, java.lang.String prefix)
public long getTimestamp()
public void addResponseHandler(ResponseHandler h)
h
- the response handlerpublic void removeResponseHandler(ResponseHandler h)
h
- the response handlerpublic void notifyResponseHandlers(Response[] responses)
responses
- the responsesprotected void processGreeting(Response r) throws ProtocolException
ProtocolException
protected ResponseInputStream getInputStream()
protected java.io.OutputStream getOutputStream()
protected boolean supportsNonSyncLiterals()
public Response readResponse() throws java.io.IOException, ProtocolException
java.io.IOException
ProtocolException
public boolean hasResponse()
protected ByteArray getResponseBuffer()
public java.lang.String writeCommand(java.lang.String command, Argument args) throws java.io.IOException, ProtocolException
java.io.IOException
ProtocolException
public Response[] command(java.lang.String command, Argument args)
command
- the commandargs
- the argumentspublic void handleResult(Response response) throws ProtocolException
response
- the responseProtocolException
- for protocol failurespublic void simpleCommand(java.lang.String cmd, Argument args) throws ProtocolException
cmd
- the commandargs
- the argumentsProtocolException
- for protocol failurespublic void startTLS(java.lang.String cmd) throws java.io.IOException, ProtocolException
cmd
is the command to issue to start TLS negotiation.
If the command succeeds, we begin TLS negotiation.
If the socket is already an SSLSocket this is a nop and the command
is not issued.cmd
- the command to issuejava.io.IOException
- for I/O errorsProtocolException
- for protocol failurespublic void startCompression(java.lang.String cmd) throws java.io.IOException, ProtocolException
cmd
is the command to issue to start compression.
If the command succeeds, we begin compression.cmd
- the command to issuejava.io.IOException
- for I/O errorsProtocolException
- for protocol failurespublic boolean isSSL()
public java.net.InetAddress getInetAddress()
public java.nio.channels.SocketChannel getChannel()
public boolean supportsUtf8()
protected void disconnect()
protected java.lang.String getLocalHost()
protected boolean isTracing()
protected void suspendTracing()
protected void resumeTracing()
protected void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable
private void commandStart(java.lang.String command)
private void commandEnd()