Here you can find the source of getCommonName(String name)
Parameter | Description |
---|---|
name | a distinguished principal name |
public static String getCommonName(String name)
//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; } } }