File: LDAPAttributeSet.java

package info (click to toggle)
ldapjdk 4.20.0%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,708 kB
  • sloc: ansic: 44,727; java: 39,132; xml: 7,419; sh: 4,185; perl: 3,774; makefile: 1,681; cpp: 979
file content (634 lines) | stat: -rw-r--r-- 21,725 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/* -*- 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) 1999
 * 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.io.Serializable;
import java.util.*;

import org.ietf.ldap.client.*;
import org.ietf.ldap.client.opers.*;

/**
 * Represents a set of attributes (for example, the set of attributes
 * in an entry).
 *
 * @version 1.0
 * @see org.ietf.ldap.LDAPAttribute
 */
public class LDAPAttributeSet implements Cloneable, Serializable, Set {
    static final long serialVersionUID = 5018474561697778100L;
    HashMap _attrHash = null;
    LDAPAttribute[] _attrs = new LDAPAttribute[0];
    /* If there are less attributes than this in the set, it's not worth
     creating a Hashtable - faster and cheaper most likely to do string
     comparisons. Most applications fetch attributes once only, anyway */
    static final int ATTR_COUNT_REQUIRES_HASH = 5;

    /**
     * Constructs a new set of attributes.  This set is initially empty.
     */
    public LDAPAttributeSet() {
        // For now, always create the hashtable
        prepareHashtable( true );
    }

    /**
     * Constructs an attribute set.
     * @param attrs the list of attributes
     */
    public LDAPAttributeSet( LDAPAttribute[] attrs ) {
        this();
        if ( attrs == null ) {
            attrs = new LDAPAttribute[0];
        }
        _attrs = attrs;
        if ( _attrs.length > 0 ) {
            for( int i = 0; i < _attrs.length; i++ ) {
                add( _attrs[i] );
            }
        }
    }

    /**
     * Removes all mappings from this attribute set
     */
    public void clear() {
        _attrHash = null;
        if ( _attrs.length > 0 ) {
            _attrs = new LDAPAttribute[0];
        }
    }

    /**
     * Adds the specified attribute to this attribute set, overriding
     * any previous definition with the same attribute name
     *
     * @param attr attribute to add to this set
     * @return true if this set changed as a result of the call
     */
    public synchronized boolean add( Object attr ) {
        if ( attr instanceof LDAPAttribute ) {
            if ( contains( attr ) ) {
                return false;
            }
            LDAPAttribute attrib = (LDAPAttribute)attr;
            LDAPAttribute[] vals = new LDAPAttribute[_attrs.length+1];
            for ( int i = 0; i < _attrs.length; i++ ) {
                vals[i] = _attrs[i];
            }
            vals[_attrs.length] = attrib;
            _attrs = vals;
            if ( _attrHash != null ) {
                _attrHash.put( attrib.getName().toLowerCase(), attr );
            }
            return true;
        } else {
            throw new ClassCastException( "Requires LDAPAttribute");
        }
    }

    /**
     * Adds the collection of attributes to this attribute set, overriding
     * any previous definition with the same attribute names
     *
     * @param attrs attributes to add to this set
     * @return true if any attribute was added
     */
    public synchronized boolean addAll( Collection attrs ) {
        if ( attrs == null ) {
            return false;
        }
        boolean present = true;
        Iterator it = attrs.iterator();
        while( it.hasNext() ) {
            Object attr = it.next();
            if ( !contains( attr ) ) {
                present = true;
                add( attr );
            }
        }
        return !present;
    }

    /**
     * Returns a deep copy of this attribute set
     *
     * @return a deep copy of this attribute set
     */
    public synchronized Object clone() {
        try {
            LDAPAttributeSet attributeSet = new LDAPAttributeSet();
            attributeSet._attrs = new LDAPAttribute[_attrs.length];
            for (int i = 0; i < _attrs.length; i++) {
                attributeSet._attrs[i] = new LDAPAttribute(_attrs[i]);
            }
             return attributeSet;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * Returns true if this attribute set contains the specified attribute
     *
     * @param attr attribute whose presence in this set is to be tested
     * @return true if the attribute set contains the specified attribute
     */
    public boolean contains( Object attr ) {
        if ( !(attr instanceof LDAPAttribute) ) {
            return false;
        } else {
            if ( _attrHash != null ) {
                return _attrHash.containsValue( attr );
            } else {
                for ( int i = 0; i < _attrs.length; i++ ) {
                    if ( attr.equals(_attrs[i]) ) {
                        return true;
                    }
                }
            }
            return false;
        }
    }

    /**
     * Returns true if this attribute set contains all the specified attributes
     *
     * @param attrs attributes whose presence in this set is to be tested
     * @return true if the attribute set contains the specified attributes
     */
    public boolean containsAll( Collection attrs ) {
        if ( _attrHash != null ) {
            Iterator it = attrs.iterator();
            while( it.hasNext() ) {
                if ( !_attrHash.containsValue( it.next() ) ) {
                    return false;
                }
            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * Returns true if this attribute set contains the specified attribute name
     *
     * @param attrName attribute name whose presence in this set is to be tested
     * @return true if the attribute set contains the specified attribute
     */
    public boolean containsKey( Object attrName ) {
        if ( !(attrName instanceof String) ) {
            return false;
        } else {
            return ( getAttribute( (String)attrName ) != null );
        }
    }

    /**
     * Returns true if this attribute set equals a specified set
     *
     * @param attrSet attribute set to compare to
     * @return true if this attribute set equals a specified set
     */
    public boolean equals( Object attrSet ) {
        if ( !(attrSet instanceof LDAPAttributeSet) ) {
            return false;
        }
        return ((LDAPAttributeSet)attrSet)._attrHash.equals( _attrHash );
    }

    /**
     * Returns the hash code for this attribute set
     *
     * @return the hash code for this attribute set
     */
    public int hashCode() {
        return _attrHash.hashCode();
    }

    /**
     * Returns true if there are no attributes in this attribute set
     *
     * @return true if there are no attributes in this attribute set
     */
    public boolean isEmpty() {
        return ( _attrs.length < 1 );
    }

    /**
     * Returns an iterator over the attributes in this attribute set
     *
     * @return an iterator over the attributes in this attribute set
     */
    public Iterator iterator() {
        return _attrHash.values().iterator();
    }

    /**
     * Removes the specified attribute
     *
     * @param attr the attribute to remove
     * @return true if the attribute was removed
     */
    public boolean remove( Object attr ) {
        if ( !(attr instanceof LDAPAttribute) ) {
            return false;
        }
        boolean present = contains( attr );
        if ( present ) {
            synchronized(this) {
                LDAPAttribute[] vals = new LDAPAttribute[_attrs.length-1];
                int j = 0;
                for (int i = 0; i < _attrs.length; i++) {
                    if ( !attr.equals(_attrs[i] ) ) {
                        vals[j++] = _attrs[i];
                    }
                }
                if (_attrHash != null) {
                    _attrHash.remove(
                        ((LDAPAttribute)attr).getName().toLowerCase() );
                }
                _attrs = vals;
            }
        }
        return present;
    }

    /**
     * Removes the specified attributes
     *
     * @param attrs the attributes to remove
     * @return true if any attribute was removed
     */
    public boolean removeAll( Collection attrs ) {
        if ( attrs == null ) {
            return false;
        }
        boolean present = true;
        Iterator it = attrs.iterator();
        while( it.hasNext() ) {
            Object attr = it.next();
            if ( !contains( attr ) ) {
                present = true;
                remove( attr );
            }
        }
        return !present;
    }

    /**
     * Retains only the attributes in this set that are contained in the
     * specified collection
     *
     * @param attrs attributes to retain
     * @return true if the attribute set was changed as a result of the
     * operation
     */
    public boolean retainAll( Collection attrs ) {
        HashMap newmap = new HashMap();
        Iterator it = attrs.iterator();
        while( it.hasNext() ) {
            Object attr = it.next();
            if ( attr instanceof LDAPAttribute ) {
                newmap.put( ((LDAPAttribute)attr).getName().toLowerCase(),
                            attr );
            }
        }
        if ( newmap.equals( _attrHash ) ) {
            return false;
        } else {
            _attrHash = newmap;
            _attrs = (LDAPAttribute[])_attrHash.values().toArray(
                new LDAPAttribute[0] );
            return true;
        }
    }

    /**
     * Returns the number of attributes in this set.
     * @return number of attributes in this attribute set.
     */
    public int size() {
        return _attrs.length;
    }

    /**
     * Returns the attributes of the set as an array
     *
     * @return the attributes of the set as an array
     */
    public Object[] toArray() {
        return _attrs;
    }

    /**
     * Returns the attributes of the set as an array
     *
     * @param attrs an attribute array to fill with the attributes of this
     * set. If the array is not large enough, a new array is allocated.
     * @return the attributes of the set as an array
     */
    public Object[] toArray( Object[] attrs ) {
        if ( !(attrs instanceof LDAPAttribute[]) ) {
            throw new ArrayStoreException(
                "Must provide an LDAPAttribute array" );
        } else if ( attrs.length >= _attrs.length ) {
            for( int i = 0; i < _attrs.length; i++ ) {
                attrs[i] = _attrs[i];
            }
            return attrs;
        } else {
            return _attrs;
        }
    }

    /**
     * Creates a new attribute set containing only the attributes
     * that have the specified subtypes.
     * <P>
     *
     * For example, suppose an attribute set contains the following attributes:
     * <P>
     *
     * <PRE>
     * cn
     * cn;lang-ja
     * sn;phonetic;lang-ja
     * sn;lang-us
     * </PRE>
     *
     * If you call the <CODE>getSubset</CODE> method and pass
     * <CODE>lang-ja</CODE> as the argument, the method returns
     * an attribute set containing the following attributes:
     * <P>
     *
     * <PRE>
     * cn;lang-ja
     * sn;phonetic;lang-ja
     * </PRE>
     *
     * @param subtype semi-colon delimited list of subtypes
     * to find within attribute names. 
     * For example:
     * <PRE>
     *     "lang-ja"        // Only Japanese language subtypes
     *     "binary"         // Only binary subtypes
     *     "binary;lang-ja" // Only Japanese language subtypes
     *                         which also are binary
     * </PRE>
     * @return attribute set containing the attributes that have
     * the specified subtypes.
     * @see org.ietf.ldap.LDAPAttribute
     * @see org.ietf.ldap.LDAPAttributeSet#getAttribute
     * @see org.ietf.ldap.LDAPEntry#getAttributeSet
     */
    public LDAPAttributeSet getSubset( String subtype ) {
        LDAPAttributeSet attrs = new LDAPAttributeSet();
        if ( subtype == null )
            return attrs;
        StringTokenizer st = new StringTokenizer(subtype, ";");
        if( st.countTokens() < 1 )
            return attrs;
        String[] searchTypes = new String[st.countTokens()];
        int i = 0;
        while( st.hasMoreTokens() ) {
            searchTypes[i] = (String)st.nextToken();
            i++;
        }
        Iterator it = _attrHash.values().iterator();
        while( it.hasNext() ) {
            LDAPAttribute attr = (LDAPAttribute)it.next();
            if( attr.hasSubtypes( searchTypes ) )
                attrs.add( new LDAPAttribute( attr ) );
        }
        return attrs;
    }

    /**
     * Returns a single attribute that exactly matches the specified attribute
     * name.
     * @param attrName name of attribute to return
     * For example:
     *<PRE>
     *     "cn"            // Only a non-subtyped version of cn
     *     "cn;lang-ja"    // Only a Japanese version of cn
     *</PRE>
     * @return attribute that has exactly the same name, or null
     * (if no attribute in the set matches the specified name).
     * @see org.ietf.ldap.LDAPAttribute
     */
    public LDAPAttribute getAttribute( String attrName ) {
        if ( attrName == null ) {
            return null;
        } else if ( _attrHash != null ) {
            return (LDAPAttribute)_attrHash.get( attrName.toLowerCase() );
        } else {
            for ( int i = 0; i < _attrs.length; i++ ) {
                if ( attrName.equalsIgnoreCase(_attrs[i].getName()) ) {
                    return _attrs[i];
                }
            }
            return null;
        }
    }

    /**
     * Prepares hashtable for fast attribute lookups.
     */
    private void prepareHashtable( boolean force ) {
        if ( (_attrHash == null) &&
             (force || (_attrs.length >= ATTR_COUNT_REQUIRES_HASH)) ) {
            if ( _attrHash == null ) {
                _attrHash = new HashMap();
            } else {
                _attrHash.clear();
            }
            for ( int j = 0; j < _attrs.length; j++ ) {
                _attrHash.put( _attrs[j].getName().toLowerCase(), _attrs[j] );
            }
        }
    }

    /**
     * Returns the subtype that matches the attribute name specified
     * by <CODE>attrName</CODE> and the language specificaton identified
     * by <CODE>lang</CODE>.
     * <P>
     *
     * If no attribute in the set has the specified name and subtype,
     * the method returns <CODE>null</CODE>.
     *
     * Attributes containing subtypes other than <CODE>lang</CODE>
     * (for example, <CODE>cn;binary</CODE>) are returned only if
     * they contain the specified <CODE>lang</CODE> subtype and if
     * the set contains no attribute having only the <CODE>lang</CODE>
     * subtype.  (For example, <CODE>getAttribute( "cn", "lang-ja" )</CODE>
     * returns <CODE>cn;lang-ja;phonetic</CODE> only if the 
     * <CODE>cn;lang-ja</CODE> attribute does not exist.)
     * <P>
     *
     * If null is specified for the <CODE>lang</CODE> argument,
     * calling this method is the same as calling the
     * <CODE>getAttribute(attrName)</CODE> method.
     * <P>
     *
     * For example, suppose an entry contains only the following attributes:
     * <P>
     * <UL>
     * <LI><CODE>cn;lang-en</CODE>
     * <LI><CODE>cn;lang-ja-JP-kanji</CODE>
     * <LI><CODE>sn</CODE>
     * </UL>
     * <P>
     *
     * Calling the following methods will return the following values:
     * <P>
     * <UL>
     * <LI><CODE>getAttribute( "cn" )</CODE> returns <CODE>null</CODE>.
     * <LI><CODE>getAttribute( "sn" )</CODE> returns the "<CODE>sn</CODE>" attribute.
     * <LI><CODE>getAttribute( "cn", "lang-en-us" )</CODE> returns the "<CODE>cn;lang-en</CODE>" attribute.
     * <LI><CODE>getAttribute( "cn", "lang-en" )</CODE> returns the "<CODE>cn;lang-en</CODE>" attribute.
     * <LI><CODE>getAttribute( "cn", "lang-ja" )</CODE> returns <CODE>null</CODE>.
     * <LI><CODE>getAttribute( "sn", "lang-en" )</CODE> returns the "<CODE>sn</CODE>" attribute.
     *</UL>
     * <P>
     * @param attrName name of attribute to find in the entry
     * @param lang a language specification
     * @return the attribute that matches the base name and that best
     * matches any specified language subtype.
     * @see org.ietf.ldap.LDAPAttribute
     */
    public LDAPAttribute getAttribute( String attrName, String lang ) {
        if ( (lang == null) || (lang.length() < 1) )
            return getAttribute( attrName );

        String langLower = lang.toLowerCase();
        if ((langLower.length() < 5) ||
            ( !langLower.substring( 0, 5 ).equals( "lang-" ) )) {
            return null;
        }
        StringTokenizer st = new StringTokenizer( langLower, "-" );
        // Skip first token, which is "lang-"
        st.nextToken();
        String[] langComponents = new String[st.countTokens()];
        int i = 0;
        while ( st.hasMoreTokens() ) {
            langComponents[i] = st.nextToken();
            i++;
        }

        String searchBasename = LDAPAttribute.getBaseName(attrName);
        String[] searchTypes = LDAPAttribute.getSubtypes(attrName);
        LDAPAttribute found = null;
        int matchCount = 0;
        for( i = 0; i < _attrs.length; i++ ) {
            boolean isCandidate = false;
            LDAPAttribute attr = _attrs[i];
            // Same base name?
            if ( attr.getBaseName().equalsIgnoreCase(searchBasename) ) {
                // Accept any subtypes?
                if( (searchTypes == null) || (searchTypes.length < 1) ) {
                    isCandidate = true;
                } else {
                    // No, have to check each subtype for inclusion
                    if( attr.hasSubtypes( searchTypes ) )
                    isCandidate = true;
                }
            }
            String attrLang = null;
            if ( isCandidate ) {
                attrLang = attr.getLangSubtype();
                // At this point, the base name and subtypes are okay
                if ( attrLang == null ) {
                    // If there are no language attributes, this one is okay
                    found = attr;
                } else {
                    // We just have to check for language match
                    st = new StringTokenizer( attrLang.toLowerCase(), "-" );
                    // Skip first token, which is "lang-"
                    st.nextToken();
                    // No match if the attribute's language spec is longer
                    // than the target one
                    if ( st.countTokens() > langComponents.length )
                        continue;

                    // How many subcomponents of the language match?
                    int j = 0;
                    while ( st.hasMoreTokens() ) {
                        if ( !langComponents[j].equals( st.nextToken() ) ) {
                            j = 0;
                            break;
                        }
                        j++;
                    }
                    if ( j > matchCount ) {
                        found = attr;
                        matchCount = j;
                    }
                }
            }
        }
        return found;
    }

    /**
     * Retrieves the string representation of all attributes
     * in the attribute set.  For example:
     *
     * <PRE>
     * LDAPAttributeSet: LDAPAttribute {type='cn', values='Barbara Jensen,Babs
     * Jensen'}LDAPAttribute {type='sn', values='Jensen'}LDAPAttribute {type='
     * givenname', values='Barbara'}LDAPAttribute {type='objectclass', values=
     * 'top,person,organizationalPerson,inetOrgPerson'}LDAPAttribute {type='ou',
     * values='Product Development,People'}
     * </PRE>
     *
     * @return string representation of all attributes in the set.
     */
    public String toString() {
        StringBuffer sb = new StringBuffer("LDAPAttributeSet: ");
        for( int i = 0; i < _attrs.length; i++ ) {
            if (i != 0) {
                sb.append(" ");
            }            
            sb.append(_attrs[i].toString());
        }
        return sb.toString();
    }
}