remove some warnings by using java 5 features
[captive-validator.git] / src / com / versign / tat / dnssec / SRRset.java
1 /*
2  * Copyright (c) 2009 VeriSign. All rights reserved.
3  * 
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * 
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer. 2. Redistributions in
9  * binary form must reproduce the above copyright notice, this list of
10  * conditions and the following disclaimer in the documentation and/or other
11  * materials provided with the distribution. 3. The name of the author may not
12  * be used to endorse or promote products derived from this software without
13  * specific prior written permission.
14  * 
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
18  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
20  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *  
26  */
27
28 package com.versign.tat.dnssec;
29
30 import java.util.*;
31
32 import org.xbill.DNS.*;
33
34 /**
35  * A version of the RRset class overrides the standard security status.
36  */
37 public class SRRset extends RRset {
38     private SecurityStatus mSecurityStatus;
39
40     /** Create a new, blank SRRset. */
41     public SRRset() {
42         super();
43         mSecurityStatus = new SecurityStatus();
44     }
45
46     
47     /**
48      * Create a new SRRset from an existing RRset. This SRRset will contain that
49      * same internal Record objects as the original RRset.
50      */
51     @SuppressWarnings("unchecked")
52     // org.xbill.DNS.RRset isn't typesafe-aware.
53     public SRRset(RRset r) {
54         this();
55
56         for (Iterator i = r.rrs(); i.hasNext();) {
57             addRR((Record) i.next());
58         }
59
60         for (Iterator i = r.sigs(); i.hasNext();) {
61             addRR((Record) i.next());
62         }
63     }
64
65     /**
66      * Return the current security status (generally: UNCHECKED, BOGUS, or
67      * SECURE).
68      */
69     public int getSecurity() {
70         return getSecurityStatus();
71     }
72
73     /**
74      * Return the current security status (generally: UNCHECKED, BOGUS, or
75      * SECURE).
76      */
77     public byte getSecurityStatus() {
78         return mSecurityStatus.getStatus();
79     }
80
81     /**
82      * Set the current security status for this SRRset. This status will be
83      * shared amongst all copies of this SRRset (created with cloneSRRset())
84      */
85     public void setSecurityStatus(byte status) {
86         mSecurityStatus.setStatus(status);
87     }
88
89     public Iterator<Record> rrs() {
90         return (Iterator<Record>) rrs();
91     }
92     
93     public Iterator<RRSIGRecord> sigs() {
94         return (Iterator<RRSIGRecord>) sigs();
95     }
96     
97     public int totalSize() {
98         int num_sigs = 0;
99         for (Iterator<RRSIGRecord> i = sigs(); i.hasNext();) {
100             num_sigs++;
101         }
102         return size() + num_sigs;
103     }
104
105     /**
106      * @return The total number of records (data + sigs) in the SRRset.
107      */
108     public int getNumRecords() {
109         return totalSize();
110     }
111
112     public RRSIGRecord firstSig() {
113         for (Iterator<RRSIGRecord> i = sigs(); i.hasNext();) {
114             return i.next();
115         }
116         return null;
117     }
118
119     /**
120      * @return true if this RRset has RRSIG records that cover data records.
121      *         (i.e., RRSIG SRRsets return false)
122      */
123     public boolean isSigned() {
124         if (getType() == Type.RRSIG) return false;
125         return firstSig() != null;
126     }
127
128     /**
129      * @return The "signer" name for this SRRset, if signed, or null if not.
130      */
131     public Name getSignerName() {
132         RRSIGRecord sig = (RRSIGRecord) firstSig();
133         if (sig == null) return null;
134         return sig.getSigner();
135     }
136 }