⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 name.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

package com.ca.commons.security.asn1;

import java.util.StringTokenizer;
import java.util.Vector;

/**
 * The definition of a X.500 directory name.
 * For more information, please refer to :
 * CCITT. Recommendation X.500: The Directory - Overview of Concepts,
 * Models and Services. 1988.
 *
 * @author who
 */
public class Name
{
    private String country = null;
    private String state = null;
    private String locality = null;
    private String org = null;
    // support multiple OU
    private Vector orgUnit = new Vector();
    private String commonName = null;
    private String emailAdd = null;
    private String uniqueID = null;
    private String addressIP = null;
    
    /* ASN1Object representation of the Name */
    private ASN1Object name = null;
    
    /**
     * Constructs an empty Name.
     */
    public Name()
    {}
    
    /**
     * Constructs a name given a string with the following format:
     * "C=country, S=state, L=locality, ..."
     */
    public Name(String input)
    {
        StringTokenizer tok  = new StringTokenizer(input, ",");
        while (tok.hasMoreTokens())
        {
            String nextToken = tok.nextToken().trim();
            StringTokenizer fieldTok = new StringTokenizer(nextToken, "=");
            if (fieldTok.countTokens() == 2)
            {
                String name = fieldTok.nextToken().trim();
                String value = fieldTok.nextToken().trim();
                
                if (name.equalsIgnoreCase("C"))
                {
                    if (!value.equalsIgnoreCase("null"))
                        country = value;
                }
                else if (name.equalsIgnoreCase("S"))
                {
                    if (!value.equalsIgnoreCase("null"))
                        state = value;
                }
                else if (name.equalsIgnoreCase("L"))
                {
                    if (!value.equalsIgnoreCase("null"))
                        locality = value;
                }
                else if (name.equalsIgnoreCase("O"))
                {
                    if (!value.equalsIgnoreCase("null"))
                        org = value;
                }
                else if (name.equalsIgnoreCase("OU"))
                {
                    if (!value.equalsIgnoreCase("null"))
                        orgUnit.add(value);
                }
                else if (name.equalsIgnoreCase("CN"))
                {
                    if (!value.equalsIgnoreCase("null"))
                        commonName = value;
                }
                else if (name.equalsIgnoreCase("E"))
                {
                    if (!value.equalsIgnoreCase("null"))
                        emailAdd = value;
                }
                else if (name.equalsIgnoreCase("EM"))
                {
                    if (!value.equalsIgnoreCase("null"))
                        emailAdd = value;
                }
                else if (name.equalsIgnoreCase("Email"))
                {
                    if (!value.equalsIgnoreCase("null"))
                        emailAdd = value;
                }
                else if (name.equalsIgnoreCase("EmailAddress"))
                {
                    if (!value.equalsIgnoreCase("null"))
                        emailAdd = value;
                }
            }
        }
        
        if (!valid())
        {
            throw new IllegalArgumentException("invalid parameters " +
                                               "for Name object.");
        }
    }
    
    /**
     * Constructs a Name with the given attributes.
     * @param	c country code
     * @param	s state or province
     * @param	l locality
     * @param	o organization
     * @param	ou organizational unit
     * @param	cn common name
     * @param	em email address
     */
    public Name(String c, String s, String l, String o, String ou,
                String cn, String em)
    {
        country = c;
        state = s;
        locality = l;
        org = o;
        orgUnit.add(ou);
        commonName = cn;
        emailAdd = em;
        
        if (!valid())
        {
            throw new IllegalArgumentException("invalid parameters " +
                                               "for Name object.");
        }
    }
    
    /**
     * Constructs a Name from an ASN1Object.
     */
    public Name(ASN1Object o)
    {
        name = o;
        init();
        if (name == null)
        {
            throw new IllegalArgumentException("cannot construct a " +
                                               "Name from input ASN1Object");
        }
    }
    
    /**
     * Constructs a Name from a byte array.
     */
    public Name(byte [] input)
    {
        name = ASN1Object.fromBytes(input);
        init();
        if (name == null)
        {
            throw new IllegalArgumentException("cannot construct a " +
                                               "Name from input byte array");
        }
    }
    
    /**
     * Initializes the Name from the ASN1Object. If error, the ASN1Object
     * is reset to null.
     */
    private void init()
    {
        ASN1Object o = name;
        ASN1Object rdn, ava;
        String type, value;
        
        if (o == null || !o.isASN1Type(ASN1Type.SEQUENCE))
        {
            name = null;
            return;
        }
        
        for (int i = 0; i < o.size(); i++)
        {
            rdn = o.getComponent(i);
            if (!rdn.isASN1Type(ASN1Type.SET))
            {
                name = null;
                return;
            }
            
            /* accepted attributes */
            for (int j = 0; j < rdn.size(); j++)
            {
                ava = rdn.getComponent(j);
                type = (String) (ava.getComponent(0).getValue());
                value = (String) (ava.getComponent(1).getValue());
                if (type.equals(ASN1OID.commonName))
                {
                    commonName = value;
                }
                else if (type.equals(ASN1OID.country))
                {
                    country = value;
                }
                else if (type.equals(ASN1OID.stateOrProvince))
                {
                    state = value;
                }
                else if (type.equals(ASN1OID.locality))
                {
                    locality = value;
                }
                else if (type.equals(ASN1OID.organization))
                {
                    org = value;
                }
                else if (type.equals(ASN1OID.organizationalUnit))
                {
                    orgUnit.add(value);
                }
                else if (type.equals(ASN1OID.emailAddress))
                {
                    emailAdd = value;
                }
                else if (type.equals(ASN1OID.unstructuredName))
                {
                    uniqueID = value;
                }
                else if (type.equals(ASN1OID.unstructuredAddress))
                {
                    addressIP = value;
                }
                else
                {			// ignore other attribute
                    //	System.out.println("Unknown attribute");
                    //	System.out.println("type " + type + " value " + value);
                }
            }
        }
        
        /* the essential attributes cannot all be null */
        if (!valid())
        {
            name = null;
        }
    }
    
    /**
     * Creates the ASN1Object with respective attibutes. If error,
     * the name object is set to null.
     */
    private void createName()
    {
        if (!valid())
        {
            name = null;
            return;
        }
        
        try
        {
            name = ASN1Object.create(ASN1Type.SEQUENCE);
            if (country != null)
            {
                name.addComponent(createRDN(ASN1OID.country, country));
            }
            if (state != null)
            {
                name.addComponent(createRDN(ASN1OID.stateOrProvince, state));
            }
            if (locality != null)
            {
                name.addComponent(createRDN(ASN1OID.locality, locality));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -