Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/jndi/ldap/LdapSearchEnumeration.java
38924 views
/*1* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.jndi.ldap;2627import java.security.AccessControlContext;28import java.security.AccessController;29import java.security.PrivilegedActionException;30import java.security.PrivilegedExceptionAction;31import java.util.Vector;32import javax.naming.*;33import javax.naming.directory.*;34import javax.naming.spi.*;35import javax.naming.ldap.*;36import javax.naming.ldap.LdapName;3738import com.sun.jndi.toolkit.ctx.Continuation;3940final class LdapSearchEnumeration41extends AbstractLdapNamingEnumeration<SearchResult> {4243private Name startName; // prefix of names of search results44private LdapCtx.SearchArgs searchArgs = null;4546private final AccessControlContext acc = AccessController.getContext();4748LdapSearchEnumeration(LdapCtx homeCtx, LdapResult search_results,49String starter, LdapCtx.SearchArgs args, Continuation cont)50throws NamingException {5152super(homeCtx, search_results,53args.name, /* listArg */54cont);5556// fully qualified name of starting context of search57startName = new LdapName(starter);58searchArgs = args;59}6061@Override62protected SearchResult createItem(String dn, Attributes attrs,63Vector<Control> respCtls)64throws NamingException {6566Object obj = null;6768String relStart; // name relative to starting search context69String relHome; // name relative to homeCtx.currentDN70boolean relative = true; // whether relative to currentDN7172// need to strip off all but lowest component of dn73// so that is relative to current context (currentDN)7475try {76Name parsed = new LdapName(dn);77// System.err.println("dn string: " + dn);78// System.err.println("dn name: " + parsed);7980if (startName != null && parsed.startsWith(startName)) {81relStart = parsed.getSuffix(startName.size()).toString();82relHome = parsed.getSuffix(homeCtx.currentParsedDN.size()).toString();83} else {84relative = false;85relHome = relStart =86LdapURL.toUrlString(homeCtx.hostname, homeCtx.port_number,87dn, homeCtx.hasLdapsScheme);88}89} catch (NamingException e) {90// could not parse name91relative = false;92relHome = relStart =93LdapURL.toUrlString(homeCtx.hostname, homeCtx.port_number,94dn, homeCtx.hasLdapsScheme);95}9697// Name relative to search context98CompositeName cn = new CompositeName();99if (!relStart.equals("")) {100cn.add(relStart);101}102103// Name relative to homeCtx104CompositeName rcn = new CompositeName();105if (!relHome.equals("")) {106rcn.add(relHome);107}108//System.err.println("relStart: " + cn);109//System.err.println("relHome: " + rcn);110111// Fix attributes to be able to get schema112homeCtx.setParents(attrs, rcn);113114// only generate object when requested115if (searchArgs.cons.getReturningObjFlag()) {116117if (attrs.get(Obj.JAVA_ATTRIBUTES[Obj.CLASSNAME]) != null) {118// Entry contains Java-object attributes (ser/ref object)119// serialized object or object reference120try {121obj = AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {122@Override123public Object run() throws NamingException {124return Obj.decodeObject(attrs);125}126}, acc);127} catch (PrivilegedActionException e) {128throw (NamingException)e.getException();129}130}131if (obj == null) {132obj = new LdapCtx(homeCtx, dn);133}134135// Call getObjectInstance before removing unrequested attributes136try {137// rcn is either relative to homeCtx or a fully qualified DN138obj = DirectoryManager.getObjectInstance(139obj, rcn, (relative ? homeCtx : null),140homeCtx.envprops, attrs);141} catch (NamingException e) {142throw e;143} catch (Exception e) {144NamingException ne =145new NamingException(146"problem generating object using object factory");147ne.setRootCause(e);148throw ne;149}150151// remove Java attributes from result, if necessary152// Even if CLASSNAME attr not there, there might be some153// residual attributes154155String[] reqAttrs;156if ((reqAttrs = searchArgs.reqAttrs) != null) {157// create an attribute set for those requested158Attributes rattrs = new BasicAttributes(true); // caseignore159for (int i = 0; i < reqAttrs.length; i++) {160rattrs.put(reqAttrs[i], null);161}162for (int i = 0; i < Obj.JAVA_ATTRIBUTES.length; i++) {163// Remove Java-object attributes if not requested164if (rattrs.get(Obj.JAVA_ATTRIBUTES[i]) == null) {165attrs.remove(Obj.JAVA_ATTRIBUTES[i]);166}167}168}169170}171172/*173* name in search result is either the stringified composite name174* relative to the search context that can be passed directly to175* methods of the search context, or the fully qualified DN176* which can be used with the initial context.177*/178SearchResult sr;179if (respCtls != null) {180sr = new SearchResultWithControls(181(relative ? cn.toString() : relStart), obj, attrs,182relative, homeCtx.convertControls(respCtls));183} else {184sr = new SearchResult(185(relative ? cn.toString() : relStart),186obj, attrs, relative);187}188sr.setNameInNamespace(dn);189return sr;190}191192@Override193public void appendUnprocessedReferrals(LdapReferralException ex) {194195// a referral has been followed so do not create relative names196startName = null;197super.appendUnprocessedReferrals(ex);198}199200@Override201protected AbstractLdapNamingEnumeration<? extends NameClassPair> getReferredResults(202LdapReferralContext refCtx) throws NamingException {203// repeat the original operation at the new context204return (AbstractLdapNamingEnumeration<? extends NameClassPair>)refCtx.search(205searchArgs.name, searchArgs.filter, searchArgs.cons);206}207208@Override209protected void update(AbstractLdapNamingEnumeration<? extends NameClassPair> ne) {210super.update(ne);211212// Update search-specific variables213LdapSearchEnumeration se = (LdapSearchEnumeration)ne;214startName = se.startName;215//VR - keep original args, don't overwite with current args216// searchArgs = se.searchArgs;217}218219void setStartName(Name nm) {220startName = nm;221}222}223224225