• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)
2 
3 package org.xbill.DNS;
4 
5 import 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 
15 public class SetResponse {
16 
17 /**
18  * The Cache contains no information about the requested name/type
19  */
20 static 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  */
26 static 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  */
33 static final int NXRRSET	= 2;
34 
35 /**
36  * A delegation enclosing the requested name was found.
37  */
38 static final int DELEGATION	= 3;
39 
40 /**
41  * The Cache/Zone found a CNAME when looking for the name.
42  * @see CNAMERecord
43  */
44 static final int CNAME		= 4;
45 
46 /**
47  * The Cache/Zone found a DNAME when looking for the name.
48  * @see DNAMERecord
49  */
50 static final int DNAME		= 5;
51 
52 /**
53  * The Cache/Zone has successfully answered the question for the
54  * requested name/type/class.
55  */
56 static final int SUCCESSFUL	= 6;
57 
58 private static final SetResponse unknown = new SetResponse(UNKNOWN);
59 private static final SetResponse nxdomain = new SetResponse(NXDOMAIN);
60 private static final SetResponse nxrrset = new SetResponse(NXRRSET);
61 
62 private int type;
63 private Object data;
64 
65 private
SetResponse()66 SetResponse() {}
67 
SetResponse(int type, RRset rrset)68 SetResponse(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 
SetResponse(int type)75 SetResponse(int type) {
76 	if (type < 0 || type > 6)
77 		throw new IllegalArgumentException("invalid type");
78 	this.type = type;
79 	this.data = null;
80 }
81 
82 static SetResponse
ofType(int type)83 ofType(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 
104 void
addRRset(RRset rrset)105 addRRset(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? */
113 public boolean
isUnknown()114 isUnknown() {
115 	return (type == UNKNOWN);
116 }
117 
118 /** Is the answer to the query that the name does not exist? */
119 public boolean
isNXDOMAIN()120 isNXDOMAIN() {
121 	return (type == NXDOMAIN);
122 }
123 
124 /** Is the answer to the query that the name exists, but the type does not? */
125 public boolean
isNXRRSET()126 isNXRRSET() {
127 	return (type == NXRRSET);
128 }
129 
130 /** Is the result of the lookup that the name is below a delegation? */
131 public boolean
isDelegation()132 isDelegation() {
133 	return (type == DELEGATION);
134 }
135 
136 /** Is the result of the lookup a CNAME? */
137 public boolean
isCNAME()138 isCNAME() {
139 	return (type == CNAME);
140 }
141 
142 /** Is the result of the lookup a DNAME? */
143 public boolean
isDNAME()144 isDNAME() {
145 	return (type == DNAME);
146 }
147 
148 /** Was the query successful? */
149 public boolean
isSuccessful()150 isSuccessful() {
151 	return (type == SUCCESSFUL);
152 }
153 
154 /** If the query was successful, return the answers */
155 public RRset []
answers()156 answers() {
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  */
166 public CNAMERecord
getCNAME()167 getCNAME() {
168 	return (CNAMERecord)((RRset)data).first();
169 }
170 
171 /**
172  * If the query encountered a DNAME, return it.
173  */
174 public DNAMERecord
getDNAME()175 getDNAME() {
176 	return (DNAMERecord)((RRset)data).first();
177 }
178 
179 /**
180  * If the query hit a delegation point, return the NS set.
181  */
182 public RRset
getNS()183 getNS() {
184 	return (RRset)data;
185 }
186 
187 /** Prints the value of the SetResponse */
188 public String
toString()189 toString() {
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 }
203