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

com.amazon.SellingPartnerAPIAA.LWAAccessTokenCacheImpl Maven / Gradle / Ivy

The newest version!
package com.amazon.SellingPartnerAPIAA;

import java.util.concurrent.ConcurrentHashMap;

public class LWAAccessTokenCacheImpl implements LWAAccessTokenCache {
  //in milliseconds; to avoid returning a token that would expire before or while a request is made
    private long expiryAdjustment = 60 * 1000;
    private static final long SECOND_TO_MILLIS = 1000;
    private ConcurrentHashMap accessTokenHashMap =
            new ConcurrentHashMap();
    @Override
    public void put(Object oLWAAccessTokenRequestMeta, String accessToken, long tokenTTLInSeconds) {
        LWAAccessTokenCacheItem accessTokenCacheItem = new LWAAccessTokenCacheItem();
        long insertTime = System.currentTimeMillis();
        long accessTokenExpiresValueMillis = (tokenTTLInSeconds * SECOND_TO_MILLIS) + insertTime;
        accessTokenCacheItem.setAccessToken(accessToken);
        accessTokenCacheItem.setAccessTokenExpiredTime(accessTokenExpiresValueMillis);
        accessTokenHashMap.put(oLWAAccessTokenRequestMeta, accessTokenCacheItem);
    }

    @Override
    public String get(Object oLWAAccessTokenRequestMeta) {
        Object accessTokenValue = accessTokenHashMap.get(oLWAAccessTokenRequestMeta);
        if (accessTokenValue != null) {
            LWAAccessTokenCacheItem accessTokenData =
                    (LWAAccessTokenCacheItem) accessTokenValue;
            long currentTime = System.currentTimeMillis();
            long accessTokenExpiredTime = accessTokenData.getAccessTokenExpiredTime() - expiryAdjustment;
            if (currentTime < accessTokenExpiredTime) {
                return accessTokenData.getAccessToken();
            }
        }
        return null;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy