查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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    *   https://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 AUTH = new SmtpCommand(AsciiString.cached("AUTH"));
35      public static final SmtpCommand MAIL = new SmtpCommand(AsciiString.cached("MAIL"));
36      public static final SmtpCommand RCPT = new SmtpCommand(AsciiString.cached("RCPT"));
37      public static final SmtpCommand DATA = new SmtpCommand(AsciiString.cached("DATA"));
38      public static final SmtpCommand NOOP = new SmtpCommand(AsciiString.cached("NOOP"));
39      public static final SmtpCommand RSET = new SmtpCommand(AsciiString.cached("RSET"));
40      public static final SmtpCommand EXPN = new SmtpCommand(AsciiString.cached("EXPN"));
41      public static final SmtpCommand VRFY = new SmtpCommand(AsciiString.cached("VRFY"));
42      public static final SmtpCommand HELP = new SmtpCommand(AsciiString.cached("HELP"));
43      public static final SmtpCommand QUIT = new SmtpCommand(AsciiString.cached("QUIT"));
44      public static final SmtpCommand EMPTY = new SmtpCommand(AsciiString.cached(""));
45  
46      private static final Map<String, SmtpCommand> COMMANDS = new HashMap<String, SmtpCommand>();
47      static {
48          COMMANDS.put(EHLO.name().toString(), EHLO);
49          COMMANDS.put(HELO.name().toString(), HELO);
50          COMMANDS.put(AUTH.name().toString(), AUTH);
51          COMMANDS.put(MAIL.name().toString(), MAIL);
52          COMMANDS.put(RCPT.name().toString(), RCPT);
53          COMMANDS.put(DATA.name().toString(), DATA);
54          COMMANDS.put(NOOP.name().toString(), NOOP);
55          COMMANDS.put(RSET.name().toString(), RSET);
56          COMMANDS.put(EXPN.name().toString(), EXPN);
57          COMMANDS.put(VRFY.name().toString(), VRFY);
58          COMMANDS.put(HELP.name().toString(), HELP);
59          COMMANDS.put(QUIT.name().toString(), QUIT);
60          COMMANDS.put(EMPTY.name().toString(), EMPTY);
61      }
62  
63      /**
64       * Returns the {@link SmtpCommand} for the given command name.
65       */
66      public static SmtpCommand valueOf(CharSequence commandName) {
67          ObjectUtil.checkNotNull(commandName, "commandName");
68          SmtpCommand command = COMMANDS.get(commandName.toString());
69          return command != null ? command : new SmtpCommand(AsciiString.of(commandName));
70      }
71  
72      private final AsciiString name;
73  
74      private SmtpCommand(AsciiString name) {
75          this.name = name;
76      }
77  
78      /**
79       * Return the command name.
80       */
81      public AsciiString name() {
82          return name;
83      }
84  
85      void encode(ByteBuf buffer) {
86          ByteBufUtil.writeAscii(buffer, name);
87      }
88  
89      boolean isContentExpected() {
90          return this.equals(DATA);
91      }
92  
93      @Override
94      public int hashCode() {
95          return name.hashCode();
96      }
97  
98      @Override
99      public boolean equals(Object obj) {
100         if (obj == this) {
101             return true;
102         }
103         if (!(obj instanceof SmtpCommand)) {
104             return false;
105         }
106         return name.contentEqualsIgnoreCase(((SmtpCommand) obj).name());
107     }
108 
109     @Override
110     public String toString() {
111         return "SmtpCommand{name=" + name + '}';
112     }
113 }