0N/A/*
2362N/A * Copyright (c) 1999, 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.jndi.ldap;
0N/A
0N/Aimport java.util.Vector;
0N/Aimport javax.naming.*;
0N/Aimport javax.naming.directory.*;
0N/Aimport javax.naming.spi.*;
0N/Aimport javax.naming.ldap.*;
0N/Aimport javax.naming.ldap.LdapName;
0N/A
0N/Aimport com.sun.jndi.toolkit.ctx.Continuation;
0N/A
0N/Afinal class LdapSearchEnumeration extends LdapNamingEnumeration {
0N/A
0N/A private Name startName; // prefix of names of search results
0N/A private LdapCtx.SearchArgs searchArgs = null;
0N/A
0N/A LdapSearchEnumeration(LdapCtx homeCtx, LdapResult search_results,
0N/A String starter, LdapCtx.SearchArgs args, Continuation cont)
0N/A throws NamingException {
0N/A
0N/A super(homeCtx, search_results,
0N/A args.name, /* listArg */
0N/A cont);
0N/A
0N/A // fully qualified name of starting context of search
0N/A startName = new LdapName(starter);
0N/A searchArgs = args;
0N/A }
0N/A
0N/A protected NameClassPair
0N/A createItem(String dn, Attributes attrs, Vector respCtls)
0N/A throws NamingException {
0N/A
0N/A Object obj = null;
0N/A
0N/A String relStart; // name relative to starting search context
0N/A String relHome; // name relative to homeCtx.currentDN
0N/A boolean relative = true; // whether relative to currentDN
0N/A
0N/A // need to strip off all but lowest component of dn
0N/A // so that is relative to current context (currentDN)
0N/A
0N/A try {
0N/A Name parsed = new LdapName(dn);
0N/A // System.err.println("dn string: " + dn);
0N/A // System.err.println("dn name: " + parsed);
0N/A
0N/A if (startName != null && parsed.startsWith(startName)) {
0N/A relStart = parsed.getSuffix(startName.size()).toString();
0N/A relHome = parsed.getSuffix(homeCtx.currentParsedDN.size()).toString();
0N/A } else {
0N/A relative = false;
0N/A relHome = relStart =
0N/A LdapURL.toUrlString(homeCtx.hostname, homeCtx.port_number,
0N/A dn, homeCtx.hasLdapsScheme);
0N/A }
0N/A } catch (NamingException e) {
0N/A // could not parse name
0N/A relative = false;
0N/A relHome = relStart =
0N/A LdapURL.toUrlString(homeCtx.hostname, homeCtx.port_number,
0N/A dn, homeCtx.hasLdapsScheme);
0N/A }
0N/A
0N/A // Name relative to search context
0N/A CompositeName cn = new CompositeName();
0N/A if (!relStart.equals("")) {
0N/A cn.add(relStart);
0N/A }
0N/A
0N/A // Name relative to homeCtx
0N/A CompositeName rcn = new CompositeName();
0N/A if (!relHome.equals("")) {
0N/A rcn.add(relHome);
0N/A }
0N/A //System.err.println("relStart: " + cn);
0N/A //System.err.println("relHome: " + rcn);
0N/A
0N/A // Fix attributes to be able to get schema
0N/A homeCtx.setParents(attrs, rcn);
0N/A
0N/A // only generate object when requested
0N/A if (searchArgs.cons.getReturningObjFlag()) {
0N/A
0N/A if (attrs.get(Obj.JAVA_ATTRIBUTES[Obj.CLASSNAME]) != null) {
0N/A // Entry contains Java-object attributes (ser/ref object)
0N/A // serialized object or object reference
0N/A obj = Obj.decodeObject(attrs);
0N/A
0N/A }
0N/A if (obj == null) {
0N/A obj = new LdapCtx(homeCtx, dn);
0N/A }
0N/A
0N/A // Call getObjectInstance before removing unrequested attributes
0N/A try {
0N/A // rcn is either relative to homeCtx or a fully qualified DN
0N/A obj = DirectoryManager.getObjectInstance(
0N/A obj, rcn, (relative ? homeCtx : null),
0N/A homeCtx.envprops, attrs);
0N/A } catch (NamingException e) {
0N/A throw e;
0N/A } catch (Exception e) {
0N/A NamingException ne =
0N/A new NamingException(
0N/A "problem generating object using object factory");
0N/A ne.setRootCause(e);
0N/A throw ne;
0N/A }
0N/A
0N/A // remove Java attributes from result, if necessary
0N/A // Even if CLASSNAME attr not there, there might be some
0N/A // residual attributes
0N/A
0N/A String[] reqAttrs;
0N/A if ((reqAttrs = searchArgs.reqAttrs) != null) {
0N/A // create an attribute set for those requested
0N/A Attributes rattrs = new BasicAttributes(true); // caseignore
0N/A for (int i = 0; i < reqAttrs.length; i++) {
0N/A rattrs.put(reqAttrs[i], null);
0N/A }
0N/A for (int i = 0; i < Obj.JAVA_ATTRIBUTES.length; i++) {
0N/A // Remove Java-object attributes if not requested
0N/A if (rattrs.get(Obj.JAVA_ATTRIBUTES[i]) == null) {
0N/A attrs.remove(Obj.JAVA_ATTRIBUTES[i]);
0N/A }
0N/A }
0N/A }
0N/A
0N/A }
0N/A
0N/A /*
0N/A * name in search result is either the stringified composite name
0N/A * relative to the search context that can be passed directly to
0N/A * methods of the search context, or the fully qualified DN
0N/A * which can be used with the initial context.
0N/A */
0N/A SearchResult sr;
0N/A if (respCtls != null) {
0N/A sr = new SearchResultWithControls(
0N/A (relative ? cn.toString() : relStart), obj, attrs,
0N/A relative, homeCtx.convertControls(respCtls));
0N/A } else {
0N/A sr = new SearchResult(
0N/A (relative ? cn.toString() : relStart),
0N/A obj, attrs, relative);
0N/A }
0N/A sr.setNameInNamespace(dn);
0N/A return sr;
0N/A }
0N/A
0N/A public void appendUnprocessedReferrals(LdapReferralException ex) {
0N/A
0N/A // a referral has been followed so do not create relative names
0N/A startName = null;
0N/A super.appendUnprocessedReferrals(ex);
0N/A }
0N/A
0N/A protected LdapNamingEnumeration
0N/A getReferredResults(LdapReferralContext refCtx) throws NamingException {
0N/A // repeat the original operation at the new context
0N/A return (LdapSearchEnumeration)
0N/A refCtx.search(searchArgs.name, searchArgs.filter, searchArgs.cons);
0N/A }
0N/A
0N/A protected void update(LdapNamingEnumeration ne) {
0N/A super.update(ne);
0N/A
0N/A // Update search-specific variables
0N/A LdapSearchEnumeration se = (LdapSearchEnumeration)ne;
0N/A startName = se.startName;
0N/A//VR - keep original args, don't overwite with current args
0N/A// searchArgs = se.searchArgs;
0N/A }
0N/A
0N/A void setStartName(Name nm) {
0N/A startName = nm;
0N/A }
0N/A}