1. /*
  2. * @(#)LdapContext.java 1.10 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.naming.ldap;
  8. import javax.naming.NamingException;
  9. import javax.naming.directory.DirContext;
  10. import java.util.Hashtable;
  11. /**
  12. * This interface represents a context in which you can perform
  13. * operations with LDAPv3-style controls and perform LDAPv3-style
  14. * extended operations.
  15. *
  16. * For applications that do not require such controls or extended
  17. * operations, the more generic <tt>javax.naming.directory.DirContext</tt>
  18. * should be used instead.
  19. *
  20. * <h3>Usage Details About Controls</h3>
  21. *
  22. * This interface provides support for LDAP v3 controls.
  23. * At a high level, this support allows a user
  24. * program to set request controls for LDAP operations that are executed
  25. * in the course of the user program's invocation of
  26. * <tt>Context</tt>/<tt>DirContext</tt>
  27. * methods, and read response controls resulting from LDAP operations.
  28. * At the implementation level, there are some details that developers of
  29. * both the user program and service providers need to understand in order
  30. * to correctly use request and response controls.
  31. *
  32. * <h3>Request Controls</h3>
  33. * <p>
  34. * There are two types of request controls:
  35. * <ul>
  36. * <li>Request controls that affect how a connection is created
  37. * <li>Request controls that affect context methods
  38. * </ul>
  39. *
  40. * The former is used whenever a connection needs to be established or
  41. * re-established with an LDAP server. The latter is used when all other
  42. * LDAP operations are sent to the LDAP server. The reason why a
  43. * distinction between these two types of request controls is necessary
  44. * is because JNDI is a high-level API that does not deal directly with
  45. * connections. It is the job of service providers to do any necessary
  46. * connection management. Consequently, a single
  47. * connection may be shared by multiple context instances, and a service provider
  48. * is free to use its own algorithms to conserve connection and network
  49. * usage. Thus, when a method is invoked on the context instance, the service
  50. * provider might need to do some connection management in addition to
  51. * performing the corresponding LDAP operations. For connection management,
  52. * it uses the <em>connection request controls</em>, while for the normal
  53. * LDAP operations, it uses the <em>context request controls</em>.
  54. *<p>Unless explicitly qualified, the term "request controls" refers to
  55. * context request controls.
  56. *
  57. * <h4>Context Request Controls</h4>
  58. * There are two ways in which a context instance gets its request controls:
  59. * <ol>
  60. * <tt>
  61. * <li>ldapContext.newInstance(<strong>reqCtls</strong>)
  62. * <li>ldapContext.setRequestControls(<strong>reqCtls</strong>)
  63. * </tt>
  64. * </ol>
  65. * where <tt>ldapContext</tt> is an instance of <tt>LdapContext</tt>.
  66. * Specifying <tt>null</tt> or an empty array for <tt>reqCtls</tt>
  67. * means no request controls.
  68. * <tt>newInstance()</tt> creates a new instance of a context using
  69. * <tt>reqCtls</tt>, while <tt>setRequestControls()</tt>
  70. * updates an existing context instance's request controls to <tt>reqCtls</tt>.
  71. * <p>
  72. * Unlike environment properties, request controls of a context instance
  73. * <em>are not inherited</em> by context instances that are derived from
  74. * it. Derived context instances have <tt>null</tt> as their context
  75. * request controls. You must set the request controls of a derived context
  76. * instance explicitly using <tt>setRequestControls()</tt>.
  77. * <p>
  78. * A context instance's request controls are retrieved using
  79. * the method <tt>getRequestControls()</tt>.
  80. *
  81. * <h4>Connection Request Controls</h4>
  82. * There are three ways in which connection request controls are set:
  83. * <ol>
  84. * <tt>
  85. * <li>
  86. * new InitialLdapContext(env, <strong>connCtls</strong>)
  87. * <li>refException.getReferralContext(env, <strong>connCtls</strong>)
  88. * <li>ldapContext.reconnect(<strong>connCtls</strong>);
  89. * </tt>
  90. * </ol>
  91. * where <tt>refException</tt> is an instance of
  92. * <tt>LdapReferralException</tt>, and <tt>ldapContext</tt> is an
  93. * instance of <tt>LdapContext</tt>.
  94. * Specifying <tt>null</tt> or an empty array for <tt>connCtls</tt>
  95. * means no connection request controls.
  96. * <p>
  97. * Like environment properties, connection request controls of a context
  98. * <em>are inherited</em> by contexts that are derived from it.
  99. * Typically, you initialize the connection request controls using the
  100. * <tt>InitialLdapContext</tt> constructor or
  101. * <tt>LdapReferralContext.getReferralContext()</tt>. These connection
  102. * request controls are inherited by contexts that share the same
  103. * connection--that is, contexts derived from the initial or referral
  104. * contexts.
  105. * <p>
  106. * Use <tt>reconnect()</tt> to change the connection request controls of
  107. * a context.
  108. * Invoking <tt>ldapContext.reconnect()</tt> affects only the
  109. * connection used by <tt>ldapContext</tt> and any new contexts instances that are
  110. * derived form <tt>ldapContext</tt>. Contexts that previously shared the
  111. * connection with <tt>ldapContext</tt> remain unchanged. That is, a context's
  112. * connection request controls must be explicitly changed and is not
  113. * affected by changes to another context's connection request
  114. * controls.
  115. * <p>
  116. * A context instance's connection request controls are retrieved using
  117. * the method <tt>getConnectControls()</tt>.
  118. *
  119. * <h4>Service Provider Requirements</h4>
  120. *
  121. * A service provider supports connection and context request controls
  122. * in the following ways. Context request controls must be associated on
  123. * a per context instance basis while connection request controls must be
  124. * associated on a per connection instance basis. The service provider
  125. * must look for the connection request controls in the environment
  126. * property "java.naming.ldap.control.connect" and pass this environment
  127. * property on to context instances that it creates.
  128. *
  129. * <h3>Response Controls</h3>
  130. *
  131. * The method <tt>LdapContext.getResponseControls()</tt> is used to
  132. * retrieve the response controls generated by LDAP operations executed
  133. * as the result of invoking a <tt>Context</tt>/<tt>DirContext</tt>
  134. * operation. The result is all of the responses controls generated
  135. * by the underlying LDAP operations, including any implicit reconnection.
  136. * To get only the reconnection response controls,
  137. * use <tt>reconnect()</tt> followed by <tt>getResponseControls()</tt>.
  138. *
  139. * <h3>Parameters</h3>
  140. *
  141. * A <tt>Control[]</tt> array
  142. * passed as a parameter to any method is owned by the caller.
  143. * The service provider will not modify the array or keep a reference to it,
  144. * although it may keep references to the individual <tt>Control</tt> objects
  145. * in the array.
  146. * A <tt>Control[]</tt> array returned by any method is immutable, and may
  147. * not subsequently be modified by either the caller or the service provider.
  148. *
  149. * @author Rosanna Lee
  150. * @author Scott Seligman
  151. * @author Vincent Ryan
  152. * @version 1.10 03/12/19
  153. *
  154. * @see InitialLdapContext
  155. * @see LdapReferralException#getReferralContext(java.util.Hashtable,javax.naming.ldap.Control[])
  156. * @since 1.3
  157. */
  158. public interface LdapContext extends DirContext {
  159. /**
  160. * Performs an extended operation.
  161. *
  162. * This method is used to support LDAPv3 extended operations.
  163. * @param request The non-null request to be performed.
  164. * @return The possibly null response of the operation. null means
  165. * the operation did not generate any response.
  166. * @throws NamingException If an error occurred while performing the
  167. * extended operation.
  168. */
  169. public ExtendedResponse extendedOperation(ExtendedRequest request)
  170. throws NamingException;
  171. /**
  172. * Creates a new instance of this context initialized using request controls.
  173. *
  174. * This method is a convenience method for creating a new instance
  175. * of this context for the purposes of multithreaded access.
  176. * For example, if multiple threads want to use different context
  177. * request controls,
  178. * each thread may use this method to get its own copy of this context
  179. * and set/get context request controls without having to synchronize with other
  180. * threads.
  181. *<p>
  182. * The new context has the same environment properties and connection
  183. * request controls as this context. See the class description for details.
  184. * Implementations might also allow this context and the new context
  185. * to share the same network connection or other resources if doing
  186. * so does not impede the independence of either context.
  187. *
  188. * @param requestControls The possibly null request controls
  189. * to use for the new context.
  190. * If null, the context is initialized with no request controls.
  191. *
  192. * @return A non-null <tt>LdapContext</tt> instance.
  193. * @exception NamingException If an error occurred while creating
  194. * the new instance.
  195. * @see InitialLdapContext
  196. */
  197. public LdapContext newInstance(Control[] requestControls)
  198. throws NamingException;
  199. /**
  200. * Reconnects to the LDAP server using the supplied controls and
  201. * this context's environment.
  202. *<p>
  203. * This method is a way to explicitly initiate an LDAP "bind" operation.
  204. * For example, you can use this method to set request controls for
  205. * the LDAP "bind" operation, or to explicitly connect to the server
  206. * to get response controls returned by the LDAP "bind" operation.
  207. *<p>
  208. * This method sets this context's <tt>connCtls</tt>
  209. * to be its new connection request controls. This context's
  210. * context request controls are not affected.
  211. * After this method has been invoked, any subsequent
  212. * implicit reconnections will be done using <tt>connCtls</tt>.
  213. * <tt>connCtls</tt> are also used as
  214. * connection request controls for new context instances derived from this
  215. * context.
  216. * These connection request controls are not
  217. * affected by <tt>setRequestControls()</tt>.
  218. *<p>
  219. * Service provider implementors should read the "Service Provider" section
  220. * in the class description for implementation details.
  221. * @param connCtls The possibly null controls to use. If null, no
  222. * controls are used.
  223. * @exception NamingException If an error occurred while reconnecting.
  224. * @see #getConnectControls
  225. * @see #newInstance
  226. */
  227. public void reconnect(Control[] connCtls) throws NamingException;
  228. /**
  229. * Retrieves the connection request controls in effect for this context.
  230. * The controls are owned by the JNDI implementation and are
  231. * immutable. Neither the array nor the controls may be modified by the
  232. * caller.
  233. *
  234. * @return A possibly-null array of controls. null means no connect controls
  235. * have been set for this context.
  236. * @exception NamingException If an error occurred while getting the request
  237. * controls.
  238. */
  239. public Control[] getConnectControls() throws NamingException;
  240. /**
  241. * Sets the request controls for methods subsequently
  242. * invoked on this context.
  243. * The request controls are owned by the JNDI implementation and are
  244. * immutable. Neither the array nor the controls may be modified by the
  245. * caller.
  246. * <p>
  247. * This removes any previous request controls and adds
  248. * <tt>requestControls</tt>
  249. * for use by subsequent methods invoked on this context.
  250. * This method does not affect this context's connection request controls.
  251. *<p>
  252. * Note that <tt>requestControls</tt> will be in effect until the next
  253. * invocation of <tt>setRequestControls()</tt>. You need to explicitly
  254. * invoke <tt>setRequestControls()</tt> with <tt>null</tt> or an empty
  255. * array to clear the controls if you don't want them to affect the
  256. * context methods any more.
  257. * To check what request controls are in effect for this context, use
  258. * <tt>getRequestControls()</tt>.
  259. * @param requestControls The possibly null controls to use. If null, no
  260. * controls are used.
  261. * @exception NamingException If an error occurred while setting the
  262. * request controls.
  263. * @see #getRequestControls
  264. */
  265. public void setRequestControls(Control[] requestControls)
  266. throws NamingException;
  267. /**
  268. * Retrieves the request controls in effect for this context.
  269. * The request controls are owned by the JNDI implementation and are
  270. * immutable. Neither the array nor the controls may be modified by the
  271. * caller.
  272. *
  273. * @return A possibly-null array of controls. null means no request controls
  274. * have been set for this context.
  275. * @exception NamingException If an error occurred while getting the request
  276. * controls.
  277. * @see #setRequestControls
  278. */
  279. public Control[] getRequestControls() throws NamingException;
  280. /**
  281. * Retrieves the response controls produced as a result of the last
  282. * method invoked on this context.
  283. * The response controls are owned by the JNDI implementation and are
  284. * immutable. Neither the array nor the controls may be modified by the
  285. * caller.
  286. *<p>
  287. * These response controls might have been generated by a successful or
  288. * failed operation.
  289. *<p>
  290. * When a context method that may return response controls is invoked,
  291. * response controls from the previous method invocation are cleared.
  292. * <tt>getResponseControls()</tt> returns all of the response controls
  293. * generated by LDAP operations used by the context method in the order
  294. * received from the LDAP server.
  295. * Invoking <tt>getResponseControls()</tt> does not
  296. * clear the response controls. You can call it many times (and get
  297. * back the same controls) until the next context method that may return
  298. * controls is invoked.
  299. *<p>
  300. * @return A possibly null array of controls. If null, the previous
  301. * method invoked on this context did not produce any controls.
  302. * @exception NamingException If an error occurred while getting the response
  303. * controls.
  304. */
  305. public Control[] getResponseControls() throws NamingException;
  306. /**
  307. * Constant that holds the name of the environment property
  308. * for specifying the list of control factories to use. The value
  309. * of the property should be a colon-separated list of the fully
  310. * qualified class names of factory classes that will create a control
  311. * given another control. See
  312. * <tt>ControlFactory.getControlInstance()</tt> for details.
  313. * This property may be specified in the environment, an applet
  314. * parameter, a system property, or one or more resource files.
  315. *<p>
  316. * The value of this constant is "java.naming.factory.control".
  317. *<p>
  318. * @see ControlFactory
  319. * @see javax.naming.Context#addToEnvironment
  320. * @see javax.naming.Context#removeFromEnvironment
  321. */
  322. static final String CONTROL_FACTORIES = "java.naming.factory.control";
  323. }