public OAuthTokens refresh(final OAuthTokens tokens) throws BackgroundException {
if(StringUtils.isBlank(tokens.getRefreshToken())) {
log.warn("Missing refresh token");
return tokens;
}
if(log.isDebugEnabled()) {
log.debug(String.format("Refresh expired tokens %s", tokens));
}
try {
final TokenResponse response = new RefreshTokenRequest(transport, json, new GenericUrl(tokenServerUrl),
tokens.getRefreshToken())
.setClientAuthentication(new ClientParametersAuthentication(clientid, clientsecret))
.executeUnparsed().parseAs(PermissiveTokenResponse.class).toTokenResponse();
final long expiryInMilliseconds = System.currentTimeMillis() + response.getExpiresInSeconds() * 1000;
if(StringUtils.isBlank(response.getRefreshToken())) {
return new OAuthTokens(response.getAccessToken(), tokens.getRefreshToken(), expiryInMilliseconds);
}
return new OAuthTokens(response.getAccessToken(), response.getRefreshToken(), expiryInMilliseconds);
}
catch(TokenResponseException e) {
throw new OAuthExceptionMappingService().map(e);
}
catch(HttpResponseException e) {
throw new HttpResponseExceptionMappingService().map(new org.apache.http.client
.HttpResponseException(e.getStatusCode(), e.getStatusMessage()));
}
catch(IOException e) {
throw new DefaultIOExceptionMappingService().map(e);
}
}