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-2016 ForgeRock AS.
015 */
016package org.forgerock.opendj.rest2ldap;
017
018import static org.forgerock.util.Reject.checkNotNull;
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(checkNotNull(parent), "authenticated connection");
053        this.connection = connection;
054    }
055
056    /**
057     * Returns the cached pre-authenticated LDAP connection which should be
058     * re-used for subsequent LDAP operations.
059     *
060     * @return The cached pre-authenticated LDAP connection which should be
061     *         re-used for subsequent LDAP operations.
062     */
063    public Connection getConnection() {
064        return connection;
065    }
066}