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 ForgeRock AS. 015 */ 016package org.forgerock.opendj.rest2ldap; 017 018import static org.forgerock.opendj.rest2ldap.Utils.ensureNotNull; 019import static org.forgerock.opendj.rest2ldap.Utils.i18n; 020 021import org.forgerock.json.fluent.JsonValue; 022import org.forgerock.json.resource.Context; 023import org.forgerock.json.resource.InternalServerErrorException; 024import org.forgerock.json.resource.PersistenceConfig; 025import org.forgerock.json.resource.ResourceException; 026import org.forgerock.opendj.ldap.Connection; 027 028/** 029 * A {@link Context} containing a cached pre-authenticated LDAP connection which 030 * should be re-used for performing subsequent LDAP operations. The LDAP 031 * connection is typically acquired while perform authentication in an HTTP 032 * servlet filter. It is the responsibility of the component which acquired the 033 * connection to release once processing has completed. 034 */ 035public final class AuthenticatedConnectionContext extends Context { 036 /* 037 * TODO: this context does not support persistence because there is no 038 * obvious way to restore the connection. We could just persist the context 039 * and restore it as null, and let rest2ldap switch to using the factory + 040 * proxied authz. 041 */ 042 private final Connection connection; 043 044 /** 045 * Creates a new pre-authenticated cached LDAP connection context having the 046 * provided parent and an ID automatically generated using 047 * {@code UUID.randomUUID()}. 048 * 049 * @param parent 050 * The parent context. 051 * @param connection 052 * The cached pre-authenticated LDAP connection which should be 053 * re-used for subsequent LDAP operations. 054 */ 055 public AuthenticatedConnectionContext(final Context parent, final Connection connection) { 056 super(ensureNotNull(parent)); 057 this.connection = connection; 058 } 059 060 /** 061 * Creates a new pre-authenticated cached LDAP connection context having the 062 * provided ID and parent. 063 * 064 * @param id 065 * The context ID. 066 * @param parent 067 * The parent context. 068 * @param connection 069 * The cached pre-authenticated LDAP connection which should be 070 * re-used for subsequent LDAP operations. 071 */ 072 public AuthenticatedConnectionContext(final String id, final Context parent, 073 final Connection connection) { 074 super(id, ensureNotNull(parent)); 075 this.connection = connection; 076 } 077 078 /** 079 * Restore from JSON representation. 080 * 081 * @param savedContext 082 * The JSON representation from which this context's attributes 083 * should be parsed. 084 * @param config 085 * The persistence configuration. 086 * @throws ResourceException 087 * If the JSON representation could not be parsed. 088 */ 089 AuthenticatedConnectionContext(final JsonValue savedContext, final PersistenceConfig config) 090 throws ResourceException { 091 super(savedContext, config); 092 throw new InternalServerErrorException(i18n("Cached LDAP connections cannot be restored")); 093 } 094 095 /** {@inheritDoc} */ 096 @Override 097 protected void saveToJson(final JsonValue savedContext, final PersistenceConfig config) 098 throws ResourceException { 099 super.saveToJson(savedContext, config); 100 throw new InternalServerErrorException(i18n("Cached LDAP connections cannot be persisted")); 101 } 102 103 /** 104 * Returns the cached pre-authenticated LDAP connection which should be 105 * re-used for subsequent LDAP operations. 106 * 107 * @return The cached pre-authenticated LDAP connection which should be 108 * re-used for subsequent LDAP operations. 109 */ 110 Connection getConnection() { 111 return connection; 112 } 113}