001package org.apache.commons.ssl.org.bouncycastle.asn1.cms;
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.ASN1Set;
007import org.apache.commons.ssl.org.bouncycastle.asn1.DLSet;
008
009/**
010 * <a href="http://tools.ietf.org/html/rfc5652">RFC 5652</a> defines
011 * 5 "SET OF Attribute" entities with 5 different names.
012 * This is common implementation for them all:
013 * <pre>
014 *   SignedAttributes      ::= SET SIZE (1..MAX) OF Attribute
015 *   UnsignedAttributes    ::= SET SIZE (1..MAX) OF Attribute
016 *   UnprotectedAttributes ::= SET SIZE (1..MAX) OF Attribute
017 *   AuthAttributes        ::= SET SIZE (1..MAX) OF Attribute
018 *   UnauthAttributes      ::= SET SIZE (1..MAX) OF Attribute
019 *
020 * Attributes ::=
021 *   SET SIZE(1..MAX) OF Attribute
022 * </pre>
023 */
024public class Attributes
025    extends ASN1Object
026{
027    private ASN1Set attributes;
028
029    private Attributes(ASN1Set set)
030    {
031        attributes = set;
032    }
033
034    public Attributes(ASN1EncodableVector v)
035    {
036        attributes = new DLSet(v);
037    }
038
039    /**
040     * Return an Attribute set object from the given object.
041     * <p>
042     * Accepted inputs:
043     * <ul>
044     * <li> null &rarr; null
045     * <li> {@link Attributes} object
046     * <li> {@link org.bouncycastle.asn1.ASN1Set#getInstance(java.lang.Object) ASN1Set} input formats with Attributes structure inside
047     * </ul>
048     *
049     * @param obj the object we want converted.
050     * @exception IllegalArgumentException if the object cannot be converted.
051     */
052    public static Attributes getInstance(Object obj)
053    {
054        if (obj instanceof Attributes)
055        {
056            return (Attributes)obj;
057        }
058        else if (obj != null)
059        {
060            return new Attributes(ASN1Set.getInstance(obj));
061        }
062
063        return null;
064    }
065
066    public Attribute[] getAttributes()
067    {
068        Attribute[] rv = new Attribute[attributes.size()];
069
070        for (int i = 0; i != rv.length; i++)
071        {
072            rv[i] = Attribute.getInstance(attributes.getObjectAt(i));
073        }
074
075        return rv;
076    }
077
078    /** 
079     * Produce an object suitable for an ASN1OutputStream.
080     */
081    public ASN1Primitive toASN1Primitive()
082    {
083        return attributes;
084    }
085}