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  import static java.util.Objects.requireNonNull;
29  
30  /**
31   * Encodes a {@link DatagramDnsResponse} (or an {@link AddressedEnvelope} of {@link DnsResponse}} into a
32   * {@link DatagramPacket}.
33   */
34  @UnstableApi
35  public class DatagramDnsResponseEncoder
36      extends MessageToMessageEncoder<AddressedEnvelope<DnsResponse, InetSocketAddress>> {
37  
38      private final DnsRecordEncoder recordEncoder;
39  
40      /**
41       * Creates a new encoder with {@linkplain DnsRecordEncoder#DEFAULT the default record encoder}.
42       */
43      public DatagramDnsResponseEncoder() {
44          this(DnsRecordEncoder.DEFAULT);
45      }
46  
47      /**
48       * Creates a new encoder with the specified {@code recordEncoder}.
49       */
50      public DatagramDnsResponseEncoder(DnsRecordEncoder recordEncoder) {
51          this.recordEncoder = requireNonNull(recordEncoder, "recordEncoder");
52      }
53  
54      @Override
55      public boolean isSharable() {
56          return true;
57      }
58  
59      @Override
60      protected void encode(ChannelHandlerContext ctx,
61                            AddressedEnvelope<DnsResponse, InetSocketAddress> in, List<Object> out) throws Exception {
62  
63          final InetSocketAddress recipient = in.recipient();
64          final DnsResponse response = in.content();
65          final Buffer buf = allocateBuffer(ctx, in);
66  
67          DnsMessageUtil.encodeDnsResponse(recordEncoder, response, buf);
68  
69          out.add(new DatagramPacket(buf, recipient, null));
70      }
71  
72      /**
73       * Allocate a {@link Buffer} which will be used for constructing a datagram packet.
74       * Sub-classes may override this method to return a {@link Buffer} with a perfect matching initial capacity.
75       */
76      protected Buffer allocateBuffer(
77          ChannelHandlerContext ctx,
78          @SuppressWarnings("unused") AddressedEnvelope<DnsResponse, InetSocketAddress> msg) throws Exception {
79          return ctx.bufferAllocator().allocate(1024);
80      }
81  }