LdapNamepublic class LdapName extends Object implements NameThis class represents a distinguished name as specified by
RFC 2253.
A distinguished name, or DN, is composed of an ordered list of
components called relative distinguished names, or RDNs.
Details of a DN's syntax are described in RFC 2253.
This class resolves a few ambiguities found in RFC 2253
as follows:
- RFC 2253 leaves the term "whitespace" undefined. The
ASCII space character 0x20 (" ") is used in its place.
- Whitespace is allowed on either side of ',', ';', '=', and '+'.
Such whitespace is accepted but not generated by this code,
and is ignored when comparing names.
- AttributeValue strings containing '=' or non-leading '#'
characters (unescaped) are accepted.
String names passed to LdapName or returned by it
use the full Unicode character set. They may also contain
characters encoded into UTF-8 with each octet represented by a
three-character substring such as "\\B4".
They may not, however, contain characters encoded into UTF-8 with
each octet represented by a single character in the string: the
meaning would be ambiguous.
LdapName will properly parse all valid names, but
does not attempt to detect all possible violations when parsing
invalid names. It is "generous" in accepting invalid names.
The "validity" of a name is determined ultimately when it
is supplied to an LDAP server, which may accept or
reject the name based on factors such as its schema information
and interoperability considerations.
When names are tested for equality, attribute types, both binary
and string values, are case-insensitive.
String values with different but equivalent usage of quoting,
escaping, or UTF8-hex-encoding are considered equal. The order of
components in multi-valued RDNs (such as "ou=Sales+cn=Bob") is not
significant.
The components of a LDAP name, that is, RDNs, are numbered. The
indexes of a LDAP name with n RDNs range from 0 to n-1.
This range may be written as [0,n).
The right most RDN is at index 0, and the left most RDN is at
index n-1. For example, the distinguished name:
"CN=Steve Kille, O=Isode Limited, C=GB" is numbered in the following
sequence ranging from 0 to 2: {C=GB, O=Isode Limited, CN=Steve Kille}. An
empty LDAP name is represented by an empty RDN list.
Concurrent multithreaded read-only access of an instance of
LdapName need not be synchronized.
Unless otherwise noted, the behavior of passing a null argument
to a constructor or method in this class will cause a
NullPointerException to be thrown. |
Fields Summary |
---|
private transient ArrayList | rdns | private transient String | unparsed | private static final long | serialVersionUID |
Constructors Summary |
---|
public LdapName(String name)Constructs an LDAP name from the given distinguished name.
unparsed = name;
parse();
| public LdapName(List rdns)Constructs an LDAP name given its parsed RDN components.
The indexing of RDNs in the list follows the numbering of
RDNs described in the class description.
// if (rdns instanceof ArrayList<Rdn>) {
// this.rdns = rdns.clone();
// } else if (rdns instanceof List<Rdn>) {
// this.rdns = new ArrayList<Rdn>(rdns);
// } else {
// throw IllegalArgumentException(
// "Invalid entries, list entries must be of type Rdn");
// }
this.rdns = new ArrayList(rdns.size());
for (int i = 0; i < rdns.size(); i++) {
Object obj = rdns.get(i);
if (!(obj instanceof Rdn)) {
throw new IllegalArgumentException("Entry:" + obj +
" not a valid type;list entries must be of type Rdn");
}
this.rdns.add(obj);
}
| private LdapName(String name, ArrayList rdns, int beg, int end)
unparsed = name;
// this.rdns = rdns.subList(beg, end);
List sList = rdns.subList(beg, end);
this.rdns = new ArrayList(sList);
|
Methods Summary |
---|
public javax.naming.Name | add(java.lang.String comp)Adds a single component to the end of this LDAP name.
return add(size(), comp);
| public javax.naming.Name | add(javax.naming.ldap.Rdn comp)Adds a single RDN to the end of this LDAP name.
return add(size(), comp);
| public javax.naming.Name | add(int posn, java.lang.String comp)Adds a single component at a specified position within this
LDAP name.
Components of this LDAP name at or after the index (if any) of the new
component are shifted up by one (away from index 0) to accommodate
the new component.
Rdn rdn = (new Rfc2253Parser(comp)).parseRdn();
rdns.add(posn, rdn);
unparsed = null; // no longer valid
return this;
| public javax.naming.Name | add(int posn, javax.naming.ldap.Rdn comp)Adds a single RDN at a specified position within this
LDAP name.
RDNs of this LDAP name at or after the index (if any) of the new
RDN are shifted up by one (away from index 0) to accommodate
the new RDN.
if (comp == null) {
throw new NullPointerException("Cannot set comp to null");
}
rdns.add(posn, comp);
unparsed = null; // no longer valid
return this;
| public javax.naming.Name | addAll(javax.naming.Name suffix)Adds the components of a name -- in order -- to the end of this name.
return addAll(size(), suffix);
| public javax.naming.Name | addAll(java.util.List suffixRdns)Adds the RDNs of a name -- in order -- to the end of this name.
return addAll(size(), suffixRdns);
| public javax.naming.Name | addAll(int posn, javax.naming.Name suffix)Adds the components of a name -- in order -- at a specified position
within this name. Components of this LDAP name at or after the
index (if any) of the first new component are shifted up
(away from index 0) to accomodate the new components.
unparsed = null; // no longer valid
if (suffix instanceof LdapName) {
LdapName s = (LdapName) suffix;
rdns.addAll(posn, s.rdns);
} else {
Enumeration comps = suffix.getAll();
while (comps.hasMoreElements()) {
rdns.add(posn++,
(new Rfc2253Parser((String) comps.nextElement()).
parseRdn()));
}
}
return this;
| public javax.naming.Name | addAll(int posn, java.util.List suffixRdns)Adds the RDNs of a name -- in order -- at a specified position
within this name. RDNs of this LDAP name at or after the
index (if any) of the first new RDN are shifted up (away from index 0) to
accomodate the new RDNs.
unparsed = null;
for (int i = 0; i < suffixRdns.size(); i++) {
Object obj = suffixRdns.get(i);
if (!(obj instanceof Rdn)) {
throw new IllegalArgumentException("Entry:" + obj +
" not a valid type;suffix list entries must be of type Rdn");
}
rdns.add(i + posn, obj);
}
return this;
| public java.lang.Object | clone()Generates a new copy of this name.
Subsequent changes to the components of this name will not
affect the new copy, and vice versa.
return new LdapName(unparsed, rdns, 0, rdns.size());
| public int | compareTo(java.lang.Object obj)Compares this LdapName with the specified Object for order.
Returns a negative integer, zero, or a positive integer as this
Name is less than, equal to, or greater than the given Object.
If obj is null or not an instance of LdapName, ClassCastException
is thrown.
Ordering of LDAP names follows the lexicographical rules for
string comparison, with the extension that this applies to all
the RDNs in the LDAP name. All the RDNs are lined up in their
specified order and compared lexicographically.
See {@link Rdn#compareTo(Object obj) Rdn.compareTo(Object obj)}
for RDN comparison rules.
If this LDAP name is lexicographically lesser than obj,
a negative number is returned.
If this LDAP name is lexicographically greater than obj,
a positive number is returned.
if (!(obj instanceof LdapName)) {
throw new ClassCastException("The obj is not a LdapName");
}
// check possible shortcuts
if (obj == this) {
return 0;
}
LdapName that = (LdapName) obj;
if (unparsed != null && unparsed.equalsIgnoreCase(
that.unparsed)) {
return 0;
}
// Compare RDNs one by one, lexicographically.
int minSize = Math.min(rdns.size(), that.rdns.size());
for (int i = 0; i < minSize; i++) {
// Compare a single pair of RDNs.
Rdn rdn1 = (Rdn)rdns.get(i);
Rdn rdn2 = (Rdn)that.rdns.get(i);
int diff = rdn1.compareTo(rdn2);
if (diff != 0) {
return diff;
}
}
return (rdns.size() - that.rdns.size()); // longer DN wins
| private boolean | doesListMatch(int beg, int end, java.util.List rdns)
for (int i = beg; i < end; i++) {
if (!this.rdns.get(i).equals(rdns.get(i - beg))) {
return false;
}
}
return true;
| public boolean | endsWith(javax.naming.Name n)Determines whether this LDAP name ends with a specified
LDAP name suffix.
A name n is a suffix if it is equal to
getSuffix(size()-n.size())--in other words this LDAP
name ends with 'n'. If n is null or not a RFC2253 formatted name
as described in the class description, false is returned.
if (n == null) {
return false;
}
int len1 = rdns.size();
int len2 = n.size();
return (len1 >= len2 &&
matches(len1 - len2, len1, n));
| public boolean | endsWith(java.util.List rdns)Determines whether the specified RDN sequence forms a suffix of this
LDAP name. Returns true if this LdapName is at least as long as rdns,
and for every position p in the range [size() - rdns.size(), size())
the component getRdn(p) matches rdns.get(p). Returns false otherwise.
If rdns is null, false is returned.
if (rdns == null) {
return false;
}
int len1 = this.rdns.size();
int len2 = rdns.size();
return (len1 >= len2 &&
doesListMatch(len1 - len2, len1, rdns));
| public boolean | equals(java.lang.Object obj)Determines whether two LDAP names are equal.
If obj is null or not an LDAP name, false is returned.
Two LDAP names are equal if each RDN in one is equal
to the corresponding RDN in the other. This implies
both have the same number of RDNs, and each RDN's
equals() test against the corresponding RDN in the other
name returns true. See {@link Rdn#equals(Object obj)}
for a definition of RDN equality.
// check possible shortcuts
if (obj == this) {
return true;
}
if (!(obj instanceof LdapName)) {
return false;
}
LdapName that = (LdapName) obj;
if (rdns.size() != that.rdns.size()) {
return false;
}
if (unparsed != null && unparsed.equalsIgnoreCase(
that.unparsed)) {
return true;
}
// Compare RDNs one by one for equality
for (int i = 0; i < rdns.size(); i++) {
// Compare a single pair of RDNs.
Rdn rdn1 = (Rdn) rdns.get(i);
Rdn rdn2 = (Rdn) that.rdns.get(i);
if (!rdn1.equals(rdn2)) {
return false;
}
}
return true;
| public java.lang.String | get(int posn)Retrieves a component of this LDAP name as a string.
return rdns.get(posn).toString();
| public java.util.Enumeration | getAll()Retrieves the components of this name as an enumeration
of strings. The effect of updates to this name on this enumeration
is undefined. If the name has zero components, an empty (non-null)
enumeration is returned.
The order of the components returned by the enumeration is same as
the order in which the components are numbered as described in the
class description.
final Iterator iter = rdns.iterator();
return new Enumeration<String>() {
public boolean hasMoreElements() {
return iter.hasNext();
}
public String nextElement() {
return iter.next().toString();
}
};
| public javax.naming.Name | getPrefix(int posn)Creates a name whose components consist of a prefix of the
components of this LDAP name.
Subsequent changes to this name will not affect the name
that is returned and vice versa.
try {
return new LdapName(null, rdns, 0, posn);
} catch (IllegalArgumentException e) {
throw new IndexOutOfBoundsException(
"Posn: " + posn + ", Size: "+ rdns.size());
}
| public javax.naming.ldap.Rdn | getRdn(int posn)Retrieves an RDN of this LDAP name as an Rdn.
return (Rdn) rdns.get(posn);
| public java.util.List | getRdns()Retrieves the list of relative distinguished names.
The contents of the list are unmodifiable.
The indexing of RDNs in the returned list follows the numbering of
RDNs as described in the class description.
If the name has zero components, an empty list is returned.
return Collections.unmodifiableList(rdns);
| public javax.naming.Name | getSuffix(int posn)Creates a name whose components consist of a suffix of the
components in this LDAP name.
Subsequent changes to this name do not affect the name that is
returned and vice versa.
try {
return new LdapName(null, rdns, posn, rdns.size());
} catch (IllegalArgumentException e) {
throw new IndexOutOfBoundsException(
"Posn: " + posn + ", Size: "+ rdns.size());
}
| public int | hashCode()Computes the hash code of this LDAP name.
The hash code is the sum of the hash codes of individual RDNs
of this name.
// Sum up the hash codes of the components.
int hash = 0;
// For each RDN...
for (int i = 0; i < rdns.size(); i++) {
Rdn rdn = (Rdn) rdns.get(i);
hash += rdn.hashCode();
}
return hash;
| public boolean | isEmpty()Determines whether this LDAP name is empty.
An empty name is one with zero components.
return rdns.isEmpty();
| private boolean | matches(int beg, int end, javax.naming.Name n)
if (n instanceof LdapName) {
LdapName ln = (LdapName) n;
return doesListMatch(beg, end, ln.rdns);
} else {
for (int i = beg; i < end; i++) {
Rdn rdn;
String rdnString = n.get(i - beg);
try {
rdn = (new Rfc2253Parser(rdnString)).parseRdn();
} catch (InvalidNameException e) {
return false;
}
if (!rdn.equals(rdns.get(i))) {
return false;
}
}
}
return true;
| private void | parse()
// rdns = (ArrayList<Rdn>) (new RFC2253Parser(unparsed)).getDN();
rdns = (ArrayList) (new Rfc2253Parser(unparsed)).parseDn();
| private void | readObject(java.io.ObjectInputStream s)
s.defaultReadObject();
unparsed = (String)s.readObject();
try {
parse();
} catch (InvalidNameException e) {
// shouldn't happen
throw new java.io.StreamCorruptedException(
"Invalid name: " + unparsed);
}
| public java.lang.Object | remove(int posn)Removes a component from this LDAP name.
The component of this name at the specified position is removed.
Components with indexes greater than this position (if any)
are shifted down (toward index 0) by one.
unparsed = null; // no longer valid
return rdns.remove(posn).toString();
| public int | size()Retrieves the number of components in this LDAP name.
return rdns.size();
| public boolean | startsWith(javax.naming.Name n)Determines whether this LDAP name starts with a specified LDAP name
prefix.
A name n is a prefix if it is equal to
getPrefix(n.size())--in other words this LDAP
name starts with 'n'. If n is null or not a RFC2253 formatted name
as described in the class description, false is returned.
if (n == null) {
return false;
}
int len1 = rdns.size();
int len2 = n.size();
return (len1 >= len2 &&
matches(0, len2, n));
| public boolean | startsWith(java.util.List rdns)Determines whether the specified RDN sequence forms a prefix of this
LDAP name. Returns true if this LdapName is at least as long as rdns,
and for every position p in the range [0, rdns.size()) the component
getRdn(p) matches rdns.get(p). Returns false otherwise. If rdns is
null, false is returned.
if (rdns == null) {
return false;
}
int len1 = this.rdns.size();
int len2 = rdns.size();
return (len1 >= len2 &&
doesListMatch(0, len2, rdns));
| public java.lang.String | toString()Returns a string representation of this LDAP name in a format
defined by RFC 2253
and described in the class description. If the name has zero
components an empty string is returned.
if (unparsed != null) {
return unparsed;
}
StringBuilder builder = new StringBuilder();
int size = rdns.size();
if ((size - 1) >= 0) {
builder.append((Rdn) rdns.get(size - 1));
}
for (int next = size - 2; next >= 0; next--) {
builder.append(',");
builder.append((Rdn) rdns.get(next));
}
unparsed = builder.toString();
return unparsed;
| private void | writeObject(java.io.ObjectOutputStream s)Serializes only the unparsed DN, for compactness and to avoid
any implementation dependency.
s.defaultWriteObject();
s.writeObject(toString());
|
|