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.buffer.Unpooled;
21  import io.netty.channel.ChannelHandlerContext;
22  import io.netty.handler.codec.MessageToMessageEncoder;
23  import io.netty.util.internal.UnstableApi;
24  
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.RandomAccess;
28  
29  /**
30   * Encoder for SMTP requests.
31   */
32  @UnstableApi
33  public final class SmtpRequestEncoder extends MessageToMessageEncoder<Object> {
34      private static final int CRLF_SHORT = ('\r' << 8) | '\n';
35      private static final byte SP = ' ';
36      private static final ByteBuf DOT_CRLF_BUFFER = Unpooled.unreleasableBuffer(
37              Unpooled.directBuffer(3).writeByte('.').writeByte('\r').writeByte('\n'));
38  
39      private boolean contentExpected;
40  
41      @Override
42      public boolean acceptOutboundMessage(Object msg) throws Exception {
43          return msg instanceof SmtpRequest || msg instanceof SmtpContent;
44      }
45  
46      @Override
47      protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
48          if (msg instanceof SmtpRequest) {
49              final SmtpRequest req = (SmtpRequest) msg;
50              if (contentExpected) {
51                  if (req.command().equals(SmtpCommand.RSET)) {
52                      contentExpected = false;
53                  } else {
54                      throw new IllegalStateException("SmtpContent expected");
55                  }
56              }
57              boolean release = true;
58              final ByteBuf buffer = ctx.alloc().buffer();
59              try {
60                  req.command().encode(buffer);
61                  writeParameters(req.parameters(), buffer);
62                  ByteBufUtil.writeShortBE(buffer, CRLF_SHORT);
63                  out.add(buffer);
64                  release = false;
65                  if (req.command().isContentExpected()) {
66                      contentExpected = true;
67                  }
68              } finally {
69                  if (release) {
70                      buffer.release();
71                  }
72              }
73          }
74  
75          if (msg instanceof SmtpContent) {
76              if (!contentExpected) {
77                  throw new IllegalStateException("No SmtpContent expected");
78              }
79              final ByteBuf content = ((SmtpContent) msg).content();
80              out.add(content.retain());
81              if (msg instanceof LastSmtpContent) {
82                  out.add(DOT_CRLF_BUFFER.retainedDuplicate());
83                  contentExpected = false;
84              }
85          }
86      }
87  
88      private static void writeParameters(List<CharSequence> parameters, ByteBuf out) {
89          if (parameters.isEmpty()) {
90              return;
91          }
92          out.writeByte(SP);
93          if (parameters instanceof RandomAccess) {
94              final int sizeMinusOne = parameters.size() - 1;
95              for (int i = 0; i < sizeMinusOne; i++) {
96                  ByteBufUtil.writeAscii(out, parameters.get(i));
97                  out.writeByte(SP);
98              }
99              ByteBufUtil.writeAscii(out, parameters.get(sizeMinusOne));
100         } else {
101             final Iterator<CharSequence> params = parameters.iterator();
102             for (;;) {
103                 ByteBufUtil.writeAscii(out, params.next());
104                 if (params.hasNext()) {
105                     out.writeByte(SP);
106                 } else {
107                     break;
108                 }
109             }
110         }
111     }
112 }