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