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.util.internal.UnstableApi;
19  
20  import static java.util.Objects.requireNonNull;
21  
22  /**
23   * The default {@link DnsResponse} implementation.
24   */
25  @UnstableApi
26  public class DefaultDnsResponse extends AbstractDnsMessage implements DnsResponse {
27  
28      private boolean authoritativeAnswer;
29      private boolean truncated;
30      private boolean recursionAvailable;
31      private DnsResponseCode code;
32  
33      /**
34       * Creates a new instance with the {@link DnsOpCode#QUERY} {@code opCode} and
35       * the {@link DnsResponseCode#NOERROR} {@code RCODE}.
36       *
37       * @param id the {@code ID} of the DNS response
38       */
39      public DefaultDnsResponse(int id) {
40          this(id, DnsOpCode.QUERY, DnsResponseCode.NOERROR);
41      }
42  
43      /**
44       * Creates a new instance with the {@link DnsResponseCode#NOERROR} {@code RCODE}.
45       *
46       * @param id the {@code ID} of the DNS response
47       * @param opCode the {@code opCode} of the DNS response
48       */
49      public DefaultDnsResponse(int id, DnsOpCode opCode) {
50          this(id, opCode, DnsResponseCode.NOERROR);
51      }
52  
53      /**
54       * Creates a new instance.
55       *
56       * @param id the {@code ID} of the DNS response
57       * @param opCode the {@code opCode} of the DNS response
58       * @param code the {@code RCODE} of the DNS response
59       */
60      public DefaultDnsResponse(int id, DnsOpCode opCode, DnsResponseCode code) {
61          super(id, opCode);
62          setCode(code);
63      }
64  
65      @Override
66      public boolean isAuthoritativeAnswer() {
67          return authoritativeAnswer;
68      }
69  
70      @Override
71      public DnsResponse setAuthoritativeAnswer(boolean authoritativeAnswer) {
72          this.authoritativeAnswer = authoritativeAnswer;
73          return this;
74      }
75  
76      @Override
77      public boolean isTruncated() {
78          return truncated;
79      }
80  
81      @Override
82      public DnsResponse setTruncated(boolean truncated) {
83          this.truncated = truncated;
84          return this;
85      }
86  
87      @Override
88      public boolean isRecursionAvailable() {
89          return recursionAvailable;
90      }
91  
92      @Override
93      public DnsResponse setRecursionAvailable(boolean recursionAvailable) {
94          this.recursionAvailable = recursionAvailable;
95          return this;
96      }
97  
98      @Override
99      public DnsResponseCode code() {
100         return code;
101     }
102 
103     @Override
104     public DnsResponse setCode(DnsResponseCode code) {
105         this.code = requireNonNull(code, "code");
106         return this;
107     }
108 
109     @Override
110     public DnsResponse setId(int id) {
111         return (DnsResponse) super.setId(id);
112     }
113 
114     @Override
115     public DnsResponse setOpCode(DnsOpCode opCode) {
116         return (DnsResponse) super.setOpCode(opCode);
117     }
118 
119     @Override
120     public DnsResponse setRecursionDesired(boolean recursionDesired) {
121         return (DnsResponse) super.setRecursionDesired(recursionDesired);
122     }
123 
124     @Override
125     public DnsResponse setZ(int z) {
126         return (DnsResponse) super.setZ(z);
127     }
128 
129     @Override
130     public DnsResponse setRecord(DnsSection section, DnsRecord record) {
131         return (DnsResponse) super.setRecord(section, record);
132     }
133 
134     @Override
135     public DnsResponse addRecord(DnsSection section, DnsRecord record) {
136         return (DnsResponse) super.addRecord(section, record);
137     }
138 
139     @Override
140     public DnsResponse addRecord(DnsSection section, int index, DnsRecord record) {
141         return (DnsResponse) super.addRecord(section, index, record);
142     }
143 
144     @Override
145     public DnsResponse clear(DnsSection section) {
146         return (DnsResponse) super.clear(section);
147     }
148 
149     @Override
150     public DnsResponse clear() {
151         return (DnsResponse) super.clear();
152     }
153 
154     @Override
155     public DnsResponse touch() {
156         return (DnsResponse) super.touch();
157     }
158 
159     @Override
160     public DnsResponse touch(Object hint) {
161         return (DnsResponse) super.touch(hint);
162     }
163 
164     @Override
165     public DnsResponse retain() {
166         return (DnsResponse) super.retain();
167     }
168 
169     @Override
170     public DnsResponse retain(int increment) {
171         return (DnsResponse) super.retain(increment);
172     }
173 
174     @Override
175     public String toString() {
176         return DnsMessageUtil.appendResponse(new StringBuilder(128), this).toString();
177     }
178 }