View Javadoc
1   /*
2    * Copyright 2016 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.smtp;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.ByteBufUtil;
20  import io.netty.util.AsciiString;
21  import io.netty.util.internal.ObjectUtil;
22  import io.netty.util.internal.UnstableApi;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  /**
28   * The command part of a {@link SmtpRequest}.
29   */
30  @UnstableApi
31  public final class SmtpCommand {
32      public static final SmtpCommand EHLO = new SmtpCommand(AsciiString.cached("EHLO"));
33      public static final SmtpCommand HELO = new SmtpCommand(AsciiString.cached("HELO"));
34      public static final SmtpCommand MAIL = new SmtpCommand(AsciiString.cached("MAIL"));
35      public static final SmtpCommand RCPT = new SmtpCommand(AsciiString.cached("RCPT"));
36      public static final SmtpCommand DATA = new SmtpCommand(AsciiString.cached("DATA"));
37      public static final SmtpCommand NOOP = new SmtpCommand(AsciiString.cached("NOOP"));
38      public static final SmtpCommand RSET = new SmtpCommand(AsciiString.cached("RSET"));
39      public static final SmtpCommand EXPN = new SmtpCommand(AsciiString.cached("EXPN"));
40      public static final SmtpCommand VRFY = new SmtpCommand(AsciiString.cached("VRFY"));
41      public static final SmtpCommand HELP = new SmtpCommand(AsciiString.cached("HELP"));
42      public static final SmtpCommand QUIT = new SmtpCommand(AsciiString.cached("QUIT"));
43  
44      private static final Map<String, SmtpCommand> COMMANDS = new HashMap<String, SmtpCommand>();
45      static {
46          COMMANDS.put(EHLO.name().toString(), EHLO);
47          COMMANDS.put(HELO.name().toString(), HELO);
48          COMMANDS.put(MAIL.name().toString(), MAIL);
49          COMMANDS.put(RCPT.name().toString(), RCPT);
50          COMMANDS.put(DATA.name().toString(), DATA);
51          COMMANDS.put(NOOP.name().toString(), NOOP);
52          COMMANDS.put(RSET.name().toString(), RSET);
53          COMMANDS.put(EXPN.name().toString(), EXPN);
54          COMMANDS.put(VRFY.name().toString(), VRFY);
55          COMMANDS.put(HELP.name().toString(), HELP);
56          COMMANDS.put(QUIT.name().toString(), QUIT);
57      }
58  
59      /**
60       * Returns the {@link SmtpCommand} for the given command name.
61       */
62      public static SmtpCommand valueOf(CharSequence commandName) {
63          ObjectUtil.checkNotNull(commandName, "commandName");
64          SmtpCommand command = COMMANDS.get(commandName.toString());
65          return command != null ? command : new SmtpCommand(AsciiString.of(commandName));
66      }
67  
68      private final AsciiString name;
69  
70      private SmtpCommand(AsciiString name) {
71          this.name = name;
72      }
73  
74      /**
75       * Return the command name.
76       */
77      public AsciiString name() {
78          return name;
79      }
80  
81      void encode(ByteBuf buffer) {
82          ByteBufUtil.writeAscii(buffer, name);
83      }
84  
85      boolean isContentExpected() {
86          return this.equals(DATA);
87      }
88  
89      @Override
90      public int hashCode() {
91          return name.hashCode();
92      }
93  
94      @Override
95      public boolean equals(Object obj) {
96          if (obj == this) {
97              return true;
98          }
99          if (!(obj instanceof SmtpCommand)) {
100             return false;
101         }
102         return name.contentEqualsIgnoreCase(((SmtpCommand) obj).name());
103     }
104 
105     @Override
106     public String toString() {
107         return "SmtpCommand{name=" + name + '}';
108     }
109 }