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