001package org.apache.commons.ssl.org.bouncycastle.asn1.isismtt.x509;
002
003import java.util.Enumeration;
004
005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Encodable;
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1EncodableVector;
007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object;
008import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive;
009import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Sequence;
010import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1TaggedObject;
011import org.apache.commons.ssl.org.bouncycastle.asn1.DERSequence;
012import org.apache.commons.ssl.org.bouncycastle.asn1.DERTaggedObject;
013import org.apache.commons.ssl.org.bouncycastle.asn1.x509.GeneralName;
014
015/**
016 * An Admissions structure.
017 * <pre>
018 *            Admissions ::= SEQUENCE
019 *            {
020 *              admissionAuthority [0] EXPLICIT GeneralName OPTIONAL
021 *              namingAuthority [1] EXPLICIT NamingAuthority OPTIONAL
022 *              professionInfos SEQUENCE OF ProfessionInfo
023 *            }
024 * </pre>
025 *
026 * @see org.bouncycastle.asn1.isismtt.x509.AdmissionSyntax
027 * @see org.bouncycastle.asn1.isismtt.x509.ProfessionInfo
028 * @see org.bouncycastle.asn1.isismtt.x509.NamingAuthority
029 */
030public class Admissions 
031    extends ASN1Object
032{
033
034    private GeneralName admissionAuthority;
035
036    private NamingAuthority namingAuthority;
037
038    private ASN1Sequence professionInfos;
039
040    public static Admissions getInstance(Object obj)
041    {
042        if (obj == null || obj instanceof Admissions)
043        {
044            return (Admissions)obj;
045        }
046
047        if (obj instanceof ASN1Sequence)
048        {
049            return new Admissions((ASN1Sequence)obj);
050        }
051
052        throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
053    }
054
055    /**
056     * Constructor from ASN1Sequence.
057     * <p>
058     * The sequence is of type ProcurationSyntax:
059     * <pre>
060     *            Admissions ::= SEQUENCE
061     *            {
062     *              admissionAuthority [0] EXPLICIT GeneralName OPTIONAL
063     *              namingAuthority [1] EXPLICIT NamingAuthority OPTIONAL
064     *              professionInfos SEQUENCE OF ProfessionInfo
065     *            }
066     * </pre>
067     * </p>
068     * @param seq The ASN.1 sequence.
069     */
070    private Admissions(ASN1Sequence seq)
071    {
072        if (seq.size() > 3)
073        {
074            throw new IllegalArgumentException("Bad sequence size: "
075                + seq.size());
076        }
077        Enumeration e = seq.getObjects();
078
079        ASN1Encodable o = (ASN1Encodable)e.nextElement();
080        if (o instanceof ASN1TaggedObject)
081        {
082            switch (((ASN1TaggedObject)o).getTagNo())
083            {
084            case 0:
085                admissionAuthority = GeneralName.getInstance((ASN1TaggedObject)o, true);
086                break;
087            case 1:
088                namingAuthority = NamingAuthority.getInstance((ASN1TaggedObject)o, true);
089                break;
090            default:
091                throw new IllegalArgumentException("Bad tag number: " + ((ASN1TaggedObject)o).getTagNo());
092            }
093            o = (ASN1Encodable)e.nextElement();
094        }
095        if (o instanceof ASN1TaggedObject)
096        {
097            switch (((ASN1TaggedObject)o).getTagNo())
098            {
099            case 1:
100                namingAuthority = NamingAuthority.getInstance((ASN1TaggedObject)o, true);
101                break;
102            default:
103                throw new IllegalArgumentException("Bad tag number: " + ((ASN1TaggedObject)o).getTagNo());
104            }
105            o = (ASN1Encodable)e.nextElement();
106        }
107        professionInfos = ASN1Sequence.getInstance(o);
108        if (e.hasMoreElements())
109        {
110            throw new IllegalArgumentException("Bad object encountered: "
111                + e.nextElement().getClass());
112        }
113    }
114
115    /**
116     * Constructor from a given details.
117     * <p>
118     * Parameter <code>professionInfos</code> is mandatory.
119     *
120     * @param admissionAuthority The admission authority.
121     * @param namingAuthority    The naming authority.
122     * @param professionInfos    The profession infos.
123     */
124    public Admissions(GeneralName admissionAuthority,
125                      NamingAuthority namingAuthority, ProfessionInfo[] professionInfos)
126    {
127        this.admissionAuthority = admissionAuthority;
128        this.namingAuthority = namingAuthority;
129        this.professionInfos = new DERSequence(professionInfos);
130    }
131
132    public GeneralName getAdmissionAuthority()
133    {
134        return admissionAuthority;
135    }
136
137    public NamingAuthority getNamingAuthority()
138    {
139        return namingAuthority;
140    }
141
142    public ProfessionInfo[] getProfessionInfos()
143    {
144        ProfessionInfo[] infos = new ProfessionInfo[professionInfos.size()];
145        int count = 0;
146        for (Enumeration e = professionInfos.getObjects(); e.hasMoreElements();)
147        {
148            infos[count++] = ProfessionInfo.getInstance(e.nextElement());
149        }
150        return infos;
151    }
152
153    /**
154     * Produce an object suitable for an ASN1OutputStream.
155     * <p>
156     * Returns:
157     * <pre>
158     *       Admissions ::= SEQUENCE
159     *       {
160     *         admissionAuthority [0] EXPLICIT GeneralName OPTIONAL
161     *         namingAuthority [1] EXPLICIT NamingAuthority OPTIONAL
162     *         professionInfos SEQUENCE OF ProfessionInfo
163     *       }
164     * </pre>
165     *
166     * @return an ASN1Primitive
167     */
168    public ASN1Primitive toASN1Primitive()
169    {
170        ASN1EncodableVector vec = new ASN1EncodableVector();
171        
172        if (admissionAuthority != null)
173        {
174            vec.add(new DERTaggedObject(true, 0, admissionAuthority));
175        }
176        if (namingAuthority != null)
177        {
178            vec.add(new DERTaggedObject(true, 1, namingAuthority));
179        }
180        vec.add(professionInfos);
181
182        return new DERSequence(vec);
183    }
184}