All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bouncycastle.asn1.x509.RoleSyntax Maven / Gradle / Ivy

The newest version!
package org.bouncycastle.asn1.x509;

import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1Object;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.ASN1String;
import org.bouncycastle.asn1.ASN1TaggedObject;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.DERTaggedObject;

/**
 * Implementation of the RoleSyntax object as specified by the RFC3281.
 * 
 * 
 * RoleSyntax ::= SEQUENCE {
 *                 roleAuthority  [0] GeneralNames OPTIONAL,
 *                 roleName       [1] GeneralName
 *           } 
 * 
*/ public class RoleSyntax extends ASN1Object { private GeneralNames roleAuthority; private GeneralName roleName; /** * RoleSyntax factory method. * @param obj the object used to construct an instance of * RoleSyntax. It must be an instance of RoleSyntax * or ASN1Sequence. * @return the instance of RoleSyntax built from the * supplied object. * @throws java.lang.IllegalArgumentException if the object passed * to the factory is not an instance of RoleSyntax or * ASN1Sequence. */ public static RoleSyntax getInstance( Object obj) { if (obj instanceof RoleSyntax) { return (RoleSyntax)obj; } else if (obj != null) { return new RoleSyntax(ASN1Sequence.getInstance(obj)); } return null; } /** * Constructor. * @param roleAuthority the role authority of this RoleSyntax. * @param roleName the role name of this RoleSyntax. */ public RoleSyntax( GeneralNames roleAuthority, GeneralName roleName) { if(roleName == null || roleName.getTagNo() != GeneralName.uniformResourceIdentifier || ((ASN1String)roleName.getName()).getString().equals("")) { throw new IllegalArgumentException("the role name MUST be non empty and MUST " + "use the URI option of GeneralName"); } this.roleAuthority = roleAuthority; this.roleName = roleName; } /** * Constructor. Invoking this constructor is the same as invoking * new RoleSyntax(null, roleName). * @param roleName the role name of this RoleSyntax. */ public RoleSyntax( GeneralName roleName) { this(null, roleName); } /** * Utility constructor. Takes a String argument representing * the role name, builds a GeneralName to hold the role name * and calls the constructor that takes a GeneralName. * @param roleName */ public RoleSyntax( String roleName) { this(new GeneralName(GeneralName.uniformResourceIdentifier, (roleName == null)? "": roleName)); } /** * Constructor that builds an instance of RoleSyntax by * extracting the encoded elements from the ASN1Sequence * object supplied. * @param seq an instance of ASN1Sequence that holds * the encoded elements used to build this RoleSyntax. */ private RoleSyntax( ASN1Sequence seq) { if (seq.size() < 1 || seq.size() > 2) { throw new IllegalArgumentException("Bad sequence size: " + seq.size()); } for (int i = 0; i != seq.size(); i++) { ASN1TaggedObject taggedObject = ASN1TaggedObject.getInstance(seq.getObjectAt(i)); switch (taggedObject.getTagNo()) { case 0: roleAuthority = GeneralNames.getInstance(taggedObject, false); break; case 1: roleName = GeneralName.getInstance(taggedObject, true); break; default: throw new IllegalArgumentException("Unknown tag in RoleSyntax"); } } } /** * Gets the role authority of this RoleSyntax. * @return an instance of GeneralNames holding the * role authority of this RoleSyntax. */ public GeneralNames getRoleAuthority() { return this.roleAuthority; } /** * Gets the role name of this RoleSyntax. * @return an instance of GeneralName holding the * role name of this RoleSyntax. */ public GeneralName getRoleName() { return this.roleName; } /** * Gets the role name as a java.lang.String object. * @return the role name of this RoleSyntax represented as a * java.lang.String object. */ public String getRoleNameAsString() { ASN1String str = (ASN1String)this.roleName.getName(); return str.getString(); } /** * Gets the role authority as a String[] object. * @return the role authority of this RoleSyntax represented as a * String[] array. */ public String[] getRoleAuthorityAsString() { if(roleAuthority == null) { return new String[0]; } GeneralName[] names = roleAuthority.getNames(); String[] namesString = new String[names.length]; for(int i = 0; i < names.length; i++) { ASN1Encodable value = names[i].getName(); if(value instanceof ASN1String) { namesString[i] = ((ASN1String)value).getString(); } else { namesString[i] = value.toString(); } } return namesString; } /** * Implementation of the method toASN1Object as * required by the superclass ASN1Encodable. * *
     * RoleSyntax ::= SEQUENCE {
     *                 roleAuthority  [0] GeneralNames OPTIONAL,
     *                 roleName       [1] GeneralName
     *           } 
     * 
*/ public ASN1Primitive toASN1Primitive() { ASN1EncodableVector v = new ASN1EncodableVector(); if(this.roleAuthority != null) { v.add(new DERTaggedObject(false, 0, roleAuthority)); } v.add(new DERTaggedObject(true, 1, roleName)); return new DERSequence(v); } public String toString() { StringBuffer buff = new StringBuffer("Name: " + this.getRoleNameAsString() + " - Auth: "); if(this.roleAuthority == null || roleAuthority.getNames().length == 0) { buff.append("N/A"); } else { String[] names = this.getRoleAuthorityAsString(); buff.append('[').append(names[0]); for(int i = 1; i < names.length; i++) { buff.append(", ").append(names[i]); } buff.append(']'); } return buff.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy