{
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode json = mapper.readTree(authCacheFile);
if (json.isNull()) {
throw new ApiException(Status.FORBIDDEN.getStatusCode(),
"Local authentication cache file " + authCacheFile.getAbsolutePath() + " is empty.");
} else if (!json.has("access_token")) {
throw new ApiException(Status.FORBIDDEN.getStatusCode(),
"No access token found in local cache file " + authCacheFile.getAbsolutePath() + "." );
} else {
AccessToken token = new AccessToken();
token.setAccessToken(json.get("access_token").asText());
token.setClientSecret(json.get("apisecret").asText());
token.setClientKey(json.get("apikey").asText());
token.setRefreshToken(json.get("refresh_token").asText());
token.setCreatedAt(new DateTime(json.get("created_at").asInt()));
token.setExpiresAt(token.getCreatedAt().plusSeconds(json.get("expires_in").asInt()));
return token;
}
} catch(JsonProcessingException e) {
throw new ApiException(Status.BAD_REQUEST.getStatusCode(),
"Unable to parse authentication cache file " + authCacheFile.getAbsolutePath() + " is empty.");
} catch (IOException e) {
throw new ApiException(Status.BAD_REQUEST.getStatusCode(),
"Unable to parse authentication cache file " + authCacheFile.getAbsolutePath() + " is empty.");
}
} else {
throw new ApiException(Status.FORBIDDEN.getStatusCode(),
"Unable to locate authentication token from "
+ "system cache file " + authCacheFile.getAbsolutePath() + " is empty.");
}
}