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 java.io.Serializable; 041import java.net.MalformedURLException; 042import java.util.ArrayList; 043import java.util.Arrays; 044import java.util.Enumeration; 045 046import com.unboundid.ldap.sdk.DN; 047import com.unboundid.ldap.sdk.Filter; 048import com.unboundid.ldap.sdk.LDAPException; 049import com.unboundid.ldap.sdk.LDAPURL; 050import com.unboundid.ldap.sdk.SearchScope; 051import com.unboundid.util.Debug; 052import com.unboundid.util.NotExtensible; 053import com.unboundid.util.NotMutable; 054import com.unboundid.util.NotNull; 055import com.unboundid.util.Nullable; 056import com.unboundid.util.ThreadSafety; 057import com.unboundid.util.ThreadSafetyLevel; 058 059 060 061/** 062 * This class provides a data structure that represents an LDAP URL. 063 * <BR><BR> 064 * This class is primarily intended to be used in the process of updating 065 * applications which use the Netscape Directory SDK for Java to switch to or 066 * coexist with the UnboundID LDAP SDK for Java. For applications not written 067 * using the Netscape Directory SDK for Java, the {@link LDAPURL} class should 068 * be used instead. 069 */ 070@NotExtensible() 071@NotMutable() 072@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 073public class LDAPUrl 074 implements Serializable 075{ 076 /** 077 * The serial version UID for this serializable class. 078 */ 079 private static final long serialVersionUID = -1716384037873600695L; 080 081 082 083 // The LDAP URL for this object. 084 @NotNull private final LDAPURL ldapURL; 085 086 087 088 /** 089 * Creates a new {@code LDAPUrl} object from the provided string 090 * representation. 091 * 092 * @param url The string representation of the LDAP URL to create. 093 * 094 * @throws MalformedURLException If the provided string cannot be parsed as 095 * a valid LDAP URL. 096 */ 097 public LDAPUrl(@NotNull final String url) 098 throws MalformedURLException 099 { 100 try 101 { 102 ldapURL = new LDAPURL(url); 103 } 104 catch (final LDAPException le) 105 { 106 Debug.debugException(le); 107 throw new MalformedURLException(le.getMessage()); 108 } 109 } 110 111 112 113 /** 114 * Creates a new {@code LDAPUrl} object with the provided information. 115 * 116 * @param host The address of the directory server, or {@code null} if there 117 * should not be an address. 118 * @param port The port of the directory server. 119 * @param dn The DN for the URL. 120 * 121 * @throws RuntimeException If any of the provided information cannot be 122 * used to create a valid LDAP URL. 123 */ 124 public LDAPUrl(@Nullable final String host, final int port, 125 @Nullable final String dn) 126 throws RuntimeException 127 { 128 try 129 { 130 final DN dnObject = (dn == null) ? null : new DN(dn); 131 ldapURL = new LDAPURL("ldap", host, port, dnObject, null, null, null); 132 } 133 catch (final Exception e) 134 { 135 Debug.debugException(e); 136 throw new RuntimeException(e); 137 } 138 } 139 140 141 142 /** 143 * Creates a new {@code LDAPUrl} object with the provided information. 144 * 145 * @param host The address of the directory server, or {@code null} if 146 * there should not be an address. 147 * @param port The port of the directory server. 148 * @param dn The DN for the URL. 149 * @param attributes The set of requested attributes. 150 * @param scope The scope to use for the LDAP URL. 151 * @param filter The filter to use for the LDAP URL. 152 * 153 * @throws RuntimeException If any of the provided information cannot be 154 * used to create a valid LDAP URL. 155 */ 156 public LDAPUrl(@Nullable final String host, final int port, 157 @Nullable final String dn, 158 @Nullable final String[] attributes, final int scope, 159 @NotNull final String filter) 160 throws RuntimeException 161 { 162 try 163 { 164 final DN dnObject = (dn == null) ? null : new DN(dn); 165 final SearchScope scopeObject = SearchScope.valueOf(scope); 166 final Filter filterObject = Filter.create(filter); 167 ldapURL = new LDAPURL("ldap", host, port, dnObject, attributes, 168 scopeObject, filterObject); 169 } 170 catch (final Exception e) 171 { 172 Debug.debugException(e); 173 throw new RuntimeException(e); 174 } 175 } 176 177 178 179 /** 180 * Creates a new {@code LDAPUrl} object with the provided information. 181 * 182 * @param host The address of the directory server, or {@code null} if 183 * there should not be an address. 184 * @param port The port of the directory server. 185 * @param dn The DN for the URL. 186 * @param attributes The set of requested attributes. 187 * @param scope The scope to use for the LDAP URL. 188 * @param filter The filter to use for the LDAP URL. 189 * 190 * @throws RuntimeException If any of the provided information cannot be 191 * used to create a valid LDAP URL. 192 */ 193 public LDAPUrl(@Nullable final String host, final int port, 194 @Nullable final String dn, 195 @Nullable final Enumeration<String> attributes, 196 final int scope, @NotNull final String filter) 197 throws RuntimeException 198 { 199 try 200 { 201 final DN dnObject = (dn == null) ? null : new DN(dn); 202 final SearchScope scopeObject = SearchScope.valueOf(scope); 203 final Filter filterObject = Filter.create(filter); 204 205 final String[] attrs; 206 if (attributes == null) 207 { 208 attrs = null; 209 } 210 else 211 { 212 final ArrayList<String> attrList = new ArrayList<>(10); 213 while (attributes.hasMoreElements()) 214 { 215 attrList.add(attributes.nextElement()); 216 } 217 attrs = new String[attrList.size()]; 218 attrList.toArray(attrs); 219 } 220 221 ldapURL = new LDAPURL("ldap", host, port, dnObject, attrs, scopeObject, 222 filterObject); 223 } 224 catch (final Exception e) 225 { 226 Debug.debugException(e); 227 throw new RuntimeException(e); 228 } 229 } 230 231 232 233 /** 234 * Creates a new {@code LDAPUrl} object from the provided {@link LDAPURL} 235 * object. 236 * 237 * @param ldapURL The {@code LDAPURL} object to use to create this LDAP URL. 238 */ 239 public LDAPUrl(@NotNull final LDAPURL ldapURL) 240 { 241 this.ldapURL = ldapURL; 242 } 243 244 245 246 /** 247 * Retrieves the address for this LDAP URL, if available. 248 * 249 * @return The address for this LDAP URL, or {@code null} if it is not 250 * available. 251 */ 252 @Nullable() 253 public String getHost() 254 { 255 return ldapURL.getHost(); 256 } 257 258 259 260 /** 261 * Retrieves the port number for this LDAP URL. 262 * 263 * @return The port number for this LDAP URL. 264 */ 265 public int getPort() 266 { 267 return ldapURL.getPort(); 268 } 269 270 271 272 /** 273 * Retrieves the DN for this LDAP URL, if available. 274 * 275 * @return The DN for this LDAP URL, or {@code null} if it is not available. 276 */ 277 @Nullable() 278 public String getDN() 279 { 280 if (ldapURL.baseDNProvided()) 281 { 282 return ldapURL.getBaseDN().toString(); 283 } 284 else 285 { 286 return null; 287 } 288 } 289 290 291 292 /** 293 * Retrieves an enumeration of the names of the requested attributes for this 294 * LDAP URL, if available. 295 * 296 * @return An enumeration of the names of the requested attributes for this 297 * LDAP URL, or {@code null} if there are none. 298 */ 299 @Nullable() 300 public Enumeration<String> getAttributes() 301 { 302 final String[] attributes = ldapURL.getAttributes(); 303 if (attributes.length == 0) 304 { 305 return null; 306 } 307 else 308 { 309 return new IterableEnumeration<>(Arrays.asList(attributes)); 310 } 311 } 312 313 314 315 /** 316 * Retrieves an array of the names of the requested attributes for this LDAP 317 * URL, if available. 318 * 319 * @return An array of the names of the requested attributes for this LDAP 320 * URL, or {@code null} if there are none. 321 */ 322 @Nullable() 323 public String[] getAttributeArray() 324 { 325 final String[] attributes = ldapURL.getAttributes(); 326 if (attributes.length == 0) 327 { 328 return null; 329 } 330 else 331 { 332 return attributes; 333 } 334 } 335 336 337 338 /** 339 * Retrieves the search scope for the LDAP URL. 340 * 341 * @return The search scope for the LDAP URL. 342 */ 343 public int getScope() 344 { 345 return ldapURL.getScope().intValue(); 346 } 347 348 349 350 /** 351 * Retrieves the filter for this LDAP URL. 352 * 353 * @return The filter for this LDAP URL. 354 */ 355 @NotNull() 356 public String getFilter() 357 { 358 return ldapURL.getFilter().toString(); 359 } 360 361 362 363 /** 364 * Retrieves a hash code for this LDAP URL. 365 * 366 * @return A hash code for this LDAP URL. 367 */ 368 @Override() 369 public int hashCode() 370 { 371 return ldapURL.hashCode(); 372 } 373 374 375 376 /** 377 * Indicates whether the provided object is equal to this LDAP URL. 378 * 379 * @param o The object for which to make the determination. 380 * 381 * @return {@code true} if the provided object is equal to this LDAP URL, or 382 * {@code false} if not. 383 */ 384 @Override() 385 public boolean equals(@Nullable final Object o) 386 { 387 if (o == null) 388 { 389 return false; 390 } 391 392 if (o instanceof LDAPUrl) 393 { 394 return ldapURL.equals(((LDAPUrl) o).ldapURL); 395 } 396 397 return false; 398 } 399 400 401 402 /** 403 * Retrieves a string representation of this LDAP URL. 404 * 405 * @return A string representation of this LDAP URL. 406 */ 407 @NotNull() 408 public String getUrl() 409 { 410 return ldapURL.toString(); 411 } 412 413 414 415 /** 416 * Retrieves an {@link LDAPURL} object that is the equivalent of this LDAP 417 * URL. 418 * 419 * @return An {@code LDAPURL} object that is the equivalent of this LDAP URL. 420 */ 421 @NotNull() 422 public final LDAPURL toLDAPURL() 423 { 424 return ldapURL; 425 } 426 427 428 429 /** 430 * Retrieves a string representation of this LDAP URL. 431 * 432 * @return A string representation of this LDAP URL. 433 */ 434 @Override() 435 @NotNull() 436 public String toString() 437 { 438 return ldapURL.toString(); 439 } 440}