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