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