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