Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add request 'tuners' for access and request token calls

  • Loading branch information...
commit 9a99a44c5f5a7e557f0a4bfa5d64a2587947b590 1 parent b8ca491
@fernandezpablo85 authored
View
17 src/main/java/org/scribe/model/Request.java
@@ -13,10 +13,13 @@
*
* @author Pablo Fernandez
*/
-class Request
+public class Request
{
private static final String CONTENT_LENGTH = "Content-Length";
private static final String CONTENT_TYPE = "Content-Type";
+ private static RequestTuner NOOP = new RequestTuner() {
+ @Override public void tune(Request _){}
+ };
public static final String DEFAULT_CONTENT_TYPE = "application/x-www-form-urlencoded";
private String url;
@@ -54,12 +57,12 @@ public Request(Verb verb, String url)
* @throws RuntimeException
* if the connection cannot be created.
*/
- public Response send()
+ public Response send(RequestTuner tuner)
{
try
{
createConnection();
- return doSend();
+ return doSend(tuner);
}
catch (Exception e)
{
@@ -67,6 +70,11 @@ public Response send()
}
}
+ public Response send()
+ {
+ return send(NOOP);
+ }
+
private void createConnection() throws IOException
{
String completeUrl = getCompleteUrl();
@@ -87,7 +95,7 @@ public String getCompleteUrl()
return querystringParams.appendTo(url);
}
- Response doSend() throws IOException
+ Response doSend(RequestTuner tuner) throws IOException
{
connection.setRequestMethod(this.verb.name());
if (connectTimeout != null)
@@ -103,6 +111,7 @@ Response doSend() throws IOException
{
addBody(connection, getByteBodyContents());
}
+ tuner.tune(this);
return new Response(connection);
}
View
6 src/main/java/org/scribe/model/RequestTuner.java
@@ -0,0 +1,6 @@
+package org.scribe.model;
+
+public abstract class RequestTuner
+{
+ public abstract void tune(Request request);
+}
View
54 src/main/java/org/scribe/oauth/OAuth10aServiceImpl.java
@@ -36,6 +36,16 @@ public OAuth10aServiceImpl(DefaultApi10a api, OAuthConfig config)
*/
public Token getRequestToken(int timeout, TimeUnit unit)
{
+ return getRequestToken(new TimeoutTuner(timeout, unit));
+ }
+
+ public Token getRequestToken()
+ {
+ return getRequestToken(2, TimeUnit.SECONDS);
+ }
+
+ public Token getRequestToken(RequestTuner tuner)
+ {
config.log("obtaining request token from " + api.getRequestTokenEndpoint());
OAuthRequest request = new OAuthRequest(api.getRequestTokenVerb(), api.getRequestTokenEndpoint());
@@ -45,8 +55,7 @@ public Token getRequestToken(int timeout, TimeUnit unit)
appendSignature(request);
config.log("sending request...");
- request.setReadTimeout(timeout, unit);
- Response response = request.send();
+ Response response = request.send(tuner);
String body = response.getBody();
config.log("response status code: " + response.getCode());
@@ -54,11 +63,6 @@ public Token getRequestToken(int timeout, TimeUnit unit)
return api.getRequestTokenExtractor().extract(body);
}
- public Token getRequestToken()
- {
- return getRequestToken(2, TimeUnit.SECONDS);
- }
-
private void addOAuthParams(OAuthRequest request, Token token)
{
request.addOAuthParameter(OAuthConstants.TIMESTAMP, api.getTimestampService().getTimestampInSeconds());
@@ -77,6 +81,16 @@ private void addOAuthParams(OAuthRequest request, Token token)
*/
public Token getAccessToken(Token requestToken, Verifier verifier, int timeout, TimeUnit unit)
{
+ return getAccessToken(requestToken, verifier, new TimeoutTuner(timeout, unit));
+ }
+
+ public Token getAccessToken(Token requestToken, Verifier verifier)
+ {
+ return getAccessToken(requestToken, verifier, 2, TimeUnit.SECONDS);
+ }
+
+ public Token getAccessToken(Token requestToken, Verifier verifier, RequestTuner tuner)
+ {
config.log("obtaining access token from " + api.getAccessTokenEndpoint());
OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
request.addOAuthParameter(OAuthConstants.TOKEN, requestToken.getToken());
@@ -85,16 +99,10 @@ public Token getAccessToken(Token requestToken, Verifier verifier, int timeout,
config.log("setting token to: " + requestToken + " and verifier to: " + verifier);
addOAuthParams(request, requestToken);
appendSignature(request);
- request.setReadTimeout(timeout, unit);
- Response response = request.send();
+ Response response = request.send(tuner);
return api.getAccessTokenExtractor().extract(response.getBody());
}
- public Token getAccessToken(Token requestToken, Verifier verifier)
- {
- return getAccessToken(requestToken, verifier, 2, TimeUnit.SECONDS);
- }
-
/**
* {@inheritDoc}
*/
@@ -159,4 +167,22 @@ private void appendSignature(OAuthRequest request)
break;
}
}
+
+ private static class TimeoutTuner extends RequestTuner
+ {
+ private final int duration;
+ private final TimeUnit unit;
+
+ public TimeoutTuner(int duration, TimeUnit unit)
+ {
+ this.duration = duration;
+ this.unit = unit;
+ }
+
+ @Override
+ public void tune(Request request)
+ {
+ request.setReadTimeout(duration, unit);
+ }
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.