0N/A/*
2362N/A * Copyright (c) 1999, 2009, 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 javax.naming.ldap;
0N/A
0N/Aimport javax.naming.*;
0N/Aimport javax.naming.directory.*;
0N/A
0N/Aimport java.util.Hashtable;
0N/A
0N/A/**
0N/A * This class is the starting context for performing
0N/A * LDAPv3-style extended operations and controls.
0N/A *<p>
0N/A * See <tt>javax.naming.InitialContext</tt> and
0N/A * <tt>javax.naming.InitialDirContext</tt> for details on synchronization,
0N/A * and the policy for how an initial context is created.
0N/A *
0N/A * <h4>Request Controls</h4>
0N/A * When you create an initial context (<tt>InitialLdapContext</tt>),
0N/A * you can specify a list of request controls.
0N/A * These controls will be used as the request controls for any
0N/A * implicit LDAP "bind" operation performed by the context or contexts
0N/A * derived from the context. These are called <em>connection request controls</em>.
0N/A * Use <tt>getConnectControls()</tt> to get a context's connection request
0N/A * controls.
0N/A *<p>
0N/A * The request controls supplied to the initial context constructor
0N/A * are <em>not</em> used as the context request controls
0N/A * for subsequent context operations such as searches and lookups.
0N/A * Context request controls are set and updated by using
0N/A * <tt>setRequestControls()</tt>.
0N/A *<p>
0N/A * As shown, there can be two different sets of request controls
0N/A * associated with a context: connection request controls and context
0N/A * request controls.
0N/A * This is required for those applications needing to send critical
0N/A * controls that might not be applicable to both the context operation and
0N/A * any implicit LDAP "bind" operation.
0N/A * A typical user program would do the following:
0N/A *<blockquote><pre>
0N/A * InitialLdapContext lctx = new InitialLdapContext(env, critConnCtls);
0N/A * lctx.setRequestControls(critModCtls);
0N/A * lctx.modifyAttributes(name, mods);
0N/A * Controls[] respCtls = lctx.getResponseControls();
0N/A *</pre></blockquote>
0N/A * It specifies first the critical controls for creating the initial context
0N/A * (<tt>critConnCtls</tt>), and then sets the context's request controls
0N/A * (<tt>critModCtls</tt>) for the context operation. If for some reason
0N/A * <tt>lctx</tt> needs to reconnect to the server, it will use
0N/A * <tt>critConnCtls</tt>. See the <tt>LdapContext</tt> interface for
0N/A * more discussion about request controls.
0N/A *<p>
0N/A * Service provider implementors should read the "Service Provider" section
0N/A * in the <tt>LdapContext</tt> class description for implementation details.
0N/A *
0N/A * @author Rosanna Lee
0N/A * @author Scott Seligman
0N/A * @author Vincent Ryan
0N/A *
0N/A * @see LdapContext
0N/A * @see javax.naming.InitialContext
0N/A * @see javax.naming.directory.InitialDirContext
0N/A * @see javax.naming.spi.NamingManager#setInitialContextFactoryBuilder
0N/A * @since 1.3
0N/A */
0N/A
0N/Apublic class InitialLdapContext extends InitialDirContext implements LdapContext {
0N/A private static final String
0N/A BIND_CONTROLS_PROPERTY = "java.naming.ldap.control.connect";
0N/A
0N/A /**
0N/A * Constructs an initial context using no environment properties or
0N/A * connection request controls.
0N/A * Equivalent to <tt>new InitialLdapContext(null, null)</tt>.
0N/A *
0N/A * @throws NamingException if a naming exception is encountered
0N/A */
0N/A public InitialLdapContext() throws NamingException {
0N/A super(null);
0N/A }
0N/A
0N/A /**
0N/A * Constructs an initial context
0N/A * using environment properties and connection request controls.
0N/A * See <tt>javax.naming.InitialContext</tt> for a discussion of
0N/A * environment properties.
0N/A *
0N/A * <p> This constructor will not modify its parameters or
0N/A * save references to them, but may save a clone or copy.
1954N/A * Caller should not modify mutable keys and values in
1954N/A * <tt>environment</tt> after it has been passed to the constructor.
0N/A *
0N/A * <p> <tt>connCtls</tt> is used as the underlying context instance's
0N/A * connection request controls. See the class description
0N/A * for details.
0N/A *
0N/A * @param environment
0N/A * environment used to create the initial DirContext.
0N/A * Null indicates an empty environment.
0N/A * @param connCtls
0N/A * connection request controls for the initial context.
0N/A * If null, no connection request controls are used.
0N/A *
0N/A * @throws NamingException if a naming exception is encountered
0N/A *
0N/A * @see #reconnect
0N/A * @see LdapContext#reconnect
0N/A */
0N/A public InitialLdapContext(Hashtable<?,?> environment,
0N/A Control[] connCtls)
0N/A throws NamingException {
0N/A super(true); // don't initialize yet
0N/A
0N/A // Clone environment since caller owns it.
0N/A Hashtable env = (environment == null)
0N/A ? new Hashtable(11)
0N/A : (Hashtable)environment.clone();
0N/A
0N/A // Put connect controls into environment. Copy them first since
0N/A // caller owns the array.
0N/A if (connCtls != null) {
0N/A Control[] copy = new Control[connCtls.length];
0N/A System.arraycopy(connCtls, 0, copy, 0, connCtls.length);
0N/A env.put(BIND_CONTROLS_PROPERTY, copy);
0N/A }
0N/A // set version to LDAPv3
0N/A env.put("java.naming.ldap.version", "3");
0N/A
0N/A // Initialize with updated environment
0N/A init(env);
0N/A }
0N/A
0N/A /**
0N/A * Retrieves the initial LDAP context.
0N/A *
0N/A * @return The non-null cached initial context.
0N/A * @exception NotContextException If the initial context is not an
0N/A * instance of <tt>LdapContext</tt>.
0N/A * @exception NamingException If a naming exception was encountered.
0N/A */
0N/A private LdapContext getDefaultLdapInitCtx() throws NamingException{
0N/A Context answer = getDefaultInitCtx();
0N/A
0N/A if (!(answer instanceof LdapContext)) {
0N/A if (answer == null) {
0N/A throw new NoInitialContextException();
0N/A } else {
0N/A throw new NotContextException(
0N/A "Not an instance of LdapContext");
0N/A }
0N/A }
0N/A return (LdapContext)answer;
0N/A }
0N/A
0N/A// LdapContext methods
0N/A// Most Javadoc is deferred to the LdapContext interface.
0N/A
0N/A public ExtendedResponse extendedOperation(ExtendedRequest request)
0N/A throws NamingException {
0N/A return getDefaultLdapInitCtx().extendedOperation(request);
0N/A }
0N/A
0N/A public LdapContext newInstance(Control[] reqCtls)
0N/A throws NamingException {
0N/A return getDefaultLdapInitCtx().newInstance(reqCtls);
0N/A }
0N/A
0N/A public void reconnect(Control[] connCtls) throws NamingException {
0N/A getDefaultLdapInitCtx().reconnect(connCtls);
0N/A }
0N/A
0N/A public Control[] getConnectControls() throws NamingException {
0N/A return getDefaultLdapInitCtx().getConnectControls();
0N/A }
0N/A
0N/A public void setRequestControls(Control[] requestControls)
0N/A throws NamingException {
0N/A getDefaultLdapInitCtx().setRequestControls(requestControls);
0N/A }
0N/A
0N/A public Control[] getRequestControls() throws NamingException {
0N/A return getDefaultLdapInitCtx().getRequestControls();
0N/A }
0N/A
0N/A public Control[] getResponseControls() throws NamingException {
0N/A return getDefaultLdapInitCtx().getResponseControls();
0N/A }
0N/A}