You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2019/12/13 02:39:30 UTC

[james-project] 14/27: PROTOCOLS-120 ImapParserFactory should rely on AbstractImapCommandParser for commandName <-> parser binding

This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit fa41121f0e539096a0a464a0b7130e2dcdcf9bd7
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Fri Dec 6 15:51:17 2019 +0700

    PROTOCOLS-120 ImapParserFactory should rely on AbstractImapCommandParser for commandName <-> parser binding
---
 .../imap/decode/parser/ImapParserFactory.java      | 152 +++++++++++----------
 1 file changed, 79 insertions(+), 73 deletions(-)

diff --git a/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/ImapParserFactory.java b/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/ImapParserFactory.java
index 0d02fc0..23e6a75 100644
--- a/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/ImapParserFactory.java
+++ b/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/ImapParserFactory.java
@@ -19,14 +19,17 @@
 
 package org.apache.james.imap.decode.parser;
 
-import java.util.HashMap;
 import java.util.Locale;
 import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Stream;
 
-import org.apache.james.imap.api.ImapConstants;
 import org.apache.james.imap.api.message.response.StatusResponseFactory;
 import org.apache.james.imap.decode.ImapCommandParser;
 import org.apache.james.imap.decode.ImapCommandParserFactory;
+import org.apache.james.imap.decode.base.AbstractImapCommandParser;
+
+import com.github.steveash.guavate.Guavate;
 
 /**
  * A factory for ImapCommand instances, provided based on the command name.
@@ -36,77 +39,80 @@ public class ImapParserFactory implements ImapCommandParserFactory {
     private final Map<String, ImapCommandParser> imapCommands;
 
     public ImapParserFactory(StatusResponseFactory statusResponseFactory) {
-        imapCommands = new HashMap<>();
-
-        // Commands valid in any state
-        // CAPABILITY, NOOP, and LOGOUT
-        imapCommands.put(ImapConstants.CAPABILITY_COMMAND_NAME, new CapabilityCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.NOOP_COMMAND_NAME, new NoopCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.LOGOUT_COMMAND_NAME, new LogoutCommandParser(statusResponseFactory));
-
-        // Commands valid in NON_AUTHENTICATED state.
-        // AUTHENTICATE and LOGIN
-        imapCommands.put(ImapConstants.AUTHENTICATE_COMMAND_NAME, new AuthenticateCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.LOGIN_COMMAND_NAME, new LoginCommandParser(statusResponseFactory));
-
-        // Commands valid in AUTHENTICATED or SELECTED state.
-        // RFC2060: SELECT, EXAMINE, CREATE, DELETE, RENAME, SUBSCRIBE,
-        // UNSUBSCRIBE, LIST, LSUB, STATUS, and APPEND
-        imapCommands.put(ImapConstants.SELECT_COMMAND_NAME, new SelectCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.EXAMINE_COMMAND_NAME, new ExamineCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.CREATE_COMMAND_NAME, new CreateCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.DELETE_COMMAND_NAME, new DeleteCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.RENAME_COMMAND_NAME, new RenameCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.SUBSCRIBE_COMMAND_NAME, new SubscribeCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.UNSUBSCRIBE_COMMAND_NAME, new UnsubscribeCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.LIST_COMMAND_NAME, new ListCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.XLIST_COMMAND_NAME, new XListCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.LSUB_COMMAND_NAME, new LsubCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.STATUS_COMMAND_NAME, new StatusCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.APPEND_COMMAND_NAME, new AppendCommandParser(statusResponseFactory));
-
-        // RFC2342 NAMESPACE
-        imapCommands.put(ImapConstants.NAMESPACE_COMMAND_NAME, new NamespaceCommandParser(statusResponseFactory));
-
-        // RFC4314 GETACL, SETACL, DELETEACL, LISTRIGHTS, MYRIGHTS
-        imapCommands.put(ImapConstants.GETACL_COMMAND_NAME, new GetACLCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.SETACL_COMMAND_NAME, new SetACLCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.DELETEACL_COMMAND_NAME, new DeleteACLCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.LISTRIGHTS_COMMAND_NAME, new ListRightsCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.MYRIGHTS_COMMAND_NAME, new MyRightsCommandParser(statusResponseFactory));
-
-        // Commands only valid in SELECTED state.
-        // CHECK, CLOSE, EXPUNGE, SEARCH, FETCH, STORE, COPY, UID and IDLE
-        imapCommands.put(ImapConstants.CHECK_COMMAND_NAME, new CheckCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.CLOSE_COMMAND_NAME, new CloseCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.EXPUNGE_COMMAND_NAME, new ExpungeCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.COPY_COMMAND_NAME, new CopyCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.MOVE_COMMAND_NAME, new MoveCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.SEARCH_COMMAND_NAME, new SearchCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.FETCH_COMMAND_NAME, new FetchCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.STORE_COMMAND_NAME, new StoreCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.UID_COMMAND_NAME, new UidCommandParser(this, statusResponseFactory));
-        imapCommands.put(ImapConstants.IDLE_COMMAND_NAME, new IdleCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.STARTTLS, new StartTLSCommandParser(statusResponseFactory));
-
-        // RFC3691
-        imapCommands.put(ImapConstants.UNSELECT_COMMAND_NAME, new UnselectCommandParser(statusResponseFactory));
-
-        // RFC4978
-        imapCommands.put(ImapConstants.COMPRESS_COMMAND_NAME, new CompressCommandParser(statusResponseFactory));
-
-        imapCommands.put(ImapConstants.ENABLE_COMMAND_NAME, new EnableCommandParser(statusResponseFactory));
-
-        // RFC2087
-        // GETQUOTAROOT, GETQUOTA, SETQUOTA
-        imapCommands.put(ImapConstants.GETQUOTAROOT_COMMAND_NAME, new GetQuotaRootCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.GETQUOTA_COMMAND_NAME, new GetQuotaCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.SETQUOTA_COMMAND_NAME, new SetQuotaCommandParser(statusResponseFactory));
-
-        //RFC5464
-        //SETMETADATA, GETMETADATA
-        imapCommands.put(ImapConstants.SETANNOTATION_COMMAND_NAME, new SetAnnotationCommandParser(statusResponseFactory));
-        imapCommands.put(ImapConstants.GETANNOTATION_COMMAND_NAME, new GetAnnotationCommandParser(statusResponseFactory));
+        Stream<AbstractImapCommandParser> parsers = Stream.of(
+            // Commands valid in any state
+            // CAPABILITY, NOOP, and LOGOUT
+            new CapabilityCommandParser(statusResponseFactory),
+            new NoopCommandParser(statusResponseFactory),
+            new LogoutCommandParser(statusResponseFactory),
+
+            // Commands valid in NON_AUTHENTICATED state.
+            // AUTHENTICATE and LOGIN
+            new AuthenticateCommandParser(statusResponseFactory),
+            new LoginCommandParser(statusResponseFactory),
+
+            // Commands valid in AUTHENTICATED or SELECTED state.
+            // RFC2060: SELECT, EXAMINE, CREATE, DELETE, RENAME, SUBSCRIBE,
+            // UNSUBSCRIBE, LIST, LSUB, STATUS, and APPEND
+            new SelectCommandParser(statusResponseFactory),
+            new ExamineCommandParser(statusResponseFactory),
+            new CreateCommandParser(statusResponseFactory),
+            new DeleteCommandParser(statusResponseFactory),
+            new RenameCommandParser(statusResponseFactory),
+            new SubscribeCommandParser(statusResponseFactory),
+            new UnsubscribeCommandParser(statusResponseFactory),
+            new ListCommandParser(statusResponseFactory),
+            new XListCommandParser(statusResponseFactory),
+            new LsubCommandParser(statusResponseFactory),
+            new StatusCommandParser(statusResponseFactory),
+            new AppendCommandParser(statusResponseFactory),
+
+            // RFC2342 NAMESPACE
+            new NamespaceCommandParser(statusResponseFactory),
+
+            // RFC4314 GETACL, SETACL, DELETEACL, LISTRIGHTS, MYRIGHTS
+            new GetACLCommandParser(statusResponseFactory),
+            new SetACLCommandParser(statusResponseFactory),
+            new DeleteACLCommandParser(statusResponseFactory),
+            new ListRightsCommandParser(statusResponseFactory),
+            new MyRightsCommandParser(statusResponseFactory),
+
+            // Commands only valid in SELECTED state.
+            // CHECK, CLOSE, EXPUNGE, SEARCH, FETCH, STORE, COPY, UID and IDLE
+            new CheckCommandParser(statusResponseFactory),
+            new CloseCommandParser(statusResponseFactory),
+            new ExpungeCommandParser(statusResponseFactory),
+            new CopyCommandParser(statusResponseFactory),
+            new MoveCommandParser(statusResponseFactory),
+            new SearchCommandParser(statusResponseFactory),
+            new FetchCommandParser(statusResponseFactory),
+            new StoreCommandParser(statusResponseFactory),
+            new UidCommandParser(this, statusResponseFactory),
+            new IdleCommandParser(statusResponseFactory),
+            new StartTLSCommandParser(statusResponseFactory),
+
+            // RFC3691
+            new UnselectCommandParser(statusResponseFactory),
+
+            // RFC4978
+            new CompressCommandParser(statusResponseFactory),
+
+            new EnableCommandParser(statusResponseFactory),
+
+            // RFC2087
+            // GETQUOTAROOT, GETQUOTA, SETQUOTA
+            new GetQuotaRootCommandParser(statusResponseFactory),
+            new GetQuotaCommandParser(statusResponseFactory),
+            new SetQuotaCommandParser(statusResponseFactory),
+
+            //RFC5464
+            //SETMETADATA, GETMETADATA
+            new SetAnnotationCommandParser(statusResponseFactory),
+            new GetAnnotationCommandParser(statusResponseFactory));
+
+        imapCommands = parsers.collect(Guavate.toImmutableMap(
+                parser -> parser.getCommand().getName(),
+                Function.identity()));
     }
 
     @Override


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org