Path: blob/master/src/java.naming/share/classes/javax/naming/ldap/LdapContext.java
41159 views
/*1* Copyright (c) 1999, 2020, 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.NamingException;28import javax.naming.directory.DirContext;29import java.util.Hashtable;3031/**32* This interface represents a context in which you can perform33* operations with LDAPv3-style controls and perform LDAPv3-style34* extended operations.35*36* For applications that do not require such controls or extended37* operations, the more generic {@code javax.naming.directory.DirContext}38* should be used instead.39*40* <h2>Usage Details About Controls</h2>41*42* This interface provides support for LDAP v3 controls.43* At a high level, this support allows a user44* program to set request controls for LDAP operations that are executed45* in the course of the user program's invocation of46* {@code Context}/{@code DirContext}47* methods, and read response controls resulting from LDAP operations.48* At the implementation level, there are some details that developers of49* both the user program and service providers need to understand in order50* to correctly use request and response controls.51*52* <h2>Request Controls</h2>53* <p>54* There are two types of request controls:55* <ul>56* <li>Request controls that affect how a connection is created57* <li>Request controls that affect context methods58* </ul>59*60* The former is used whenever a connection needs to be established or61* re-established with an LDAP server. The latter is used when all other62* LDAP operations are sent to the LDAP server. The reason why a63* distinction between these two types of request controls is necessary64* is because JNDI is a high-level API that does not deal directly with65* connections. It is the job of service providers to do any necessary66* connection management. Consequently, a single67* connection may be shared by multiple context instances, and a service provider68* is free to use its own algorithms to conserve connection and network69* usage. Thus, when a method is invoked on the context instance, the service70* provider might need to do some connection management in addition to71* performing the corresponding LDAP operations. For connection management,72* it uses the <em>connection request controls</em>, while for the normal73* LDAP operations, it uses the <em>context request controls</em>.74*<p>Unless explicitly qualified, the term "request controls" refers to75* context request controls.76*77* <h3>Context Request Controls</h3>78* There are two ways in which a context instance gets its request controls:79* <ol>80* <li><code>ldapContext.newInstance(<strong>reqCtls</strong>)</code>81* <li><code>ldapContext.setRequestControls(<strong>reqCtls</strong>)</code>82* </ol>83* where {@code ldapContext} is an instance of {@code LdapContext}.84* Specifying {@code null} or an empty array for {@code reqCtls}85* means no request controls.86* {@code newInstance()} creates a new instance of a context using87* {@code reqCtls}, while {@code setRequestControls()}88* updates an existing context instance's request controls to {@code reqCtls}.89* <p>90* Unlike environment properties, request controls of a context instance91* <em>are not inherited</em> by context instances that are derived from92* it. Derived context instances have {@code null} as their context93* request controls. You must set the request controls of a derived context94* instance explicitly using {@code setRequestControls()}.95* <p>96* A context instance's request controls are retrieved using97* the method {@code getRequestControls()}.98*99* <h3>Connection Request Controls</h3>100* There are three ways in which connection request controls are set:101* <ol>102* <li><code>103* new InitialLdapContext(env, <strong>connCtls</strong>)</code>104* <li><code>refException.getReferralContext(env, <strong>connCtls</strong>)</code>105* <li><code>ldapContext.reconnect(<strong>connCtls</strong>);</code>106* </ol>107* where {@code refException} is an instance of108* {@code LdapReferralException}, and {@code ldapContext} is an109* instance of {@code LdapContext}.110* Specifying {@code null} or an empty array for {@code connCtls}111* means no connection request controls.112* <p>113* Like environment properties, connection request controls of a context114* <em>are inherited</em> by contexts that are derived from it.115* Typically, you initialize the connection request controls using the116* {@code InitialLdapContext} constructor or117* {@code LdapReferralContext.getReferralContext()}. These connection118* request controls are inherited by contexts that share the same119* connection--that is, contexts derived from the initial or referral120* contexts.121* <p>122* Use {@code reconnect()} to change the connection request controls of123* a context.124* Invoking {@code ldapContext.reconnect()} affects only the125* connection used by {@code ldapContext} and any new contexts instances that are126* derived form {@code ldapContext}. Contexts that previously shared the127* connection with {@code ldapContext} remain unchanged. That is, a context's128* connection request controls must be explicitly changed and is not129* affected by changes to another context's connection request130* controls.131* <p>132* A context instance's connection request controls are retrieved using133* the method {@code getConnectControls()}.134*135* <h3>Service Provider Requirements</h3>136*137* A service provider supports connection and context request controls138* in the following ways. Context request controls must be associated on139* a per context instance basis while connection request controls must be140* associated on a per connection instance basis. The service provider141* must look for the connection request controls in the environment142* property "java.naming.ldap.control.connect" and pass this environment143* property on to context instances that it creates.144*145* <h2>Response Controls</h2>146*147* The method {@code LdapContext.getResponseControls()} is used to148* retrieve the response controls generated by LDAP operations executed149* as the result of invoking a {@code Context}/{@code DirContext}150* operation. The result is all of the responses controls generated151* by the underlying LDAP operations, including any implicit reconnection.152* To get only the reconnection response controls,153* use {@code reconnect()} followed by {@code getResponseControls()}.154*155* <h2>Parameters</h2>156*157* A {@code Control[]} array158* passed as a parameter to any method is owned by the caller.159* The service provider will not modify the array or keep a reference to it,160* although it may keep references to the individual {@code Control} objects161* in the array.162* A {@code Control[]} array returned by any method is immutable, and may163* not subsequently be modified by either the caller or the service provider.164*165* @author Rosanna Lee166* @author Scott Seligman167* @author Vincent Ryan168*169* @see InitialLdapContext170* @see LdapReferralException#getReferralContext(java.util.Hashtable,javax.naming.ldap.Control[])171* @since 1.3172*/173174public interface LdapContext extends DirContext {175/**176* Performs an extended operation.177*178* This method is used to support LDAPv3 extended operations.179* @param request The non-null request to be performed.180* @return The possibly null response of the operation. null means181* the operation did not generate any response.182* @throws NamingException If an error occurred while performing the183* extended operation.184*/185public ExtendedResponse extendedOperation(ExtendedRequest request)186throws NamingException;187188/**189* Creates a new instance of this context initialized using request controls.190*191* This method is a convenience method for creating a new instance192* of this context for the purposes of multithreaded access.193* For example, if multiple threads want to use different context194* request controls,195* each thread may use this method to get its own copy of this context196* and set/get context request controls without having to synchronize with other197* threads.198*<p>199* The new context has the same environment properties and connection200* request controls as this context. See the class description for details.201* Implementations might also allow this context and the new context202* to share the same network connection or other resources if doing203* so does not impede the independence of either context.204*205* @param requestControls The possibly null request controls206* to use for the new context.207* If null, the context is initialized with no request controls.208*209* @return A non-null {@code LdapContext} instance.210* @throws NamingException If an error occurred while creating211* the new instance.212* @see InitialLdapContext213*/214public LdapContext newInstance(Control[] requestControls)215throws NamingException;216217/**218* Reconnects to the LDAP server using the supplied controls and219* this context's environment.220*<p>221* This method is a way to explicitly initiate an LDAP "bind" operation.222* For example, you can use this method to set request controls for223* the LDAP "bind" operation, or to explicitly connect to the server224* to get response controls returned by the LDAP "bind" operation.225*<p>226* This method sets this context's {@code connCtls}227* to be its new connection request controls. This context's228* context request controls are not affected.229* After this method has been invoked, any subsequent230* implicit reconnections will be done using {@code connCtls}.231* {@code connCtls} are also used as232* connection request controls for new context instances derived from this233* context.234* These connection request controls are not235* affected by {@code setRequestControls()}.236*<p>237* Service provider implementors should read the "Service Provider" section238* in the class description for implementation details.239* @param connCtls The possibly null controls to use. If null, no240* controls are used.241* @throws NamingException If an error occurred while reconnecting.242* @see #getConnectControls243* @see #newInstance244*/245public void reconnect(Control[] connCtls) throws NamingException;246247/**248* Retrieves the connection request controls in effect for this context.249* The controls are owned by the JNDI implementation and are250* immutable. Neither the array nor the controls may be modified by the251* caller.252*253* @return A possibly-null array of controls. null means no connect controls254* have been set for this context.255* @throws NamingException If an error occurred while getting the request256* controls.257*/258public Control[] getConnectControls() throws NamingException;259260/**261* Sets the request controls for methods subsequently262* invoked on this context.263* The request controls are owned by the JNDI implementation and are264* immutable. Neither the array nor the controls may be modified by the265* caller.266* <p>267* This removes any previous request controls and adds268* {@code requestControls}269* for use by subsequent methods invoked on this context.270* This method does not affect this context's connection request controls.271*<p>272* Note that {@code requestControls} will be in effect until the next273* invocation of {@code setRequestControls()}. You need to explicitly274* invoke {@code setRequestControls()} with {@code null} or an empty275* array to clear the controls if you don't want them to affect the276* context methods any more.277* To check what request controls are in effect for this context, use278* {@code getRequestControls()}.279* @param requestControls The possibly null controls to use. If null, no280* controls are used.281* @throws NamingException If an error occurred while setting the282* request controls.283* @see #getRequestControls284*/285public void setRequestControls(Control[] requestControls)286throws NamingException;287288/**289* Retrieves the request controls in effect for this context.290* The request controls are owned by the JNDI implementation and are291* immutable. Neither the array nor the controls may be modified by the292* caller.293*294* @return A possibly-null array of controls. null means no request controls295* have been set for this context.296* @exception NamingException If an error occurred while getting the request297* controls.298* @see #setRequestControls299*/300public Control[] getRequestControls() throws NamingException;301302/**303* Retrieves the response controls produced as a result of the last304* method invoked on this context.305* The response controls are owned by the JNDI implementation and are306* immutable. Neither the array nor the controls may be modified by the307* caller.308*<p>309* These response controls might have been generated by a successful or310* failed operation.311*<p>312* When a context method that may return response controls is invoked,313* response controls from the previous method invocation are cleared.314* {@code getResponseControls()} returns all of the response controls315* generated by LDAP operations used by the context method in the order316* received from the LDAP server.317* Invoking {@code getResponseControls()} does not318* clear the response controls. You can call it many times (and get319* back the same controls) until the next context method that may return320* controls is invoked.321*322* @return A possibly null array of controls. If null, the previous323* method invoked on this context did not produce any controls.324* @exception NamingException If an error occurred while getting the response325* controls.326*/327public Control[] getResponseControls() throws NamingException;328329/**330* Constant that holds the name of the environment property331* for specifying the list of control factories to use. The value332* of the property should be a colon-separated list of the fully333* qualified class names of factory classes that will create a control334* given another control. See335* {@code ControlFactory.getControlInstance()} for details.336* This property may be specified in the environment, a system property,337* or one or more resource files.338*<p>339* The value of this constant is "java.naming.factory.control".340*341* @see ControlFactory342* @see javax.naming.Context#addToEnvironment343* @see javax.naming.Context#removeFromEnvironment344*/345static final String CONTROL_FACTORIES = "java.naming.factory.control";346}347348349