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     */
017    
018    package org.apache.geronimo.util.asn1.x509;
019    
020    import org.apache.geronimo.util.asn1.ASN1Encodable;
021    import org.apache.geronimo.util.asn1.ASN1EncodableVector;
022    import org.apache.geronimo.util.asn1.ASN1Sequence;
023    import org.apache.geronimo.util.asn1.DERObject;
024    import org.apache.geronimo.util.asn1.DERSequence;
025    
026    /**
027     * <code>UserNotice</code> class, used in
028     * <code>CertificatePolicies</code> X509 extensions (in policy
029     * qualifiers).
030     * <pre>
031     * UserNotice ::= SEQUENCE {
032     *      noticeRef        NoticeReference OPTIONAL,
033     *      explicitText     DisplayText OPTIONAL}
034     *
035     * </pre>
036     *
037     * @see PolicyQualifierId
038     * @see PolicyInformation
039     */
040    public class UserNotice
041        extends ASN1Encodable
042    {
043        NoticeReference noticeRef;
044        DisplayText     explicitText;
045    
046        /**
047         * Creates a new <code>UserNotice</code> instance.
048         *
049         * @param noticeRef a <code>NoticeReference</code> value
050         * @param explicitText a <code>DisplayText</code> value
051         */
052        public UserNotice(
053            NoticeReference noticeRef,
054            DisplayText explicitText)
055        {
056            this.noticeRef = noticeRef;
057            this.explicitText = explicitText;
058        }
059    
060        /**
061         * Creates a new <code>UserNotice</code> instance.
062         *
063         * @param noticeRef a <code>NoticeReference</code> value
064         * @param str the explicitText field as a String.
065         */
066        public UserNotice(
067            NoticeReference noticeRef,
068            String str)
069        {
070            this.noticeRef = noticeRef;
071            this.explicitText = new DisplayText(str);
072        }
073    
074       /**
075        * Creates a new <code>UserNotice</code> instance.
076        * <p>Useful from reconstructing a <code>UserNotice</code> instance
077        * from its encodable/encoded form.
078        *
079        * @param as an <code>ASN1Sequence</code> value obtained from either
080        * calling @{link toASN1Object()} for a <code>UserNotice</code>
081        * instance or from parsing it from a DER-encoded stream.
082        */
083       public UserNotice(
084           ASN1Sequence as)
085       {
086           if (as.size() == 2)
087           {
088               noticeRef = NoticeReference.getInstance(as.getObjectAt(0));
089               explicitText = DisplayText.getInstance(as.getObjectAt(1));
090           }
091           else if (as.size() == 1)
092           {
093               if (as.getObjectAt(0).getDERObject() instanceof ASN1Sequence)
094               {
095                   noticeRef = NoticeReference.getInstance(as.getObjectAt(0));
096               }
097               else
098               {
099                   explicitText = DisplayText.getInstance(as.getObjectAt(0));
100               }
101           }
102        }
103    
104        public DERObject toASN1Object()
105        {
106            ASN1EncodableVector av = new ASN1EncodableVector();
107    
108            if (noticeRef != null)
109            {
110                av.add(noticeRef);
111            }
112    
113            if (explicitText != null)
114            {
115                av.add(explicitText);
116            }
117    
118            return new DERSequence(av);
119        }
120    }