001package org.apache.commons.ssl.org.bouncycastle.asn1.x509;
002
003import java.util.Enumeration;
004import java.util.Vector;
005
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.DERSequence;
011
012/**
013 * This extension may contain further X.500 attributes of the subject. See also
014 * RFC 3039.
015 * 
016 * <pre>
017 *     SubjectDirectoryAttributes ::= Attributes
018 *     Attributes ::= SEQUENCE SIZE (1..MAX) OF Attribute
019 *     Attribute ::= SEQUENCE 
020 *     {
021 *       type AttributeType 
022 *       values SET OF AttributeValue 
023 *     }
024 *     
025 *     AttributeType ::= OBJECT IDENTIFIER
026 *     AttributeValue ::= ANY DEFINED BY AttributeType
027 * </pre>
028 * 
029 * @see org.bouncycastle.asn1.x500.style.BCStyle for AttributeType ObjectIdentifiers.
030 */
031public class SubjectDirectoryAttributes 
032    extends ASN1Object
033{
034    private Vector attributes = new Vector();
035
036    public static SubjectDirectoryAttributes getInstance(
037        Object obj)
038    {
039        if (obj instanceof SubjectDirectoryAttributes)
040        {
041            return (SubjectDirectoryAttributes)obj;
042        }
043
044        if (obj != null)
045        {
046            return new SubjectDirectoryAttributes(ASN1Sequence.getInstance(obj));
047        }
048
049        return null;
050    }
051
052    /**
053     * Constructor from ASN1Sequence.
054     * 
055     * The sequence is of type SubjectDirectoryAttributes:
056     * 
057     * <pre>
058     *      SubjectDirectoryAttributes ::= Attributes
059     *      Attributes ::= SEQUENCE SIZE (1..MAX) OF Attribute
060     *      Attribute ::= SEQUENCE 
061     *      {
062     *        type AttributeType 
063     *        values SET OF AttributeValue 
064     *      }
065     *      
066     *      AttributeType ::= OBJECT IDENTIFIER
067     *      AttributeValue ::= ANY DEFINED BY AttributeType
068     * </pre>
069     * 
070     * @param seq
071     *            The ASN.1 sequence.
072     */
073    private SubjectDirectoryAttributes(ASN1Sequence seq)
074    {
075        Enumeration e = seq.getObjects();
076
077        while (e.hasMoreElements())
078        {
079            ASN1Sequence s = ASN1Sequence.getInstance(e.nextElement());
080            attributes.addElement(Attribute.getInstance(s));
081        }
082    }
083
084    /**
085     * Constructor from a vector of attributes.
086     * 
087     * The vector consists of attributes of type {@link Attribute Attribute}
088     * 
089     * @param attributes
090     *            The attributes.
091     * 
092     */
093    public SubjectDirectoryAttributes(Vector attributes)
094    {
095        Enumeration e = attributes.elements();
096
097        while (e.hasMoreElements())
098        {
099            this.attributes.addElement(e.nextElement());
100        }
101    }
102
103    /**
104     * Produce an object suitable for an ASN1OutputStream.
105     * 
106     * Returns:
107     * 
108     * <pre>
109     *      SubjectDirectoryAttributes ::= Attributes
110     *      Attributes ::= SEQUENCE SIZE (1..MAX) OF Attribute
111     *      Attribute ::= SEQUENCE 
112     *      {
113     *        type AttributeType 
114     *        values SET OF AttributeValue 
115     *      }
116     *      
117     *      AttributeType ::= OBJECT IDENTIFIER
118     *      AttributeValue ::= ANY DEFINED BY AttributeType
119     * </pre>
120     * 
121     * @return a ASN1Primitive
122     */
123    public ASN1Primitive toASN1Primitive()
124    {
125        ASN1EncodableVector vec = new ASN1EncodableVector();
126        Enumeration e = attributes.elements();
127
128        while (e.hasMoreElements())
129        {
130
131            vec.add((Attribute)e.nextElement());
132        }
133
134        return new DERSequence(vec);
135    }
136
137    /**
138     * @return Returns the attributes.
139     */
140    public Vector getAttributes()
141    {
142        return attributes;
143    }
144}