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