Skip to content

Commit

Permalink
Fix for issue #11 (#12)
Browse files Browse the repository at this point in the history
* Fix in caching

* Revert "Fix in caching"

This reverts commit a4bd74d.

* Enabling cache per environment
  • Loading branch information
sdhiman30 authored and sengopal committed Jan 22, 2019
1 parent 217b309 commit 568751f
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/main/java/com/ebay/api/client/auth/oauth2/OAuth2Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import java.util.concurrent.ConcurrentHashMap;
import static com.ebay.api.client.auth.oauth2.CredentialUtil.CredentialType.*;
import static com.ebay.api.client.auth.oauth2.OAuth2Util.buildScopeForRequest;

public class OAuth2Api {
private static final Logger logger = LoggerFactory.getLogger(OAuth2Api.class);
public static final String CRED_SEPERATOR = ":";
private static TimedCacheValue appAccessToken = null;
private static Map<Environment, TimedCacheValue> appAccessTokenMap = new ConcurrentHashMap<>();

private static class TimedCacheValue {
private OAuthResponse value;
Expand All @@ -60,6 +60,7 @@ private OAuthResponse getValue() {
}

public OAuthResponse getApplicationToken(Environment environment, List<String> scopes) throws IOException {
TimedCacheValue appAccessToken = appAccessTokenMap.get(environment);
if (appAccessToken != null && appAccessToken.getValue() != null) {
logger.debug("application access token returned from cache");
return appAccessToken.getValue();
Expand All @@ -84,6 +85,7 @@ public OAuthResponse getApplicationToken(Environment environment, List<String> s
OAuthResponse oAuthResponse = OAuth2Util.parseApplicationToken(response.body().string());
AccessToken accessToken = oAuthResponse.getAccessToken().get();
appAccessToken = new TimedCacheValue(oAuthResponse, new DateTime(accessToken.getExpiresOn()));
appAccessTokenMap.put(environment, appAccessToken);
return oAuthResponse;
} else {
return OAuth2Util.handleError(response);
Expand Down

0 comments on commit 568751f

Please sign in to comment.