001/* 002 * Copyright 2009-2018 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright (C) 2009-2018 Ping Identity Corporation 007 * 008 * This program is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU General Public License (GPLv2 only) 010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 011 * as published by the Free Software Foundation. 012 * 013 * This program is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU General Public License for more details. 017 * 018 * You should have received a copy of the GNU General Public License 019 * along with this program; if not, see <http://www.gnu.org/licenses>. 020 */ 021package com.unboundid.ldap.sdk.migrate.ldapjdk; 022 023 024 025import com.unboundid.ldap.sdk.DN; 026import com.unboundid.ldap.sdk.RDN; 027import com.unboundid.util.Debug; 028import com.unboundid.util.NotMutable; 029import com.unboundid.util.StaticUtils; 030import com.unboundid.util.ThreadSafety; 031import com.unboundid.util.ThreadSafetyLevel; 032 033 034 035/** 036 * This class provides a set of utility methods for working with LDAP DNs. 037 * <BR><BR> 038 * This class is primarily intended to be used in the process of updating 039 * applications which use the Netscape Directory SDK for Java to switch to or 040 * coexist with the UnboundID LDAP SDK for Java. For applications not written 041 * using the Netscape Directory SDK for Java, the {@link DN} class should be 042 * used instead. 043 */ 044@NotMutable() 045@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 046public final class LDAPDN 047{ 048 /** 049 * Prevent this class from being instantiated. 050 */ 051 private LDAPDN() 052 { 053 // No implementation required. 054 } 055 056 057 058 /** 059 * Retrieves a normalized representation of the provided DN. If the provided 060 * string does not represent a valid distinguished name, then the value 061 * returned by this method will not be reliable. 062 * 063 * @param dn The string representation of the DN to be normalized. 064 * 065 * @return A normalized representation of the provided DN. 066 */ 067 public static String normalize(final String dn) 068 { 069 try 070 { 071 return DN.normalize(dn); 072 } 073 catch (final Exception e) 074 { 075 Debug.debugException(e); 076 return StaticUtils.toLowerCase(dn.trim()); 077 } 078 } 079 080 081 082 /** 083 * Explodes the provided DN into individual RDN components. If the provided 084 * string does not represent a valid distinguished name, then the value 085 * returned by this method will not be reliable. 086 * 087 * @param dn The DN to be exploded into its RDN components. 088 * @param noTypes Indicates whether to exclude the attribute names and 089 * equal signs and only include the values of the RDN 090 * components. 091 * 092 * @return An exploded representation of the provided DN. 093 */ 094 public static String[] explodeDN(final String dn, final boolean noTypes) 095 { 096 try 097 { 098 final RDN[] rdns = new DN(dn).getRDNs(); 099 final String[] rdnStrings = new String[rdns.length]; 100 for (int i=0; i < rdns.length; i++) 101 { 102 if (noTypes) 103 { 104 final StringBuilder buffer = new StringBuilder(); 105 for (final String s : rdns[i].getAttributeValues()) 106 { 107 if (buffer.length() > 0) 108 { 109 buffer.append('+'); 110 } 111 buffer.append(s); 112 } 113 rdnStrings[i] = buffer.toString(); 114 } 115 else 116 { 117 rdnStrings[i] = rdns[i].toString(); 118 } 119 } 120 return rdnStrings; 121 } 122 catch (final Exception e) 123 { 124 Debug.debugException(e); 125 return new String[] { dn }; 126 } 127 } 128 129 130 131 /** 132 * Explodes the provided RDN into individual name-value pairs. If the 133 * provided string does not represent a valid relative distinguished name, 134 * then the value returned by this method will not be reliable. 135 * 136 * @param rdn The RDN to be exploded into its name-value pairs. 137 * @param noTypes Indicates whether to exclude the attribute names and 138 * equal signs and only include the values of the components. 139 * 140 * @return An exploded representation of the provided DN. 141 */ 142 public static String[] explodeRDN(final String rdn, final boolean noTypes) 143 { 144 try 145 { 146 final RDN rdnObject = new RDN(rdn); 147 148 final String[] values = rdnObject.getAttributeValues(); 149 if (noTypes) 150 { 151 return values; 152 } 153 154 final String[] names = rdnObject.getAttributeNames(); 155 final String[] returnStrs = new String[names.length]; 156 157 for (int i=0; i < names.length; i++) 158 { 159 returnStrs[i] = names[i] + '=' + values[i]; 160 } 161 162 return returnStrs; 163 } 164 catch (final Exception e) 165 { 166 Debug.debugException(e); 167 return new String[] { rdn }; 168 } 169 } 170 171 172 173 /** 174 * Indicates whether the provided strings represent the same distinguished 175 * name. 176 * 177 * @param dn1 The first DN to be compared. 178 * @param dn2 The second DN to be compared. 179 * 180 * @return {@code true} if the provided strings represent the same 181 * distinguished name, or {@code false} if not or if either of the 182 * values cannot be parsed as a valid DN. 183 */ 184 public static boolean equals(final String dn1, final String dn2) 185 { 186 try 187 { 188 return DN.equals(dn1, dn2); 189 } 190 catch (final Exception e) 191 { 192 Debug.debugException(e); 193 return false; 194 } 195 } 196}