Top Description Fields Constructors Methods
com.sun.mail.iap

public Class Protocol

extends Object
Class Inheritance
Known Direct Subclasses
com.sun.mail.imap.protocol.IMAPProtocol
Imports
java.util.Properties, .ArrayList, .List, java.io.*, java.nio.channels.SocketChannel, java.net.*, javax.net.ssl.SSLSocket, java.util.concurrent.CopyOnWriteArrayList, java.util.concurrent.atomic.AtomicInteger, java.util.logging.Level, java.util.zip.Deflater, .DeflaterOutputStream, .Inflater, .InflaterInputStream, java.lang.reflect.Constructor, .Field, com.sun.mail.util.PropUtil, .MailLogger, .SocketFetcher, .TraceInputStream, .TraceOutputStream

General protocol handling code for IMAP-like protocols.

The Protocol object is multithread safe.

Authors
John Mani, Max Spivak, Bill Shannon

Field Summary

Modifier and TypeField and Description
private static final byte[]
private final List<ResponseHandler>
protected String
private volatile ResponseInputStream
private String
protected MailLogger
private volatile DataOutputStream
protected String
protected Properties
protected boolean
private Socket
private int
pack-priv static final AtomicInteger
private final String
private volatile long
private TraceInputStream
protected MailLogger
private TraceOutputStream

Constructor Summary

AccessConstructor and Description
public
Protocol(String
host to connect to
host
,
int
portnumber to connect to
port
,
Properties
Properties object used by this protocol
props
,
String
Prefix to prepend to property keys
prefix
,
boolean
use SSL?
isSSL
,
MailLogger
log messages here
logger
)

Constructor.

public
Protocol(InputStream
the InputStream to read from
in
,
PrintStream
the PrintStream to write to
out
,
Properties
Properties object used by this protocol
props
,
boolean
true to enable debugging output
debug
)

Constructor for debugging.

Method Summary

Modifier and TypeMethod and Description
public void
addResponseHandler(ResponseHandler
the response handler
h
)

Adds a response handler.

public synchronized Response[]

Returns:

array of Response objects returned by the server
command
(String
the command
command
,
Argument
the arguments
args
)

Send a command to the server.

private void
private void
private String
computePrefix(Properties props, String prefix)

Compute the tag prefix to be used for this connection.

protected synchronized void
disconnect()

Disconnect.

protected void
finalize()

Overrides java.lang.Object.finalize.

Finalizer.
public SocketChannel

Returns:

the SocketChannel
getChannel
()

Return the SocketChannel associated with this connection, if any.

public InetAddress

Returns:

the InetAddress the socket is connected to
getInetAddress
()

Return the address the socket connected to.

protected ResponseInputStream

Returns:

the input stream
getInputStream
()

Return the Protocol's InputStream.

protected synchronized String

Returns:

the name of the local host
getLocalHost
()

Get the name of the local host.

protected OutputStream

Returns:

the output stream
getOutputStream
()

Return the Protocol's OutputStream

protected ByteArray

Returns:

the buffer to use
getResponseBuffer
()

Return a buffer to be used to read a response.

public long

Returns:

the timestamp
getTimestamp
()

Returns the timestamp.

public void
handleResult(Response
the response
response
)

Convenience routine to handle OK, NO, BAD and BYE responses.

public boolean

Returns:

true if another response is in the buffer
hasResponse
()

Is another response available in our buffer?

private void
public boolean

Returns:

true if using SSL
isSSL
()

Is this connection using an SSL socket?

protected boolean

Returns:

true if protocol tracing is enabled
isTracing
()

Is protocol tracing enabled?

public void
notifyResponseHandlers(Response[]
the responses
responses
)

Notify response handlers

protected void
public Response
public void
removeResponseHandler(ResponseHandler
the response handler
h
)

Removed the specified response handler.

protected void
resumeTracing()

Resume protocol tracing, if it was enabled to begin with.

public void
simpleCommand(String
the command
cmd
,
Argument
the arguments
args
)

Convenience routine to handle simple IAP commands that do not have responses specific to that command.

public synchronized void
startCompression(String
the command to issue
cmd
)

Start compression on the current connection.

public synchronized void
startTLS(String
the command to issue
cmd
)

Start TLS on the current connection.

protected synchronized boolean

Returns:

true if the server supports non-synchronizing literals
supportsNonSyncLiterals
()

Returns whether this Protocol supports non-synchronizing literals Default is false.

public boolean

Returns:

true if the server supports UTF-8
supportsUtf8
()

Does the server support UTF-8?

protected void
suspendTracing()

Temporarily turn off protocol tracing, e.g., to prevent tracing the authentication sequence, including the password.

public String
writeCommand(String command, Argument args)

Inherited from java.lang.Object:
cloneequalsgetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CRLFback to summary
private static final byte[] CRLF
handlersback to summary
private final List<ResponseHandler> handlers
hostback to summary
protected String host
inputback to summary
private volatile ResponseInputStream input
localHostNameback to summary
private String localHostName
loggerback to summary
protected MailLogger logger
outputback to summary
private volatile DataOutputStream output
prefixback to summary
protected String prefix
propsback to summary
protected Properties props
quoteback to summary
protected boolean quote
socketback to summary
private Socket socket
tagCounterback to summary
private int tagCounter
tagNumback to summary
pack-priv static final AtomicInteger tagNum
tagPrefixback to summary
private final String tagPrefix
timestampback to summary
private volatile long timestamp
traceInputback to summary
private TraceInputStream traceInput
traceLoggerback to summary
protected MailLogger traceLogger
traceOutputback to summary
private TraceOutputStream traceOutput

Constructor Detail

Protocolback to summary
public Protocol(String host, int port, Properties props, String prefix, boolean isSSL, MailLogger logger) throws IOException, ProtocolException

Constructor.

Opens a connection to the given host at given port.

Parameters
host:String

host to connect to

port:int

portnumber to connect to

props:Properties

Properties object used by this protocol

prefix:String

Prefix to prepend to property keys

isSSL:boolean

use SSL?

logger:MailLogger

log messages here

Exceptions
IOException:
for I/O errors
ProtocolException:
for protocol failures
Protocolback to summary
public Protocol(InputStream in, PrintStream out, Properties props, boolean debug) throws IOException

Constructor for debugging.

Parameters
in:InputStream

the InputStream to read from

out:PrintStream

the PrintStream to write to

props:Properties

Properties object used by this protocol

debug:boolean

true to enable debugging output

Exceptions
IOException:
for I/O errors

Method Detail

addResponseHandlerback to summary
public void addResponseHandler(ResponseHandler h)

Adds a response handler.

Parameters
h:ResponseHandler

the response handler

commandback to summary
public synchronized Response[] command(String command, Argument args)

Send a command to the server. Collect all responses until either the corresponding command completion response or a BYE response (indicating server failure). Return all the collected responses.

Parameters
command:String

the command

args:Argument

the arguments

Returns:Response[]

array of Response objects returned by the server

commandEndback to summary
private void commandEnd()
commandStartback to summary
private void commandStart(String command)
computePrefixback to summary
private String computePrefix(Properties props, String prefix)

Compute the tag prefix to be used for this connection. Start with "A" - "Z", then "AA" - "ZZ", and finally "AAA" - "ZZZ". Wrap around after that.

disconnectback to summary
protected synchronized void disconnect()

Disconnect.

finalizeback to summary
protected void finalize() throws Throwable

Overrides java.lang.Object.finalize.

Finalizer.

Annotations
@Override
Exceptions
Throwable:

Doc from java.lang.Object.finalize.

the Exception raised by this method

getChannelback to summary
public SocketChannel getChannel()

Return the SocketChannel associated with this connection, if any.

Returns:SocketChannel

the SocketChannel

Since
JavaMail 1.5.2
getInetAddressback to summary
public InetAddress getInetAddress()

Return the address the socket connected to.

Returns:InetAddress

the InetAddress the socket is connected to

Since
JavaMail 1.5.2
getInputStreamback to summary
protected ResponseInputStream getInputStream()

Return the Protocol's InputStream.

Returns:ResponseInputStream

the input stream

getLocalHostback to summary
protected synchronized String getLocalHost()

Get the name of the local host. The property <prefix>.localhost overrides <prefix>.localaddress, which overrides what InetAddress would tell us.

Returns:String

the name of the local host

getOutputStreamback to summary
protected OutputStream getOutputStream()

Return the Protocol's OutputStream

Returns:OutputStream

the output stream

getResponseBufferback to summary
protected ByteArray getResponseBuffer()

Return a buffer to be used to read a response. The default implementation returns null, which causes a new buffer to be allocated for every response.

Returns:ByteArray

the buffer to use

Since
JavaMail 1.4.1
getTimestampback to summary
public long getTimestamp()

Returns the timestamp.

Returns:long

the timestamp

handleResultback to summary
public void handleResult(Response response) throws ProtocolException

Convenience routine to handle OK, NO, BAD and BYE responses.

Parameters
response:Response

the response

Exceptions
ProtocolException:
for protocol failures
hasResponseback to summary
public boolean hasResponse()

Is another response available in our buffer?

Returns:boolean

true if another response is in the buffer

Since
JavaMail 1.5.4
initStreamsback to summary
private void initStreams() throws IOException
isSSLback to summary
public boolean isSSL()

Is this connection using an SSL socket?

Returns:boolean

true if using SSL

Since
JavaMail 1.4.6
isTracingback to summary
protected boolean isTracing()

Is protocol tracing enabled?

Returns:boolean

true if protocol tracing is enabled

notifyResponseHandlersback to summary
public void notifyResponseHandlers(Response[] responses)

Notify response handlers

Parameters
responses:Response[]

the responses

processGreetingback to summary
protected void processGreeting(Response r) throws ProtocolException
readResponseback to summary
public Response readResponse() throws IOException, ProtocolException
removeResponseHandlerback to summary
public void removeResponseHandler(ResponseHandler h)

Removed the specified response handler.

Parameters
h:ResponseHandler

the response handler

resumeTracingback to summary
protected void resumeTracing()

Resume protocol tracing, if it was enabled to begin with.

simpleCommandback to summary
public void simpleCommand(String cmd, Argument args) throws ProtocolException

Convenience routine to handle simple IAP commands that do not have responses specific to that command.

Parameters
cmd:String

the command

args:Argument

the arguments

Exceptions
ProtocolException:
for protocol failures
startCompressionback to summary
public synchronized void startCompression(String cmd) throws IOException, ProtocolException

Start compression on the current connection. cmd is the command to issue to start compression. If the command succeeds, we begin compression.

Parameters
cmd:String

the command to issue

Exceptions
IOException:
for I/O errors
ProtocolException:
for protocol failures
startTLSback to summary
public synchronized void startTLS(String cmd) throws IOException, ProtocolException

Start TLS on the current connection. 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.

Parameters
cmd:String

the command to issue

Exceptions
IOException:
for I/O errors
ProtocolException:
for protocol failures
supportsNonSyncLiteralsback to summary
protected synchronized boolean supportsNonSyncLiterals()

Returns whether this Protocol supports non-synchronizing literals Default is false. Subclasses should override this if required

Returns:boolean

true if the server supports non-synchronizing literals

supportsUtf8back to summary
public boolean supportsUtf8()

Does the server support UTF-8? This implementation returns false. Subclasses should override as appropriate.

Returns:boolean

true if the server supports UTF-8

Since
JavaMail 1.6.0
suspendTracingback to summary
protected void suspendTracing()

Temporarily turn off protocol tracing, e.g., to prevent tracing the authentication sequence, including the password.

writeCommandback to summary
public String writeCommand(String command, Argument args) throws IOException, ProtocolException