blob: 05d9f32699531f9dae1c759eb02bf29fcbded3ba [file] [log] [blame]
Shuyi Chend7955ce2013-05-22 14:51:55 -07001// Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)
2
3package org.xbill.DNS;
4
5import java.util.*;
6
7/**
8 * The Response from a query to Cache.lookupRecords() or Zone.findRecords()
9 * @see Cache
10 * @see Zone
11 *
12 * @author Brian Wellington
13 */
14
15public class SetResponse {
16
17/**
18 * The Cache contains no information about the requested name/type
19 */
20static final int UNKNOWN = 0;
21
22/**
23 * The Zone does not contain the requested name, or the Cache has
24 * determined that the name does not exist.
25 */
26static final int NXDOMAIN = 1;
27
28/**
29 * The Zone contains the name, but no data of the requested type,
30 * or the Cache has determined that the name exists and has no data
31 * of the requested type.
32 */
33static final int NXRRSET = 2;
34
35/**
36 * A delegation enclosing the requested name was found.
37 */
38static final int DELEGATION = 3;
39
40/**
41 * The Cache/Zone found a CNAME when looking for the name.
42 * @see CNAMERecord
43 */
44static final int CNAME = 4;
45
46/**
47 * The Cache/Zone found a DNAME when looking for the name.
48 * @see DNAMERecord
49 */
50static final int DNAME = 5;
51
52/**
53 * The Cache/Zone has successfully answered the question for the
54 * requested name/type/class.
55 */
56static final int SUCCESSFUL = 6;
57
58private static final SetResponse unknown = new SetResponse(UNKNOWN);
59private static final SetResponse nxdomain = new SetResponse(NXDOMAIN);
60private static final SetResponse nxrrset = new SetResponse(NXRRSET);
61
62private int type;
63private Object data;
64
65private
66SetResponse() {}
67
68SetResponse(int type, RRset rrset) {
69 if (type < 0 || type > 6)
70 throw new IllegalArgumentException("invalid type");
71 this.type = type;
72 this.data = rrset;
73}
74
75SetResponse(int type) {
76 if (type < 0 || type > 6)
77 throw new IllegalArgumentException("invalid type");
78 this.type = type;
79 this.data = null;
80}
81
82static SetResponse
83ofType(int type) {
84 switch (type) {
85 case UNKNOWN:
86 return unknown;
87 case NXDOMAIN:
88 return nxdomain;
89 case NXRRSET:
90 return nxrrset;
91 case DELEGATION:
92 case CNAME:
93 case DNAME:
94 case SUCCESSFUL:
95 SetResponse sr = new SetResponse();
96 sr.type = type;
97 sr.data = null;
98 return sr;
99 default:
100 throw new IllegalArgumentException("invalid type");
101 }
102}
103
104void
105addRRset(RRset rrset) {
106 if (data == null)
107 data = new ArrayList();
108 List l = (List) data;
109 l.add(rrset);
110}
111
112/** Is the answer to the query unknown? */
113public boolean
114isUnknown() {
115 return (type == UNKNOWN);
116}
117
118/** Is the answer to the query that the name does not exist? */
119public boolean
120isNXDOMAIN() {
121 return (type == NXDOMAIN);
122}
123
124/** Is the answer to the query that the name exists, but the type does not? */
125public boolean
126isNXRRSET() {
127 return (type == NXRRSET);
128}
129
130/** Is the result of the lookup that the name is below a delegation? */
131public boolean
132isDelegation() {
133 return (type == DELEGATION);
134}
135
136/** Is the result of the lookup a CNAME? */
137public boolean
138isCNAME() {
139 return (type == CNAME);
140}
141
142/** Is the result of the lookup a DNAME? */
143public boolean
144isDNAME() {
145 return (type == DNAME);
146}
147
148/** Was the query successful? */
149public boolean
150isSuccessful() {
151 return (type == SUCCESSFUL);
152}
153
154/** If the query was successful, return the answers */
155public RRset []
156answers() {
157 if (type != SUCCESSFUL)
158 return null;
159 List l = (List) data;
160 return (RRset []) l.toArray(new RRset[l.size()]);
161}
162
163/**
164 * If the query encountered a CNAME, return it.
165 */
166public CNAMERecord
167getCNAME() {
168 return (CNAMERecord)((RRset)data).first();
169}
170
171/**
172 * If the query encountered a DNAME, return it.
173 */
174public DNAMERecord
175getDNAME() {
176 return (DNAMERecord)((RRset)data).first();
177}
178
179/**
180 * If the query hit a delegation point, return the NS set.
181 */
182public RRset
183getNS() {
184 return (RRset)data;
185}
186
187/** Prints the value of the SetResponse */
188public String
189toString() {
190 switch (type) {
191 case UNKNOWN: return "unknown";
192 case NXDOMAIN: return "NXDOMAIN";
193 case NXRRSET: return "NXRRSET";
194 case DELEGATION: return "delegation: " + data;
195 case CNAME: return "CNAME: " + data;
196 case DNAME: return "DNAME: " + data;
197 case SUCCESSFUL: return "successful";
198 default: throw new IllegalStateException();
199 }
200}
201
202}