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