1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.apache.geronimo.util.asn1.x509;
20
21 import java.text.ParsePosition;
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24 import java.util.SimpleTimeZone;
25
26 import org.apache.geronimo.util.asn1.ASN1Choice;
27 import org.apache.geronimo.util.asn1.ASN1Encodable;
28 import org.apache.geronimo.util.asn1.ASN1TaggedObject;
29 import org.apache.geronimo.util.asn1.DERGeneralizedTime;
30 import org.apache.geronimo.util.asn1.DERObject;
31 import org.apache.geronimo.util.asn1.DERUTCTime;
32
33 public class Time
34 extends ASN1Encodable
35 implements ASN1Choice
36 {
37 DERObject time;
38
39 public static Time getInstance(
40 ASN1TaggedObject obj,
41 boolean explicit)
42 {
43 return getInstance(obj.getObject());
44 }
45
46 public Time(
47 DERObject time)
48 {
49 if (!(time instanceof DERUTCTime)
50 && !(time instanceof DERGeneralizedTime))
51 {
52 throw new IllegalArgumentException("unknown object passed to Time");
53 }
54
55 this.time = time;
56 }
57
58 /**
59 * creates a time object from a given date - if the date is between 1950
60 * and 2049 a UTCTime object is generated, otherwise a GeneralizedTime
61 * is used.
62 */
63 public Time(
64 Date date)
65 {
66 SimpleTimeZone tz = new SimpleTimeZone(0, "Z");
67 SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMddHHmmss");
68
69 dateF.setTimeZone(tz);
70
71 String d = dateF.format(date) + "Z";
72 int year = Integer.parseInt(d.substring(0, 4));
73
74 if (year < 1950 || year > 2049)
75 {
76 time = new DERGeneralizedTime(d);
77 }
78 else
79 {
80 time = new DERUTCTime(d.substring(2));
81 }
82 }
83
84 public static Time getInstance(
85 Object obj)
86 {
87 if (obj instanceof Time)
88 {
89 return (Time)obj;
90 }
91 else if (obj instanceof DERUTCTime)
92 {
93 return new Time((DERUTCTime)obj);
94 }
95 else if (obj instanceof DERGeneralizedTime)
96 {
97 return new Time((DERGeneralizedTime)obj);
98 }
99
100 throw new IllegalArgumentException("unknown object in factory");
101 }
102
103 public String getTime()
104 {
105 if (time instanceof DERUTCTime)
106 {
107 return ((DERUTCTime)time).getAdjustedTime();
108 }
109 else
110 {
111 return ((DERGeneralizedTime)time).getTime();
112 }
113 }
114
115 public Date getDate()
116 {
117 SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMddHHmmssz");
118
119 return dateF.parse(this.getTime(), new ParsePosition(0));
120 }
121
122 /**
123 * Produce an object suitable for an ASN1OutputStream.
124 * <pre>
125 * Time ::= CHOICE {
126 * utcTime UTCTime,
127 * generalTime GeneralizedTime }
128 * </pre>
129 */
130 public DERObject toASN1Object()
131 {
132 return time;
133 }
134 }