KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > novosec > pkix > asn1 > crmf > OptionalValidity


1 // CMP implementation copyright (c) 2003 NOVOSEC AG (http://www.novosec.com)
2
//
3
// Author: Maik Stohn
4
//
5
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
6
// software and associated documentation files (the "Software"), to deal in the Software
7
// without restriction, including without limitation the rights to use, copy, modify, merge,
8
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
9
// to whom the Software is furnished to do so, subject to the following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included in all copies or
12
// substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
15
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19

20 package com.novosec.pkix.asn1.crmf;
21
22 import java.util.Enumeration JavaDoc;
23
24 import org.bouncycastle.asn1.ASN1EncodableVector;
25 import org.bouncycastle.asn1.ASN1Sequence;
26 import org.bouncycastle.asn1.ASN1TaggedObject;
27 import org.bouncycastle.asn1.DEREncodable;
28 import org.bouncycastle.asn1.DERObject;
29 import org.bouncycastle.asn1.DERSequence;
30 import org.bouncycastle.asn1.DERTaggedObject;
31 import org.bouncycastle.asn1.x509.Time;
32
33 /**
34  * ASN.1 structure DER En/DeCoder.
35  *
36  * <pre>
37  * OptionalValidity ::= SEQUENCE {
38  * notBefore [0] Time OPTIONAL,
39  * notAfter [1] Time OPTIONAL } --at least one MUST be present
40  *
41  * </pre>
42  */

43 public class OptionalValidity implements DEREncodable
44 {
45     // time is a choice type --> tag it explicit
46
public static final boolean bTimeIsExplicit = true;
47
48     private Time notBefore = null;
49     private Time notAfter = null;
50
51     public static OptionalValidity getInstance( ASN1TaggedObject obj, boolean explicit )
52     {
53         return getInstance(ASN1Sequence.getInstance(obj, explicit));
54     }
55
56     public static OptionalValidity getInstance( Object JavaDoc obj )
57     {
58         if (obj == null) {
59             return new OptionalValidity();
60         }
61         else if (obj instanceof OptionalValidity) {
62             return (OptionalValidity)obj;
63         }
64         else if (obj instanceof ASN1Sequence) {
65             return new OptionalValidity((ASN1Sequence)obj);
66         }
67         else {
68             throw new IllegalArgumentException JavaDoc("unknown object in factory");
69         }
70     }
71
72     public OptionalValidity( ASN1Sequence seq )
73     {
74       Enumeration JavaDoc e = (seq == null ? null : seq.getObjects());
75       while (e != null && e.hasMoreElements())
76       {
77         DERTaggedObject obj = (DERTaggedObject)e.nextElement();
78         int tagno = (obj == null ? -1 : obj.getTagNo());
79         switch( tagno )
80         {
81           case 0: this.notBefore = Time.getInstance( obj, bTimeIsExplicit ); break;
82           case 1: this.notAfter = Time.getInstance( obj, bTimeIsExplicit ); break;
83           default : throw new IllegalArgumentException JavaDoc("invalid asn1 sequence");
84         }
85       }
86     }
87
88     public OptionalValidity()
89     {
90     }
91
92     public void setNotBefore( Time notBefore )
93     {
94       this.notBefore = notBefore;
95     }
96
97     public Time getNotBefore()
98     {
99       return notBefore;
100     }
101
102     public void setNotAfter( Time notAfter )
103     {
104       this.notAfter = notAfter;
105     }
106
107     public Time getNotAfter()
108     {
109       return notAfter;
110     }
111
112     public DERObject getDERObject()
113     {
114         ASN1EncodableVector v = new ASN1EncodableVector();
115
116         if( notBefore != null )
117           v.add( new DERTaggedObject( bTimeIsExplicit, 0, notBefore ) );
118
119         if( notAfter != null )
120           v.add( new DERTaggedObject( bTimeIsExplicit, 1, notAfter ) );
121
122         return new DERSequence(v);
123     }
124
125     public String JavaDoc toString() {
126         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(this.getClass().getName());
127         sb.append(" (");
128
129         if( this.getNotBefore() != null )
130             sb.append("notBefore: " + this.getNotBefore() + ", ");
131
132         if( this.getNotBefore() != null )
133             sb.append("notAfter: " + this.getNotAfter() + ", ");
134
135         sb.append("hashCode: " + Integer.toHexString(this.hashCode()) + ")");
136         return sb.toString();
137     }
138 }
139
Popular Tags