Class IMAPClientProtocolHandler
- All Implemented Interfaces:
ClientAppendState,ClientAuthenticatedState,ClientAuthExchange,ClientIdleState,ClientNotAuthenticatedState,ClientPostStarttls,ClientSelectedState,LineParser.Callback,ProtocolHandler
Implements a type-safe IMAP client state machine with staged callback interfaces constraining which operations are valid at each protocol stage. Tagged command tracking ensures exactly one command is in-flight at a time (RFC 9051 section 5.5).
Line parsing is handled by the composable LineParser utility.
Incoming literal data (FETCH body sections) is tracked by
LiteralTracker and streamed as ByteBuffer chunks.
Supported features:
- STARTTLS — RFC 9051 section 6.2.1
- SASL AUTHENTICATE with initial response — RFC 4959
- IDLE — RFC 2177
- NAMESPACE — RFC 2342
- MOVE — RFC 6851
- APPEND with literal streaming
- Author:
- Chris Burdess
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionIMAPClientProtocolHandler(ServerGreeting handler) Creates an IMAP client protocol handler. -
Method Summary
Modifier and TypeMethodDescriptionvoidabort(ServerAuthAbortHandler callback) voidappend(String mailbox, String[] flags, String date, long size, ServerAppendReplyHandler callback) voidauthenticate(String mechanism, byte[] initialResponse, ServerAuthReplyHandler callback) voidcapability(ServerCapabilityReplyHandler callback) voidclose()voidclose(ServerCloseReplyHandler callback) voidCalled when the endpoint is established and ready for protocol traffic.booleanCalled after each line to determine whether parsing should continue.voidcopy(String sequenceSet, String mailbox, ServerCopyReplyHandler callback) voidcreate(String mailbox, ServerMailboxReplyHandler callback) voiddelete(String mailbox, ServerMailboxReplyHandler callback) voidCalled when the peer has closed the connection or stream.voiddone()voidvoidCalled when an unrecoverable error occurs on the endpoint.voidexamine(String mailbox, ServerSelectReplyHandler callback) voidexpunge(ServerExpungeReplyHandler callback) voidfetch(String sequenceSet, String dataItems, ServerFetchReplyHandler callback) voidgetQuota(String quotaRoot, ServerQuotaReplyHandler callback) voidgetQuotaRoot(String mailbox, ServerQuotaReplyHandler callback) voididle(ServerIdleEventHandler callback) booleanisOpen()voidlineReceived(ByteBuffer line) Called when a complete CRLF-terminated line has been parsed.voidlist(String reference, String pattern, ServerListReplyHandler callback) voidvoidliteralContent(ByteBuffer data) voidlogin(String username, String password, ServerLoginReplyHandler callback) voidlogout()voidlsub(String reference, String pattern, ServerListReplyHandler callback) voidmove(String sequenceSet, String mailbox, ServerCopyReplyHandler callback) voidnamespace(ServerNamespaceReplyHandler callback) voidnoop(ServerNoopReplyHandler callback) voidonWriteReady(Runnable callback) Registers a one-shot callback invoked when the transport is ready for more data.voidreceive(ByteBuffer data) Called when plaintext application data is received from the peer.voidrename(String from, String to, ServerMailboxReplyHandler callback) voidrespond(byte[] response, ServerAuthReplyHandler callback) voidsearch(String criteria, ServerSearchReplyHandler callback) voidCalled when the security layer becomes active.voidselect(String mailbox, ServerSelectReplyHandler callback) voidsetMailboxEventListener(MailboxEventListener listener) Sets the listener for unsolicited mailbox events.voidsetSecure(boolean secure) Sets whether this connection started in secure mode.voidstarttls(ServerStarttlsReplyHandler callback) voidstatus(String mailbox, String[] items, ServerStatusReplyHandler callback) voidstore(String sequenceSet, String action, String[] flags, ServerStoreReplyHandler callback) voidsubscribe(String mailbox, ServerMailboxReplyHandler callback) voiduidCopy(String sequenceSet, String mailbox, ServerCopyReplyHandler callback) voiduidFetch(String sequenceSet, String dataItems, ServerFetchReplyHandler callback) voiduidMove(String sequenceSet, String mailbox, ServerCopyReplyHandler callback) voiduidSearch(String criteria, ServerSearchReplyHandler callback) voiduidStore(String sequenceSet, String action, String[] flags, ServerStoreReplyHandler callback) voidunselect(ServerCloseReplyHandler callback) voidunsubscribe(String mailbox, ServerMailboxReplyHandler callback) voidwriteContent(ByteBuffer data)
-
Constructor Details
-
IMAPClientProtocolHandler
Creates an IMAP client protocol handler.- Parameters:
handler- the server greeting handler
-
-
Method Details
-
setSecure
public void setSecure(boolean secure) Sets whether this connection started in secure mode.- Parameters:
secure- true for implicit TLS (IMAPS port 993)
-
setMailboxEventListener
Sets the listener for unsolicited mailbox events.- Parameters:
listener- the event listener, or null to clear
-
connected
Description copied from interface:ProtocolHandlerCalled when the endpoint is established and ready for protocol traffic.The endpoint reference passed here should be stored by the handler for sending data and querying connection state.
- Specified by:
connectedin interfaceProtocolHandler- Parameters:
ep- the endpoint that is now connected
-
receive
Description copied from interface:ProtocolHandlerCalled when plaintext application data is received from the peer.The buffer is in read mode (position at data start, limit at data end). The handler should consume as much data as it can. After this method returns, any unconsumed data (between the buffer's position and limit) will be preserved for the next call.
- Specified by:
receivein interfaceProtocolHandler- Parameters:
data- the application data received
-
disconnected
public void disconnected()Description copied from interface:ProtocolHandlerCalled when the peer has closed the connection or stream.After this method returns, the endpoint is no longer usable. Protocol handlers should perform any cleanup here.
- Specified by:
disconnectedin interfaceProtocolHandler
-
securityEstablished
Description copied from interface:ProtocolHandlerCalled when the security layer becomes active.For TCP with STARTTLS, this is called after the TLS handshake completes. For QUIC (always secure), this is called before
ProtocolHandler.connected(Endpoint). For initially-secure TCP, this is also called beforeProtocolHandler.connected(Endpoint).Protocol handlers that need to reset state after STARTTLS (e.g., SMTP EHLO re-issue) should do so here.
- Specified by:
securityEstablishedin interfaceProtocolHandler- Parameters:
info- details about the negotiated security parameters
-
error
Description copied from interface:ProtocolHandlerCalled when an unrecoverable error occurs on the endpoint.This covers I/O errors, TLS handshake failures, and connection failures for client-initiated endpoints. The endpoint may or may not be usable after this call.
- Specified by:
errorin interfaceProtocolHandler- Parameters:
cause- the exception that caused the error
-
lineReceived
Description copied from interface:LineParser.CallbackCalled when a complete CRLF-terminated line has been parsed.The buffer contains exactly one line including its CRLF terminator, with position at the start of the line and limit immediately after the terminating LF.
- Specified by:
lineReceivedin interfaceLineParser.Callback- Parameters:
line- a buffer containing the complete line including CRLF
-
continueLineProcessing
public boolean continueLineProcessing()Description copied from interface:LineParser.CallbackCalled after each line to determine whether parsing should continue.Return false when the protocol transitions out of line-based mode (e.g., SMTP DATA, IMAP APPEND). When this returns false,
LineParser.parse(ByteBuffer, Callback)stops and returns, leaving remaining data unconsumed in the buffer.- Specified by:
continueLineProcessingin interfaceLineParser.Callback- Returns:
- true to continue parsing lines, false to stop
-
literalContent
-
literalComplete
public void literalComplete() -
isOpen
public boolean isOpen() -
close
public void close() -
capability
- Specified by:
capabilityin interfaceClientNotAuthenticatedState- Specified by:
capabilityin interfaceClientPostStarttls
-
login
- Specified by:
loginin interfaceClientNotAuthenticatedState- Specified by:
loginin interfaceClientPostStarttls
-
authenticate
- Specified by:
authenticatein interfaceClientNotAuthenticatedState- Specified by:
authenticatein interfaceClientPostStarttls
-
starttls
- Specified by:
starttlsin interfaceClientNotAuthenticatedState
-
logout
public void logout()- Specified by:
logoutin interfaceClientAuthenticatedState- Specified by:
logoutin interfaceClientNotAuthenticatedState- Specified by:
logoutin interfaceClientPostStarttls
-
respond
- Specified by:
respondin interfaceClientAuthExchange
-
abort
- Specified by:
abortin interfaceClientAuthExchange
-
select
- Specified by:
selectin interfaceClientAuthenticatedState
-
examine
- Specified by:
examinein interfaceClientAuthenticatedState
-
create
- Specified by:
createin interfaceClientAuthenticatedState
-
delete
- Specified by:
deletein interfaceClientAuthenticatedState
-
rename
- Specified by:
renamein interfaceClientAuthenticatedState
-
subscribe
- Specified by:
subscribein interfaceClientAuthenticatedState
-
unsubscribe
- Specified by:
unsubscribein interfaceClientAuthenticatedState
-
list
- Specified by:
listin interfaceClientAuthenticatedState
-
lsub
- Specified by:
lsubin interfaceClientAuthenticatedState
-
status
- Specified by:
statusin interfaceClientAuthenticatedState
-
namespace
- Specified by:
namespacein interfaceClientAuthenticatedState
-
getQuota
- Specified by:
getQuotain interfaceClientAuthenticatedState
-
getQuotaRoot
- Specified by:
getQuotaRootin interfaceClientAuthenticatedState
-
append
public void append(String mailbox, String[] flags, String date, long size, ServerAppendReplyHandler callback) - Specified by:
appendin interfaceClientAuthenticatedState
-
idle
- Specified by:
idlein interfaceClientAuthenticatedState
-
noop
- Specified by:
noopin interfaceClientAuthenticatedState
-
close
- Specified by:
closein interfaceClientSelectedState
-
unselect
- Specified by:
unselectin interfaceClientSelectedState
-
expunge
- Specified by:
expungein interfaceClientSelectedState
-
search
- Specified by:
searchin interfaceClientSelectedState
-
uidSearch
- Specified by:
uidSearchin interfaceClientSelectedState
-
fetch
- Specified by:
fetchin interfaceClientSelectedState
-
uidFetch
- Specified by:
uidFetchin interfaceClientSelectedState
-
store
public void store(String sequenceSet, String action, String[] flags, ServerStoreReplyHandler callback) - Specified by:
storein interfaceClientSelectedState
-
uidStore
public void uidStore(String sequenceSet, String action, String[] flags, ServerStoreReplyHandler callback) - Specified by:
uidStorein interfaceClientSelectedState
-
copy
- Specified by:
copyin interfaceClientSelectedState
-
uidCopy
- Specified by:
uidCopyin interfaceClientSelectedState
-
move
- Specified by:
movein interfaceClientSelectedState
-
uidMove
- Specified by:
uidMovein interfaceClientSelectedState
-
done
public void done()- Specified by:
donein interfaceClientIdleState
-
writeContent
- Specified by:
writeContentin interfaceClientAppendState
-
onWriteReady
Description copied from interface:ClientAppendStateRegisters a one-shot callback invoked when the transport is ready for more data. Use this to pace large APPEND uploads: send a chunk, register a callback, and send the next chunk from the callback.- Specified by:
onWriteReadyin interfaceClientAppendState- Parameters:
callback- the callback, or null to clear
-
endAppend
public void endAppend()- Specified by:
endAppendin interfaceClientAppendState
-