| 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.Comparator; |
| 42 |
import java.util.Vector; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public class LDAPSearchResults implements Serializable { |
| 60 |
|
| 61 |
static final long serialVersionUID = -501692208613904825L; |
| 62 |
private Vector<Object> entries = null; |
| 63 |
private LDAPSearchQueue resultSource; |
| 64 |
private boolean searchComplete = false; |
| 65 |
private LDAPConnection connectionToClose; |
| 66 |
private LDAPConnection currConn; |
| 67 |
private boolean persistentSearch = false; |
| 68 |
private LDAPSearchConstraints currCons; |
| 69 |
private String currBase; |
| 70 |
private int currScope; |
| 71 |
private String currFilter; |
| 72 |
private String[] currAttrs; |
| 73 |
private boolean currAttrsOnly; |
| 74 |
private Vector<LDAPSearchResults> referralResults = new Vector<>(); |
| 75 |
private Vector<LDAPException> exceptions; |
| 76 |
private int msgID = -1; |
| 77 |
|
| 78 |
|
| 79 |
private boolean firstResult = false; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public LDAPSearchResults() { |
| 89 |
entries = new Vector<Object>(); |
| 90 |
connectionToClose = null; |
| 91 |
searchComplete = true; |
| 92 |
currCons = new LDAPSearchConstraints(); |
| 93 |
} |
| 94 |
|
| 95 |
LDAPSearchResults( LDAPConnection conn, |
| 96 |
LDAPSearchConstraints cons, |
| 97 |
String base, |
| 98 |
int scope, |
| 99 |
String filter, |
| 100 |
String[] attrs, |
| 101 |
boolean attrsOnly ) { |
| 102 |
this(); |
| 103 |
currConn = conn; |
| 104 |
currCons = cons; |
| 105 |
currBase = base; |
| 106 |
currScope = scope; |
| 107 |
currFilter = filter; |
| 108 |
currAttrs = attrs; |
| 109 |
currAttrsOnly = attrsOnly; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
LDAPSearchResults( Vector<Object> v ) { |
| 119 |
this(); |
| 120 |
entries = (Vector<Object>)v.clone(); |
| 121 |
|
| 122 |
if ((entries != null) && (entries.size() >= 1)) { |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
entries.removeElementAt(0); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
LDAPSearchResults( Vector<Object> v, |
| 131 |
LDAPConnection conn, |
| 132 |
LDAPSearchConstraints cons, |
| 133 |
String base, |
| 134 |
int scope, |
| 135 |
String filter, |
| 136 |
String[] attrs, |
| 137 |
boolean attrsOnly ) { |
| 138 |
this( v ); |
| 139 |
currConn = conn; |
| 140 |
currCons = cons; |
| 141 |
currBase = base; |
| 142 |
currScope = scope; |
| 143 |
currFilter = filter; |
| 144 |
currAttrs = attrs; |
| 145 |
currAttrsOnly = attrsOnly; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public int getCount() { |
| 157 |
|
| 158 |
while (resultSource != null && resultSource.getMessageCount() > 0) { |
| 159 |
fetchResult(); |
| 160 |
} |
| 161 |
|
| 162 |
int count = entries.size(); |
| 163 |
|
| 164 |
for ( int i = 0; i < referralResults.size(); i++ ) { |
| 165 |
LDAPSearchResults res = referralResults.elementAt(i); |
| 166 |
count += res.getCount(); |
| 167 |
} |
| 168 |
|
| 169 |
if ( exceptions != null ) { |
| 170 |
count += exceptions.size(); |
| 171 |
} |
| 172 |
|
| 173 |
return count; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public LDAPControl[] getResponseControls() { |
| 185 |
return currConn.getResponseControls(msgID); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public boolean hasMore() { |
| 206 |
|
| 207 |
while ((entries.size() == 0) && (!searchComplete)) { |
| 208 |
fetchResult(); |
| 209 |
} |
| 210 |
|
| 211 |
if ((entries.size() == 0) && |
| 212 |
((exceptions == null) || (exceptions.size() == 0))) { |
| 213 |
while (referralResults.size() > 0) { |
| 214 |
LDAPSearchResults res = |
| 215 |
referralResults.elementAt(0); |
| 216 |
if (res.hasMore()) |
| 217 |
return true; |
| 218 |
else |
| 219 |
referralResults.removeElementAt(0); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
return ((entries.size() > 0) || |
| 224 |
((exceptions != null) && (exceptions.size() > 0))); |
| 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 |
public LDAPEntry next() throws LDAPException { |
| 264 |
Object o = nextElement(); |
| 265 |
if ((o instanceof LDAPReferralException) || |
| 266 |
(o instanceof LDAPException)) { |
| 267 |
throw (LDAPException)o; |
| 268 |
} |
| 269 |
if (o instanceof LDAPEntry) { |
| 270 |
return (LDAPEntry)o; |
| 271 |
} |
| 272 |
return null; |
| 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 |
public synchronized void sort(Comparator<LDAPEntry> compare) { |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
if (currCons.getReferralFollowing()) { |
| 313 |
while (referralResults.size() > 0) { |
| 314 |
Object obj = null; |
| 315 |
if ((obj=nextReferralElement()) != null) { |
| 316 |
if (obj instanceof LDAPException) { |
| 317 |
add((LDAPException)obj); |
| 318 |
} |
| 319 |
else { |
| 320 |
entries.addElement(obj); |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
int numEntries = entries.size(); |
| 327 |
if (numEntries <= 0) { |
| 328 |
return; |
| 329 |
} |
| 330 |
|
| 331 |
LDAPEntry[] toSort = new LDAPEntry[numEntries]; |
| 332 |
entries.copyInto (toSort); |
| 333 |
|
| 334 |
if (toSort.length > 1) { |
| 335 |
quicksort (toSort, compare, 0, numEntries-1); |
| 336 |
} |
| 337 |
|
| 338 |
entries.removeAllElements(); |
| 339 |
for (int i = 0; i < numEntries; i++) { |
| 340 |
entries.addElement (toSort[i]); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
void add( LDAPMessage msg ) { |
| 350 |
if ( msg instanceof LDAPSearchResult ) { |
| 351 |
entries.addElement( ((LDAPSearchResult)msg).getEntry()); |
| 352 |
} else if ( msg instanceof LDAPSearchResultReference ) { |
| 353 |
|
| 354 |
String urls[] = ((LDAPSearchResultReference)msg).getReferrals(); |
| 355 |
if ( urls != null ) { |
| 356 |
if (exceptions == null) { |
| 357 |
exceptions = new Vector<>(); |
| 358 |
} |
| 359 |
exceptions.addElement( |
| 360 |
new LDAPReferralException(null, 0, urls) ); |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
void add( LDAPException e ) { |
| 370 |
if ( exceptions == null ) { |
| 371 |
exceptions = new Vector<>(); |
| 372 |
} |
| 373 |
exceptions.addElement( e ); |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
void associate( LDAPSearchQueue l ) { |
| 382 |
resultSource = l; |
| 383 |
searchComplete = false; |
| 384 |
} |
| 385 |
|
| 386 |
void associatePersistentSearch( LDAPSearchQueue l) { |
| 387 |
resultSource = l; |
| 388 |
persistentSearch = true; |
| 389 |
searchComplete = false; |
| 390 |
firstResult = true; |
| 391 |
} |
| 392 |
|
| 393 |
void addReferralEntries( LDAPSearchResults res ) { |
| 394 |
referralResults.addElement( res ); |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
void closeOnCompletion( LDAPConnection toClose ) { |
| 403 |
if ( searchComplete ) { |
| 404 |
try { |
| 405 |
toClose.disconnect(); |
| 406 |
} catch( LDAPException e ) { |
| 407 |
} |
| 408 |
} else { |
| 409 |
connectionToClose = toClose; |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
void quicksort ( LDAPEntry[] toSort, Comparator<LDAPEntry> compare, |
| 418 |
int low, int high ) { |
| 419 |
if (low >= high) { |
| 420 |
return; |
| 421 |
} |
| 422 |
|
| 423 |
LDAPEntry pivot = toSort[low]; |
| 424 |
int slow = low-1, shigh = high+1; |
| 425 |
|
| 426 |
while (true) { |
| 427 |
do { |
| 428 |
shigh--; |
| 429 |
} while ( compare.compare(toSort[shigh], pivot) > 0 ); |
| 430 |
do { |
| 431 |
slow++; |
| 432 |
} while ( compare.compare(pivot, toSort[slow]) > 0 ); |
| 433 |
|
| 434 |
if (slow >= shigh) { |
| 435 |
break; |
| 436 |
} |
| 437 |
|
| 438 |
LDAPEntry temp = toSort[slow]; |
| 439 |
toSort[slow] = toSort[shigh]; |
| 440 |
toSort[shigh] = temp; |
| 441 |
} |
| 442 |
|
| 443 |
quicksort (toSort, compare, low, shigh); |
| 444 |
quicksort (toSort, compare, shigh+1, high); |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
void setMsgID( int msgID ) { |
| 453 |
this.msgID = msgID; |
| 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 |
Object nextElement() { |
| 487 |
if ( entries.size() > 0 ) { |
| 488 |
Object obj = entries.elementAt(0); |
| 489 |
entries.removeElementAt(0); |
| 490 |
return obj; |
| 491 |
} |
| 492 |
|
| 493 |
if (referralResults.size() > 0) { |
| 494 |
return nextReferralElement(); |
| 495 |
} |
| 496 |
|
| 497 |
if ((exceptions != null) && (exceptions.size() > 0)) { |
| 498 |
LDAPException obj = exceptions.elementAt(0); |
| 499 |
exceptions.removeElementAt(0); |
| 500 |
return obj; |
| 501 |
} |
| 502 |
|
| 503 |
return null; |
| 504 |
} |
| 505 |
|
| 506 |
Object nextReferralElement() { |
| 507 |
LDAPSearchResults res = referralResults.elementAt(0); |
| 508 |
if ((!res.persistentSearch && res.hasMore()) || |
| 509 |
(res.persistentSearch)) { |
| 510 |
Object obj = res.nextElement(); |
| 511 |
if (obj != null) { |
| 512 |
return obj; |
| 513 |
} |
| 514 |
|
| 515 |
if ((obj == null) || (!res.hasMore())) { |
| 516 |
referralResults.removeElementAt(0); |
| 517 |
} |
| 518 |
} else { |
| 519 |
referralResults.removeElementAt(0); |
| 520 |
} |
| 521 |
|
| 522 |
return null; |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
int getMessageID() { |
| 530 |
if ( resultSource == null ) { |
| 531 |
return -1; |
| 532 |
} |
| 533 |
return resultSource.getMessageID(); |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
private synchronized void fetchResult() { |
| 540 |
|
| 541 |
|
| 542 |
if ( resultSource != null ) { |
| 543 |
synchronized( this ) { |
| 544 |
if (searchComplete || firstResult) { |
| 545 |
firstResult = false; |
| 546 |
return; |
| 547 |
} |
| 548 |
|
| 549 |
LDAPMessage msg = null; |
| 550 |
try { |
| 551 |
msg = resultSource.getResponse(); |
| 552 |
} catch (LDAPException e) { |
| 553 |
add(e); |
| 554 |
currConn.releaseSearchListener(resultSource); |
| 555 |
searchComplete = true; |
| 556 |
return; |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
if (msg == null) { |
| 561 |
searchComplete = true; |
| 562 |
currConn.releaseSearchListener(resultSource); |
| 563 |
return; |
| 564 |
|
| 565 |
} else if (msg instanceof LDAPResponse) { |
| 566 |
try { |
| 567 |
|
| 568 |
|
| 569 |
currConn.checkSearchMsg(this, msg, currCons, |
| 570 |
currBase, currScope, currFilter, |
| 571 |
currAttrs, currAttrsOnly); |
| 572 |
} catch (LDAPException e) { |
| 573 |
System.err.println("LDAPSearchResults.fetchResult: "+e); |
| 574 |
} finally { |
| 575 |
currConn.releaseSearchListener(resultSource); |
| 576 |
} |
| 577 |
searchComplete = true; |
| 578 |
if (connectionToClose != null) { |
| 579 |
try { |
| 580 |
connectionToClose.disconnect (); |
| 581 |
} catch (LDAPException e) { |
| 582 |
} |
| 583 |
connectionToClose = null; |
| 584 |
} |
| 585 |
return; |
| 586 |
} else { |
| 587 |
try { |
| 588 |
currConn.checkSearchMsg(this, msg, currCons, |
| 589 |
currBase, currScope, currFilter, currAttrs, currAttrsOnly); |
| 590 |
} catch (LDAPException e) { |
| 591 |
System.err.println("Exception: "+e); |
| 592 |
} |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
} |
| 597 |
} |