Debian Codegraph @

java-sdk/ietfldap/org/ietf/ldap/util/DN.java

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is mozilla.org code.
17 *
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1999
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38 package org.ietf.ldap.util;
39
40 import java.io.Serializable;
41 import java.util.StringTokenizer;
42 import java.util.Vector;
43
44 /**
45 * Objects of this class represent distinguished names (DN). A
46 * distinguished name is used to identify an entry in a directory.
47 * <P>
48 *
49 * The <CODE>org.ietf.ldap.LDAPDN</CODE> class uses this class
50 * internally. In most cases, when working with DNs in the
51 * LDAP Java classes, you should use the
52 * <CODE>org.ietf.ldap.LDAPDN</CODE> class.
53 * <P>
54 *
55 * The following DNs are examples of the different formats
56 * for DNs that may appear:
57 * <UL>
58 * <LI>uid=bjensen, ou=People, o=Airius.com
59 * (<A HREF="http://ds.internic.net/rfc/rfc1485.txt"
60 * TARGET="_blank">RFC 1485</A> format)
61 * <LI>o=Airius.com/ou=People/uid=bjensen (OSF format)
62 * </UL>
63 * <P>
64 *
65 * @version 1.0
66 * @see org.ietf.ldap.LDAPDN
67 */
68 public final class DN implements Serializable {
69
70 /**
71 * List of RDNs. DN consists of one or more RDNs.
72 * RDNs follow RFC1485 order.
73 */
74 private Vector<RDN> m_rdns = new Vector<>();
75
76 /**
77 * Type specifying a DN in the RFC format.
78 * <P>
79 *
80 * @see org.ietf.ldap.util.DN#getDNType
81 * @see org.ietf.ldap.util.DN#setDNType
82 */
83 public static int RFC = 0;
84
85 /**
86 * Type specifying a DN in the OSF format.
87 * <P>
88 *
89 * @see org.ietf.ldap.util.DN#getDNType
90 * @see org.ietf.ldap.util.DN#setDNType
91 */
92 public static int OSF = 1;
93
94 private int m_dnType = RFC;
95 static final long serialVersionUID = -8867457218975952548L;
96
97 /**
98 * Constructs an empty <CODE>DN</CODE> object.
99 */
100 public DN() {
101 }
102
103 /**
104 * Constructs a <CODE>DN</CODE> object from the specified
105 * distinguished name. The string representation of the DN
106 * can be in RFC 1485 or OSF format.
107 * <P>
108 *
109 * @param dn string representation of the distinguished name
110 */
111 public DN(String dn) {
112
113 String neutralDN = neutralizeEscapes(dn);
114 if (neutralDN == null) {
115 return; // malformed
116 }
117
118 // RFC1485
119 if (neutralDN.indexOf(',') != -1 || neutralDN.indexOf(';') != -1) {
120 parseRDNs(neutralDN, dn, ",;");
121 }
122 else if (dn.indexOf('/') != -1) { /* OSF */
123 m_dnType = OSF;
124 StringTokenizer st = new StringTokenizer(dn, "/");
125 Vector<RDN> rdns = new Vector<>();
126 while (st.hasMoreTokens()) {
127 String rdn = st.nextToken();
128 if (RDN.isRDN(rdn))
129 rdns.addElement(new RDN(rdn));
130 else
131 return;
132 }
133 /* reverse the RDNs order */
134 for (int i = rdns.size() - 1; i >= 0; i--) {
135 m_rdns.addElement(rdns.elementAt(i));
136 }
137 }
138 else if (RDN.isRDN(dn)) {
139 m_rdns.addElement(new RDN(dn));
140 }
141 }
142
143 /**
144 * Neutralize backslash escapes and quoted sequences for easy parsing.
145 * @return dn string with disabled escapes or null if malformed dn
146 */
147 private String neutralizeEscapes(String dn) {
148 String neutralDN = RDN.neutralizeEscapes(dn);
149 if (neutralDN == null) {
150 return null; // malformed
151 }
152
153 String dn2 = neutralDN.trim();
154 if (dn2.length() == 0) {
155 return neutralDN;
156 }
157 if (dn2.charAt(0) == ';' || dn2.charAt(0) == ',') {
158 return null; // malformed
159 }
160 int lastIdx = dn2.length() -1;
161 if (dn2.charAt(lastIdx) == ';' || dn2.charAt(lastIdx) == ',') {
162 return null; // malformed
163 }
164 return neutralDN;
165 }
166
167 /**
168 * Parse RDNs in the DN
169 */
170 private void parseRDNs(String neutralDN, String dn, String sep) {
171 int startIdx=0, endIdx=0;
172 RDN rdn = null;
173 StringTokenizer tok = new StringTokenizer(neutralDN, sep);
174 while (tok.hasMoreElements()) {
175 String neutralRDN = tok.nextToken();
176 endIdx = startIdx + neutralRDN.length();
177 rdn = new RDN (dn.substring(startIdx, endIdx));
178 if (rdn.getTypes() != null) {
179 m_rdns.addElement(rdn);
180 }
181 else { // malformed rdn
182 m_rdns.removeAllElements();
183 return;
184 }
185 startIdx = endIdx + 1;
186 }
187 }
188
189 /**
190 * Adds the specified relative distinguished name (RDN) to the
191 * beginning of the current DN.
192 * <P>
193 *
194 * @param rdn the relative distinguished name to add to the
195 * beginning of the current DN
196 * @see org.ietf.ldap.util.RDN
197 */
198 public void addRDNToFront(RDN rdn) {
199 m_rdns.insertElementAt(rdn, 0);
200 }
201
202 /**
203 * Adds the specified relative distinguished name (RDN) to the
204 * end of the current DN.
205 * <P>
206 *
207 * @param rdn the relative distinguished name to append to the current DN
208 * @see org.ietf.ldap.util.RDN
209 */
210 public void addRDNToBack(RDN rdn) {
211 m_rdns.addElement(rdn);
212 }
213
214 /**
215 * Adds the specified relative distinguished name (RDN) to the current DN.
216 * If the DN is in RFC 1485 format, the RDN is added to the beginning
217 * of the DN. If the DN is in OSF format, the RDN is appended to the
218 * end of the DN.
219 * <P>
220 *
221 * @param rdn the relative distinguished name to add to the current DN
222 * @see org.ietf.ldap.util.RDN
223 */
224 public void addRDN(RDN rdn) {
225 if (m_dnType == RFC) {
226 addRDNToFront(rdn);
227 } else {
228 addRDNToBack(rdn);
229 }
230 }
231
232 /**
233 * Sets the type of format used for the DN (RFC format or OSF format).
234 * <P>
235 *
236 * @param type one of the following constants: <CODE>DN.RFC</CODE>
237 * (to use the RFC format) or <CODE>DN.OSF</CODE> (to use the OSF format)
238 * @see org.ietf.ldap.util.DN#getDNType
239 * @see org.ietf.ldap.util.DN#RFC
240 * @see org.ietf.ldap.util.DN#OSF
241 */
242 public void setDNType(int type) {
243 m_dnType = type;
244 }
245
246 /**
247 * Gets the type of format used for the DN (RFC format or OSF format).
248 * <P>
249 *
250 * @return one of the following constants: <CODE>DN.RFC</CODE>
251 * (if the DN is in RFC format) or <CODE>DN.OSF</CODE>
252 * (if the DN is in OSF format).
253 * @see org.ietf.ldap.util.DN#setDNType
254 * @see org.ietf.ldap.util.DN#RFC
255 * @see org.ietf.ldap.util.DN#OSF
256 */
257 public int getDNType() {
258 return m_dnType;
259 }
260
261 /**
262 * Returns the number of components that make up the current DN.
263 * @return the number of components in this DN.
264 */
265 public int countRDNs() {
266 return m_rdns.size();
267 }
268
269 /**
270 * Returns a list of the components (<CODE>RDN</CODE> objects)
271 * that make up the current DN.
272 * @return a list of the components of this DN.
273 * @see org.ietf.ldap.util.RDN
274 */
275 public Vector<RDN> getRDNs() {
276 return m_rdns;
277 }
278
279 /**
280 * Returns an array of the individual components that make up
281 * the current distinguished name.
282 * @param noTypes specify <code>true</code> to remove the attribute type
283 * and equals sign (for example, "cn=") from each component
284 */
285 public String[] explodeDN(boolean noTypes) {
286 if (m_rdns.size() == 0)
287 return null;
288 String str[] = new String[m_rdns.size()];
289 for (int i = 0; i < m_rdns.size(); i++) {
290 if (noTypes)
291 str[i] = m_rdns.elementAt(i).getValue();
292 else
293 str[i] = m_rdns.elementAt(i).toString();
294 }
295 return str;
296 }
297
298 /**
299 * Determines if the DN is in RFC 1485 format.
300 * @return <code>true</code> if the DN is in RFC 1485 format.
301 */
302 public boolean isRFC() {
303 return (m_dnType == RFC);
304 }
305
306 /**
307 * Returns the DN in RFC 1485 format.
308 * @return the DN in RFC 1485 format.
309 */
310 public String toRFCString() {
311 String dn = "";
312 for (int i = 0; i < m_rdns.size(); i++) {
313 if (i != 0)
314 dn += ",";
315 dn = dn + m_rdns.elementAt(i).toString();
316 }
317 return dn;
318 }
319
320 /**
321 * Returns the DN in OSF format.
322 * @return the DN in OSF format.
323 */
324 public String toOSFString() {
325 String dn = "";
326 for (int i = 0; i < m_rdns.size(); i++) {
327 if (i != 0) {
328 dn = "/" + dn;
329 }
330 RDN rdn = m_rdns.elementAt(i);
331 dn = rdn.toString() + dn;
332 }
333 return dn;
334 }
335
336 /**
337 * Returns the string representation of the DN
338 * in its original format. (For example, if the
339 * <CODE>DN</CODE> object was constructed from a DN
340 * in RFC 1485 format, this method returns the DN
341 * in RFC 1485 format.
342 * @return the string representation of the DN.
343 */
344 public String toString() {
345 if (m_dnType == RFC)
346 return toRFCString();
347 else
348 return toOSFString();
349 }
350
351 /**
352 * Determines if the given string is an distinguished name or
353 * not.
354 * @param dn distinguished name
355 * @return <code>true</code> or <code>false</code>.
356 */
357 public static boolean isDN(String dn) {
358 if ( dn.equals( "" ) ) {
359 return true;
360 }
361 DN newdn = new DN(dn);
362 return (newdn.countRDNs() > 0);
363 }
364
365 /**
366 * Determines if the current DN is equal to the specified DN.
367 * @param dn DN to compare against the current DN
368 * @return <code>true</code> if the two DNs are the same.
369 */
370 public boolean equals(DN dn) {
371 return (dn.toRFCString().toUpperCase().equals(toRFCString().toUpperCase()));
372 }
373
374 /**
375 * Gets the parent DN for this DN.
376 * <P>
377 *
378 * For example, the following section of code gets the
379 * parent DN of "uid=bjensen, ou=People, o=Airius.com."
380 * <PRE>
381 * DN dn = new DN("uid=bjensen, ou=People, o=Airius.com");
382 * DN parent = dn.getParent();
383 * </PRE>
384 * The parent DN in this example is "ou=People, o=Airius.com".
385 * <P>
386 *
387 * @return DN of the parent of this DN.
388 */
389 public DN getParent() {
390 DN newdn = new DN();
391 for (int i = m_rdns.size() - 1; i > 0; i--) {
392 newdn.addRDN(m_rdns.elementAt(i));
393 }
394 return newdn;
395 }
396
397 /**
398 * Determines if the given DN is under the subtree defined by this DN.
399 * <P>
400 *
401 * For example, the following section of code determines if the
402 * DN specified by <CODE>dn1</CODE> is under the subtree specified
403 * by <CODE>dn2</CODE>.
404 * <PRE>
405 * DN dn1 = new DN("uid=bjensen, ou=People, o=Airius.com");
406 * DN dn2 = new DN("ou=People, o=Airius.com");
407 *
408 * boolean isContain = dn1.contains(dn2)
409 * </PRE>
410 * In this case, since "uid=bjensen, ou=People, o=Airius.com"
411 * is an entry under the subtree "ou=People, o=Airius.com",
412 * the value of <CODE>isContain</CODE> is true.
413 * <P>
414 *
415 * @param dn the DN of a subtree to check
416 * @return <code>true</code> if the current DN belongs to the subtree
417 * specified by <CODE>dn</CODE>.
418 * @deprecated Please use isDescendantOf() instead.
419 */
420
421 public boolean contains(DN dn) {
422
423 return isDescendantOf(dn);
424 }
425
426 /**
427 * Determines if this DN is a descendant of the given DN.
428 * <P>
429 *
430 * For example, the following section of code determines if the
431 * DN specified by <CODE>dn1</CODE> is a descendant of the DN specified
432 * by <CODE>dn2</CODE>.
433 * <PRE>
434 * DN dn1 = new DN("uid=bjensen, ou=People, o=Airius.com");
435 * DN dn2 = new DN("ou=People, o=Airius.com");
436 *
437 * boolean isDescendant = dn1.isDescendantOf(dn2)
438 * </PRE>
439 * In this case, since "uid=bjensen, ou=People, o=Airius.com"
440 * is an entry under the subtree "ou=People, o=Airius.com",
441 * the value of <CODE>isDescendant</CODE> is true.
442 * <P>
443 *
444 * In the case where the given DN is equal to this DN
445 * it returns false.
446 *
447 * @param dn the DN of a subtree to check
448 * @return <code>true</code> if the current DN is a descendant of the DN
449 * specified by <CODE>dn</CODE>.
450 */
451
452 public boolean isDescendantOf(DN dn) {
453
454 Vector<RDN> rdns1 = dn.m_rdns;
455
456 Vector<RDN> rdns2 = this.m_rdns;
457
458 int i = rdns1.size() - 1;
459 int j = rdns2.size() - 1;
460
461 if ((j < i) || (equals(dn) == true))
462 return false;
463
464 for (; i>=0 && j>=0; i--, j--) {
465 RDN rdn1 = rdns1.elementAt(i);
466 RDN rdn2 = rdns2.elementAt(j);
467 if (!rdn2.equals(rdn1)) {
468 return false;
469 }
470 }
471
472 return true;
473 }
474
475 /**
476 * Array of the characters that may be escaped in a DN.
477 */
478 public static final char[] ESCAPED_CHAR = {',', '+', '"', '\\', '<', '>', ';'};
479 }