001: /*
002: * Title: Oyster Project
003: * Description: S/MIME email sending capabilities
004: * @Author Vladimir Radisic
005: * @Version 2.1.6
006: */
007:
008: package org.enhydra.oyster.der;
009:
010: import org.enhydra.oyster.exception.SMIMEException;
011: import org.enhydra.oyster.exception.ErrorStorage;
012: import java.util.GregorianCalendar;
013: import java.util.Date;
014: import java.util.TimeZone;
015: import java.text.SimpleDateFormat;
016:
017: /**
018: * DERUTCTime is primitive type of DER encoded object which represents Coordinate
019: * Universal Time (or Greenwich Mean Time) in the ASN.1 notation in format
020: * YYMMDDhhmmssZ. For example: 01.02.1971. 11:25:30 GMT+1h take a form in
021: * DERUTCTime object: 710201102530Z.
022: */
023: public class DERUTCTime extends DERObject {
024:
025: /**
026: * Constructs UTC time according to user's current local settings (increased
027: * or decreased for time difference to obtain Greenwich Mean Time).
028: * @exception SMIMEException thrown in super class constructor. Also it can be
029: * caused by non SMIMEException which is: UnsupportedEncodingException.
030: */
031: public DERUTCTime() throws SMIMEException {
032: super (23);
033: TimeZone tz = TimeZone.getDefault();
034: tz.setRawOffset(0); // Setting of GMT 0 offset
035: GregorianCalendar cal = new GregorianCalendar(tz);
036: SimpleDateFormat datForm = new SimpleDateFormat(
037: "yyMMddHHmmss'Z'"); // Appropriate format for UTC DER value
038: datForm.setCalendar(cal);
039: byte[] utcTime = null;
040: try {
041: utcTime = datForm.format(cal.getTime()).getBytes(
042: "ISO-8859-1");
043: } catch (Exception e) {
044: throw new SMIMEException(e);
045: }
046: this .addContent(utcTime);
047: }
048:
049: /**
050: * Constructs UTC time according to the submited information in Date class
051: * @param dat0 information about date and time.
052: * @exception SMIMEException thrown in super class constructor. Also, it can be
053: * caused by non SMIMEException which is: UnsupportedEncodingException.
054: */
055: public DERUTCTime(Date dat0) throws SMIMEException {
056: super (23);
057: TimeZone tz = TimeZone.getDefault();
058: tz.setRawOffset(0); // Setting of GMT 0 offset
059: GregorianCalendar cal = new GregorianCalendar(tz);
060: cal.setTime(dat0);
061: SimpleDateFormat datForm = new SimpleDateFormat(
062: "yyMMddHHmmss'Z'"); // Appropriate format for UTC DER value
063: datForm.setCalendar(cal);
064: byte[] utcTime = null;
065: try {
066: utcTime = datForm.format(cal.getTime()).getBytes(
067: "ISO-8859-1");
068: } catch (Exception e) {
069: throw new SMIMEException(e);
070: }
071: this .addContent(utcTime);
072: }
073:
074: /**
075: * Constructs UTC time according to submited information in GregorianCalendar
076: * class.
077: * @param cal0 information about date and time.
078: * @exception SMIMEException thrown in super class constructor. Also, it can be
079: * caused by non SMIMEException which is: UnsupportedEncodingException.
080: */
081: public DERUTCTime(GregorianCalendar cal0) throws SMIMEException {
082: super (23);
083: TimeZone tz = TimeZone.getDefault();
084: tz.setRawOffset(0); // Setting of GMT 0 offset
085: cal0.setTimeZone(tz);
086: SimpleDateFormat datForm = new SimpleDateFormat(
087: "yyMMddHHmmss'Z'"); // Appropriate format for UTC DER value
088: datForm.setCalendar(cal0);
089: byte[] utcTime = null;
090: try {
091: utcTime = datForm.format(cal0.getTime()).getBytes(
092: "ISO-8859-1");
093: } catch (Exception e) {
094: throw new SMIMEException(e);
095: }
096: this .addContent(utcTime);
097: }
098:
099: /**
100: * Constructs UTC time according to the definition of elements in byte array
101: * it the following form: YYMMDDhhmmssZ
102: * @param utcTime0 byte array representation of UTC elements.
103: * @exception SMIMEException thrown in super class constructor.
104: */
105: public DERUTCTime(byte[] utcTime0) throws SMIMEException {
106: super (23);
107: this .addContent(utcTime0);
108: }
109:
110: /**
111: * Constructs UTC time according to the definition of elements in String
112: * it the following form: YYMMDDhhmmssZ
113: * @param utcTime0 is String representation of UTC elements.
114: * @exception SMIMEException thrown in super class constructor. Also, it can be
115: * caused by non SMIMEException which is: UnsupportedEncodingException.
116: */
117: public DERUTCTime(String utcTime0) throws SMIMEException {
118: super (23);
119: try {
120: this .addContent(utcTime0.getBytes("ISO-8859-1"));
121: } catch (Exception e) {
122: throw new SMIMEException(e);
123: }
124: }
125: }
|