RoleSyntax.cs :  » PDF » iTextSharp » Org » BouncyCastle » Asn1 » X509 » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » PDF » iTextSharp 
iTextSharp » Org » BouncyCastle » Asn1 » X509 » RoleSyntax.cs
using System;
using System.Text;

namespace Org.BouncyCastle.Asn1.X509{
  /**
  * Implementation of the RoleSyntax object as specified by the RFC3281.
  *
  * <pre>
  * RoleSyntax ::= SEQUENCE {
  *                 roleAuthority  [0] GeneralNames OPTIONAL,
  *                 roleName       [1] GeneralName
  *           }
  * </pre>
  */
  public class RoleSyntax
    : Asn1Encodable
  {
    private readonly GeneralNames  roleAuthority;
    private readonly GeneralName  roleName;

    /**
     * RoleSyntax factory method.
     * @param obj the object used to construct an instance of <code>
     * RoleSyntax</code>. It must be an instance of <code>RoleSyntax
     * </code> or <code>Asn1Sequence</code>.
     * @return the instance of <code>RoleSyntax</code> built from the
     * supplied object.
     * @throws java.lang.ArgumentException if the object passed
     * to the factory is not an instance of <code>RoleSyntax</code> or
     * <code>Asn1Sequence</code>.
     */
    public static RoleSyntax GetInstance(
      object obj)
    {
      if (obj == null || obj is RoleSyntax)
      {
        return (RoleSyntax) obj;
      }

      if (obj is Asn1Sequence)
      {
        return new RoleSyntax((Asn1Sequence) obj);
      }

      throw new ArgumentException("unknown object in 'RoleSyntax' factory: " + obj.GetType().Name, "obj");
    }

    /**
    * 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.TagNo != GeneralName.UniformResourceIdentifier
        || ((IAsn1String) roleName.Name).GetString().Equals(""))
      {
        throw new ArgumentException("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
    * <code>new RoleSyntax(null, roleName)</code>.
    * @param roleName    the role name of this RoleSyntax.
    */
    public RoleSyntax(
      GeneralName roleName)
      : this(null, roleName)
    {
    }

    /**
    * Utility constructor. Takes a <code>string</code> argument representing
    * the role name, builds a <code>GeneralName</code> to hold the role name
    * and calls the constructor that takes a <code>GeneralName</code>.
    * @param roleName
    */
    public RoleSyntax(
      string roleName)
      : this(new GeneralName(GeneralName.UniformResourceIdentifier,
        (roleName == null)? "": roleName))
    {
    }

    /**
    * Constructor that builds an instance of <code>RoleSyntax</code> by
    * extracting the encoded elements from the <code>Asn1Sequence</code>
    * object supplied.
    * @param seq    an instance of <code>Asn1Sequence</code> that holds
    * the encoded elements used to build this <code>RoleSyntax</code>.
    */
    private RoleSyntax(
      Asn1Sequence seq)
    {
      if (seq.Count < 1 || seq.Count > 2)
      {
        throw new ArgumentException("Bad sequence size: " + seq.Count);
      }

      for (int i = 0; i != seq.Count; i++)
      {
        Asn1TaggedObject taggedObject = Asn1TaggedObject.GetInstance(seq[i]);
        switch (taggedObject.TagNo)
        {
          case 0:
            roleAuthority = GeneralNames.GetInstance(taggedObject, false);
            break;
          case 1:
            roleName = GeneralName.GetInstance(taggedObject, true);
            break;
          default:
            throw new ArgumentException("Unknown tag in RoleSyntax");
        }
      }
    }

    /**
    * Gets the role authority of this RoleSyntax.
    * @return    an instance of <code>GeneralNames</code> holding the
    * role authority of this RoleSyntax.
    */
    public GeneralNames RoleAuthority
    {
      get { return this.roleAuthority; }
    }

    /**
    * Gets the role name of this RoleSyntax.
    * @return    an instance of <code>GeneralName</code> holding the
    * role name of this RoleSyntax.
    */
    public GeneralName RoleName
    {
      get { return this.roleName; }
    }

    /**
    * Gets the role name as a <code>java.lang.string</code> object.
    * @return    the role name of this RoleSyntax represented as a
    * <code>string</code> object.
    */
    public string GetRoleNameAsString()
    {
      return ((IAsn1String) this.roleName.Name).GetString();
    }

    /**
    * Gets the role authority as a <code>string[]</code> object.
    * @return the role authority of this RoleSyntax represented as a
    * <code>string[]</code> 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 asn1Value = names[i].Name;
        if (asn1Value is IAsn1String)
        {
          namesString[i] = ((IAsn1String) asn1Value).GetString();
        }
        else
        {
          namesString[i] = asn1Value.ToString();
        }
      }

      return namesString;
    }

    /**
    * Implementation of the method <code>ToAsn1Object</code> as
    * required by the superclass <code>ASN1Encodable</code>.
    *
    * <pre>
    * RoleSyntax ::= SEQUENCE {
    *                 roleAuthority  [0] GeneralNames OPTIONAL,
    *                 roleName       [1] GeneralName
    *           }
    * </pre>
    */
    public override Asn1Object ToAsn1Object()
    {
      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 override string ToString()
    {
      StringBuilder buff = new StringBuilder("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();
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.