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.util.Send;
20  import io.netty5.util.internal.StringUtil;
21  import io.netty5.util.internal.UnstableApi;
22  
23  import static java.util.Objects.requireNonNull;
24  
25  /**
26   * The default {@code DnsRawRecord} implementation.
27   */
28  @UnstableApi
29  public class DefaultDnsRawRecord extends AbstractDnsRecord implements DnsRawRecord {
30  
31      private final Buffer content;
32  
33      /**
34       * Creates a new {@link #CLASS_IN IN-class} record.
35       *
36       * @param name the domain name
37       * @param type the type of the record
38       * @param timeToLive the TTL value of the record
39       */
40      public DefaultDnsRawRecord(String name, DnsRecordType type, long timeToLive, Buffer content) {
41          this(name, type, DnsRecord.CLASS_IN, timeToLive, content);
42      }
43  
44      /**
45       * Creates a new record.
46       *
47       * @param name the domain name
48       * @param type the type of the record
49       * @param dnsClass the class of the record, usually one of the following:
50       *                 <ul>
51       *                     <li>{@link #CLASS_IN}</li>
52       *                     <li>{@link #CLASS_CSNET}</li>
53       *                     <li>{@link #CLASS_CHAOS}</li>
54       *                     <li>{@link #CLASS_HESIOD}</li>
55       *                     <li>{@link #CLASS_NONE}</li>
56       *                     <li>{@link #CLASS_ANY}</li>
57       *                 </ul>
58       * @param timeToLive the TTL value of the record
59       */
60      public DefaultDnsRawRecord(
61              String name, DnsRecordType type, int dnsClass, long timeToLive, Buffer content) {
62          super(name, type, dnsClass, timeToLive);
63          this.content = requireNonNull(content, "content").makeReadOnly();
64      }
65  
66      public DefaultDnsRawRecord(
67              String name, DnsRecordType type, int dnsClass, long timeToLive, Send<Buffer> content) {
68          super(name, type, dnsClass, timeToLive);
69          this.content = requireNonNull(content, "content").receive().makeReadOnly();
70      }
71  
72      @Override
73      public Buffer content() {
74          return content;
75      }
76  
77      public DnsRawRecord replace(Buffer content) {
78          return new DefaultDnsRawRecord(name(), type(), dnsClass(), timeToLive(), content);
79      }
80  
81      @Override
82      public Send<DnsRawRecord> send() {
83          return content.send().map(DnsRawRecord.class, this::replace);
84      }
85  
86      @Override
87      public void close() {
88          content.close();
89      }
90  
91      @Override
92      public boolean isAccessible() {
93          return content.isAccessible();
94      }
95  
96      @Override
97      public DnsRawRecord touch(Object hint) {
98          content.touch(hint);
99          return this;
100     }
101 
102     @Override
103     public String toString() {
104         final StringBuilder buf = new StringBuilder(64).append(StringUtil.simpleClassName(this)).append('(');
105         final DnsRecordType type = type();
106         if (type != DnsRecordType.OPT) {
107             buf.append(name().isEmpty()? "<root>" : name())
108                .append(' ')
109                .append(timeToLive())
110                .append(' ');
111 
112             DnsMessageUtil.appendRecordClass(buf, dnsClass())
113                           .append(' ')
114                           .append(type.name());
115         } else {
116             buf.append("OPT flags:")
117                .append(timeToLive())
118                .append(" udp:")
119                .append(dnsClass());
120         }
121 
122         buf.append(' ')
123            .append(content().readableBytes())
124            .append("B)");
125 
126         return buf.toString();
127     }
128 
129     @Override
130     public DnsRawRecord copy() {
131         // We're required to copy here, because we need independent life-time for 'content'.
132         return new DefaultDnsRawRecord(name(), type(), dnsClass(), timeToLive(), content.copy(true));
133     }
134 }