Skip to content

Commit 0a63f93

Browse files
added 'debug' method to service builder
1 parent 61c0b4c commit 0a63f93

File tree

8 files changed

+136
-42
lines changed

8 files changed

+136
-42
lines changed

README.textile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ h1. Questions?
7676

7777
Feel free to drop me an email, but there's already a "StackOvferflow":http://stackoverflow.com tag for "scribe":http://stackoverflow.com/questions/tagged/scribe you should use. I'm subscribed to it so I'll pick the question immediately.
7878

79+
Note that it really helps to run scribe on **debug** mode (since 1.3.0), to get additional info. To do this simply call the @.debug()@ method on the @ServiceBuilder@.
80+
7981
h1. About me
8082

8183
"LinkedIn profile":http://www.linkedin.com/in/fernandezpablo85

src/main/java/org/scribe/builder/ServiceBuilder.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.scribe.builder;
22

3+
import java.io.*;
34
import org.scribe.builder.api.*;
45
import org.scribe.exceptions.*;
56
import org.scribe.model.*;
@@ -21,13 +22,16 @@ public class ServiceBuilder
2122
private Api api;
2223
private String scope;
2324
private SignatureType signatureType;
25+
private OutputStream debugStream;
2426

2527
/**
2628
* Default constructor
2729
*/
2830
public ServiceBuilder()
2931
{
3032
this.callback = OAuthConstants.OUT_OF_BAND;
33+
this.signatureType = SignatureType.Header;
34+
this.debugStream = null;
3135
}
3236

3337
/**
@@ -136,6 +140,19 @@ public ServiceBuilder signatureType(SignatureType type)
136140
this.signatureType = type;
137141
return this;
138142
}
143+
144+
public ServiceBuilder debugStream(OutputStream stream)
145+
{
146+
Preconditions.checkNotNull(stream, "debug stream can't be null");
147+
this.debugStream = stream;
148+
return this;
149+
}
150+
151+
public ServiceBuilder debug()
152+
{
153+
this.debugStream(System.out);
154+
return this;
155+
}
139156

140157
/**
141158
* Returns the fully configured {@link OAuthService}
@@ -147,6 +164,6 @@ public OAuthService build()
147164
Preconditions.checkNotNull(api, "You must specify a valid api through the provider() method");
148165
Preconditions.checkEmptyString(apiKey, "You must provide an api key");
149166
Preconditions.checkEmptyString(apiSecret, "You must provide an api secret");
150-
return api.createService(new OAuthConfig(apiKey, apiSecret, callback, signatureType, scope));
167+
return api.createService(new OAuthConfig(apiKey, apiSecret, callback, signatureType, scope, debugStream));
151168
}
152169
}

src/main/java/org/scribe/model/OAuthConfig.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.scribe.model;
22

3+
import java.io.*;
4+
35
/**
46
* Parameter object that groups OAuth config values
57
*
@@ -12,19 +14,21 @@ public class OAuthConfig
1214
private final String callback;
1315
private final SignatureType signatureType;
1416
private final String scope;
17+
private final OutputStream debugStream;
1518

1619
public OAuthConfig(String key, String secret)
1720
{
18-
this(key, secret, null, null, null);
21+
this(key, secret, null, null, null, null);
1922
}
2023

21-
public OAuthConfig(String key, String secret, String callback, SignatureType type, String scope)
24+
public OAuthConfig(String key, String secret, String callback, SignatureType type, String scope, OutputStream stream)
2225
{
2326
this.apiKey = key;
2427
this.apiSecret = secret;
25-
this.callback = callback != null ? callback : OAuthConstants.OUT_OF_BAND;
26-
this.signatureType = (type != null) ? type : SignatureType.Header;
28+
this.callback = callback;
29+
this.signatureType = type;
2730
this.scope = scope;
31+
this.debugStream = stream;
2832
}
2933

3034
public String getApiKey()
@@ -56,4 +60,20 @@ public boolean hasScope()
5660
{
5761
return scope != null;
5862
}
59-
}
63+
64+
public void log(String message)
65+
{
66+
if (debugStream != null)
67+
{
68+
message = message + "\n";
69+
try
70+
{
71+
debugStream.write(message.getBytes("UTF8"));
72+
}
73+
catch (Exception e)
74+
{
75+
// do nothing.
76+
}
77+
}
78+
}
79+
}

src/main/java/org/scribe/oauth/OAuth10aServiceImpl.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import org.scribe.builder.api.*;
66
import org.scribe.model.*;
7+
import org.scribe.utils.*;
78

89
/**
910
* OAuth 1.0a implementation of {@link OAuthService}
@@ -34,12 +35,21 @@ public OAuth10aServiceImpl(DefaultApi10a api, OAuthConfig config)
3435
*/
3536
public Token getRequestToken()
3637
{
38+
config.log("obtaining request token from " + api.getRequestTokenEndpoint());
3739
OAuthRequest request = new OAuthRequest(api.getRequestTokenVerb(), api.getRequestTokenEndpoint());
40+
41+
config.log("setting oauth_callback to " + config.getCallback());
3842
request.addOAuthParameter(OAuthConstants.CALLBACK, config.getCallback());
3943
addOAuthParams(request, OAuthConstants.EMPTY_TOKEN);
40-
addSignature(request);
44+
appendSignature(request);
45+
46+
config.log("sending request...");
4147
Response response = request.send();
42-
return api.getRequestTokenExtractor().extract(response.getBody());
48+
String body = response.getBody();
49+
50+
config.log("response status code: " + response.getCode());
51+
config.log("response body: " + body);
52+
return api.getRequestTokenExtractor().extract(body);
4353
}
4454

4555
private void addOAuthParams(OAuthRequest request, Token token)
@@ -51,18 +61,23 @@ private void addOAuthParams(OAuthRequest request, Token token)
5161
request.addOAuthParameter(OAuthConstants.VERSION, getVersion());
5262
if(config.hasScope()) request.addOAuthParameter(OAuthConstants.SCOPE, config.getScope());
5363
request.addOAuthParameter(OAuthConstants.SIGNATURE, getSignature(request, token));
64+
65+
config.log("appended additional OAuth parameters: " + MapUtils.toString(request.getOauthParameters()));
5466
}
5567

5668
/**
5769
* {@inheritDoc}
5870
*/
5971
public Token getAccessToken(Token requestToken, Verifier verifier)
6072
{
73+
config.log("obtaining access token from " + api.getAccessTokenEndpoint());
6174
OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
6275
request.addOAuthParameter(OAuthConstants.TOKEN, requestToken.getToken());
6376
request.addOAuthParameter(OAuthConstants.VERIFIER, verifier.getValue());
77+
78+
config.log("setting token to: " + requestToken + " and verifier to: " + verifier);
6479
addOAuthParams(request, requestToken);
65-
addSignature(request);
80+
appendSignature(request);
6681
Response response = request.send();
6782
return api.getAccessTokenExtractor().extract(response.getBody());
6883
}
@@ -72,9 +87,12 @@ public Token getAccessToken(Token requestToken, Verifier verifier)
7287
*/
7388
public void signRequest(Token token, OAuthRequest request)
7489
{
90+
config.log("signing request: " + request.getCompleteUrl());
7591
request.addOAuthParameter(OAuthConstants.TOKEN, token.getToken());
92+
93+
config.log("setting token to: " + token);
7694
addOAuthParams(request, token);
77-
addSignature(request);
95+
appendSignature(request);
7896
}
7997

8098
/**
@@ -95,19 +113,28 @@ public String getAuthorizationUrl(Token requestToken)
95113

96114
private String getSignature(OAuthRequest request, Token token)
97115
{
116+
config.log("generating signature...");
98117
String baseString = api.getBaseStringExtractor().extract(request);
99-
return api.getSignatureService().getSignature(baseString, config.getApiSecret(), token.getSecret());
118+
String signature = api.getSignatureService().getSignature(baseString, config.getApiSecret(), token.getSecret());
119+
120+
config.log("base string is: " + baseString);
121+
config.log("signature is: " + signature);
122+
return signature;
100123
}
101124

102-
private void addSignature(OAuthRequest request)
125+
private void appendSignature(OAuthRequest request)
103126
{
104127
switch (config.getSignatureType())
105128
{
106129
case Header:
130+
config.log("using Http Header signature");
131+
107132
String oauthHeader = api.getHeaderExtractor().extract(request);
108133
request.addHeader(OAuthConstants.HEADER, oauthHeader);
109134
break;
110135
case QueryString:
136+
config.log("using Querystring signature");
137+
111138
for (Map.Entry<String, String> entry : request.getOauthParameters().entrySet())
112139
{
113140
request.addQuerystringParameter(entry.getKey(), entry.getValue());
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.scribe.utils;
2+
3+
import java.util.Map;
4+
5+
/**
6+
* @author: Pablo Fernandez
7+
*/
8+
public class MapUtils
9+
{
10+
public static <K,V> String toString(Map<K,V> map)
11+
{
12+
if (map == null) return "";
13+
if (map.isEmpty()) return "{}";
14+
15+
StringBuilder result = new StringBuilder();
16+
for(Map.Entry<K,V> entry : map.entrySet())
17+
{
18+
result.append(String.format(", %s -> %s ", entry.getKey().toString(), entry.getValue().toString()));
19+
}
20+
return "{" + result.substring(1) + "}";
21+
}
22+
}

src/test/java/org/scribe/builder/ServiceBuilderTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ public void shouldAcceptAnScope()
6363
public static class ApiMock implements Api
6464
{
6565
public static OAuthConfig config;
66-
67-
@Override
66+
6867
public OAuthService createService(OAuthConfig config)
6968
{
7069
ApiMock.config = config;

src/test/java/org/scribe/model/OAuthConfigTest.java

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.scribe.utils;
2+
3+
import java.util.*;
4+
import org.junit.*;
5+
6+
/**
7+
* @author: Pablo Fernandez
8+
*/
9+
public class MapUtilsTest
10+
{
11+
12+
@Test
13+
public void shouldPrettyPrintMap()
14+
{
15+
Map<Integer, String> map = new HashMap<Integer, String>();
16+
map.put(1, "one");
17+
map.put(2, "two");
18+
map.put(3, "three");
19+
map.put(4, "four");
20+
Assert.assertEquals("{ 1 -> one , 2 -> two , 3 -> three , 4 -> four }", MapUtils.toString(map));
21+
}
22+
23+
@Test
24+
public void shouldHandleEmptyMap()
25+
{
26+
Map<Integer, String> map = new HashMap<Integer, String>();
27+
Assert.assertEquals("{}", MapUtils.toString(map));
28+
}
29+
30+
@Test
31+
public void shouldHandleNullInputs()
32+
{
33+
Assert.assertEquals("", MapUtils.toString(null));
34+
}
35+
}

0 commit comments

Comments
 (0)