Java Security getCommonName(String name)

Here you can find the source of getCommonName(String name)

Description

Gets the common name (CN) component of an X.500 principal name.

License

Apache License

Parameter

Parameter Description
name a distinguished principal name

Return

the common name (CN) component or a string representation of name if the name does not have a common name component

Declaration

public static String getCommonName(String name) 

Method Source Code


//package com.java2s;
/*//from   ww  w  . ja  va2s.co m
 * File created on Mar 20, 2014 
 *
 * Copyright (c) 2014 Virginia Polytechnic Institute and State University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

import javax.naming.NamingException;
import javax.naming.ldap.LdapName;
import javax.naming.ldap.Rdn;
import javax.security.auth.x500.X500Principal;

public class Main {
    /**
     * Gets the common name (CN) component of an X.500 principal name.
     * @param principal a distinguished principal name
     * @return the common name (CN) component or a string representation of 
     *   {@code name} if the name does not have a common name component
     */
    public static String getCommonName(X500Principal principal) {
        return getCommonName(principal.getName());
    }

    /**
     * Gets the common name (CN) component of an X.500 principal name.
     * @param name a distinguished principal name
     * @return the common name (CN) component or a string representation of 
     *   {@code name} if the name does not have a common name component
     */
    public static String getCommonName(String name) {
        try {
            LdapName ldapName = new LdapName(name);
            for (Rdn rdn : ldapName.getRdns()) {
                if (rdn.getType().equalsIgnoreCase("cn")) {
                    return rdn.getValue().toString();
                }
            }
            return name;
        } catch (NamingException ex) {
            return name;
        }
    }
}

Related

  1. convertLegacyToRFC2253(String dn)
  2. credsToTicket(Credentials serviceCreds)
  3. extractDirective(HashMap map, String key, String value)
  4. filterMechs(String[] mechs, int[] policies, Map props)
  5. get(Configuration configuration, String section, String key)
  6. getDirectiveValue( HashMap directivesMap, String directive, boolean mandatory)
  7. getEntries(Configuration configuration, String section)
  8. getPublicCredential(Class type, Subject subject)
  9. getRefreshTime(KerberosTicket ticket)