Skip to content

Commit

Permalink
optimize debug log performance impact on prod in OAuth1 and fix NoCla…
Browse files Browse the repository at this point in the history
…ssDefFoundError on Android device with SDK 18 and lower (thanks to https://github.com/arcao)
  • Loading branch information
kullfar committed May 18, 2018
1 parent e719d25 commit ae14353
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
2 changes: 2 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[SNAPSHOT]
* fix error parsing for Fitbit (thanks to https://github.com/danmana)
* optimize debug log performance impact on prod in OAuth1 and fix
NoClassDefFoundError on Android device with SDK 18 and lower (thanks to https://github.com/arcao)

[5.4.0]
* fix missing support for scope for refresh_token grant_type (thanks to https://github.com/tlxtellef)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ public OAuth10aService(DefaultApi10a api, String apiKey, String apiSecret, Strin
}

public OAuth1RequestToken getRequestToken() throws IOException, InterruptedException, ExecutionException {
log("obtaining request token from " + api.getRequestTokenEndpoint());
log("obtaining request token from %s", api.getRequestTokenEndpoint());
final OAuthRequest request = prepareRequestTokenRequest();

log("sending request...");
final Response response = execute(request);
final String body = response.getBody();

log("response status code: " + response.getCode());
log("response body: " + body);
log("response status code: %s", response.getCode());
log("response body: %s", body);
return api.getRequestTokenExtractor().extract(response);
}

Expand All @@ -62,7 +62,7 @@ public Future<OAuth1RequestToken> getRequestTokenAsync() {
}

public Future<OAuth1RequestToken> getRequestTokenAsync(OAuthAsyncRequestCallback<OAuth1RequestToken> callback) {
log("async obtaining request token from " + api.getRequestTokenEndpoint());
log("async obtaining request token from %s", api.getRequestTokenEndpoint());
final OAuthRequest request = prepareRequestTokenRequest();
return execute(request, callback, new OAuthRequest.ResponseConverter<OAuth1RequestToken>() {
@Override
Expand All @@ -78,7 +78,7 @@ protected OAuthRequest prepareRequestTokenRequest() {
if (callback == null) {
callback = OAuthConstants.OOB;
}
log("setting oauth_callback to " + callback);
log("setting oauth_callback to %s", callback);
request.addOAuthParameter(OAuthConstants.CALLBACK, callback);
addOAuthParams(request, "");
appendSignature(request);
Expand All @@ -97,12 +97,12 @@ protected void addOAuthParams(OAuthRequest request, String tokenSecret) {
}
request.addOAuthParameter(OAuthConstants.SIGNATURE, getSignature(request, tokenSecret));

log("appended additional OAuth parameters: " + request.getOauthParameters());
log("appended additional OAuth parameters: %s", request.getOauthParameters());
}

public OAuth1AccessToken getAccessToken(OAuth1RequestToken requestToken, String oauthVerifier)
throws IOException, InterruptedException, ExecutionException {
log("obtaining access token from " + api.getAccessTokenEndpoint());
log("obtaining access token from %s", api.getAccessTokenEndpoint());
final OAuthRequest request = prepareAccessTokenRequest(requestToken, oauthVerifier);
final Response response = execute(request);
return api.getAccessTokenExtractor().extract(response);
Expand All @@ -113,8 +113,8 @@ public Future<OAuth1AccessToken> getAccessTokenAsync(OAuth1RequestToken requestT
}

/**
* Start the request to retrieve the access token. The optionally provided
* callback will be called with the Token when it is available.
* Start the request to retrieve the access token. The optionally provided callback will be called with the Token
* when it is available.
*
* @param requestToken request token (obtained previously or null)
* @param oauthVerifier oauth_verifier
Expand All @@ -123,7 +123,7 @@ public Future<OAuth1AccessToken> getAccessTokenAsync(OAuth1RequestToken requestT
*/
public Future<OAuth1AccessToken> getAccessTokenAsync(OAuth1RequestToken requestToken, String oauthVerifier,
OAuthAsyncRequestCallback<OAuth1AccessToken> callback) {
log("async obtaining access token from " + api.getAccessTokenEndpoint());
log("async obtaining access token from %s", api.getAccessTokenEndpoint());
final OAuthRequest request = prepareAccessTokenRequest(requestToken, oauthVerifier);
return execute(request, callback, new OAuthRequest.ResponseConverter<OAuth1AccessToken>() {
@Override
Expand All @@ -137,19 +137,19 @@ protected OAuthRequest prepareAccessTokenRequest(OAuth1RequestToken requestToken
final OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
request.addOAuthParameter(OAuthConstants.TOKEN, requestToken.getToken());
request.addOAuthParameter(OAuthConstants.VERIFIER, oauthVerifier);
log("setting token to: " + requestToken + " and verifier to: " + oauthVerifier);
log("setting token to: %s and verifier to: %s", requestToken, oauthVerifier);
addOAuthParams(request, requestToken.getTokenSecret());
appendSignature(request);
return request;
}

public void signRequest(OAuth1AccessToken token, OAuthRequest request) {
log("signing request: " + request.getCompleteUrl());
log("signing request: %s", request.getCompleteUrl());

if (!token.isEmpty() || api.isEmptyOAuthTokenParamIsRequired()) {
request.addOAuthParameter(OAuthConstants.TOKEN, token.getToken());
}
log("setting token to: " + token);
log("setting token to: %s", token);
addOAuthParams(request, token.getTokenSecret());
appendSignature(request);
}
Expand All @@ -160,8 +160,7 @@ public String getVersion() {
}

/**
* Returns the URL where you should redirect your users to authenticate your
* application.
* Returns the URL where you should redirect your users to authenticate your application.
*
* @param requestToken the request token you need to authorize
* @return the URL where you should redirect your users
Expand All @@ -175,8 +174,8 @@ private String getSignature(OAuthRequest request, String tokenSecret) {
final String baseString = api.getBaseStringExtractor().extract(request);
final String signature = api.getSignatureService().getSignature(baseString, getApiSecret(), tokenSecret);

log("base string is: " + baseString);
log("signature is: " + signature);
log("base string is: %s", baseString);
log("signature is: %s", signature);
return signature;
}

Expand Down Expand Up @@ -205,9 +204,9 @@ public DefaultApi10a getApi() {
return api;
}

public void log(String message) {
public void log(String messagePattern, Object... params) {
if (debugStream != null) {
message += '\n';
final String message = String.format(messagePattern, params) + '\n';
try {
debugStream.write(message.getBytes("UTF8"));
} catch (IOException | RuntimeException e) {
Expand Down

0 comments on commit ae14353

Please sign in to comment.