Package com.cloudbees.api

Source Code of com.cloudbees.api.CachedTokenValidator

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<String,CachedToken> cache;

    /**
     * @param cb
     *      Partially configured cache.
     */
    CachedTokenValidator(CacheBuilder<Object,Object> cb, final TokenValidator base) {
        cache = cb.build(new CacheLoader<String,CachedToken>() {
            @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);
        }
    }
}
TOP

Related Classes of com.cloudbees.api.CachedTokenValidator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.