001/*
002 * Copyright 2009-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2009-2024 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-2024 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.NotNull;
045import com.unboundid.util.StaticUtils;
046import com.unboundid.util.ThreadSafety;
047import com.unboundid.util.ThreadSafetyLevel;
048
049
050
051/**
052 * This class provides a set of utility methods for working with LDAP DNs.
053 * <BR><BR>
054 * This class is primarily intended to be used in the process of updating
055 * applications which use the Netscape Directory SDK for Java to switch to or
056 * coexist with the UnboundID LDAP SDK for Java.  For applications not written
057 * using the Netscape Directory SDK for Java, the {@link DN} class should be
058 * used instead.
059 */
060@NotMutable()
061@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
062public final class LDAPDN
063{
064  /**
065   * Prevent this class from being instantiated.
066   */
067  private LDAPDN()
068  {
069    // No implementation required.
070  }
071
072
073
074  /**
075   * Retrieves a normalized representation of the provided DN.  If the provided
076   * string does not represent a valid distinguished name, then the value
077   * returned by this method will not be reliable.
078   *
079   * @param  dn  The string representation of the DN to be normalized.
080   *
081   * @return  A normalized representation of the provided DN.
082   */
083  @NotNull()
084  public static String normalize(@NotNull final String dn)
085  {
086    try
087    {
088      return DN.normalize(dn);
089    }
090    catch (final Exception e)
091    {
092      Debug.debugException(e);
093      return StaticUtils.toLowerCase(dn.trim());
094    }
095  }
096
097
098
099  /**
100   * Explodes the provided DN into individual RDN components.  If the provided
101   * string does not represent a valid distinguished name, then the value
102   * returned by this method will not be reliable.
103   *
104   * @param  dn       The DN to be exploded into its RDN components.
105   * @param  noTypes  Indicates whether to exclude the attribute names and
106   *                  equal signs and only include the values of the RDN
107   *                  components.
108   *
109   * @return  An exploded representation of the provided DN.
110   */
111  @NotNull()
112  public static String[] explodeDN(@NotNull final String dn,
113                                   final boolean noTypes)
114  {
115    try
116    {
117      final RDN[] rdns = new DN(dn).getRDNs();
118      final String[] rdnStrings = new String[rdns.length];
119      for (int i=0; i < rdns.length; i++)
120      {
121        if (noTypes)
122        {
123          final StringBuilder buffer = new StringBuilder();
124          for (final String s : rdns[i].getAttributeValues())
125          {
126            if (buffer.length() > 0)
127            {
128              buffer.append('+');
129            }
130            buffer.append(s);
131          }
132          rdnStrings[i] = buffer.toString();
133        }
134        else
135        {
136          rdnStrings[i] = rdns[i].toString();
137        }
138      }
139      return rdnStrings;
140    }
141    catch (final Exception e)
142    {
143      Debug.debugException(e);
144      return new String[] { dn };
145    }
146  }
147
148
149
150  /**
151   * Explodes the provided RDN into individual name-value pairs.  If the
152   * provided string does not represent a valid relative distinguished name,
153   * then the value returned by this method will not be reliable.
154   *
155   * @param  rdn      The RDN to be exploded into its name-value pairs.
156   * @param  noTypes  Indicates whether to exclude the attribute names and
157   *                  equal signs and only include the values of the components.
158   *
159   * @return  An exploded representation of the provided DN.
160   */
161  @NotNull()
162  public static String[] explodeRDN(@NotNull final String rdn,
163                                    final boolean noTypes)
164  {
165    try
166    {
167      final RDN      rdnObject  = new RDN(rdn);
168
169      final String[] values = rdnObject.getAttributeValues();
170      if (noTypes)
171      {
172        return values;
173      }
174
175      final String[] names      = rdnObject.getAttributeNames();
176      final String[] returnStrs = new String[names.length];
177
178      for (int i=0; i < names.length; i++)
179      {
180        returnStrs[i] = names[i] + '=' + values[i];
181      }
182
183      return returnStrs;
184    }
185    catch (final Exception e)
186    {
187      Debug.debugException(e);
188      return new String[] { rdn };
189    }
190  }
191
192
193
194  /**
195   * Indicates whether the provided strings represent the same distinguished
196   * name.
197   *
198   * @param  dn1  The first DN to be compared.
199   * @param  dn2  The second DN to be compared.
200   *
201   * @return  {@code true} if the provided strings represent the same
202   *          distinguished name, or {@code false} if not or if either of the
203   *          values cannot be parsed as a valid DN.
204   */
205  public static boolean equals(@NotNull final String dn1,
206                               @NotNull final String dn2)
207  {
208    try
209    {
210      return DN.equals(dn1, dn2);
211    }
212    catch (final Exception e)
213    {
214      Debug.debugException(e);
215      return false;
216    }
217  }
218}