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