001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2013-2015 ForgeRock AS.
015 */
016package org.forgerock.opendj.rest2ldap;
017
018import static org.forgerock.opendj.rest2ldap.Utils.*;
019
020import org.forgerock.opendj.ldap.Connection;
021import org.forgerock.services.context.AbstractContext;
022import org.forgerock.services.context.Context;
023
024/**
025 * A {@link Context} containing a cached pre-authenticated LDAP connection which
026 * should be re-used for performing subsequent LDAP operations. The LDAP
027 * connection is typically acquired while perform authentication in an HTTP
028 * servlet filter. It is the responsibility of the component which acquired the
029 * connection to release once processing has completed.
030 */
031public final class AuthenticatedConnectionContext extends AbstractContext {
032    /*
033     * TODO: this context does not support persistence because there is no
034     * obvious way to restore the connection. We could just persist the context
035     * and restore it as null, and let rest2ldap switch to using the factory +
036     * proxied authz.
037     */
038    private final Connection connection;
039
040    /**
041     * Creates a new pre-authenticated cached LDAP connection context having the
042     * provided parent and an ID automatically generated using
043     * {@code UUID.randomUUID()}.
044     *
045     * @param parent
046     *            The parent context.
047     * @param connection
048     *            The cached pre-authenticated LDAP connection which should be
049     *            re-used for subsequent LDAP operations.
050     */
051    public AuthenticatedConnectionContext(final Context parent, final Connection connection) {
052        super(ensureNotNull(parent), "authenticated connection");
053        this.connection = connection;
054    }
055
056    /**
057     * Creates a new pre-authenticated cached LDAP connection context having the
058     * provided ID and parent.
059     *
060     * @param id
061     *            The context ID.
062     * @param parent
063     *            The parent context.
064     * @param connection
065     *            The cached pre-authenticated LDAP connection which should be
066     *            re-used for subsequent LDAP operations.
067     */
068    AuthenticatedConnectionContext(final String id, final Context parent,
069            final Connection connection) {
070        super(id, "authenticated connection", ensureNotNull(parent));
071        this.connection = connection;
072    }
073
074    /**
075     * Returns the cached pre-authenticated LDAP connection which should be
076     * re-used for subsequent LDAP operations.
077     *
078     * @return The cached pre-authenticated LDAP connection which should be
079     *         re-used for subsequent LDAP operations.
080     */
081    Connection getConnection() {
082        return connection;
083    }
084}