001: /*
002: * Copyright 1999 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.url.ldap;
027:
028: import javax.naming.spi.ResolveResult;
029: import javax.naming.*;
030: import javax.naming.directory.*;
031: import java.util.Hashtable;
032: import java.util.StringTokenizer;
033: import com.sun.jndi.ldap.LdapURL;
034:
035: /**
036: * An LDAP URL context.
037: *
038: * @author Rosanna Lee
039: * @author Scott Seligman
040: * @version 1.15 07/05/05
041: */
042:
043: final public class ldapURLContext extends
044: com.sun.jndi.toolkit.url.GenericURLDirContext {
045:
046: ldapURLContext(Hashtable env) {
047: super (env);
048: }
049:
050: /**
051: * Resolves 'name' into a target context with remaining name.
052: * It only resolves the hostname/port number. The remaining name
053: * contains the root DN.
054: *
055: * For example, with a LDAP URL "ldap://localhost:389/o=widget,c=us",
056: * this method resolves "ldap://localhost:389/" to the root LDAP
057: * context on the server 'localhost' on port 389,
058: * and returns as the remaining name "o=widget, c=us".
059: */
060: protected ResolveResult getRootURLContext(String name, Hashtable env)
061: throws NamingException {
062: return ldapURLContextFactory.getUsingURLIgnoreRootDN(name, env);
063: }
064:
065: /**
066: * Return the suffix of an ldap url.
067: * prefix parameter is ignored.
068: */
069: protected Name getURLSuffix(String prefix, String url)
070: throws NamingException {
071:
072: LdapURL ldapUrl = new LdapURL(url);
073: String dn = (ldapUrl.getDN() != null ? ldapUrl.getDN() : "");
074:
075: // Represent DN as empty or single-component composite name.
076: CompositeName remaining = new CompositeName();
077: if (!"".equals(dn)) {
078: // if nonempty, add component
079: remaining.add(dn);
080: }
081: return remaining;
082: }
083:
084: /*
085: * Override context operations.
086: * Test for presence of LDAP URL query components in the name argument.
087: * Query components are permitted only for search operations and only
088: * when the name has a single component.
089: */
090:
091: public Object lookup(String name) throws NamingException {
092: if (LdapURL.hasQueryComponents(name)) {
093: throw new InvalidNameException(name);
094: } else {
095: return super .lookup(name);
096: }
097: }
098:
099: public Object lookup(Name name) throws NamingException {
100: if (LdapURL.hasQueryComponents(name.get(0))) {
101: throw new InvalidNameException(name.toString());
102: } else {
103: return super .lookup(name);
104: }
105: }
106:
107: public void bind(String name, Object obj) throws NamingException {
108: if (LdapURL.hasQueryComponents(name)) {
109: throw new InvalidNameException(name);
110: } else {
111: super .bind(name, obj);
112: }
113: }
114:
115: public void bind(Name name, Object obj) throws NamingException {
116: if (LdapURL.hasQueryComponents(name.get(0))) {
117: throw new InvalidNameException(name.toString());
118: } else {
119: super .bind(name, obj);
120: }
121: }
122:
123: public void rebind(String name, Object obj) throws NamingException {
124: if (LdapURL.hasQueryComponents(name)) {
125: throw new InvalidNameException(name);
126: } else {
127: super .rebind(name, obj);
128: }
129: }
130:
131: public void rebind(Name name, Object obj) throws NamingException {
132: if (LdapURL.hasQueryComponents(name.get(0))) {
133: throw new InvalidNameException(name.toString());
134: } else {
135: super .rebind(name, obj);
136: }
137: }
138:
139: public void unbind(String name) throws NamingException {
140: if (LdapURL.hasQueryComponents(name)) {
141: throw new InvalidNameException(name);
142: } else {
143: super .unbind(name);
144: }
145: }
146:
147: public void unbind(Name name) throws NamingException {
148: if (LdapURL.hasQueryComponents(name.get(0))) {
149: throw new InvalidNameException(name.toString());
150: } else {
151: super .unbind(name);
152: }
153: }
154:
155: public void rename(String oldName, String newName)
156: throws NamingException {
157: if (LdapURL.hasQueryComponents(oldName)) {
158: throw new InvalidNameException(oldName);
159: } else if (LdapURL.hasQueryComponents(newName)) {
160: throw new InvalidNameException(newName);
161: } else {
162: super .rename(oldName, newName);
163: }
164: }
165:
166: public void rename(Name oldName, Name newName)
167: throws NamingException {
168: if (LdapURL.hasQueryComponents(oldName.get(0))) {
169: throw new InvalidNameException(oldName.toString());
170: } else if (LdapURL.hasQueryComponents(newName.get(0))) {
171: throw new InvalidNameException(newName.toString());
172: } else {
173: super .rename(oldName, newName);
174: }
175: }
176:
177: public NamingEnumeration list(String name) throws NamingException {
178: if (LdapURL.hasQueryComponents(name)) {
179: throw new InvalidNameException(name);
180: } else {
181: return super .list(name);
182: }
183: }
184:
185: public NamingEnumeration list(Name name) throws NamingException {
186: if (LdapURL.hasQueryComponents(name.get(0))) {
187: throw new InvalidNameException(name.toString());
188: } else {
189: return super .list(name);
190: }
191: }
192:
193: public NamingEnumeration listBindings(String name)
194: throws NamingException {
195: if (LdapURL.hasQueryComponents(name)) {
196: throw new InvalidNameException(name);
197: } else {
198: return super .listBindings(name);
199: }
200: }
201:
202: public NamingEnumeration listBindings(Name name)
203: throws NamingException {
204: if (LdapURL.hasQueryComponents(name.get(0))) {
205: throw new InvalidNameException(name.toString());
206: } else {
207: return super .listBindings(name);
208: }
209: }
210:
211: public void destroySubcontext(String name) throws NamingException {
212: if (LdapURL.hasQueryComponents(name)) {
213: throw new InvalidNameException(name);
214: } else {
215: super .destroySubcontext(name);
216: }
217: }
218:
219: public void destroySubcontext(Name name) throws NamingException {
220: if (LdapURL.hasQueryComponents(name.get(0))) {
221: throw new InvalidNameException(name.toString());
222: } else {
223: super .destroySubcontext(name);
224: }
225: }
226:
227: public Context createSubcontext(String name) throws NamingException {
228: if (LdapURL.hasQueryComponents(name)) {
229: throw new InvalidNameException(name);
230: } else {
231: return super .createSubcontext(name);
232: }
233: }
234:
235: public Context createSubcontext(Name name) throws NamingException {
236: if (LdapURL.hasQueryComponents(name.get(0))) {
237: throw new InvalidNameException(name.toString());
238: } else {
239: return super .createSubcontext(name);
240: }
241: }
242:
243: public Object lookupLink(String name) throws NamingException {
244: if (LdapURL.hasQueryComponents(name)) {
245: throw new InvalidNameException(name);
246: } else {
247: return super .lookupLink(name);
248: }
249: }
250:
251: public Object lookupLink(Name name) throws NamingException {
252: if (LdapURL.hasQueryComponents(name.get(0))) {
253: throw new InvalidNameException(name.toString());
254: } else {
255: return super .lookupLink(name);
256: }
257: }
258:
259: public NameParser getNameParser(String name) throws NamingException {
260: if (LdapURL.hasQueryComponents(name)) {
261: throw new InvalidNameException(name);
262: } else {
263: return super .getNameParser(name);
264: }
265: }
266:
267: public NameParser getNameParser(Name name) throws NamingException {
268: if (LdapURL.hasQueryComponents(name.get(0))) {
269: throw new InvalidNameException(name.toString());
270: } else {
271: return super .getNameParser(name);
272: }
273: }
274:
275: public String composeName(String name, String prefix)
276: throws NamingException {
277: if (LdapURL.hasQueryComponents(name)) {
278: throw new InvalidNameException(name);
279: } else if (LdapURL.hasQueryComponents(prefix)) {
280: throw new InvalidNameException(prefix);
281: } else {
282: return super .composeName(name, prefix);
283: }
284: }
285:
286: public Name composeName(Name name, Name prefix)
287: throws NamingException {
288: if (LdapURL.hasQueryComponents(name.get(0))) {
289: throw new InvalidNameException(name.toString());
290: } else if (LdapURL.hasQueryComponents(prefix.get(0))) {
291: throw new InvalidNameException(prefix.toString());
292: } else {
293: return super .composeName(name, prefix);
294: }
295: }
296:
297: public Attributes getAttributes(String name) throws NamingException {
298: if (LdapURL.hasQueryComponents(name)) {
299: throw new InvalidNameException(name);
300: } else {
301: return super .getAttributes(name);
302: }
303: }
304:
305: public Attributes getAttributes(Name name) throws NamingException {
306: if (LdapURL.hasQueryComponents(name.get(0))) {
307: throw new InvalidNameException(name.toString());
308: } else {
309: return super .getAttributes(name);
310: }
311: }
312:
313: public Attributes getAttributes(String name, String[] attrIds)
314: throws NamingException {
315: if (LdapURL.hasQueryComponents(name)) {
316: throw new InvalidNameException(name);
317: } else {
318: return super .getAttributes(name, attrIds);
319: }
320: }
321:
322: public Attributes getAttributes(Name name, String[] attrIds)
323: throws NamingException {
324: if (LdapURL.hasQueryComponents(name.get(0))) {
325: throw new InvalidNameException(name.toString());
326: } else {
327: return super .getAttributes(name, attrIds);
328: }
329: }
330:
331: public void modifyAttributes(String name, int mod_op,
332: Attributes attrs) throws NamingException {
333: if (LdapURL.hasQueryComponents(name)) {
334: throw new InvalidNameException(name);
335: } else {
336: super .modifyAttributes(name, mod_op, attrs);
337: }
338: }
339:
340: public void modifyAttributes(Name name, int mod_op, Attributes attrs)
341: throws NamingException {
342: if (LdapURL.hasQueryComponents(name.get(0))) {
343: throw new InvalidNameException(name.toString());
344: } else {
345: super .modifyAttributes(name, mod_op, attrs);
346: }
347: }
348:
349: public void modifyAttributes(String name, ModificationItem[] mods)
350: throws NamingException {
351: if (LdapURL.hasQueryComponents(name)) {
352: throw new InvalidNameException(name);
353: } else {
354: super .modifyAttributes(name, mods);
355: }
356: }
357:
358: public void modifyAttributes(Name name, ModificationItem[] mods)
359: throws NamingException {
360: if (LdapURL.hasQueryComponents(name.get(0))) {
361: throw new InvalidNameException(name.toString());
362: } else {
363: super .modifyAttributes(name, mods);
364: }
365: }
366:
367: public void bind(String name, Object obj, Attributes attrs)
368: throws NamingException {
369: if (LdapURL.hasQueryComponents(name)) {
370: throw new InvalidNameException(name);
371: } else {
372: super .bind(name, obj, attrs);
373: }
374: }
375:
376: public void bind(Name name, Object obj, Attributes attrs)
377: throws NamingException {
378: if (LdapURL.hasQueryComponents(name.get(0))) {
379: throw new InvalidNameException(name.toString());
380: } else {
381: super .bind(name, obj, attrs);
382: }
383: }
384:
385: public void rebind(String name, Object obj, Attributes attrs)
386: throws NamingException {
387: if (LdapURL.hasQueryComponents(name)) {
388: throw new InvalidNameException(name);
389: } else {
390: super .rebind(name, obj, attrs);
391: }
392: }
393:
394: public void rebind(Name name, Object obj, Attributes attrs)
395: throws NamingException {
396: if (LdapURL.hasQueryComponents(name.get(0))) {
397: throw new InvalidNameException(name.toString());
398: } else {
399: super .rebind(name, obj, attrs);
400: }
401: }
402:
403: public DirContext createSubcontext(String name, Attributes attrs)
404: throws NamingException {
405: if (LdapURL.hasQueryComponents(name)) {
406: throw new InvalidNameException(name);
407: } else {
408: return super .createSubcontext(name, attrs);
409: }
410: }
411:
412: public DirContext createSubcontext(Name name, Attributes attrs)
413: throws NamingException {
414: if (LdapURL.hasQueryComponents(name.get(0))) {
415: throw new InvalidNameException(name.toString());
416: } else {
417: return super .createSubcontext(name, attrs);
418: }
419: }
420:
421: public DirContext getSchema(String name) throws NamingException {
422: if (LdapURL.hasQueryComponents(name)) {
423: throw new InvalidNameException(name);
424: } else {
425: return super .getSchema(name);
426: }
427: }
428:
429: public DirContext getSchema(Name name) throws NamingException {
430: if (LdapURL.hasQueryComponents(name.get(0))) {
431: throw new InvalidNameException(name.toString());
432: } else {
433: return super .getSchema(name);
434: }
435: }
436:
437: public DirContext getSchemaClassDefinition(String name)
438: throws NamingException {
439: if (LdapURL.hasQueryComponents(name)) {
440: throw new InvalidNameException(name);
441: } else {
442: return super .getSchemaClassDefinition(name);
443: }
444: }
445:
446: public DirContext getSchemaClassDefinition(Name name)
447: throws NamingException {
448: if (LdapURL.hasQueryComponents(name.get(0))) {
449: throw new InvalidNameException(name.toString());
450: } else {
451: return super .getSchemaClassDefinition(name);
452: }
453: }
454:
455: // divert the search operation when the LDAP URL has query components
456: public NamingEnumeration search(String name,
457: Attributes matchingAttributes) throws NamingException {
458:
459: if (LdapURL.hasQueryComponents(name)) {
460: return searchUsingURL(name);
461: } else {
462: return super .search(name, matchingAttributes);
463: }
464: }
465:
466: // divert the search operation when name has a single component
467: public NamingEnumeration search(Name name,
468: Attributes matchingAttributes) throws NamingException {
469: if (name.size() == 1) {
470: return search(name.get(0), matchingAttributes);
471: } else if (LdapURL.hasQueryComponents(name.get(0))) {
472: throw new InvalidNameException(name.toString());
473: } else {
474: return super .search(name, matchingAttributes);
475: }
476: }
477:
478: // divert the search operation when the LDAP URL has query components
479: public NamingEnumeration search(String name,
480: Attributes matchingAttributes, String[] attributesToReturn)
481: throws NamingException {
482:
483: if (LdapURL.hasQueryComponents(name)) {
484: return searchUsingURL(name);
485: } else {
486: return super .search(name, matchingAttributes,
487: attributesToReturn);
488: }
489: }
490:
491: // divert the search operation when name has a single component
492: public NamingEnumeration search(Name name,
493: Attributes matchingAttributes, String[] attributesToReturn)
494: throws NamingException {
495:
496: if (name.size() == 1) {
497: return search(name.get(0), matchingAttributes,
498: attributesToReturn);
499: } else if (LdapURL.hasQueryComponents(name.get(0))) {
500: throw new InvalidNameException(name.toString());
501: } else {
502: return super .search(name, matchingAttributes,
503: attributesToReturn);
504: }
505: }
506:
507: // divert the search operation when the LDAP URL has query components
508: public NamingEnumeration search(String name, String filter,
509: SearchControls cons) throws NamingException {
510:
511: if (LdapURL.hasQueryComponents(name)) {
512: return searchUsingURL(name);
513: } else {
514: return super .search(name, filter, cons);
515: }
516: }
517:
518: // divert the search operation when name has a single component
519: public NamingEnumeration search(Name name, String filter,
520: SearchControls cons) throws NamingException {
521:
522: if (name.size() == 1) {
523: return search(name.get(0), filter, cons);
524: } else if (LdapURL.hasQueryComponents(name.get(0))) {
525: throw new InvalidNameException(name.toString());
526: } else {
527: return super .search(name, filter, cons);
528: }
529: }
530:
531: // divert the search operation when the LDAP URL has query components
532: public NamingEnumeration search(String name, String filterExpr,
533: Object[] filterArgs, SearchControls cons)
534: throws NamingException {
535:
536: if (LdapURL.hasQueryComponents(name)) {
537: return searchUsingURL(name);
538: } else {
539: return super .search(name, filterExpr, filterArgs, cons);
540: }
541: }
542:
543: // divert the search operation when name has a single component
544: public NamingEnumeration search(Name name, String filterExpr,
545: Object[] filterArgs, SearchControls cons)
546: throws NamingException {
547:
548: if (name.size() == 1) {
549: return search(name.get(0), filterExpr, filterArgs, cons);
550: } else if (LdapURL.hasQueryComponents(name.get(0))) {
551: throw new InvalidNameException(name.toString());
552: } else {
553: return super .search(name, filterExpr, filterArgs, cons);
554: }
555: }
556:
557: // Search using the LDAP URL in name.
558: // LDAP URL query components override the search argments.
559: private NamingEnumeration searchUsingURL(String name)
560: throws NamingException {
561:
562: LdapURL url = new LdapURL(name);
563:
564: ResolveResult res = getRootURLContext(name, myEnv);
565: DirContext ctx = (DirContext) res.getResolvedObj();
566: try {
567: return ctx.search(res.getRemainingName(),
568: setFilterUsingURL(url),
569: setSearchControlsUsingURL(url));
570: } finally {
571: ctx.close();
572: }
573: }
574:
575: /*
576: * Initialize a String filter using the LDAP URL filter component.
577: * If filter is not present in the URL it is initialized to its default
578: * value as specified in RFC-2255.
579: */
580: private static String setFilterUsingURL(LdapURL url) {
581:
582: String filter = url.getFilter();
583:
584: if (filter == null) {
585: filter = "(objectClass=*)"; //default value
586: }
587: return filter;
588: }
589:
590: /*
591: * Initialize a SearchControls object using LDAP URL query components.
592: * Components not present in the URL are initialized to their default
593: * values as specified in RFC-2255.
594: */
595: private static SearchControls setSearchControlsUsingURL(LdapURL url) {
596:
597: SearchControls cons = new SearchControls();
598: String scope = url.getScope();
599: String attributes = url.getAttributes();
600:
601: if (scope == null) {
602: cons.setSearchScope(SearchControls.OBJECT_SCOPE); //default value
603: } else {
604: if (scope.equals("sub")) {
605: cons.setSearchScope(SearchControls.SUBTREE_SCOPE);
606: } else if (scope.equals("one")) {
607: cons.setSearchScope(SearchControls.ONELEVEL_SCOPE);
608: } else if (scope.equals("base")) {
609: cons.setSearchScope(SearchControls.OBJECT_SCOPE);
610: }
611: }
612:
613: if (attributes == null) {
614: cons.setReturningAttributes(null); //default value
615: } else {
616: StringTokenizer tokens = new StringTokenizer(attributes,
617: ",");
618: int count = tokens.countTokens();
619: String[] attrs = new String[count];
620: for (int i = 0; i < count; i++) {
621: attrs[i] = tokens.nextToken();
622: }
623: cons.setReturningAttributes(attrs);
624: }
625: return cons;
626: }
627: }
|