001package org.apache.commons.ssl.org.bouncycastle.asn1.x509;
002
003import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1EncodableVector;
004import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object;
005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive;
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Sequence;
007import org.apache.commons.ssl.org.bouncycastle.asn1.DERSequence;
008
009/**
010 * <code>UserNotice</code> class, used in
011 * <code>CertificatePolicies</code> X509 extensions (in policy
012 * qualifiers).
013 * <pre>
014 * UserNotice ::= SEQUENCE {
015 *      noticeRef        NoticeReference OPTIONAL,
016 *      explicitText     DisplayText OPTIONAL}
017 *
018 * </pre>
019 * 
020 * @see PolicyQualifierId
021 * @see PolicyInformation
022 */
023public class UserNotice 
024    extends ASN1Object
025{
026    private NoticeReference noticeRef;
027    private DisplayText     explicitText;
028   
029    /**
030     * Creates a new <code>UserNotice</code> instance.
031     *
032     * @param noticeRef a <code>NoticeReference</code> value
033     * @param explicitText a <code>DisplayText</code> value
034     */
035    public UserNotice(
036        NoticeReference noticeRef, 
037        DisplayText explicitText) 
038    {
039        this.noticeRef = noticeRef;
040        this.explicitText = explicitText;
041    }
042
043    /**
044     * Creates a new <code>UserNotice</code> instance.
045     *
046     * @param noticeRef a <code>NoticeReference</code> value
047     * @param str the explicitText field as a String. 
048     */
049    public UserNotice(
050        NoticeReference noticeRef, 
051        String str) 
052    {
053        this(noticeRef, new DisplayText(str));
054    }
055
056    /**
057     * Creates a new <code>UserNotice</code> instance.
058     * <p>Useful from reconstructing a <code>UserNotice</code> instance
059     * from its encodable/encoded form. 
060     *
061     * @param as an <code>ASN1Sequence</code> value obtained from either
062     * calling @{link toASN1Primitive()} for a <code>UserNotice</code>
063     * instance or from parsing it from a DER-encoded stream. 
064     */
065    private UserNotice(
066       ASN1Sequence as) 
067    {
068       if (as.size() == 2)
069       {
070           noticeRef = NoticeReference.getInstance(as.getObjectAt(0));
071           explicitText = DisplayText.getInstance(as.getObjectAt(1));
072       }
073       else if (as.size() == 1)
074       {
075           if (as.getObjectAt(0).toASN1Primitive() instanceof ASN1Sequence)
076           {
077               noticeRef = NoticeReference.getInstance(as.getObjectAt(0));
078           }
079           else
080           {
081               explicitText = DisplayText.getInstance(as.getObjectAt(0));
082           }
083       }
084       else
085       {
086           throw new IllegalArgumentException("Bad sequence size: " + as.size());
087       }
088    }
089
090    public static UserNotice getInstance(
091        Object obj)
092    {
093        if (obj instanceof UserNotice)
094        {
095            return (UserNotice)obj;
096        }
097
098        if (obj != null)
099        {
100            return new UserNotice(ASN1Sequence.getInstance(obj));
101        }
102
103        return null;
104    }
105
106    public NoticeReference getNoticeRef()
107    {
108        return noticeRef;
109    }
110    
111    public DisplayText getExplicitText()
112    {
113        return explicitText;
114    }
115    
116    public ASN1Primitive toASN1Primitive()
117    {
118        ASN1EncodableVector av = new ASN1EncodableVector();
119      
120        if (noticeRef != null)
121        {
122            av.add(noticeRef);
123        }
124        
125        if (explicitText != null)
126        {
127            av.add(explicitText);
128        }
129         
130        return new DERSequence(av);
131    }
132}