KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > asn1 > DERUTCTime


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.util.asn1;
19
20 import java.io.IOException JavaDoc;
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.SimpleTimeZone JavaDoc;
24
25 /**
26  * UTC time object.
27  */

28 public class DERUTCTime
29     extends DERObject
30 {
31     String JavaDoc time;
32
33     /**
34      * return an UTC Time from the passed in object.
35      *
36      * @exception IllegalArgumentException if the object cannot be converted.
37      */

38     public static DERUTCTime getInstance(
39         Object JavaDoc obj)
40     {
41         if (obj == null || obj instanceof DERUTCTime)
42         {
43             return (DERUTCTime)obj;
44         }
45
46         if (obj instanceof ASN1OctetString)
47         {
48             return new DERUTCTime(((ASN1OctetString)obj).getOctets());
49         }
50
51         throw new IllegalArgumentException JavaDoc("illegal object in getInstance: " + obj.getClass().getName());
52     }
53
54     /**
55      * return an UTC Time from a tagged object.
56      *
57      * @param obj the tagged object holding the object we want
58      * @param explicit true if the object is meant to be explicitly
59      * tagged false otherwise.
60      * @exception IllegalArgumentException if the tagged object cannot
61      * be converted.
62      */

63     public static DERUTCTime getInstance(
64         ASN1TaggedObject obj,
65         boolean explicit)
66     {
67         return getInstance(obj.getObject());
68     }
69
70     /**
71      * The correct format for this is YYMMDDHHMMSSZ (it used to be that seconds were
72      * never encoded. When you're creating one of these objects from scratch, that's
73      * what you want to use, otherwise we'll try to deal with whatever gets read from
74      * the input stream... (this is why the input format is different from the getTime()
75      * method output).
76      * <p>
77      *
78      * @param time the time string.
79      */

80     public DERUTCTime(
81         String JavaDoc time)
82     {
83         this.time = time;
84     }
85
86     /**
87      * base constructer from a java.util.date object
88      */

89     public DERUTCTime(
90         Date JavaDoc time)
91     {
92         SimpleDateFormat JavaDoc dateF = new SimpleDateFormat JavaDoc("yyMMddHHmmss'Z'");
93
94         dateF.setTimeZone(new SimpleTimeZone JavaDoc(0,"Z"));
95
96         this.time = dateF.format(time);
97     }
98
99     DERUTCTime(
100         byte[] bytes)
101     {
102         //
103
// explicitly convert to characters
104
//
105
char[] dateC = new char[bytes.length];
106
107         for (int i = 0; i != dateC.length; i++)
108         {
109             dateC[i] = (char)(bytes[i] & 0xff);
110         }
111
112         this.time = new String JavaDoc(dateC);
113     }
114
115     /**
116      * return the time - always in the form of
117      * YYMMDDhhmmssGMT(+hh:mm|-hh:mm).
118      * <p>
119      * Normally in a certificate we would expect "Z" rather than "GMT",
120      * however adding the "GMT" means we can just use:
121      * <pre>
122      * dateF = new SimpleDateFormat("yyMMddHHmmssz");
123      * </pre>
124      * To read in the time and get a date which is compatible with our local
125      * time zone.
126      * <p>
127      * <b>Note:</b> In some cases, due to the local date processing, this
128      * may lead to unexpected results. If you want to stick the normal
129      * convention of 1950 to 2049 use the getAdjustedTime() method.
130      */

131     public String JavaDoc getTime()
132     {
133         //
134
// standardise the format.
135
//
136
if (time.length() == 11)
137         {
138             return time.substring(0, 10) + "00GMT+00:00";
139         }
140         else if (time.length() == 13)
141         {
142             return time.substring(0, 12) + "GMT+00:00";
143         }
144         else if (time.length() == 17)
145         {
146             return time.substring(0, 12) + "GMT" + time.substring(12, 15) + ":" + time.substring(15, 17);
147         }
148
149         return time;
150     }
151
152     /**
153      * return the time as an adjusted date with a 4 digit year. This goes
154      * in the range of 1950 - 2049.
155      */

156     public String JavaDoc getAdjustedTime()
157     {
158         String JavaDoc d = this.getTime();
159
160         if (d.charAt(0) < '5')
161         {
162             return "20" + d;
163         }
164         else
165         {
166             return "19" + d;
167         }
168     }
169
170     private byte[] getOctets()
171     {
172         char[] cs = time.toCharArray();
173         byte[] bs = new byte[cs.length];
174
175         for (int i = 0; i != cs.length; i++)
176         {
177             bs[i] = (byte)cs[i];
178         }
179
180         return bs;
181     }
182
183     void encode(
184         DEROutputStream out)
185         throws IOException JavaDoc
186     {
187         out.writeEncoded(UTC_TIME, this.getOctets());
188     }
189
190     public boolean equals(
191         Object JavaDoc o)
192     {
193         if ((o == null) || !(o instanceof DERUTCTime))
194         {
195             return false;
196         }
197
198         return time.equals(((DERUTCTime)o).time);
199     }
200
201     public int hashCode()
202     {
203         return time.hashCode();
204     }
205 }
206
Popular Tags