KickJava   Java API By Example, From Geeks To Geeks.

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


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.ParseException JavaDoc;
22 import java.text.SimpleDateFormat JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.SimpleTimeZone JavaDoc;
25
26 /**
27  * Generalized time object.
28  */

29 public class DERGeneralizedTime
30     extends DERObject
31 {
32     String JavaDoc time;
33
34     /**
35      * return a generalized time from the passed in object
36      *
37      * @exception IllegalArgumentException if the object cannot be converted.
38      */

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

64     public static DERGeneralizedTime getInstance(
65         ASN1TaggedObject obj,
66         boolean explicit)
67     {
68         return getInstance(obj.getObject());
69     }
70
71     /**
72      * The correct format for this is YYYYMMDDHHMMSSZ, or without the Z
73      * for local time, or Z+-HHMM on the end, for difference between local
74      * time and UTC time.
75      * <p>
76      *
77      * @param time the time string.
78      */

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

88     public DERGeneralizedTime(
89         Date JavaDoc time)
90     {
91         SimpleDateFormat JavaDoc dateF = new SimpleDateFormat JavaDoc("yyyyMMddHHmmss'Z'");
92
93         dateF.setTimeZone(new SimpleTimeZone JavaDoc(0,"Z"));
94
95         this.time = dateF.format(time);
96     }
97
98     DERGeneralizedTime(
99         byte[] bytes)
100     {
101         //
102
// explicitly convert to characters
103
//
104
char[] dateC = new char[bytes.length];
105
106         for (int i = 0; i != dateC.length; i++)
107         {
108             dateC[i] = (char)(bytes[i] & 0xff);
109         }
110
111         this.time = new String JavaDoc(dateC);
112     }
113
114     /**
115      * return the time - always in the form of
116      * YYYYMMDDhhmmssGMT(+hh:mm|-hh:mm).
117      * <p>
118      * Normally in a certificate we would expect "Z" rather than "GMT",
119      * however adding the "GMT" means we can just use:
120      * <pre>
121      * dateF = new SimpleDateFormat("yyyyMMddHHmmssz");
122      * </pre>
123      * To read in the time and get a date which is compatible with our local
124      * time zone.
125      */

126     public String JavaDoc getTime()
127     {
128         //
129
// standardise the format.
130
//
131
if (time.charAt(time.length() - 1) == 'Z')
132         {
133             return time.substring(0, time.length() - 1) + "GMT+00:00";
134         }
135         else
136         {
137             int signPos = time.length() - 5;
138             char sign = time.charAt(signPos);
139             if (sign == '-' || sign == '+')
140             {
141                 return time.substring(0, signPos)
142                     + "GMT"
143                     + time.substring(signPos, signPos + 3)
144                     + ":"
145                     + time.substring(signPos + 3);
146             }
147             else
148             {
149                 signPos = time.length() - 3;
150                 sign = time.charAt(signPos);
151                 if (sign == '-' || sign == '+')
152                 {
153                     return time.substring(0, signPos)
154                         + "GMT"
155                         + time.substring(signPos)
156                         + ":00";
157                 }
158             }
159         }
160
161         return time;
162     }
163
164     public Date JavaDoc getDate()
165         throws ParseException JavaDoc
166     {
167         SimpleDateFormat JavaDoc dateF;
168
169         if (time.indexOf('.') == 14)
170         {
171             dateF = new SimpleDateFormat JavaDoc("yyyyMMddHHmmss.SSS'Z'");
172         }
173         else
174         {
175             dateF = new SimpleDateFormat JavaDoc("yyyyMMddHHmmss'Z'");
176         }
177
178         dateF.setTimeZone(new SimpleTimeZone JavaDoc(0, "Z"));
179
180         return dateF.parse(time);
181     }
182
183     private byte[] getOctets()
184     {
185         char[] cs = time.toCharArray();
186         byte[] bs = new byte[cs.length];
187
188         for (int i = 0; i != cs.length; i++)
189         {
190             bs[i] = (byte)cs[i];
191         }
192
193         return bs;
194     }
195
196
197     void encode(
198         DEROutputStream out)
199         throws IOException JavaDoc
200     {
201         out.writeEncoded(GENERALIZED_TIME, this.getOctets());
202     }
203
204     public boolean equals(
205         Object JavaDoc o)
206     {
207         if ((o == null) || !(o instanceof DERGeneralizedTime))
208         {
209             return false;
210         }
211
212         return time.equals(((DERGeneralizedTime)o).time);
213     }
214
215     public int hashCode()
216     {
217         return time.hashCode();
218     }
219 }
220
Popular Tags