001package org.apache.commons.ssl.org.bouncycastle.asn1.x509;
002
003import java.util.Enumeration;
004
005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object;
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive;
007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Sequence;
008import org.apache.commons.ssl.org.bouncycastle.asn1.DERSequence;
009
010/**
011 * Targets structure used in target information extension for attribute
012 * certificates from RFC 3281.
013 * 
014 * <pre>
015 *            Targets ::= SEQUENCE OF Target
016 *           
017 *            Target  ::= CHOICE {
018 *              targetName          [0] GeneralName,
019 *              targetGroup         [1] GeneralName,
020 *              targetCert          [2] TargetCert
021 *            }
022 *           
023 *            TargetCert  ::= SEQUENCE {
024 *              targetCertificate    IssuerSerial,
025 *              targetName           GeneralName OPTIONAL,
026 *              certDigestInfo       ObjectDigestInfo OPTIONAL
027 *            }
028 * </pre>
029 * 
030 * @see org.bouncycastle.asn1.x509.Target
031 * @see org.bouncycastle.asn1.x509.TargetInformation
032 */
033public class Targets
034    extends ASN1Object
035{
036    private ASN1Sequence targets;
037
038    /**
039     * Creates an instance of a Targets from the given object.
040     * <p>
041     * <code>obj</code> can be a Targets or a {@link ASN1Sequence}
042     * 
043     * @param obj The object.
044     * @return A Targets instance.
045     * @throws IllegalArgumentException if the given object cannot be
046     *             interpreted as Target.
047     */
048    public static Targets getInstance(Object obj)
049    {
050        if (obj instanceof Targets)
051        {
052            return (Targets)obj;
053        }
054        else if (obj != null)
055        {
056            return new Targets(ASN1Sequence.getInstance(obj));
057        }
058
059        return null;
060    }
061
062    /**
063     * Constructor from ASN1Sequence.
064     * 
065     * @param targets The ASN.1 SEQUENCE.
066     * @throws IllegalArgumentException if the contents of the sequence are
067     *             invalid.
068     */
069    private Targets(ASN1Sequence targets)
070    {
071        this.targets = targets;
072    }
073
074    /**
075     * Constructor from given targets.
076     * <p>
077     * The vector is copied.
078     * 
079     * @param targets A <code>Vector</code> of {@link Target}s.
080     * @see Target
081     * @throws IllegalArgumentException if the vector contains not only Targets.
082     */
083    public Targets(Target[] targets)
084    {
085        this.targets = new DERSequence(targets);
086    }
087
088    /**
089     * Returns the targets in a <code>Vector</code>.
090     * <p>
091     * The vector is cloned before it is returned.
092     * 
093     * @return Returns the targets.
094     */
095    public Target[] getTargets()
096    {
097        Target[] targs = new Target[targets.size()];
098        int count = 0;
099        for (Enumeration e = targets.getObjects(); e.hasMoreElements();)
100        {
101            targs[count++] = Target.getInstance(e.nextElement());
102        }
103        return targs;
104    }
105
106    /**
107     * Produce an object suitable for an ASN1OutputStream.
108     * 
109     * Returns:
110     * 
111     * <pre>
112     *            Targets ::= SEQUENCE OF Target
113     * </pre>
114     * 
115     * @return a ASN1Primitive
116     */
117    public ASN1Primitive toASN1Primitive()
118    {
119        return targets;
120    }
121}