001: /*
002: * Copyright 1999-2001 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.jndi.ldap;
027:
028: import javax.naming.*;
029: import javax.naming.directory.*;
030: import javax.naming.spi.*;
031: import javax.naming.ldap.*;
032:
033: import java.util.Hashtable;
034: import java.util.StringTokenizer;
035: import com.sun.jndi.toolkit.dir.SearchFilter;
036:
037: /**
038: * A context for handling referrals.
039: *
040: * @author Vincent Ryan
041: */
042: final class LdapReferralContext implements DirContext, LdapContext {
043:
044: private DirContext refCtx = null;
045: private Name urlName = null; // override the supplied name
046: private String urlAttrs = null; // override attributes
047: private String urlScope = null; // override scope
048: private String urlFilter = null; // override filter
049:
050: private LdapReferralException refEx = null;
051: private boolean skipThisReferral = false;
052: private int hopCount = 1;
053: private NamingException previousEx = null;
054:
055: LdapReferralContext(LdapReferralException ex, Hashtable env,
056: Control[] connCtls, Control[] reqCtls, String nextName,
057: boolean skipThisReferral, int handleReferrals)
058: throws NamingException {
059:
060: refEx = ex;
061:
062: if (this .skipThisReferral = skipThisReferral) {
063: return; // don't create a DirContext for this referral
064: }
065:
066: String referral;
067:
068: // Make copies of environment and connect controls for our own use.
069: if (env != null) {
070: env = (Hashtable) env.clone();
071: // Remove old connect controls from environment, unless we have new
072: // ones that will override them anyway.
073: if (connCtls == null) {
074: env.remove(LdapCtx.BIND_CONTROLS);
075: }
076: } else if (connCtls != null) {
077: env = new Hashtable(5);
078: }
079: if (connCtls != null) {
080: Control[] copiedCtls = new Control[connCtls.length];
081: System.arraycopy(connCtls, 0, copiedCtls, 0,
082: connCtls.length);
083: // Add copied controls to environment, replacing any old ones.
084: env.put(LdapCtx.BIND_CONTROLS, copiedCtls);
085: }
086:
087: while (true) {
088: try {
089: referral = refEx.getNextReferral();
090: if (referral == null) {
091: throw (NamingException) (previousEx
092: .fillInStackTrace());
093: }
094:
095: } catch (LdapReferralException e) {
096:
097: if (handleReferrals == LdapClient.LDAP_REF_THROW) {
098: throw e;
099: } else {
100: refEx = e;
101: continue;
102: }
103: }
104:
105: // Create a Reference containing the referral URL.
106: Reference ref = new Reference(
107: "javax.naming.directory.DirContext",
108: new StringRefAddr("URL", referral));
109:
110: Object obj;
111: try {
112: obj = NamingManager.getObjectInstance(ref, null, null,
113: env);
114:
115: } catch (NamingException e) {
116:
117: if (handleReferrals == LdapClient.LDAP_REF_THROW) {
118: throw e;
119: }
120:
121: // mask the exception and save it for later
122: previousEx = e;
123:
124: // follow another referral
125: continue;
126:
127: } catch (Exception e) {
128: NamingException e2 = new NamingException(
129: "problem generating object using object factory");
130: e2.setRootCause(e);
131: throw e2;
132: }
133: if (obj instanceof DirContext) {
134: refCtx = (DirContext) obj;
135: if (refCtx instanceof LdapContext && reqCtls != null) {
136: ((LdapContext) refCtx).setRequestControls(reqCtls);
137: }
138: initDefaults(referral, nextName);
139:
140: break;
141: } else {
142: NamingException ne = new NotContextException(
143: "Cannot create context for: " + referral);
144: ne
145: .setRemainingName((new CompositeName())
146: .add(nextName));
147: throw ne;
148: }
149: }
150: }
151:
152: private void initDefaults(String referral, String nextName)
153: throws NamingException {
154: String urlString;
155: try {
156: // parse URL
157: LdapURL url = new LdapURL(referral);
158: urlString = url.getDN();
159: urlAttrs = url.getAttributes();
160: urlScope = url.getScope();
161: urlFilter = url.getFilter();
162:
163: } catch (NamingException e) {
164: // Not an LDAP URL; use original URL
165: urlString = referral;
166: urlAttrs = urlScope = urlFilter = null;
167: }
168:
169: // reuse original name if URL DN is absent
170: if (urlString == null) {
171: urlString = nextName;
172: } else {
173: // concatenate with remaining name if URL DN is present
174: urlString = "";
175: }
176:
177: if (urlString == null) {
178: urlName = null;
179: } else {
180: urlName = urlString.equals("") ? new CompositeName()
181: : new CompositeName().add(urlString);
182: }
183: }
184:
185: public void close() throws NamingException {
186: if (refCtx != null) {
187: refCtx.close();
188: refCtx = null;
189: }
190: refEx = null;
191: }
192:
193: void setHopCount(int hopCount) {
194: this .hopCount = hopCount;
195: if ((refCtx != null) && (refCtx instanceof LdapCtx)) {
196: ((LdapCtx) refCtx).setHopCount(hopCount);
197: }
198: }
199:
200: public Object lookup(String name) throws NamingException {
201: return lookup(toName(name));
202: }
203:
204: public Object lookup(Name name) throws NamingException {
205: if (skipThisReferral) {
206: throw (NamingException) ((refEx
207: .appendUnprocessedReferrals(null))
208: .fillInStackTrace());
209: }
210:
211: return refCtx.lookup(overrideName(name));
212: }
213:
214: public void bind(String name, Object obj) throws NamingException {
215: bind(toName(name), obj);
216: }
217:
218: public void bind(Name name, Object obj) throws NamingException {
219: if (skipThisReferral) {
220: throw (NamingException) ((refEx
221: .appendUnprocessedReferrals(null))
222: .fillInStackTrace());
223: }
224:
225: refCtx.bind(overrideName(name), obj);
226: }
227:
228: public void rebind(String name, Object obj) throws NamingException {
229: rebind(toName(name), obj);
230: }
231:
232: public void rebind(Name name, Object obj) throws NamingException {
233: if (skipThisReferral) {
234: throw (NamingException) ((refEx
235: .appendUnprocessedReferrals(null))
236: .fillInStackTrace());
237: }
238:
239: refCtx.rebind(overrideName(name), obj);
240: }
241:
242: public void unbind(String name) throws NamingException {
243: unbind(toName(name));
244: }
245:
246: public void unbind(Name name) throws NamingException {
247: if (skipThisReferral) {
248: throw (NamingException) ((refEx
249: .appendUnprocessedReferrals(null))
250: .fillInStackTrace());
251: }
252:
253: refCtx.unbind(overrideName(name));
254: }
255:
256: public void rename(String oldName, String newName)
257: throws NamingException {
258: rename(toName(oldName), toName(newName));
259: }
260:
261: public void rename(Name oldName, Name newName)
262: throws NamingException {
263: if (skipThisReferral) {
264: throw (NamingException) ((refEx
265: .appendUnprocessedReferrals(null))
266: .fillInStackTrace());
267: }
268:
269: refCtx.rename(overrideName(oldName), toName(refEx.getNewRdn()));
270: }
271:
272: public NamingEnumeration list(String name) throws NamingException {
273: return list(toName(name));
274: }
275:
276: public NamingEnumeration list(Name name) throws NamingException {
277: if (skipThisReferral) {
278: throw (NamingException) ((refEx
279: .appendUnprocessedReferrals(null))
280: .fillInStackTrace());
281: }
282: try {
283: NamingEnumeration ne = null;
284:
285: if (urlScope != null && urlScope.equals("base")) {
286: SearchControls cons = new SearchControls();
287: cons.setReturningObjFlag(true);
288: cons.setSearchScope(SearchControls.OBJECT_SCOPE);
289:
290: ne = refCtx.search(overrideName(name),
291: "(objectclass=*)", cons);
292:
293: } else {
294: ne = refCtx.list(overrideName(name));
295: }
296:
297: refEx.setNameResolved(true);
298:
299: // append (referrals from) the exception that generated this
300: // context to the new search results, so that referral processing
301: // can continue
302: ((ReferralEnumeration) ne)
303: .appendUnprocessedReferrals(refEx);
304:
305: return (ne);
306:
307: } catch (LdapReferralException e) {
308:
309: // append (referrals from) the exception that generated this
310: // context to the new exception, so that referral processing
311: // can continue
312:
313: e.appendUnprocessedReferrals(refEx);
314: throw (NamingException) (e.fillInStackTrace());
315:
316: } catch (NamingException e) {
317:
318: // record the exception if there are no remaining referrals
319: if ((refEx != null) && (!refEx.hasMoreReferrals())) {
320: refEx.setNamingException(e);
321: }
322: if ((refEx != null)
323: && (refEx.hasMoreReferrals() || refEx
324: .hasMoreReferralExceptions())) {
325: throw (NamingException) ((refEx
326: .appendUnprocessedReferrals(null))
327: .fillInStackTrace());
328: } else {
329: throw e;
330: }
331: }
332: }
333:
334: public NamingEnumeration listBindings(String name)
335: throws NamingException {
336: return listBindings(toName(name));
337: }
338:
339: public NamingEnumeration listBindings(Name name)
340: throws NamingException {
341: if (skipThisReferral) {
342: throw (NamingException) ((refEx
343: .appendUnprocessedReferrals(null))
344: .fillInStackTrace());
345: }
346:
347: try {
348: NamingEnumeration be = null;
349:
350: if (urlScope != null && urlScope.equals("base")) {
351: SearchControls cons = new SearchControls();
352: cons.setReturningObjFlag(true);
353: cons.setSearchScope(SearchControls.OBJECT_SCOPE);
354:
355: be = refCtx.search(overrideName(name),
356: "(objectclass=*)", cons);
357:
358: } else {
359: be = refCtx.listBindings(overrideName(name));
360: }
361:
362: refEx.setNameResolved(true);
363:
364: // append (referrals from) the exception that generated this
365: // context to the new search results, so that referral processing
366: // can continue
367: ((ReferralEnumeration) be)
368: .appendUnprocessedReferrals(refEx);
369:
370: return (be);
371:
372: } catch (LdapReferralException e) {
373:
374: // append (referrals from) the exception that generated this
375: // context to the new exception, so that referral processing
376: // can continue
377:
378: e.appendUnprocessedReferrals(refEx);
379: throw (NamingException) (e.fillInStackTrace());
380:
381: } catch (NamingException e) {
382:
383: // record the exception if there are no remaining referrals
384: if ((refEx != null) && (!refEx.hasMoreReferrals())) {
385: refEx.setNamingException(e);
386: }
387: if ((refEx != null)
388: && (refEx.hasMoreReferrals() || refEx
389: .hasMoreReferralExceptions())) {
390: throw (NamingException) ((refEx
391: .appendUnprocessedReferrals(null))
392: .fillInStackTrace());
393: } else {
394: throw e;
395: }
396: }
397: }
398:
399: public void destroySubcontext(String name) throws NamingException {
400: destroySubcontext(toName(name));
401: }
402:
403: public void destroySubcontext(Name name) throws NamingException {
404: if (skipThisReferral) {
405: throw (NamingException) ((refEx
406: .appendUnprocessedReferrals(null))
407: .fillInStackTrace());
408: }
409:
410: refCtx.destroySubcontext(overrideName(name));
411: }
412:
413: public Context createSubcontext(String name) throws NamingException {
414: return createSubcontext(toName(name));
415: }
416:
417: public Context createSubcontext(Name name) throws NamingException {
418: if (skipThisReferral) {
419: throw (NamingException) ((refEx
420: .appendUnprocessedReferrals(null))
421: .fillInStackTrace());
422: }
423:
424: return refCtx.createSubcontext(overrideName(name));
425: }
426:
427: public Object lookupLink(String name) throws NamingException {
428: return lookupLink(toName(name));
429: }
430:
431: public Object lookupLink(Name name) throws NamingException {
432: if (skipThisReferral) {
433: throw (NamingException) ((refEx
434: .appendUnprocessedReferrals(null))
435: .fillInStackTrace());
436: }
437:
438: return refCtx.lookupLink(overrideName(name));
439: }
440:
441: public NameParser getNameParser(String name) throws NamingException {
442: return getNameParser(toName(name));
443: }
444:
445: public NameParser getNameParser(Name name) throws NamingException {
446: if (skipThisReferral) {
447: throw (NamingException) ((refEx
448: .appendUnprocessedReferrals(null))
449: .fillInStackTrace());
450: }
451:
452: return refCtx.getNameParser(overrideName(name));
453: }
454:
455: public String composeName(String name, String prefix)
456: throws NamingException {
457: return composeName(toName(name), toName(prefix)).toString();
458: }
459:
460: public Name composeName(Name name, Name prefix)
461: throws NamingException {
462: if (skipThisReferral) {
463: throw (NamingException) ((refEx
464: .appendUnprocessedReferrals(null))
465: .fillInStackTrace());
466: }
467: return refCtx.composeName(name, prefix);
468: }
469:
470: public Object addToEnvironment(String propName, Object propVal)
471: throws NamingException {
472: if (skipThisReferral) {
473: throw (NamingException) ((refEx
474: .appendUnprocessedReferrals(null))
475: .fillInStackTrace());
476: }
477:
478: return refCtx.addToEnvironment(propName, propVal);
479: }
480:
481: public Object removeFromEnvironment(String propName)
482: throws NamingException {
483: if (skipThisReferral) {
484: throw (NamingException) ((refEx
485: .appendUnprocessedReferrals(null))
486: .fillInStackTrace());
487: }
488:
489: return refCtx.removeFromEnvironment(propName);
490: }
491:
492: public Hashtable getEnvironment() throws NamingException {
493: if (skipThisReferral) {
494: throw (NamingException) ((refEx
495: .appendUnprocessedReferrals(null))
496: .fillInStackTrace());
497: }
498:
499: return refCtx.getEnvironment();
500: }
501:
502: public Attributes getAttributes(String name) throws NamingException {
503: return getAttributes(toName(name));
504: }
505:
506: public Attributes getAttributes(Name name) throws NamingException {
507: if (skipThisReferral) {
508: throw (NamingException) ((refEx
509: .appendUnprocessedReferrals(null))
510: .fillInStackTrace());
511: }
512:
513: return refCtx.getAttributes(overrideName(name));
514: }
515:
516: public Attributes getAttributes(String name, String[] attrIds)
517: throws NamingException {
518: return getAttributes(toName(name), attrIds);
519: }
520:
521: public Attributes getAttributes(Name name, String[] attrIds)
522: throws NamingException {
523: if (skipThisReferral) {
524: throw (NamingException) ((refEx
525: .appendUnprocessedReferrals(null))
526: .fillInStackTrace());
527: }
528:
529: return refCtx.getAttributes(overrideName(name), attrIds);
530: }
531:
532: public void modifyAttributes(String name, int mod_op,
533: Attributes attrs) throws NamingException {
534: modifyAttributes(toName(name), mod_op, attrs);
535: }
536:
537: public void modifyAttributes(Name name, int mod_op, Attributes attrs)
538: throws NamingException {
539: if (skipThisReferral) {
540: throw (NamingException) ((refEx
541: .appendUnprocessedReferrals(null))
542: .fillInStackTrace());
543: }
544:
545: refCtx.modifyAttributes(overrideName(name), mod_op, attrs);
546: }
547:
548: public void modifyAttributes(String name, ModificationItem[] mods)
549: throws NamingException {
550: modifyAttributes(toName(name), mods);
551: }
552:
553: public void modifyAttributes(Name name, ModificationItem[] mods)
554: throws NamingException {
555: if (skipThisReferral) {
556: throw (NamingException) ((refEx
557: .appendUnprocessedReferrals(null))
558: .fillInStackTrace());
559: }
560:
561: refCtx.modifyAttributes(overrideName(name), mods);
562: }
563:
564: public void bind(String name, Object obj, Attributes attrs)
565: throws NamingException {
566: bind(toName(name), obj, attrs);
567: }
568:
569: public void bind(Name name, Object obj, Attributes attrs)
570: throws NamingException {
571: if (skipThisReferral) {
572: throw (NamingException) ((refEx
573: .appendUnprocessedReferrals(null))
574: .fillInStackTrace());
575: }
576:
577: refCtx.bind(overrideName(name), obj, attrs);
578: }
579:
580: public void rebind(String name, Object obj, Attributes attrs)
581: throws NamingException {
582: rebind(toName(name), obj, attrs);
583: }
584:
585: public void rebind(Name name, Object obj, Attributes attrs)
586: throws NamingException {
587: if (skipThisReferral) {
588: throw (NamingException) ((refEx
589: .appendUnprocessedReferrals(null))
590: .fillInStackTrace());
591: }
592:
593: refCtx.rebind(overrideName(name), obj, attrs);
594: }
595:
596: public DirContext createSubcontext(String name, Attributes attrs)
597: throws NamingException {
598: return createSubcontext(toName(name), attrs);
599: }
600:
601: public DirContext createSubcontext(Name name, Attributes attrs)
602: throws NamingException {
603: if (skipThisReferral) {
604: throw (NamingException) ((refEx
605: .appendUnprocessedReferrals(null))
606: .fillInStackTrace());
607: }
608:
609: return refCtx.createSubcontext(overrideName(name), attrs);
610: }
611:
612: public DirContext getSchema(String name) throws NamingException {
613: return getSchema(toName(name));
614: }
615:
616: public DirContext getSchema(Name name) throws NamingException {
617: if (skipThisReferral) {
618: throw (NamingException) ((refEx
619: .appendUnprocessedReferrals(null))
620: .fillInStackTrace());
621: }
622:
623: return refCtx.getSchema(overrideName(name));
624: }
625:
626: public DirContext getSchemaClassDefinition(String name)
627: throws NamingException {
628: return getSchemaClassDefinition(toName(name));
629: }
630:
631: public DirContext getSchemaClassDefinition(Name name)
632: throws NamingException {
633: if (skipThisReferral) {
634: throw (NamingException) ((refEx
635: .appendUnprocessedReferrals(null))
636: .fillInStackTrace());
637: }
638:
639: return refCtx.getSchemaClassDefinition(overrideName(name));
640: }
641:
642: public NamingEnumeration search(String name,
643: Attributes matchingAttributes) throws NamingException {
644: return search(toName(name), SearchFilter
645: .format(matchingAttributes), new SearchControls());
646: }
647:
648: public NamingEnumeration search(Name name,
649: Attributes matchingAttributes) throws NamingException {
650: return search(name, SearchFilter.format(matchingAttributes),
651: new SearchControls());
652: }
653:
654: public NamingEnumeration search(String name,
655: Attributes matchingAttributes, String[] attributesToReturn)
656: throws NamingException {
657: SearchControls cons = new SearchControls();
658: cons.setReturningAttributes(attributesToReturn);
659:
660: return search(toName(name), SearchFilter
661: .format(matchingAttributes), cons);
662: }
663:
664: public NamingEnumeration search(Name name,
665: Attributes matchingAttributes, String[] attributesToReturn)
666: throws NamingException {
667: SearchControls cons = new SearchControls();
668: cons.setReturningAttributes(attributesToReturn);
669:
670: return search(name, SearchFilter.format(matchingAttributes),
671: cons);
672: }
673:
674: public NamingEnumeration search(String name, String filter,
675: SearchControls cons) throws NamingException {
676: return search(toName(name), filter, cons);
677: }
678:
679: public NamingEnumeration search(Name name, String filter,
680: SearchControls cons) throws NamingException {
681:
682: if (skipThisReferral) {
683: throw (NamingException) ((refEx
684: .appendUnprocessedReferrals(null))
685: .fillInStackTrace());
686: }
687:
688: try {
689: NamingEnumeration se = refCtx.search(overrideName(name),
690: overrideFilter(filter),
691: overrideAttributesAndScope(cons));
692:
693: refEx.setNameResolved(true);
694:
695: // append (referrals from) the exception that generated this
696: // context to the new search results, so that referral processing
697: // can continue
698: ((ReferralEnumeration) se)
699: .appendUnprocessedReferrals(refEx);
700:
701: return (se);
702:
703: } catch (LdapReferralException e) {
704:
705: // %%% VR - setNameResolved(true);
706:
707: // append (referrals from) the exception that generated this
708: // context to the new exception, so that referral processing
709: // can continue
710:
711: e.appendUnprocessedReferrals(refEx);
712: throw (NamingException) (e.fillInStackTrace());
713:
714: } catch (NamingException e) {
715:
716: // record the exception if there are no remaining referrals
717: if ((refEx != null) && (!refEx.hasMoreReferrals())) {
718: refEx.setNamingException(e);
719: }
720: if ((refEx != null)
721: && (refEx.hasMoreReferrals() || refEx
722: .hasMoreReferralExceptions())) {
723: throw (NamingException) ((refEx
724: .appendUnprocessedReferrals(null))
725: .fillInStackTrace());
726: } else {
727: throw e;
728: }
729: }
730: }
731:
732: public NamingEnumeration search(String name, String filterExpr,
733: Object[] filterArgs, SearchControls cons)
734: throws NamingException {
735: return search(toName(name), filterExpr, filterArgs, cons);
736: }
737:
738: public NamingEnumeration search(Name name, String filterExpr,
739: Object[] filterArgs, SearchControls cons)
740: throws NamingException {
741:
742: if (skipThisReferral) {
743: throw (NamingException) ((refEx
744: .appendUnprocessedReferrals(null))
745: .fillInStackTrace());
746: }
747:
748: try {
749: NamingEnumeration se;
750:
751: if (urlFilter != null) {
752: se = refCtx.search(overrideName(name), urlFilter,
753: overrideAttributesAndScope(cons));
754: } else {
755: se = refCtx.search(overrideName(name), filterExpr,
756: filterArgs, overrideAttributesAndScope(cons));
757: }
758:
759: refEx.setNameResolved(true);
760:
761: // append (referrals from) the exception that generated this
762: // context to the new search results, so that referral processing
763: // can continue
764: ((ReferralEnumeration) se)
765: .appendUnprocessedReferrals(refEx);
766:
767: return (se);
768:
769: } catch (LdapReferralException e) {
770:
771: // append (referrals from) the exception that generated this
772: // context to the new exception, so that referral processing
773: // can continue
774:
775: e.appendUnprocessedReferrals(refEx);
776: throw (NamingException) (e.fillInStackTrace());
777:
778: } catch (NamingException e) {
779:
780: // record the exception if there are no remaining referrals
781: if ((refEx != null) && (!refEx.hasMoreReferrals())) {
782: refEx.setNamingException(e);
783: }
784: if ((refEx != null)
785: && (refEx.hasMoreReferrals() || refEx
786: .hasMoreReferralExceptions())) {
787: throw (NamingException) ((refEx
788: .appendUnprocessedReferrals(null))
789: .fillInStackTrace());
790: } else {
791: throw e;
792: }
793: }
794: }
795:
796: public String getNameInNamespace() throws NamingException {
797: if (skipThisReferral) {
798: throw (NamingException) ((refEx
799: .appendUnprocessedReferrals(null))
800: .fillInStackTrace());
801: }
802: return urlName != null && !urlName.isEmpty() ? urlName.get(0)
803: : "";
804: }
805:
806: // ---------------------- LdapContext ---------------------
807:
808: public ExtendedResponse extendedOperation(ExtendedRequest request)
809: throws NamingException {
810:
811: if (skipThisReferral) {
812: throw (NamingException) ((refEx
813: .appendUnprocessedReferrals(null))
814: .fillInStackTrace());
815: }
816:
817: if (!(refCtx instanceof LdapContext)) {
818: throw new NotContextException(
819: "Referral context not an instance of LdapContext");
820: }
821:
822: return ((LdapContext) refCtx).extendedOperation(request);
823: }
824:
825: public LdapContext newInstance(Control[] requestControls)
826: throws NamingException {
827:
828: if (skipThisReferral) {
829: throw (NamingException) ((refEx
830: .appendUnprocessedReferrals(null))
831: .fillInStackTrace());
832: }
833:
834: if (!(refCtx instanceof LdapContext)) {
835: throw new NotContextException(
836: "Referral context not an instance of LdapContext");
837: }
838:
839: return ((LdapContext) refCtx).newInstance(requestControls);
840: }
841:
842: public void reconnect(Control[] connCtls) throws NamingException {
843: if (skipThisReferral) {
844: throw (NamingException) ((refEx
845: .appendUnprocessedReferrals(null))
846: .fillInStackTrace());
847: }
848:
849: if (!(refCtx instanceof LdapContext)) {
850: throw new NotContextException(
851: "Referral context not an instance of LdapContext");
852: }
853:
854: ((LdapContext) refCtx).reconnect(connCtls);
855: }
856:
857: public Control[] getConnectControls() throws NamingException {
858: if (skipThisReferral) {
859: throw (NamingException) ((refEx
860: .appendUnprocessedReferrals(null))
861: .fillInStackTrace());
862: }
863:
864: if (!(refCtx instanceof LdapContext)) {
865: throw new NotContextException(
866: "Referral context not an instance of LdapContext");
867: }
868:
869: return ((LdapContext) refCtx).getConnectControls();
870: }
871:
872: public void setRequestControls(Control[] requestControls)
873: throws NamingException {
874:
875: if (skipThisReferral) {
876: throw (NamingException) ((refEx
877: .appendUnprocessedReferrals(null))
878: .fillInStackTrace());
879: }
880:
881: if (!(refCtx instanceof LdapContext)) {
882: throw new NotContextException(
883: "Referral context not an instance of LdapContext");
884: }
885:
886: ((LdapContext) refCtx).setRequestControls(requestControls);
887: }
888:
889: public Control[] getRequestControls() throws NamingException {
890: if (skipThisReferral) {
891: throw (NamingException) ((refEx
892: .appendUnprocessedReferrals(null))
893: .fillInStackTrace());
894: }
895:
896: if (!(refCtx instanceof LdapContext)) {
897: throw new NotContextException(
898: "Referral context not an instance of LdapContext");
899: }
900: return ((LdapContext) refCtx).getRequestControls();
901: }
902:
903: public Control[] getResponseControls() throws NamingException {
904: if (skipThisReferral) {
905: throw (NamingException) ((refEx
906: .appendUnprocessedReferrals(null))
907: .fillInStackTrace());
908: }
909:
910: if (!(refCtx instanceof LdapContext)) {
911: throw new NotContextException(
912: "Referral context not an instance of LdapContext");
913: }
914: return ((LdapContext) refCtx).getResponseControls();
915: }
916:
917: // ---------------------- Private methods ---------------------
918: private Name toName(String name) throws InvalidNameException {
919: return name.equals("") ? new CompositeName()
920: : new CompositeName().add(name);
921: }
922:
923: /*
924: * Use the DN component from the LDAP URL (if present) to override the
925: * supplied DN.
926: */
927: private Name overrideName(Name name) throws InvalidNameException {
928: return (urlName == null ? name : urlName);
929: }
930:
931: /*
932: * Use the attributes and scope components from the LDAP URL (if present)
933: * to override the corrpesonding components supplied in SearchControls.
934: */
935: private SearchControls overrideAttributesAndScope(
936: SearchControls cons) {
937: SearchControls urlCons;
938:
939: if ((urlScope != null) || (urlAttrs != null)) {
940: urlCons = new SearchControls(cons.getSearchScope(), cons
941: .getCountLimit(), cons.getTimeLimit(), cons
942: .getReturningAttributes(), cons
943: .getReturningObjFlag(), cons.getDerefLinkFlag());
944:
945: if (urlScope != null) {
946: if (urlScope.equals("base")) {
947: urlCons.setSearchScope(SearchControls.OBJECT_SCOPE);
948: } else if (urlScope.equals("one")) {
949: urlCons
950: .setSearchScope(SearchControls.ONELEVEL_SCOPE);
951: } else if (urlScope.equals("sub")) {
952: urlCons
953: .setSearchScope(SearchControls.SUBTREE_SCOPE);
954: }
955: }
956:
957: if (urlAttrs != null) {
958: StringTokenizer tokens = new StringTokenizer(urlAttrs,
959: ",");
960: int count = tokens.countTokens();
961: String[] attrs = new String[count];
962: for (int i = 0; i < count; i++) {
963: attrs[i] = tokens.nextToken();
964: }
965: urlCons.setReturningAttributes(attrs);
966: }
967:
968: return urlCons;
969:
970: } else {
971: return cons;
972: }
973: }
974:
975: /*
976: * Use the filter component from the LDAP URL (if present) to override the
977: * supplied filter.
978: */
979: private String overrideFilter(String filter) {
980: return (urlFilter == null ? filter : urlFilter);
981: }
982:
983: }
|