fix warnings and findbugs hints
[captive-validator.git] / src / com / verisign / tat / dnssec / SRRset.java
1 /***************************** -*- Java -*- ********************************\
2  *                                                                         *
3  *   Copyright (c) 2009 VeriSign, Inc. All rights reserved.                *
4  *                                                                         *
5  * This software is provided solely in connection with the terms of the    *
6  * license agreement.  Any other use without the prior express written     *
7  * permission of VeriSign is completely prohibited.  The software and      *
8  * documentation are "Commercial Items", as that term is defined in 48     *
9  * C.F.R.  section 2.101, consisting of "Commercial Computer Software" and *
10  * "Commercial Computer Software Documentation" as such terms are defined  *
11  * in 48 C.F.R. section 252.227-7014(a)(5) and 48 C.F.R. section           *
12  * 252.227-7014(a)(1), and used in 48 C.F.R. section 12.212 and 48 C.F.R.  *
13  * section 227.7202, as applicable.  Pursuant to the above and other       *
14  * relevant sections of the Code of Federal Regulations, as applicable,    *
15  * VeriSign's publications, commercial computer software, and commercial   *
16  * computer software documentation are distributed and licensed to United  *
17  * States Government end users with only those rights as granted to all    *
18  * other end users, according to the terms and conditions contained in the *
19  * license agreement(s) that accompany the products and software           *
20  * documentation.                                                          *
21  *                                                                         *
22 \***************************************************************************/
23
24 package com.verisign.tat.dnssec;
25
26 import org.xbill.DNS.*;
27
28 import java.util.*;
29
30 /**
31  * A version of the RRset class overrides the standard security status.
32  */
33 public class SRRset extends RRset {
34     private static final long serialVersionUID = 1L;
35     private SecurityStatus mSecurityStatus;
36
37     /** Create a new, blank SRRset. */
38     public SRRset() {
39         super();
40         mSecurityStatus = new SecurityStatus();
41     }
42
43     /**
44      * Create a new SRRset from an existing RRset. This SRRset will contain that
45      * same internal Record objects as the original RRset.
46      */
47     @SuppressWarnings("rawtypes")
48     public SRRset(RRset r) {
49         this();
50
51         for (Iterator i = r.rrs(); i.hasNext();) {
52             addRR((Record) i.next());
53         }
54
55         for (Iterator i = r.sigs(); i.hasNext();) {
56             addRR((Record) i.next());
57         }
58     }
59
60     /**
61      * Return the current security status (generally: UNCHECKED, BOGUS, or
62      * SECURE).
63      */
64     public int getSecurity() {
65         return getSecurityStatus();
66     }
67
68     /**
69      * Return the current security status (generally: UNCHECKED, BOGUS, or
70      * SECURE).
71      */
72     public byte getSecurityStatus() {
73         return mSecurityStatus.getStatus();
74     }
75
76     /**
77      * Set the current security status for this SRRset. This status will be
78      * shared amongst all copies of this SRRset (created with cloneSRRset())
79      */
80     public void setSecurityStatus(byte status) {
81         mSecurityStatus.setStatus(status);
82     }
83
84     @SuppressWarnings("unchecked")
85     public Iterator<Record> rrs() {
86         return (Iterator<Record>) super.rrs();
87     }
88
89     @SuppressWarnings("unchecked")
90     public Iterator<RRSIGRecord> sigs() {
91         return (Iterator<RRSIGRecord>) super.sigs();
92     }
93
94     public int totalSize() {
95         int num_sigs = 0;
96
97         for (Iterator<RRSIGRecord> i = sigs(); i.hasNext();) {
98             num_sigs++;
99             i.next();
100         }
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
117         return null;
118     }
119
120     /**
121      * @return true if this RRset has RRSIG records that cover data records.
122      *         (i.e., RRSIG SRRsets return false)
123      */
124     public boolean isSigned() {
125         if (getType() == Type.RRSIG) {
126             return false;
127         }
128
129         return firstSig() != null;
130     }
131
132     /**
133      * @return The "signer" name for this SRRset, if signed, or null if not.
134      */
135     public Name getSignerName() {
136         RRSIGRecord sig = (RRSIGRecord) firstSig();
137
138         if (sig == null) {
139             return null;
140         }
141
142         return sig.getSigner();
143     }
144 }