35 import com.sun.jndi.toolkit.dir.SearchFilter;
36
37 /**
38 * A context for handling referrals.
39 *
40 * @author Vincent Ryan
41 */
42 final class LdapReferralContext implements DirContext, LdapContext {
43
44 private DirContext refCtx = null;
45 private Name urlName = null; // override the supplied name
46 private String urlAttrs = null; // override attributes
47 private String urlScope = null; // override scope
48 private String urlFilter = null; // override filter
49
50 private LdapReferralException refEx = null;
51 private boolean skipThisReferral = false;
52 private int hopCount = 1;
53 private NamingException previousEx = null;
54
55 LdapReferralContext(LdapReferralException ex, Hashtable env,
56 Control[] connCtls,
57 Control[] reqCtls,
58 String nextName,
59 boolean skipThisReferral,
60 int handleReferrals) throws NamingException {
61
62 refEx = ex;
63
64 if (this.skipThisReferral = skipThisReferral) {
65 return; // don't create a DirContext for this referral
66 }
67
68 String referral;
69
70 // Make copies of environment and connect controls for our own use.
71 if (env != null) {
72 env = (Hashtable) env.clone();
73 // Remove old connect controls from environment, unless we have new
74 // ones that will override them anyway.
75 if (connCtls == null) {
76 env.remove(LdapCtx.BIND_CONTROLS);
77 }
78 } else if (connCtls != null) {
79 env = new Hashtable(5);
80 }
81 if (connCtls != null) {
82 Control[] copiedCtls = new Control[connCtls.length];
83 System.arraycopy(connCtls, 0, copiedCtls, 0, connCtls.length);
84 // Add copied controls to environment, replacing any old ones.
85 env.put(LdapCtx.BIND_CONTROLS, copiedCtls);
86 }
87
88 while (true) {
89 try {
90 referral = refEx.getNextReferral();
91 if (referral == null) {
92 throw (NamingException)(previousEx.fillInStackTrace());
93 }
94
95 } catch (LdapReferralException e) {
96
97 if (handleReferrals == LdapClient.LDAP_REF_THROW) {
98 throw e;
99 } else {
100 refEx = e;
101 continue;
102 }
103 }
104
105 // Create a Reference containing the referral URL.
243 throw (NamingException)
244 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
245 }
246
247 refCtx.unbind(overrideName(name));
248 }
249
250 public void rename(String oldName, String newName) throws NamingException {
251 rename(toName(oldName), toName(newName));
252 }
253
254 public void rename(Name oldName, Name newName) throws NamingException {
255 if (skipThisReferral) {
256 throw (NamingException)
257 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
258 }
259
260 refCtx.rename(overrideName(oldName), toName(refEx.getNewRdn()));
261 }
262
263 public NamingEnumeration list(String name) throws NamingException {
264 return list(toName(name));
265 }
266
267 public NamingEnumeration list(Name name) throws NamingException {
268 if (skipThisReferral) {
269 throw (NamingException)
270 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
271 }
272 try {
273 NamingEnumeration ne = null;
274
275 if (urlScope != null && urlScope.equals("base")) {
276 SearchControls cons = new SearchControls();
277 cons.setReturningObjFlag(true);
278 cons.setSearchScope(SearchControls.OBJECT_SCOPE);
279
280 ne = refCtx.search(overrideName(name), "(objectclass=*)", cons);
281
282 } else {
283 ne = refCtx.list(overrideName(name));
284 }
285
286 refEx.setNameResolved(true);
287
288 // append (referrals from) the exception that generated this
289 // context to the new search results, so that referral processing
290 // can continue
291 ((ReferralEnumeration)ne).appendUnprocessedReferrals(refEx);
292
293 return (ne);
294
295 } catch (LdapReferralException e) {
296
297 // append (referrals from) the exception that generated this
298 // context to the new exception, so that referral processing
299 // can continue
300
301 e.appendUnprocessedReferrals(refEx);
302 throw (NamingException)(e.fillInStackTrace());
303
304 } catch (NamingException e) {
305
306 // record the exception if there are no remaining referrals
307 if ((refEx != null) && (! refEx.hasMoreReferrals())) {
308 refEx.setNamingException(e);
309 }
310 if ((refEx != null) &&
311 (refEx.hasMoreReferrals() ||
312 refEx.hasMoreReferralExceptions())) {
313 throw (NamingException)
314 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
315 } else {
316 throw e;
317 }
318 }
319 }
320
321 public NamingEnumeration listBindings(String name) throws NamingException {
322 return listBindings(toName(name));
323 }
324
325 public NamingEnumeration listBindings(Name name) throws NamingException {
326 if (skipThisReferral) {
327 throw (NamingException)
328 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
329 }
330
331 try {
332 NamingEnumeration be = null;
333
334 if (urlScope != null && urlScope.equals("base")) {
335 SearchControls cons = new SearchControls();
336 cons.setReturningObjFlag(true);
337 cons.setSearchScope(SearchControls.OBJECT_SCOPE);
338
339 be = refCtx.search(overrideName(name), "(objectclass=*)", cons);
340
341 } else {
342 be = refCtx.listBindings(overrideName(name));
343 }
344
345 refEx.setNameResolved(true);
346
347 // append (referrals from) the exception that generated this
348 // context to the new search results, so that referral processing
349 // can continue
350 ((ReferralEnumeration)be).appendUnprocessedReferrals(refEx);
351
352 return (be);
353
354 } catch (LdapReferralException e) {
355
356 // append (referrals from) the exception that generated this
357 // context to the new exception, so that referral processing
358 // can continue
359
360 e.appendUnprocessedReferrals(refEx);
361 throw (NamingException)(e.fillInStackTrace());
362
363 } catch (NamingException e) {
364
365 // record the exception if there are no remaining referrals
366 if ((refEx != null) && (! refEx.hasMoreReferrals())) {
367 refEx.setNamingException(e);
368 }
369 if ((refEx != null) &&
370 (refEx.hasMoreReferrals() ||
445 public Object addToEnvironment(String propName, Object propVal)
446 throws NamingException {
447 if (skipThisReferral) {
448 throw (NamingException)
449 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
450 }
451
452 return refCtx.addToEnvironment(propName, propVal);
453 }
454
455 public Object removeFromEnvironment(String propName)
456 throws NamingException {
457 if (skipThisReferral) {
458 throw (NamingException)
459 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
460 }
461
462 return refCtx.removeFromEnvironment(propName);
463 }
464
465 public Hashtable getEnvironment() throws NamingException {
466 if (skipThisReferral) {
467 throw (NamingException)
468 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
469 }
470
471 return refCtx.getEnvironment();
472 }
473
474 public Attributes getAttributes(String name) throws NamingException {
475 return getAttributes(toName(name));
476 }
477
478 public Attributes getAttributes(Name name) throws NamingException {
479 if (skipThisReferral) {
480 throw (NamingException)
481 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
482 }
483
484 return refCtx.getAttributes(overrideName(name));
485 }
585 }
586
587 return refCtx.getSchema(overrideName(name));
588 }
589
590 public DirContext getSchemaClassDefinition(String name)
591 throws NamingException {
592 return getSchemaClassDefinition(toName(name));
593 }
594
595 public DirContext getSchemaClassDefinition(Name name)
596 throws NamingException {
597 if (skipThisReferral) {
598 throw (NamingException)
599 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
600 }
601
602 return refCtx.getSchemaClassDefinition(overrideName(name));
603 }
604
605 public NamingEnumeration search(String name,
606 Attributes matchingAttributes)
607 throws NamingException {
608 return search(toName(name), SearchFilter.format(matchingAttributes),
609 new SearchControls());
610 }
611
612 public NamingEnumeration search(Name name,
613 Attributes matchingAttributes)
614 throws NamingException {
615 return search(name, SearchFilter.format(matchingAttributes),
616 new SearchControls());
617 }
618
619 public NamingEnumeration search(String name,
620 Attributes matchingAttributes,
621 String[] attributesToReturn)
622 throws NamingException {
623 SearchControls cons = new SearchControls();
624 cons.setReturningAttributes(attributesToReturn);
625
626 return search(toName(name), SearchFilter.format(matchingAttributes),
627 cons);
628 }
629
630 public NamingEnumeration search(Name name,
631 Attributes matchingAttributes,
632 String[] attributesToReturn)
633 throws NamingException {
634 SearchControls cons = new SearchControls();
635 cons.setReturningAttributes(attributesToReturn);
636
637 return search(name, SearchFilter.format(matchingAttributes), cons);
638 }
639
640 public NamingEnumeration search(String name,
641 String filter,
642 SearchControls cons)
643 throws NamingException {
644 return search(toName(name), filter, cons);
645 }
646
647 public NamingEnumeration search(Name name,
648 String filter,
649 SearchControls cons) throws NamingException {
650
651 if (skipThisReferral) {
652 throw (NamingException)
653 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
654 }
655
656 try {
657 NamingEnumeration se = refCtx.search(overrideName(name),
658 overrideFilter(filter), overrideAttributesAndScope(cons));
659
660 refEx.setNameResolved(true);
661
662 // append (referrals from) the exception that generated this
663 // context to the new search results, so that referral processing
664 // can continue
665 ((ReferralEnumeration)se).appendUnprocessedReferrals(refEx);
666
667 return (se);
668
669 } catch (LdapReferralException e) {
670
671 // %%% VR - setNameResolved(true);
672
673 // append (referrals from) the exception that generated this
674 // context to the new exception, so that referral processing
675 // can continue
676
677 e.appendUnprocessedReferrals(refEx);
678 throw (NamingException)(e.fillInStackTrace());
679
680 } catch (NamingException e) {
681
682 // record the exception if there are no remaining referrals
683 if ((refEx != null) && (! refEx.hasMoreReferrals())) {
684 refEx.setNamingException(e);
685 }
686 if ((refEx != null) &&
687 (refEx.hasMoreReferrals() ||
688 refEx.hasMoreReferralExceptions())) {
689 throw (NamingException)
690 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
691 } else {
692 throw e;
693 }
694 }
695 }
696
697 public NamingEnumeration search(String name,
698 String filterExpr,
699 Object[] filterArgs,
700 SearchControls cons)
701 throws NamingException {
702 return search(toName(name), filterExpr, filterArgs, cons);
703 }
704
705 public NamingEnumeration search(Name name,
706 String filterExpr,
707 Object[] filterArgs,
708 SearchControls cons) throws NamingException {
709
710 if (skipThisReferral) {
711 throw (NamingException)
712 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
713 }
714
715 try {
716 NamingEnumeration se;
717
718 if (urlFilter != null) {
719 se = refCtx.search(overrideName(name), urlFilter,
720 overrideAttributesAndScope(cons));
721 } else {
722 se = refCtx.search(overrideName(name), filterExpr,
723 filterArgs, overrideAttributesAndScope(cons));
724 }
725
726 refEx.setNameResolved(true);
727
728 // append (referrals from) the exception that generated this
729 // context to the new search results, so that referral processing
730 // can continue
731 ((ReferralEnumeration)se).appendUnprocessedReferrals(refEx);
732
733 return (se);
734
735 } catch (LdapReferralException e) {
736
|
35 import com.sun.jndi.toolkit.dir.SearchFilter;
36
37 /**
38 * A context for handling referrals.
39 *
40 * @author Vincent Ryan
41 */
42 final class LdapReferralContext implements DirContext, LdapContext {
43
44 private DirContext refCtx = null;
45 private Name urlName = null; // override the supplied name
46 private String urlAttrs = null; // override attributes
47 private String urlScope = null; // override scope
48 private String urlFilter = null; // override filter
49
50 private LdapReferralException refEx = null;
51 private boolean skipThisReferral = false;
52 private int hopCount = 1;
53 private NamingException previousEx = null;
54
55 @SuppressWarnings("unchecked") // clone()
56 LdapReferralContext(LdapReferralException ex,
57 Hashtable<?,?> env,
58 Control[] connCtls,
59 Control[] reqCtls,
60 String nextName,
61 boolean skipThisReferral,
62 int handleReferrals) throws NamingException {
63
64 refEx = ex;
65
66 if (this.skipThisReferral = skipThisReferral) {
67 return; // don't create a DirContext for this referral
68 }
69
70 String referral;
71
72 // Make copies of environment and connect controls for our own use.
73 if (env != null) {
74 env = (Hashtable<?,?>) env.clone();
75 // Remove old connect controls from environment, unless we have new
76 // ones that will override them anyway.
77 if (connCtls == null) {
78 env.remove(LdapCtx.BIND_CONTROLS);
79 }
80 } else if (connCtls != null) {
81 env = new Hashtable<String, Control[]>(5);
82 }
83 if (connCtls != null) {
84 Control[] copiedCtls = new Control[connCtls.length];
85 System.arraycopy(connCtls, 0, copiedCtls, 0, connCtls.length);
86 // Add copied controls to environment, replacing any old ones.
87 ((Hashtable<? super String, ? super Control[]>)env)
88 .put(LdapCtx.BIND_CONTROLS, copiedCtls);
89 }
90
91 while (true) {
92 try {
93 referral = refEx.getNextReferral();
94 if (referral == null) {
95 throw (NamingException)(previousEx.fillInStackTrace());
96 }
97
98 } catch (LdapReferralException e) {
99
100 if (handleReferrals == LdapClient.LDAP_REF_THROW) {
101 throw e;
102 } else {
103 refEx = e;
104 continue;
105 }
106 }
107
108 // Create a Reference containing the referral URL.
246 throw (NamingException)
247 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
248 }
249
250 refCtx.unbind(overrideName(name));
251 }
252
253 public void rename(String oldName, String newName) throws NamingException {
254 rename(toName(oldName), toName(newName));
255 }
256
257 public void rename(Name oldName, Name newName) throws NamingException {
258 if (skipThisReferral) {
259 throw (NamingException)
260 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
261 }
262
263 refCtx.rename(overrideName(oldName), toName(refEx.getNewRdn()));
264 }
265
266 public NamingEnumeration<NameClassPair> list(String name) throws NamingException {
267 return list(toName(name));
268 }
269
270 @SuppressWarnings("unchecked")
271 public NamingEnumeration<NameClassPair> list(Name name) throws NamingException {
272 if (skipThisReferral) {
273 throw (NamingException)
274 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
275 }
276 try {
277 NamingEnumeration<NameClassPair> ne = null;
278
279 if (urlScope != null && urlScope.equals("base")) {
280 SearchControls cons = new SearchControls();
281 cons.setReturningObjFlag(true);
282 cons.setSearchScope(SearchControls.OBJECT_SCOPE);
283
284 ne = (NamingEnumeration)
285 refCtx.search(overrideName(name), "(objectclass=*)", cons);
286
287 } else {
288 ne = refCtx.list(overrideName(name));
289 }
290
291 refEx.setNameResolved(true);
292
293 // append (referrals from) the exception that generated this
294 // context to the new search results, so that referral processing
295 // can continue
296 ((ReferralEnumeration)ne).appendUnprocessedReferrals(refEx);
297
298 return (ne);
299
300 } catch (LdapReferralException e) {
301
302 // append (referrals from) the exception that generated this
303 // context to the new exception, so that referral processing
304 // can continue
305
306 e.appendUnprocessedReferrals(refEx);
307 throw (NamingException)(e.fillInStackTrace());
308
309 } catch (NamingException e) {
310
311 // record the exception if there are no remaining referrals
312 if ((refEx != null) && (! refEx.hasMoreReferrals())) {
313 refEx.setNamingException(e);
314 }
315 if ((refEx != null) &&
316 (refEx.hasMoreReferrals() ||
317 refEx.hasMoreReferralExceptions())) {
318 throw (NamingException)
319 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
320 } else {
321 throw e;
322 }
323 }
324 }
325
326 public NamingEnumeration<Binding> listBindings(String name) throws
327 NamingException {
328 return listBindings(toName(name));
329 }
330
331 @SuppressWarnings("unchecked")
332 public NamingEnumeration<Binding> listBindings(Name name) throws
333 NamingException {
334 if (skipThisReferral) {
335 throw (NamingException)
336 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
337 }
338
339 try {
340 NamingEnumeration<Binding> be = null;
341
342 if (urlScope != null && urlScope.equals("base")) {
343 SearchControls cons = new SearchControls();
344 cons.setReturningObjFlag(true);
345 cons.setSearchScope(SearchControls.OBJECT_SCOPE);
346
347 be = (NamingEnumeration)refCtx.search(overrideName(name),
348 "(objectclass=*)", cons);
349
350 } else {
351 be = refCtx.listBindings(overrideName(name));
352 }
353
354 refEx.setNameResolved(true);
355
356 // append (referrals from) the exception that generated this
357 // context to the new search results, so that referral processing
358 // can continue
359 ((ReferralEnumeration<Binding>)be).appendUnprocessedReferrals(refEx);
360
361 return (be);
362
363 } catch (LdapReferralException e) {
364
365 // append (referrals from) the exception that generated this
366 // context to the new exception, so that referral processing
367 // can continue
368
369 e.appendUnprocessedReferrals(refEx);
370 throw (NamingException)(e.fillInStackTrace());
371
372 } catch (NamingException e) {
373
374 // record the exception if there are no remaining referrals
375 if ((refEx != null) && (! refEx.hasMoreReferrals())) {
376 refEx.setNamingException(e);
377 }
378 if ((refEx != null) &&
379 (refEx.hasMoreReferrals() ||
454 public Object addToEnvironment(String propName, Object propVal)
455 throws NamingException {
456 if (skipThisReferral) {
457 throw (NamingException)
458 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
459 }
460
461 return refCtx.addToEnvironment(propName, propVal);
462 }
463
464 public Object removeFromEnvironment(String propName)
465 throws NamingException {
466 if (skipThisReferral) {
467 throw (NamingException)
468 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
469 }
470
471 return refCtx.removeFromEnvironment(propName);
472 }
473
474 public Hashtable<?,?> getEnvironment() throws NamingException {
475 if (skipThisReferral) {
476 throw (NamingException)
477 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
478 }
479
480 return refCtx.getEnvironment();
481 }
482
483 public Attributes getAttributes(String name) throws NamingException {
484 return getAttributes(toName(name));
485 }
486
487 public Attributes getAttributes(Name name) throws NamingException {
488 if (skipThisReferral) {
489 throw (NamingException)
490 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
491 }
492
493 return refCtx.getAttributes(overrideName(name));
494 }
594 }
595
596 return refCtx.getSchema(overrideName(name));
597 }
598
599 public DirContext getSchemaClassDefinition(String name)
600 throws NamingException {
601 return getSchemaClassDefinition(toName(name));
602 }
603
604 public DirContext getSchemaClassDefinition(Name name)
605 throws NamingException {
606 if (skipThisReferral) {
607 throw (NamingException)
608 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
609 }
610
611 return refCtx.getSchemaClassDefinition(overrideName(name));
612 }
613
614 public NamingEnumeration<SearchResult> search(String name,
615 Attributes matchingAttributes)
616 throws NamingException {
617 return search(toName(name), SearchFilter.format(matchingAttributes),
618 new SearchControls());
619 }
620
621 public NamingEnumeration<SearchResult> search(Name name,
622 Attributes matchingAttributes)
623 throws NamingException {
624 return search(name, SearchFilter.format(matchingAttributes),
625 new SearchControls());
626 }
627
628 public NamingEnumeration<SearchResult> search(String name,
629 Attributes matchingAttributes,
630 String[] attributesToReturn)
631 throws NamingException {
632 SearchControls cons = new SearchControls();
633 cons.setReturningAttributes(attributesToReturn);
634
635 return search(toName(name), SearchFilter.format(matchingAttributes),
636 cons);
637 }
638
639 public NamingEnumeration<SearchResult> search(Name name,
640 Attributes matchingAttributes,
641 String[] attributesToReturn)
642 throws NamingException {
643 SearchControls cons = new SearchControls();
644 cons.setReturningAttributes(attributesToReturn);
645
646 return search(name, SearchFilter.format(matchingAttributes), cons);
647 }
648
649 public NamingEnumeration<SearchResult> search(String name,
650 String filter,
651 SearchControls cons)
652 throws NamingException {
653 return search(toName(name), filter, cons);
654 }
655
656 public NamingEnumeration<SearchResult> search(Name name,
657 String filter,
658 SearchControls cons) throws NamingException {
659
660 if (skipThisReferral) {
661 throw (NamingException)
662 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
663 }
664
665 try {
666 NamingEnumeration<SearchResult> se =
667 refCtx.search(overrideName(name),
668 overrideFilter(filter),
669 overrideAttributesAndScope(cons));
670
671 refEx.setNameResolved(true);
672
673 // append (referrals from) the exception that generated this
674 // context to the new search results, so that referral processing
675 // can continue
676 ((ReferralEnumeration)se).appendUnprocessedReferrals(refEx);
677
678 return (se);
679
680 } catch (LdapReferralException e) {
681
682 // %%% VR - setNameResolved(true);
683
684 // append (referrals from) the exception that generated this
685 // context to the new exception, so that referral processing
686 // can continue
687
688 e.appendUnprocessedReferrals(refEx);
689 throw (NamingException)(e.fillInStackTrace());
690
691 } catch (NamingException e) {
692
693 // record the exception if there are no remaining referrals
694 if ((refEx != null) && (! refEx.hasMoreReferrals())) {
695 refEx.setNamingException(e);
696 }
697 if ((refEx != null) &&
698 (refEx.hasMoreReferrals() ||
699 refEx.hasMoreReferralExceptions())) {
700 throw (NamingException)
701 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
702 } else {
703 throw e;
704 }
705 }
706 }
707
708 public NamingEnumeration<SearchResult> search(String name,
709 String filterExpr,
710 Object[] filterArgs,
711 SearchControls cons)
712 throws NamingException {
713 return search(toName(name), filterExpr, filterArgs, cons);
714 }
715
716 public NamingEnumeration<SearchResult> search(Name name,
717 String filterExpr,
718 Object[] filterArgs,
719 SearchControls cons) throws NamingException {
720
721 if (skipThisReferral) {
722 throw (NamingException)
723 ((refEx.appendUnprocessedReferrals(null)).fillInStackTrace());
724 }
725
726 try {
727 NamingEnumeration<SearchResult> se;
728
729 if (urlFilter != null) {
730 se = refCtx.search(overrideName(name), urlFilter,
731 overrideAttributesAndScope(cons));
732 } else {
733 se = refCtx.search(overrideName(name), filterExpr,
734 filterArgs, overrideAttributesAndScope(cons));
735 }
736
737 refEx.setNameResolved(true);
738
739 // append (referrals from) the exception that generated this
740 // context to the new search results, so that referral processing
741 // can continue
742 ((ReferralEnumeration)se).appendUnprocessedReferrals(refEx);
743
744 return (se);
745
746 } catch (LdapReferralException e) {
747
|