View Javadoc
1   /*
2    * Copyright 2015 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.dns;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.channel.AddressedEnvelope;
20  import io.netty.channel.ChannelHandler;
21  import io.netty.channel.ChannelHandlerContext;
22  import io.netty.channel.socket.DatagramPacket;
23  import io.netty.handler.codec.MessageToMessageEncoder;
24  import io.netty.util.internal.UnstableApi;
25  
26  import java.net.InetSocketAddress;
27  import java.util.List;
28  
29  /**
30   * Encodes a {@link DatagramDnsQuery} (or an {@link AddressedEnvelope} of {@link DnsQuery}} into a
31   * {@link DatagramPacket}.
32   */
33  @UnstableApi
34  @ChannelHandler.Sharable
35  public class DatagramDnsQueryEncoder extends MessageToMessageEncoder<AddressedEnvelope<DnsQuery, InetSocketAddress>> {
36  
37      private final DnsQueryEncoder encoder;
38  
39      /**
40       * Creates a new encoder with {@linkplain DnsRecordEncoder#DEFAULT the default record encoder}.
41       */
42      public DatagramDnsQueryEncoder() {
43          this(DnsRecordEncoder.DEFAULT);
44      }
45  
46      /**
47       * Creates a new encoder with the specified {@code recordEncoder}.
48       */
49      public DatagramDnsQueryEncoder(DnsRecordEncoder recordEncoder) {
50          this.encoder = new DnsQueryEncoder(recordEncoder);
51      }
52  
53      @Override
54      protected void encode(
55          ChannelHandlerContext ctx,
56          AddressedEnvelope<DnsQuery, InetSocketAddress> in, List<Object> out) throws Exception {
57  
58          final InetSocketAddress recipient = in.recipient();
59          final DnsQuery query = in.content();
60          final ByteBuf buf = allocateBuffer(ctx, in);
61  
62          boolean success = false;
63          try {
64              encoder.encode(query, buf);
65              success = true;
66          } finally {
67              if (!success) {
68                  buf.release();
69              }
70          }
71  
72          out.add(new DatagramPacket(buf, recipient, null));
73      }
74  
75      /**
76       * Allocate a {@link ByteBuf} which will be used for constructing a datagram packet.
77       * Sub-classes may override this method to return a {@link ByteBuf} with a perfect matching initial capacity.
78       */
79      protected ByteBuf allocateBuffer(
80          ChannelHandlerContext ctx,
81          @SuppressWarnings("unused") AddressedEnvelope<DnsQuery, InetSocketAddress> msg) throws Exception {
82          return ctx.alloc().ioBuffer(1024);
83      }
84  }