All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cloudbees.api.CachedTokenValidator Maven / Gradle / Ivy

The newest version!
package com.cloudbees.api;

import com.cloudbees.api.oauth.OauthClientException;
import com.cloudbees.api.oauth.OauthToken;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;

import java.util.concurrent.ExecutionException;

/**
 * Cached {@link TokenValidator}
 *
 * @author Kohsuke Kawaguchi
 */
class CachedTokenValidator extends TokenValidator {

    private final Cache cache;

    /**
     * @param cb
     *      Partially configured cache.
     */
    CachedTokenValidator(CacheBuilder cb, final TokenValidator base) {
        cache = cb.build(new CacheLoader() {
            @Override
            public CachedToken load(String token) throws Exception {
                return new CachedToken(base.validateToken(token));
            }
        });
    }

    @Override
    public OauthToken validateToken(String token) throws OauthClientException {
        try {
            if (token==null)    return null;

            CachedToken cache = this.cache.get(token);
            OauthToken t = cache.get();
            if(t==null || t.isExpired()){
                return null;
            }
            return t;
        } catch (ExecutionException e) {
            // not unwrapping an exception to capture the call stack
            throw new OauthClientException(e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy