| 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 |
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 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 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 |
|
| 59 |
|
| 60 |
|
| 61 |
static final int ATTR_COUNT_REQUIRES_HASH = 5; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public LDAPAttributeSet() { |
| 67 |
|
| 68 |
prepareHashtable( true ); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 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 |
|
| 90 |
|
| 91 |
public void clear() { |
| 92 |
_attrHash = null; |
| 93 |
if ( _attrs.length > 0 ) { |
| 94 |
_attrs = new LDAPAttribute[0]; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 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 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 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 |
|
| 146 |
|
| 147 |
|
| 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 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 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 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 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 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 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 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 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 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public int hashCode() { |
| 238 |
return _attrHash.hashCode(); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
public boolean isEmpty() { |
| 247 |
return ( _attrs.length < 1 ); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public Iterator<LDAPAttribute> iterator() { |
| 256 |
return _attrHash.values().iterator(); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 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 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 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 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 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 |
|
| 341 |
|
| 342 |
|
| 343 |
public int size() { |
| 344 |
return _attrs.length; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
public Object[] toArray() { |
| 353 |
return _attrs; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 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 |
|
| 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 |
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 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 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 |
|
| 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 |
|
| 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 |
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 |
|
| 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 |
|
| 560 |
if ( attr.getBaseName().equalsIgnoreCase(searchBasename) ) { |
| 561 |
|
| 562 |
if( (searchTypes == null) || (searchTypes.length < 1) ) { |
| 563 |
isCandidate = true; |
| 564 |
} else { |
| 565 |
|
| 566 |
if( attr.hasSubtypes( searchTypes ) ) |
| 567 |
isCandidate = true; |
| 568 |
} |
| 569 |
} |
| 570 |
String attrLang = null; |
| 571 |
if ( isCandidate ) { |
| 572 |
attrLang = attr.getLangSubtype(); |
| 573 |
|
| 574 |
if ( attrLang == null ) { |
| 575 |
|
| 576 |
found = attr; |
| 577 |
} else { |
| 578 |
|
| 579 |
st = new StringTokenizer( attrLang.toLowerCase(), "-" ); |
| 580 |
|
| 581 |
st.nextToken(); |
| 582 |
|
| 583 |
|
| 584 |
if ( st.countTokens() > langComponents.length ) |
| 585 |
continue; |
| 586 |
|
| 587 |
|
| 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 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 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 |
} |