/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is mozilla.org code. * * The Initial Developer of the Original Code is * Netscape Communications Corporation. * Portions created by the Initial Developer are Copyright (C) 2001 * the Initial Developer. All Rights Reserved. * * Contributor(s): * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ package org.ietf.ldap; import java.net.MalformedURLException; import java.util.Enumeration; import java.util.NoSuchElementException; import java.util.StringTokenizer; import java.util.Vector; import org.ietf.ldap.factory.JSSESocketFactory; /** * Represents an LDAP URL. The complete specification for LDAP URLs is in * RFC 1959. In addition, the secure ldap (ldaps://) is also * supported. LDAP URLs have the following format: * *
* "ldap[s]://" [ hostName [":" portNumber] ] "/" * distinguishedName * ["?" attributeList ["?" scope * "?" filterString ] ] ** where *
*
*
hostName and portNumber
* identify the location of the LDAP server.*
distinguishedName is the name of an entry
* within the given directory (the entry represents the starting point
* of the search)*
attributeList contains a list of attributes
* to retrieve (if null, fetch all attributes). This is a comma-delimited
* list of attribute names.*
scope is one of the following:
* base indicates that this is a search only for the
* specified entry
* one indicates that this is a search for matching entries
* one level under the specified entry (and not including the entry itself)
* sub indicates that this is a search for matching entries
* at all levels under the specified entry (including the entry itself)
*
* If not specified, scope is base by
* default.
*
filterString is a human-readable representation
* of the search criteria. This value is used only for one-level or subtree
* searches.*
* Note that if scope and filterString
* are not specified, an LDAP URL identifies exactly one entry in the
* directory.
* The same encoding rules for other URLs (e.g. HTTP) apply for LDAP
* URLs. Specifically, any "illegal" characters are escaped with
*
* If the factory is not explicitly specified with
* %HH, where HH represent the
* two hex digits which correspond to the ASCII value of the character.
* This encoding is only legal (or necessary) on the DN and filter portions
* of the URL.
*
* @version 1.0
*/
public class LDAPUrl implements java.io.Serializable {
static final long serialVersionUID = -3245440798565713640L;
public static String defaultFilter = "(objectClass=*)";
private String _hostName;
private int _portNumber;
private String _DN;
private VectorLDAPUrl.setSocketFactory, the method will
* attempt the determine the default factory based on the
* available factories in the org.ietf.ldap.factory package.
*
* @return the socket factory to be used for ldaps:// URLs
*/
public static LDAPSocketFactory getSocketFactory() {
if (_factory == null) {
// No factory explicity set, try to determine
// the default one.
try {
// First try iPlanet JSSSocketFactory
Class> c = Class.forName("org.ietf.ldap.factory.JSSSocketFactory");
_factory = (LDAPSocketFactory) c.newInstance();
}
catch (Throwable e) {
}
if (_factory != null) {
return _factory;
}
try {
// then try Sun JSSESocketFactory
_factory = new JSSESocketFactory((String[])null);
}
catch (Throwable e) {
}
}
return _factory;
}
/**
* Returns true if the secure ldap protocol is used.
* @return true if ldaps is used.
*/
public boolean isSecure() {
return _secure;
}
/**
* Sets the socket factory to be used for ldaps:// URLs.
* Overrides the default factory assigned by the LDAPUrl
* class.
* @param the socket factory to be used for ldaps:// URLs
* @see org.ietf.ldap.LDAPUrl#getSocketFactory
*/
public static void setSocketFactory( LDAPSocketFactory factory ) {
_factory = factory;
}
/**
* Sets the protocol to ldaps or ldap
*
* @param secure true for ldaps
*/
public void setSecure( boolean secure ) {
_secure = secure;
initialize( _hostName, _portNumber, _DN, getAttributes(), _scope,
_filter, _extensions );
}
/**
* Returns the URL in String format
*
* @return the URL in String format
*/
public String toString() {
return _URL;
}
/**
* Parse URL as defined in RFC 1959. Beyond the RFC, the secure ldap
* (ldaps) is also supported.
*/
private void parseUrl(String url) throws MalformedURLException {
StringTokenizer urlParser = new StringTokenizer (url, ":/?", true);
String currentToken;
String str = null;
try {
currentToken = urlParser.nextToken();
if (currentToken.equalsIgnoreCase ("LDAPS")) {
_secure = true;
}
else if (!currentToken.equalsIgnoreCase ("LDAP")) {
throw new MalformedURLException ();
}
currentToken = urlParser.nextToken();
if (!currentToken.equals(":")) {
throw new MalformedURLException ();
}
currentToken = urlParser.nextToken();
if (!currentToken.equals("/")) {
throw new MalformedURLException ();
}
currentToken = urlParser.nextToken();
if (!currentToken.equals("/")) {
throw new MalformedURLException ();
}
currentToken = urlParser.nextToken();
}
catch (NoSuchElementException e) {
throw new MalformedURLException ();
}
// host-port
if (currentToken.equals ("/")) {
_hostName = null;
_portNumber = _secure ? DEFAULT_SECURE_PORT : LDAPConnection.DEFAULT_PORT;
} else if (currentToken.equals (":")) {
// port number without host name is not allowed
throw new MalformedURLException ("No hostname");
} else if (currentToken.equals ("?")) {
throw new MalformedURLException ("No host[:port]");
} else {
_hostName = currentToken;
if (urlParser.countTokens() == 0) {
_portNumber = _secure ? DEFAULT_SECURE_PORT : LDAPConnection.DEFAULT_PORT;
return;
}
currentToken = urlParser.nextToken (); // either ":" or "/"
if (currentToken.equals (":")) {
try {
_portNumber = Integer.parseInt (urlParser.nextToken());
} catch (NumberFormatException nf) {
throw new MalformedURLException ("Port not a number");
} catch (NoSuchElementException ex) {
throw new MalformedURLException ("No port number");
}
if (urlParser.countTokens() == 0) {
return;
}
else if (! urlParser.nextToken().equals("/")) {
throw new MalformedURLException ();
}
} else if (currentToken.equals ("/")) {
_portNumber = _secure ? DEFAULT_SECURE_PORT : LDAPConnection.DEFAULT_PORT;
} else {
// expecting ":" or "/"
throw new MalformedURLException ();
}
}
// DN
if (!urlParser.hasMoreTokens ()) {
return;
}
_DN = decode(readNextConstruct(urlParser));
if (_DN.equals("?")) {
_DN = "";
}
else if (_DN.equals("/")) {
throw new MalformedURLException ();
}
// attribute
if (!urlParser.hasMoreTokens ()) {
return;
}
str = readNextConstruct(urlParser);
if (!str.equals("?")) {
StringTokenizer attributeParser = new
StringTokenizer (decode(str), ", ");
_attributes = new Vectortrue if the two are equivalent
*/
public boolean equals( LDAPUrl url ) {
if ( getHost() == null ) {
if ( url.getHost() != null ) {
return false;
}
} else if ( !getHost().equals( url.getHost() ) ) {
return false;
}
if ( getPort() != url.getPort() ) {
return false;
}
if ( getDN() == null ) {
if ( url.getDN() != null ) {
return false;
}
} else if ( !getDN().equals( url.getDN() ) ) {
return false;
}
if ( getFilter() == null ) {
if ( url.getFilter() != null ) {
return false;
}
} else if ( !getFilter().equals( url.getFilter() ) ) {
return false;
}
if ( getScope() != url.getScope() ) {
return false;
}
if ( _attributes == null ) {
if ( url._attributes != null ) {
return false;
}
} else if ( _attributes.size() != url._attributes.size() ) {
return false;
} else {
for( int i = 0; i < _attributes.size(); i++ ) {
if ( _attributes.elementAt( i ) !=
url._attributes.elementAt( i ) ) {
return false;
}
}
}
return true;
}
}