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 2014 ForgeRock AS.
015 */
016
017package org.forgerock.openig.filter.oauth2.cache;
018
019import java.util.concurrent.Callable;
020import java.util.concurrent.ExecutionException;
021
022import org.forgerock.openig.filter.oauth2.AccessToken;
023import org.forgerock.openig.filter.oauth2.AccessTokenResolver;
024import org.forgerock.openig.filter.oauth2.OAuth2TokenException;
025import org.forgerock.openig.util.ThreadSafeCache;
026import org.forgerock.services.context.Context;
027
028/**
029 * A {@link CachingAccessTokenResolver} is a delegating {@link AccessTokenResolver} that uses a write-through cache
030 * to enable fast {@link AccessToken} resolution.
031 */
032public class CachingAccessTokenResolver implements AccessTokenResolver {
033
034    private final AccessTokenResolver resolver;
035    private final ThreadSafeCache<String, AccessToken> cache;
036
037    /**
038     * Builds a {@link CachingAccessTokenResolver} delegating to the given {@link AccessTokenResolver} using the given
039     * (pre-configured) cache.
040     *
041     * @param resolver
042     *         resolver to delegates to
043     * @param cache
044     *         access token cache
045     */
046    public CachingAccessTokenResolver(final AccessTokenResolver resolver,
047                                      final ThreadSafeCache<String, AccessToken> cache) {
048        this.resolver = resolver;
049        this.cache = cache;
050    }
051
052    @Override
053    public AccessToken resolve(final Context context, final String token) throws OAuth2TokenException {
054        try {
055            return cache.getValue(token, new Callable<AccessToken>() {
056                @Override
057                public AccessToken call() throws Exception {
058                    return resolver.resolve(context, token);
059                }
060            });
061        } catch (InterruptedException e) {
062            throw new OAuth2TokenException("Timed out retrieving OAuth2 access token information", e);
063        } catch (ExecutionException e) {
064            throw new OAuth2TokenException("Initial token resolution has failed", e);
065        }
066    }
067}