001: // Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)
002:
003: package org.xbill.DNS;
004:
005: import java.util.*;
006:
007: /**
008: * The Response from a query to Cache.lookupRecords() or Zone.findRecords()
009: * @see Cache
010: * @see Zone
011: *
012: * @author Brian Wellington
013: */
014:
015: public class SetResponse {
016:
017: /**
018: * The Cache contains no information about the requested name/type
019: */
020: static final int UNKNOWN = 0;
021:
022: /**
023: * The Zone does not contain the requested name, or the Cache has
024: * determined that the name does not exist.
025: */
026: static final int NXDOMAIN = 1;
027:
028: /**
029: * The Zone contains the name, but no data of the requested type,
030: * or the Cache has determined that the name exists and has no data
031: * of the requested type.
032: */
033: static final int NXRRSET = 2;
034:
035: /**
036: * A delegation enclosing the requested name was found.
037: */
038: static final int DELEGATION = 3;
039:
040: /**
041: * The Cache/Zone found a CNAME when looking for the name.
042: * @see CNAMERecord
043: */
044: static final int CNAME = 4;
045:
046: /**
047: * The Cache/Zone found a DNAME when looking for the name.
048: * @see DNAMERecord
049: */
050: static final int DNAME = 5;
051:
052: /**
053: * The Cache/Zone has successfully answered the question for the
054: * requested name/type/class.
055: */
056: static final int SUCCESSFUL = 6;
057:
058: private static final SetResponse unknown = new SetResponse(UNKNOWN);
059: private static final SetResponse nxdomain = new SetResponse(
060: NXDOMAIN);
061: private static final SetResponse nxrrset = new SetResponse(NXRRSET);
062:
063: private int type;
064: private Object data;
065:
066: private SetResponse() {
067: }
068:
069: SetResponse(int type, RRset rrset) {
070: if (type < 0 || type > 6)
071: throw new IllegalArgumentException("invalid type");
072: this .type = type;
073: this .data = rrset;
074: }
075:
076: SetResponse(int type) {
077: if (type < 0 || type > 6)
078: throw new IllegalArgumentException("invalid type");
079: this .type = type;
080: this .data = null;
081: }
082:
083: static SetResponse ofType(int type) {
084: switch (type) {
085: case UNKNOWN:
086: return unknown;
087: case NXDOMAIN:
088: return nxdomain;
089: case NXRRSET:
090: return nxrrset;
091: case DELEGATION:
092: case CNAME:
093: case DNAME:
094: case SUCCESSFUL:
095: SetResponse sr = new SetResponse();
096: sr.type = type;
097: sr.data = null;
098: return sr;
099: default:
100: throw new IllegalArgumentException("invalid type");
101: }
102: }
103:
104: void addRRset(RRset rrset) {
105: if (data == null)
106: data = new ArrayList();
107: List l = (List) data;
108: l.add(rrset);
109: }
110:
111: /** Is the answer to the query unknown? */
112: public boolean isUnknown() {
113: return (type == UNKNOWN);
114: }
115:
116: /** Is the answer to the query that the name does not exist? */
117: public boolean isNXDOMAIN() {
118: return (type == NXDOMAIN);
119: }
120:
121: /** Is the answer to the query that the name exists, but the type does not? */
122: public boolean isNXRRSET() {
123: return (type == NXRRSET);
124: }
125:
126: /** Is the result of the lookup that the name is below a delegation? */
127: public boolean isDelegation() {
128: return (type == DELEGATION);
129: }
130:
131: /** Is the result of the lookup a CNAME? */
132: public boolean isCNAME() {
133: return (type == CNAME);
134: }
135:
136: /** Is the result of the lookup a DNAME? */
137: public boolean isDNAME() {
138: return (type == DNAME);
139: }
140:
141: /** Was the query successful? */
142: public boolean isSuccessful() {
143: return (type == SUCCESSFUL);
144: }
145:
146: /** If the query was successful, return the answers */
147: public RRset[] answers() {
148: if (type != SUCCESSFUL)
149: return null;
150: List l = (List) data;
151: return (RRset[]) l.toArray(new RRset[l.size()]);
152: }
153:
154: /**
155: * If the query encountered a CNAME, return it.
156: */
157: public CNAMERecord getCNAME() {
158: return (CNAMERecord) ((RRset) data).first();
159: }
160:
161: /**
162: * If the query encountered a DNAME, return it.
163: */
164: public DNAMERecord getDNAME() {
165: return (DNAMERecord) ((RRset) data).first();
166: }
167:
168: /**
169: * If the query hit a delegation point, return the NS set.
170: */
171: public RRset getNS() {
172: return (RRset) data;
173: }
174:
175: /** Prints the value of the SetResponse */
176: public String toString() {
177: switch (type) {
178: case UNKNOWN:
179: return "unknown";
180: case NXDOMAIN:
181: return "NXDOMAIN";
182: case NXRRSET:
183: return "NXRRSET";
184: case DELEGATION:
185: return "delegation: " + data;
186: case CNAME:
187: return "CNAME: " + data;
188: case DNAME:
189: return "DNAME: " + data;
190: case SUCCESSFUL:
191: return "successful";
192: default:
193: return null;
194: }
195: }
196:
197: }
|