001 /**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements. See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package org.apache.geronimo.util.asn1.x509;
020
021 import java.text.ParsePosition;
022 import java.text.SimpleDateFormat;
023 import java.util.Date;
024 import java.util.SimpleTimeZone;
025
026 import org.apache.geronimo.util.asn1.ASN1Choice;
027 import org.apache.geronimo.util.asn1.ASN1Encodable;
028 import org.apache.geronimo.util.asn1.ASN1TaggedObject;
029 import org.apache.geronimo.util.asn1.DERGeneralizedTime;
030 import org.apache.geronimo.util.asn1.DERObject;
031 import org.apache.geronimo.util.asn1.DERUTCTime;
032
033 public class Time
034 extends ASN1Encodable
035 implements ASN1Choice
036 {
037 DERObject time;
038
039 public static Time getInstance(
040 ASN1TaggedObject obj,
041 boolean explicit)
042 {
043 return getInstance(obj.getObject()); // must be explicitly tagged
044 }
045
046 public Time(
047 DERObject time)
048 {
049 if (!(time instanceof DERUTCTime)
050 && !(time instanceof DERGeneralizedTime))
051 {
052 throw new IllegalArgumentException("unknown object passed to Time");
053 }
054
055 this.time = time;
056 }
057
058 /**
059 * creates a time object from a given date - if the date is between 1950
060 * and 2049 a UTCTime object is generated, otherwise a GeneralizedTime
061 * is used.
062 */
063 public Time(
064 Date date)
065 {
066 SimpleTimeZone tz = new SimpleTimeZone(0, "Z");
067 SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMddHHmmss");
068
069 dateF.setTimeZone(tz);
070
071 String d = dateF.format(date) + "Z";
072 int year = Integer.parseInt(d.substring(0, 4));
073
074 if (year < 1950 || year > 2049)
075 {
076 time = new DERGeneralizedTime(d);
077 }
078 else
079 {
080 time = new DERUTCTime(d.substring(2));
081 }
082 }
083
084 public static Time getInstance(
085 Object obj)
086 {
087 if (obj instanceof Time)
088 {
089 return (Time)obj;
090 }
091 else if (obj instanceof DERUTCTime)
092 {
093 return new Time((DERUTCTime)obj);
094 }
095 else if (obj instanceof DERGeneralizedTime)
096 {
097 return new Time((DERGeneralizedTime)obj);
098 }
099
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 }