bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington/*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: DNUtils.java,v 1.6 2009/11/20 23:52:54 ww203982 Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
aa8e8a8f9065be8110c9c348155baa64ca9e8178sachiko * Portions Copyrighted 2010-2016 ForgeRock AS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.common;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport static org.forgerock.openam.ldap.LDAPUtils.rdnValueFromDn;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington
aa8e8a8f9065be8110c9c348155baa64ca9e8178sachikoimport org.forgerock.i18n.LocalizedIllegalArgumentException;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport org.forgerock.openam.ldap.LDAPUtils;
aa8e8a8f9065be8110c9c348155baa64ca9e8178sachikoimport org.forgerock.openam.utils.StringUtils;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport org.forgerock.opendj.ldap.DN;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
aa8e8a8f9065be8110c9c348155baa64ca9e8178sachikoimport com.sun.identity.shared.debug.Debug;
aa8e8a8f9065be8110c9c348155baa64ca9e8178sachiko
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class DNUtils {
aa8e8a8f9065be8110c9c348155baa64ca9e8178sachiko
aa8e8a8f9065be8110c9c348155baa64ca9e8178sachiko private static final Debug DEBUG = Debug.getInstance("DNUtils");
aa8e8a8f9065be8110c9c348155baa64ca9e8178sachiko
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns the normalized DN string.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param dn
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * string needs to be normalized.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return returns the normalized DN string if the passed in string is in DN
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * format otherwise returns null.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static String normalizeDN(String dn) {
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington return normalizeDN(LDAPUtils.newDN(dn));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static String normalizeDN(DN dn) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String newDN = null;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington if (dn != null && dn.size() > 0) {
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington newDN = dn.toString().toLowerCase();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return newDN;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Converts a DN string to the token value of the naming attribute.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param dn
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The passed in DN string
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return returns the token value of the naming attribute in the passed in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DN string. If the dn string is not in DN format, returns itself.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static String DNtoName(String dn) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // String dn is guaranteed type of DN
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return DNtoName(dn, true);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Converts a DN string to the token value of the naming attribute.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param dn
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The passed in DN string
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param noTypes
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * if true, returns only the values of the components and
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * not the names(such as "cn")
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return returns the token value of the naming attribute in the passed in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DN string. If the dn string is not in DN format, returns itself.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static String DNtoName(String dn, boolean noTypes) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // String dn is guaranteed type of DN
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String id = dn;
aa8e8a8f9065be8110c9c348155baa64ca9e8178sachiko if (StringUtils.isNotEmpty(dn) && LDAPUtils.isDN(dn, 1)) {
aa8e8a8f9065be8110c9c348155baa64ca9e8178sachiko try {
aa8e8a8f9065be8110c9c348155baa64ca9e8178sachiko DN name = DN.valueOf(dn);
ba7bff1ff4a7c8d40fad793bab5133d2bb16fd59Peter Major id = LDAPUtils.unescapeValue(noTypes ? rdnValueFromDn(name) : name.rdn().toString());
aa8e8a8f9065be8110c9c348155baa64ca9e8178sachiko } catch (LocalizedIllegalArgumentException e) {
aa8e8a8f9065be8110c9c348155baa64ca9e8178sachiko DEBUG.error("DNUtils.isDN: Invalid DN {}", dn, e);
aa8e8a8f9065be8110c9c348155baa64ca9e8178sachiko }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return id;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}