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.util.AsciiString;
19  import io.netty.util.internal.ObjectUtil;
20  import io.netty.util.internal.UnstableApi;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  /**
26   * Provides utility methods to create {@link SmtpRequest}s.
27   */
28  @UnstableApi
29  public final class SmtpRequests {
30  
31      private static final SmtpRequest DATA = new DefaultSmtpRequest(SmtpCommand.DATA);
32      private static final SmtpRequest NOOP = new DefaultSmtpRequest(SmtpCommand.NOOP);
33      private static final SmtpRequest RSET = new DefaultSmtpRequest(SmtpCommand.RSET);
34      private static final SmtpRequest HELP_NO_ARG = new DefaultSmtpRequest(SmtpCommand.HELP);
35      private static final SmtpRequest QUIT = new DefaultSmtpRequest(SmtpCommand.QUIT);
36      private static final AsciiString FROM_NULL_SENDER = AsciiString.cached("FROM:<>");
37  
38      /**
39       * Creates a {@code HELO} request.
40       */
41      public static SmtpRequest helo(CharSequence hostname) {
42          return new DefaultSmtpRequest(SmtpCommand.HELO, hostname);
43      }
44  
45      /**
46       * Creates a {@code EHLO} request.
47       */
48      public static SmtpRequest ehlo(CharSequence hostname) {
49          return new DefaultSmtpRequest(SmtpCommand.EHLO, hostname);
50      }
51  
52      /**
53       * Creates a {@code NOOP} request.
54       */
55      public static SmtpRequest noop() {
56          return NOOP;
57      }
58  
59      /**
60       * Creates a {@code DATA} request.
61       */
62      public static SmtpRequest data() {
63          return DATA;
64      }
65  
66      /**
67       * Creates a {@code RSET} request.
68       */
69      public static SmtpRequest rset() {
70          return RSET;
71      }
72  
73      /**
74       * Creates a {@code HELP} request.
75       */
76      public static SmtpRequest help(String cmd) {
77          return cmd == null ? HELP_NO_ARG : new DefaultSmtpRequest(SmtpCommand.HELP, cmd);
78      }
79  
80      /**
81       * Creates a {@code QUIT} request.
82       */
83      public static SmtpRequest quit() {
84          return QUIT;
85      }
86  
87      /**
88       * Creates a {@code MAIL} request.
89       */
90      public static SmtpRequest mail(CharSequence sender, CharSequence... mailParameters) {
91          if (mailParameters == null || mailParameters.length == 0) {
92              return new DefaultSmtpRequest(SmtpCommand.MAIL,
93                                            sender != null ? "FROM:<" + sender + '>' : FROM_NULL_SENDER);
94          } else {
95              List<CharSequence> params = new ArrayList<CharSequence>(mailParameters.length + 1);
96              params.add(sender != null? "FROM:<" + sender + '>' : FROM_NULL_SENDER);
97              for (CharSequence param : mailParameters) {
98                  params.add(param);
99              }
100             return new DefaultSmtpRequest(SmtpCommand.MAIL, params);
101         }
102     }
103 
104     /**
105      * Creates a {@code RCPT} request.
106      */
107     public static SmtpRequest rcpt(CharSequence recipient, CharSequence... rcptParameters) {
108         ObjectUtil.checkNotNull(recipient, "recipient");
109         if (rcptParameters == null || rcptParameters.length == 0) {
110             return new DefaultSmtpRequest(SmtpCommand.RCPT, "TO:<" + recipient + '>');
111         } else {
112             List<CharSequence> params = new ArrayList<CharSequence>(rcptParameters.length + 1);
113             params.add("TO:<" + recipient + '>');
114             for (CharSequence param : rcptParameters) {
115                 params.add(param);
116             }
117             return new DefaultSmtpRequest(SmtpCommand.RCPT, params);
118         }
119     }
120 
121     /**
122      * Creates a {@code EXPN} request.
123      */
124     public static SmtpRequest expn(CharSequence mailingList) {
125         return new DefaultSmtpRequest(SmtpCommand.EXPN, ObjectUtil.checkNotNull(mailingList, "mailingList"));
126     }
127 
128     /**
129      * Creates a {@code VRFY} request.
130      */
131     public static SmtpRequest vrfy(CharSequence user) {
132         return new DefaultSmtpRequest(SmtpCommand.VRFY, ObjectUtil.checkNotNull(user, "user"));
133     }
134 
135     private SmtpRequests() { }
136 }