Debian Codegraph @

java-sdk/ietfldap/org/ietf/ldap/LDAPAttributeSet.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;
39
40 import java.io.Serializable;
41 import java.util.Collection;
42 import java.util.HashMap;
43 import java.util.Iterator;
44 import java.util.Set;
45 import java.util.StringTokenizer;
46
47 /**
48 * Represents a set of attributes (for example, the set of attributes
49 * in an entry).
50 *
51 * @version 1.0
52 * @see org.ietf.ldap.LDAPAttribute
53 */
54 public class LDAPAttributeSet implements Cloneable, Serializable, Set<LDAPAttribute> {
55 static final long serialVersionUID = 5018474561697778100L;
56 HashMap<String, LDAPAttribute> _attrHash = null;
57 LDAPAttribute[] _attrs = new LDAPAttribute[0];
58 /* If there are less attributes than this in the set, it's not worth
59 creating a Hashtable - faster and cheaper most likely to do string
60 comparisons. Most applications fetch attributes once only, anyway */
61 static final int ATTR_COUNT_REQUIRES_HASH = 5;
62
63 /**
64 * Constructs a new set of attributes. This set is initially empty.
65 */
66 public LDAPAttributeSet() {
67 // For now, always create the hashtable
68 prepareHashtable( true );
69 }
70
71 /**
72 * Constructs an attribute set.
73 * @param attrs the list of attributes
74 */
75 public LDAPAttributeSet( LDAPAttribute[] attrs ) {
76 this();
77 if ( attrs == null ) {
78 attrs = new LDAPAttribute[0];
79 }
80 _attrs = attrs;
81 if ( _attrs.length > 0 ) {
82 for( int i = 0; i < _attrs.length; i++ ) {
83 add( _attrs[i] );
84 }
85 }
86 }
87
88 /**
89 * Removes all mappings from this attribute set
90 */
91 public void clear() {
92 _attrHash = null;
93 if ( _attrs.length > 0 ) {
94 _attrs = new LDAPAttribute[0];
95 }
96 }
97
98 /**
99 * Adds the specified attribute to this attribute set, overriding
100 * any previous definition with the same attribute name
101 *
102 * @param attr attribute to add to this set
103 * @return true if this set changed as a result of the call
104 */
105 public synchronized boolean add( LDAPAttribute attr ) {
106 if ( contains( attr ) ) {
107 return false;
108 }
109 LDAPAttribute[] vals = new LDAPAttribute[_attrs.length+1];
110 for ( int i = 0; i < _attrs.length; i++ ) {
111 vals[i] = _attrs[i];
112 }
113 vals[_attrs.length] = attr;
114 _attrs = vals;
115 if ( _attrHash != null ) {
116 _attrHash.put( attr.getName().toLowerCase(), attr );
117 }
118 return true;
119 }
120
121 /**
122 * Adds the collection of attributes to this attribute set, overriding
123 * any previous definition with the same attribute names
124 *
125 * @param attrs attributes to add to this set
126 * @return true if any attribute was added
127 */
128 public synchronized boolean addAll( Collection<? extends LDAPAttribute> attrs ) {
129 if ( attrs == null ) {
130 return false;
131 }
132 boolean present = true;
133 Iterator<? extends LDAPAttribute> it = attrs.iterator();
134 while( it.hasNext() ) {
135 LDAPAttribute attr = it.next();
136 if ( !contains( attr ) ) {
137 present = true;
138 add( attr );
139 }
140 }
141 return !present;
142 }
143
144 /**
145 * Returns a deep copy of this attribute set
146 *
147 * @return a deep copy of this attribute set
148 */
149 public synchronized Object clone() {
150 try {
151 LDAPAttributeSet attributeSet = new LDAPAttributeSet();
152 attributeSet._attrs = new LDAPAttribute[_attrs.length];
153 for (int i = 0; i < _attrs.length; i++) {
154 attributeSet._attrs[i] = new LDAPAttribute(_attrs[i]);
155 }
156 return attributeSet;
157 } catch (Exception e) {
158 return null;
159 }
160 }
161
162 /**
163 * Returns true if this attribute set contains the specified attribute
164 *
165 * @param attr attribute whose presence in this set is to be tested
166 * @return true if the attribute set contains the specified attribute
167 */
168 public boolean contains( Object attr ) {
169 if ( !(attr instanceof LDAPAttribute) ) {
170 return false;
171 } else {
172 if ( _attrHash != null ) {
173 return _attrHash.containsValue( attr );
174 } else {
175 for ( int i = 0; i < _attrs.length; i++ ) {
176 if ( attr.equals(_attrs[i]) ) {
177 return true;
178 }
179 }
180 }
181 return false;
182 }
183 }
184
185 /**
186 * Returns true if this attribute set contains all the specified attributes
187 *
188 * @param attrs attributes whose presence in this set is to be tested
189 * @return true if the attribute set contains the specified attributes
190 */
191 public boolean containsAll( Collection<?> attrs ) {
192 if ( _attrHash != null ) {
193 Iterator<?> it = attrs.iterator();
194 while( it.hasNext() ) {
195 if ( !_attrHash.containsValue( it.next() ) ) {
196 return false;
197 }
198 }
199 return true;
200 } else {
201 return false;
202 }
203 }
204
205 /**
206 * Returns true if this attribute set contains the specified attribute name
207 *
208 * @param attrName attribute name whose presence in this set is to be tested
209 * @return true if the attribute set contains the specified attribute
210 */
211 public boolean containsKey( Object attrName ) {
212 if ( !(attrName instanceof String) ) {
213 return false;
214 } else {
215 return ( getAttribute( (String)attrName ) != null );
216 }
217 }
218
219 /**
220 * Returns true if this attribute set equals a specified set
221 *
222 * @param attrSet attribute set to compare to
223 * @return true if this attribute set equals a specified set
224 */
225 public boolean equals( Object attrSet ) {
226 if ( !(attrSet instanceof LDAPAttributeSet) ) {
227 return false;
228 }
229 return ((LDAPAttributeSet)attrSet)._attrHash.equals( _attrHash );
230 }
231
232 /**
233 * Returns the hash code for this attribute set
234 *
235 * @return the hash code for this attribute set
236 */
237 public int hashCode() {
238 return _attrHash.hashCode();
239 }
240
241 /**
242 * Returns true if there are no attributes in this attribute set
243 *
244 * @return true if there are no attributes in this attribute set
245 */
246 public boolean isEmpty() {
247 return ( _attrs.length < 1 );
248 }
249
250 /**
251 * Returns an iterator over the attributes in this attribute set
252 *
253 * @return an iterator over the attributes in this attribute set
254 */
255 public Iterator<LDAPAttribute> iterator() {
256 return _attrHash.values().iterator();
257 }
258
259 /**
260 * Removes the specified attribute
261 *
262 * @param attr the attribute to remove
263 * @return true if the attribute was removed
264 */
265 public boolean remove( Object attr ) {
266 if ( !(attr instanceof LDAPAttribute) ) {
267 return false;
268 }
269 boolean present = contains( attr );
270 if ( present ) {
271 synchronized(this) {
272 LDAPAttribute[] vals = new LDAPAttribute[_attrs.length-1];
273 int j = 0;
274 for (int i = 0; i < _attrs.length; i++) {
275 if ( !attr.equals(_attrs[i] ) ) {
276 vals[j++] = _attrs[i];
277 }
278 }
279 if (_attrHash != null) {
280 _attrHash.remove(
281 ((LDAPAttribute)attr).getName().toLowerCase() );
282 }
283 _attrs = vals;
284 }
285 }
286 return present;
287 }
288
289 /**
290 * Removes the specified attributes
291 *
292 * @param attrs the attributes to remove
293 * @return true if any attribute was removed
294 */
295 public boolean removeAll( Collection<?> attrs ) {
296 if ( attrs == null ) {
297 return false;
298 }
299 boolean present = true;
300 Iterator<?> it = attrs.iterator();
301 while( it.hasNext() ) {
302 Object attr = it.next();
303 if ( !contains( attr ) ) {
304 present = true;
305 remove( attr );
306 }
307 }
308 return !present;
309 }
310
311 /**
312 * Retains only the attributes in this set that are contained in the
313 * specified collection
314 *
315 * @param attrs attributes to retain
316 * @return true if the attribute set was changed as a result of the
317 * operation
318 */
319 public boolean retainAll( Collection<?> attrs ) {
320 HashMap<String, LDAPAttribute> newmap = new HashMap<>();
321 Iterator<?> it = attrs.iterator();
322 while( it.hasNext() ) {
323 Object attr = it.next();
324 if ( attr instanceof LDAPAttribute ) {
325 newmap.put( ((LDAPAttribute)attr).getName().toLowerCase(),
326 (LDAPAttribute)attr );
327 }
328 }
329 if ( newmap.equals( _attrHash ) ) {
330 return false;
331 } else {
332 _attrHash = newmap;
333 _attrs = _attrHash.values().toArray(
334 new LDAPAttribute[0] );
335 return true;
336 }
337 }
338
339 /**
340 * Returns the number of attributes in this set.
341 * @return number of attributes in this attribute set.
342 */
343 public int size() {
344 return _attrs.length;
345 }
346
347 /**
348 * Returns the attributes of the set as an array
349 *
350 * @return the attributes of the set as an array
351 */
352 public Object[] toArray() {
353 return _attrs;
354 }
355
356 /**
357 * Returns the attributes of the set as an array
358 *
359 * @param attrs an attribute array to fill with the attributes of this
360 * set. If the array is not large enough, a new array is allocated.
361 * @return the attributes of the set as an array
362 */
363 public Object[] toArray( Object[] attrs ) {
364 if ( !(attrs instanceof LDAPAttribute[]) ) {
365 throw new ArrayStoreException(
366 "Must provide an LDAPAttribute array" );
367 } else if ( attrs.length >= _attrs.length ) {
368 for( int i = 0; i < _attrs.length; i++ ) {
369 attrs[i] = _attrs[i];
370 }
371 return attrs;
372 } else {
373 return _attrs;
374 }
375 }
376
377 /**
378 * Creates a new attribute set containing only the attributes
379 * that have the specified subtypes.
380 * <P>
381 *
382 * For example, suppose an attribute set contains the following attributes:
383 * <P>
384 *
385 * <PRE>
386 * cn
387 * cn;lang-ja
388 * sn;phonetic;lang-ja
389 * sn;lang-us
390 * </PRE>
391 *
392 * If you call the <CODE>getSubset</CODE> method and pass
393 * <CODE>lang-ja</CODE> as the argument, the method returns
394 * an attribute set containing the following attributes:
395 * <P>
396 *
397 * <PRE>
398 * cn;lang-ja
399 * sn;phonetic;lang-ja
400 * </PRE>
401 *
402 * @param subtype semi-colon delimited list of subtypes
403 * to find within attribute names.
404 * For example:
405 * <PRE>
406 * "lang-ja" // Only Japanese language subtypes
407 * "binary" // Only binary subtypes
408 * "binary;lang-ja" // Only Japanese language subtypes
409 * which also are binary
410 * </PRE>
411 * @return attribute set containing the attributes that have
412 * the specified subtypes.
413 * @see org.ietf.ldap.LDAPAttribute
414 * @see org.ietf.ldap.LDAPAttributeSet#getAttribute
415 * @see org.ietf.ldap.LDAPEntry#getAttributeSet
416 */
417 public LDAPAttributeSet getSubset( String subtype ) {
418 LDAPAttributeSet attrs = new LDAPAttributeSet();
419 if ( subtype == null )
420 return attrs;
421 StringTokenizer st = new StringTokenizer(subtype, ";");
422 if( st.countTokens() < 1 )
423 return attrs;
424 String[] searchTypes = new String[st.countTokens()];
425 int i = 0;
426 while( st.hasMoreTokens() ) {
427 searchTypes[i] = st.nextToken();
428 i++;
429 }
430 Iterator<LDAPAttribute> it = _attrHash.values().iterator();
431 while( it.hasNext() ) {
432 LDAPAttribute attr = it.next();
433 if( attr.hasSubtypes( searchTypes ) )
434 attrs.add( new LDAPAttribute( attr ) );
435 }
436 return attrs;
437 }
438
439 /**
440 * Returns a single attribute that exactly matches the specified attribute
441 * name.
442 * @param attrName name of attribute to return
443 * For example:
444 *<PRE>
445 * "cn" // Only a non-subtyped version of cn
446 * "cn;lang-ja" // Only a Japanese version of cn
447 *</PRE>
448 * @return attribute that has exactly the same name, or null
449 * (if no attribute in the set matches the specified name).
450 * @see org.ietf.ldap.LDAPAttribute
451 */
452 public LDAPAttribute getAttribute( String attrName ) {
453 if ( attrName == null ) {
454 return null;
455 } else if ( _attrHash != null ) {
456 return _attrHash.get( attrName.toLowerCase() );
457 } else {
458 for ( int i = 0; i < _attrs.length; i++ ) {
459 if ( attrName.equalsIgnoreCase(_attrs[i].getName()) ) {
460 return _attrs[i];
461 }
462 }
463 return null;
464 }
465 }
466
467 /**
468 * Prepares hashtable for fast attribute lookups.
469 */
470 private void prepareHashtable( boolean force ) {
471 if ( (_attrHash == null) &&
472 (force || (_attrs.length >= ATTR_COUNT_REQUIRES_HASH)) ) {
473 if ( _attrHash == null ) {
474 _attrHash = new HashMap<>();
475 } else {
476 _attrHash.clear();
477 }
478 for ( int j = 0; j < _attrs.length; j++ ) {
479 _attrHash.put( _attrs[j].getName().toLowerCase(), _attrs[j] );
480 }
481 }
482 }
483
484 /**
485 * Returns the subtype that matches the attribute name specified
486 * by <CODE>attrName</CODE> and the language specificaton identified
487 * by <CODE>lang</CODE>.
488 * <P>
489 *
490 * If no attribute in the set has the specified name and subtype,
491 * the method returns <CODE>null</CODE>.
492 *
493 * Attributes containing subtypes other than <CODE>lang</CODE>
494 * (for example, <CODE>cn;binary</CODE>) are returned only if
495 * they contain the specified <CODE>lang</CODE> subtype and if
496 * the set contains no attribute having only the <CODE>lang</CODE>
497 * subtype. (For example, <CODE>getAttribute( "cn", "lang-ja" )</CODE>
498 * returns <CODE>cn;lang-ja;phonetic</CODE> only if the
499 * <CODE>cn;lang-ja</CODE> attribute does not exist.)
500 * <P>
501 *
502 * If null is specified for the <CODE>lang</CODE> argument,
503 * calling this method is the same as calling the
504 * <CODE>getAttribute(attrName)</CODE> method.
505 * <P>
506 *
507 * For example, suppose an entry contains only the following attributes:
508 * <P>
509 * <UL>
510 * <LI><CODE>cn;lang-en</CODE>
511 * <LI><CODE>cn;lang-ja-JP-kanji</CODE>
512 * <LI><CODE>sn</CODE>
513 * </UL>
514 * <P>
515 *
516 * Calling the following methods will return the following values:
517 * <P>
518 * <UL>
519 * <LI><CODE>getAttribute( "cn" )</CODE> returns <CODE>null</CODE>.
520 * <LI><CODE>getAttribute( "sn" )</CODE> returns the "<CODE>sn</CODE>" attribute.
521 * <LI><CODE>getAttribute( "cn", "lang-en-us" )</CODE> returns the "<CODE>cn;lang-en</CODE>" attribute.
522 * <LI><CODE>getAttribute( "cn", "lang-en" )</CODE> returns the "<CODE>cn;lang-en</CODE>" attribute.
523 * <LI><CODE>getAttribute( "cn", "lang-ja" )</CODE> returns <CODE>null</CODE>.
524 * <LI><CODE>getAttribute( "sn", "lang-en" )</CODE> returns the "<CODE>sn</CODE>" attribute.
525 *</UL>
526 * <P>
527 * @param attrName name of attribute to find in the entry
528 * @param lang a language specification
529 * @return the attribute that matches the base name and that best
530 * matches any specified language subtype.
531 * @see org.ietf.ldap.LDAPAttribute
532 */
533 public LDAPAttribute getAttribute( String attrName, String lang ) {
534 if ( (lang == null) || (lang.length() < 1) )
535 return getAttribute( attrName );
536
537 String langLower = lang.toLowerCase();
538 if ((langLower.length() < 5) ||
539 ( !langLower.substring( 0, 5 ).equals( "lang-" ) )) {
540 return null;
541 }
542 StringTokenizer st = new StringTokenizer( langLower, "-" );
543 // Skip first token, which is "lang-"
544 st.nextToken();
545 String[] langComponents = new String[st.countTokens()];
546 int i = 0;
547 while ( st.hasMoreTokens() ) {
548 langComponents[i] = st.nextToken();
549 i++;
550 }
551
552 String searchBasename = LDAPAttribute.getBaseName(attrName);
553 String[] searchTypes = LDAPAttribute.getSubtypes(attrName);
554 LDAPAttribute found = null;
555 int matchCount = 0;
556 for( i = 0; i < _attrs.length; i++ ) {
557 boolean isCandidate = false;
558 LDAPAttribute attr = _attrs[i];
559 // Same base name?
560 if ( attr.getBaseName().equalsIgnoreCase(searchBasename) ) {
561 // Accept any subtypes?
562 if( (searchTypes == null) || (searchTypes.length < 1) ) {
563 isCandidate = true;
564 } else {
565 // No, have to check each subtype for inclusion
566 if( attr.hasSubtypes( searchTypes ) )
567 isCandidate = true;
568 }
569 }
570 String attrLang = null;
571 if ( isCandidate ) {
572 attrLang = attr.getLangSubtype();
573 // At this point, the base name and subtypes are okay
574 if ( attrLang == null ) {
575 // If there are no language attributes, this one is okay
576 found = attr;
577 } else {
578 // We just have to check for language match
579 st = new StringTokenizer( attrLang.toLowerCase(), "-" );
580 // Skip first token, which is "lang-"
581 st.nextToken();
582 // No match if the attribute's language spec is longer
583 // than the target one
584 if ( st.countTokens() > langComponents.length )
585 continue;
586
587 // How many subcomponents of the language match?
588 int j = 0;
589 while ( st.hasMoreTokens() ) {
590 if ( !langComponents[j].equals( st.nextToken() ) ) {
591 j = 0;
592 break;
593 }
594 j++;
595 }
596 if ( j > matchCount ) {
597 found = attr;
598 matchCount = j;
599 }
600 }
601 }
602 }
603 return found;
604 }
605
606 /**
607 * Retrieves the string representation of all attributes
608 * in the attribute set. For example:
609 *
610 * <PRE>
611 * LDAPAttributeSet: LDAPAttribute {type='cn', values='Barbara Jensen,Babs
612 * Jensen'}LDAPAttribute {type='sn', values='Jensen'}LDAPAttribute {type='
613 * givenname', values='Barbara'}LDAPAttribute {type='objectclass', values=
614 * 'top,person,organizationalPerson,inetOrgPerson'}LDAPAttribute {type='ou',
615 * values='Product Development,People'}
616 * </PRE>
617 *
618 * @return string representation of all attributes in the set.
619 */
620 public String toString() {
621 StringBuffer sb = new StringBuffer("LDAPAttributeSet: ");
622 for( int i = 0; i < _attrs.length; i++ ) {
623 if (i != 0) {
624 sb.append(" ");
625 }
626 sb.append(_attrs[i].toString());
627 }
628 return sb.toString();
629 }
630 }