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    *   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 static io.netty.util.internal.ObjectUtil.checkNotNull;
19  
20  import io.netty.util.internal.StringUtil;
21  import io.netty.util.internal.UnstableApi;
22  
23  @UnstableApi
24  public class DefaultDnsPtrRecord extends AbstractDnsRecord implements DnsPtrRecord {
25  
26      private final String hostname;
27  
28      /**
29       * Creates a new PTR record.
30       *
31       * @param name the domain name
32       * @param dnsClass the class of the record, usually one of the following:
33       *                 <ul>
34       *                     <li>{@link #CLASS_IN}</li>
35       *                     <li>{@link #CLASS_CSNET}</li>
36       *                     <li>{@link #CLASS_CHAOS}</li>
37       *                     <li>{@link #CLASS_HESIOD}</li>
38       *                     <li>{@link #CLASS_NONE}</li>
39       *                     <li>{@link #CLASS_ANY}</li>
40       *                 </ul>
41       * @param timeToLive the TTL value of the record
42       * @param hostname the hostname this PTR record resolves to.
43       */
44      public DefaultDnsPtrRecord(
45              String name, int dnsClass, long timeToLive, String hostname) {
46          super(name, DnsRecordType.PTR, dnsClass, timeToLive);
47          this.hostname = checkNotNull(hostname, "hostname");
48      }
49  
50      @Override
51      public String hostname() {
52          return hostname;
53      }
54  
55      @Override
56      public String toString() {
57          final StringBuilder buf = new StringBuilder(64).append(StringUtil.simpleClassName(this)).append('(');
58          final DnsRecordType type = type();
59          buf.append(name().isEmpty()? "<root>" : name())
60             .append(' ')
61             .append(timeToLive())
62             .append(' ');
63  
64          DnsMessageUtil.appendRecordClass(buf, dnsClass())
65                        .append(' ')
66                        .append(type.name());
67  
68          buf.append(' ')
69             .append(hostname);
70  
71          return buf.toString();
72      }
73  }