Path: blob/master/src/java.naming/share/classes/javax/naming/ldap/InitialLdapContext.java
41159 views
/*1* Copyright (c) 1999, 2019, 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 javax.naming.ldap;2627import javax.naming.*;28import javax.naming.directory.*;2930import java.util.Hashtable;3132/**33* This class is the starting context for performing34* LDAPv3-style extended operations and controls.35*<p>36* See {@code javax.naming.InitialContext} and37* {@code javax.naming.InitialDirContext} for details on synchronization,38* and the policy for how an initial context is created.39*40* <h2>Request Controls</h2>41* When you create an initial context ({@code InitialLdapContext}),42* you can specify a list of request controls.43* These controls will be used as the request controls for any44* implicit LDAP "bind" operation performed by the context or contexts45* derived from the context. These are called <em>connection request controls</em>.46* Use {@code getConnectControls()} to get a context's connection request47* controls.48*<p>49* The request controls supplied to the initial context constructor50* are <em>not</em> used as the context request controls51* for subsequent context operations such as searches and lookups.52* Context request controls are set and updated by using53* {@code setRequestControls()}.54*<p>55* As shown, there can be two different sets of request controls56* associated with a context: connection request controls and context57* request controls.58* This is required for those applications needing to send critical59* controls that might not be applicable to both the context operation and60* any implicit LDAP "bind" operation.61* A typical user program would do the following:62*<blockquote><pre>63* InitialLdapContext lctx = new InitialLdapContext(env, critConnCtls);64* lctx.setRequestControls(critModCtls);65* lctx.modifyAttributes(name, mods);66* Controls[] respCtls = lctx.getResponseControls();67*</pre></blockquote>68* It specifies first the critical controls for creating the initial context69* ({@code critConnCtls}), and then sets the context's request controls70* ({@code critModCtls}) for the context operation. If for some reason71* {@code lctx} needs to reconnect to the server, it will use72* {@code critConnCtls}. See the {@code LdapContext} interface for73* more discussion about request controls.74*<p>75* Service provider implementors should read the "Service Provider" section76* in the {@code LdapContext} class description for implementation details.77*78* @author Rosanna Lee79* @author Scott Seligman80* @author Vincent Ryan81*82* @see LdapContext83* @see javax.naming.InitialContext84* @see javax.naming.directory.InitialDirContext85* @see javax.naming.spi.NamingManager#setInitialContextFactoryBuilder86* @since 1.387*/8889public class InitialLdapContext extends InitialDirContext implements LdapContext {90private static final String91BIND_CONTROLS_PROPERTY = "java.naming.ldap.control.connect";9293/**94* Constructs an initial context using no environment properties or95* connection request controls.96* Equivalent to {@code new InitialLdapContext(null, null)}.97*98* @throws NamingException if a naming exception is encountered99*/100public InitialLdapContext() throws NamingException {101super(null);102}103104/**105* Constructs an initial context106* using environment properties and connection request controls.107* See {@code javax.naming.InitialContext} for a discussion of108* environment properties.109*110* <p> This constructor will not modify its parameters or111* save references to them, but may save a clone or copy.112* Caller should not modify mutable keys and values in113* {@code environment} after it has been passed to the constructor.114*115* <p> {@code connCtls} is used as the underlying context instance's116* connection request controls. See the class description117* for details.118*119* @param environment120* environment used to create the initial DirContext.121* Null indicates an empty environment.122* @param connCtls123* connection request controls for the initial context.124* If null, no connection request controls are used.125*126* @throws NamingException if a naming exception is encountered127*128* @see #reconnect129* @see LdapContext#reconnect130*/131@SuppressWarnings("unchecked")132public InitialLdapContext(Hashtable<?,?> environment,133Control[] connCtls)134throws NamingException {135super(true); // don't initialize yet136137// Clone environment since caller owns it.138Hashtable<Object,Object> env = (environment == null)139? new Hashtable<>(11)140: (Hashtable<Object,Object>)environment.clone();141142// Put connect controls into environment. Copy them first since143// caller owns the array.144if (connCtls != null) {145Control[] copy = new Control[connCtls.length];146System.arraycopy(connCtls, 0, copy, 0, connCtls.length);147env.put(BIND_CONTROLS_PROPERTY, copy);148}149// set version to LDAPv3150env.put("java.naming.ldap.version", "3");151152// Initialize with updated environment153init(env);154}155156/**157* Retrieves the initial LDAP context.158*159* @return The non-null cached initial context.160* @exception NotContextException If the initial context is not an161* instance of {@code LdapContext}.162* @exception NamingException If a naming exception was encountered.163*/164private LdapContext getDefaultLdapInitCtx() throws NamingException{165Context answer = getDefaultInitCtx();166167if (!(answer instanceof LdapContext)) {168if (answer == null) {169throw new NoInitialContextException();170} else {171throw new NotContextException(172"Not an instance of LdapContext");173}174}175return (LdapContext)answer;176}177178// LdapContext methods179// Most Javadoc is deferred to the LdapContext interface.180181public ExtendedResponse extendedOperation(ExtendedRequest request)182throws NamingException {183return getDefaultLdapInitCtx().extendedOperation(request);184}185186public LdapContext newInstance(Control[] reqCtls)187throws NamingException {188return getDefaultLdapInitCtx().newInstance(reqCtls);189}190191public void reconnect(Control[] connCtls) throws NamingException {192getDefaultLdapInitCtx().reconnect(connCtls);193}194195public Control[] getConnectControls() throws NamingException {196return getDefaultLdapInitCtx().getConnectControls();197}198199public void setRequestControls(Control[] requestControls)200throws NamingException {201getDefaultLdapInitCtx().setRequestControls(requestControls);202}203204public Control[] getRequestControls() throws NamingException {205return getDefaultLdapInitCtx().getRequestControls();206}207208public Control[] getResponseControls() throws NamingException {209return getDefaultLdapInitCtx().getResponseControls();210}211}212213214