added 'debug' method to service builder

This commit is contained in:
Pablo Fernandez committed 2011-12-16 16:13:57 -03:00
1 parent 61c0b4cc9f
commit 0a63f935d2
8 files changed
+136 -42

No files matched your search

+2
View File
@@ -76,6 +76,8 @@ h1. Questions?
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. 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.
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@.
h1. About me h1. About me
"LinkedIn profile":http://www.linkedin.com/in/fernandezpablo85 "LinkedIn profile":http://www.linkedin.com/in/fernandezpablo85
@@ -1,5 +1,6 @@
package org.scribe.builder; package org.scribe.builder;
import java.io.*;
import org.scribe.builder.api.*; import org.scribe.builder.api.*;
import org.scribe.exceptions.*; import org.scribe.exceptions.*;
import org.scribe.model.*; import org.scribe.model.*;
@@ -21,6 +22,7 @@ public class ServiceBuilder
private Api api; private Api api;
private String scope; private String scope;
private SignatureType signatureType; private SignatureType signatureType;
private OutputStream debugStream;
/** /**
* Default constructor * Default constructor
@@ -28,6 +30,8 @@ public class ServiceBuilder
public ServiceBuilder() public ServiceBuilder()
{ {
this.callback = OAuthConstants.OUT_OF_BAND; this.callback = OAuthConstants.OUT_OF_BAND;
this.signatureType = SignatureType.Header;
this.debugStream = null;
} }
/** /**
@@ -136,6 +140,19 @@ public class ServiceBuilder
this.signatureType = type; this.signatureType = type;
return this; return this;
} }
public ServiceBuilder debugStream(OutputStream stream)
{
Preconditions.checkNotNull(stream, "debug stream can't be null");
this.debugStream = stream;
return this;
}
public ServiceBuilder debug()
{
this.debugStream(System.out);
return this;
}
/** /**
* Returns the fully configured {@link OAuthService} * Returns the fully configured {@link OAuthService}
@@ -147,6 +164,6 @@ public class ServiceBuilder
Preconditions.checkNotNull(api, "You must specify a valid api through the provider() method"); Preconditions.checkNotNull(api, "You must specify a valid api through the provider() method");
Preconditions.checkEmptyString(apiKey, "You must provide an api key"); Preconditions.checkEmptyString(apiKey, "You must provide an api key");
Preconditions.checkEmptyString(apiSecret, "You must provide an api secret"); Preconditions.checkEmptyString(apiSecret, "You must provide an api secret");
return api.createService(new OAuthConfig(apiKey, apiSecret, callback, signatureType, scope)); return api.createService(new OAuthConfig(apiKey, apiSecret, callback, signatureType, scope, debugStream));
} }
} }
@@ -1,5 +1,7 @@
package org.scribe.model; package org.scribe.model;
import java.io.*;
/** /**
* Parameter object that groups OAuth config values * Parameter object that groups OAuth config values
* *
@@ -12,19 +14,21 @@ public class OAuthConfig
private final String callback; private final String callback;
private final SignatureType signatureType; private final SignatureType signatureType;
private final String scope; private final String scope;
private final OutputStream debugStream;
public OAuthConfig(String key, String secret) public OAuthConfig(String key, String secret)
{ {
this(key, secret, null, null, null); this(key, secret, null, null, null, null);
} }
public OAuthConfig(String key, String secret, String callback, SignatureType type, String scope) public OAuthConfig(String key, String secret, String callback, SignatureType type, String scope, OutputStream stream)
{ {
this.apiKey = key; this.apiKey = key;
this.apiSecret = secret; this.apiSecret = secret;
this.callback = callback != null ? callback : OAuthConstants.OUT_OF_BAND; this.callback = callback;
this.signatureType = (type != null) ? type : SignatureType.Header; this.signatureType = type;
this.scope = scope; this.scope = scope;
this.debugStream = stream;
} }
public String getApiKey() public String getApiKey()
@@ -56,4 +60,20 @@ public class OAuthConfig
{ {
return scope != null; return scope != null;
} }
}
public void log(String message)
{
if (debugStream != null)
{
message = message + "\n";
try
{
debugStream.write(message.getBytes("UTF8"));
}
catch (Exception e)
{
// do nothing.
}
}
}
}
@@ -4,6 +4,7 @@ import java.util.*;
import org.scribe.builder.api.*; import org.scribe.builder.api.*;
import org.scribe.model.*; import org.scribe.model.*;
import org.scribe.utils.*;
/** /**
* OAuth 1.0a implementation of {@link OAuthService} * OAuth 1.0a implementation of {@link OAuthService}
@@ -34,12 +35,21 @@ public class OAuth10aServiceImpl implements OAuthService
*/ */
public Token getRequestToken() public Token getRequestToken()
{ {
config.log("obtaining request token from " + api.getRequestTokenEndpoint());
OAuthRequest request = new OAuthRequest(api.getRequestTokenVerb(), api.getRequestTokenEndpoint()); OAuthRequest request = new OAuthRequest(api.getRequestTokenVerb(), api.getRequestTokenEndpoint());
config.log("setting oauth_callback to " + config.getCallback());
request.addOAuthParameter(OAuthConstants.CALLBACK, config.getCallback()); request.addOAuthParameter(OAuthConstants.CALLBACK, config.getCallback());
addOAuthParams(request, OAuthConstants.EMPTY_TOKEN); addOAuthParams(request, OAuthConstants.EMPTY_TOKEN);
addSignature(request); appendSignature(request);
config.log("sending request...");
Response response = request.send(); Response response = request.send();
return api.getRequestTokenExtractor().extract(response.getBody()); String body = response.getBody();
config.log("response status code: " + response.getCode());
config.log("response body: " + body);
return api.getRequestTokenExtractor().extract(body);
} }
private void addOAuthParams(OAuthRequest request, Token token) private void addOAuthParams(OAuthRequest request, Token token)
@@ -51,6 +61,8 @@ public class OAuth10aServiceImpl implements OAuthService
request.addOAuthParameter(OAuthConstants.VERSION, getVersion()); request.addOAuthParameter(OAuthConstants.VERSION, getVersion());
if(config.hasScope()) request.addOAuthParameter(OAuthConstants.SCOPE, config.getScope()); if(config.hasScope()) request.addOAuthParameter(OAuthConstants.SCOPE, config.getScope());
request.addOAuthParameter(OAuthConstants.SIGNATURE, getSignature(request, token)); request.addOAuthParameter(OAuthConstants.SIGNATURE, getSignature(request, token));
config.log("appended additional OAuth parameters: " + MapUtils.toString(request.getOauthParameters()));
} }
/** /**
@@ -58,11 +70,14 @@ public class OAuth10aServiceImpl implements OAuthService
*/ */
public Token getAccessToken(Token requestToken, Verifier verifier) public Token getAccessToken(Token requestToken, Verifier verifier)
{ {
config.log("obtaining access token from " + api.getAccessTokenEndpoint());
OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint()); OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
request.addOAuthParameter(OAuthConstants.TOKEN, requestToken.getToken()); request.addOAuthParameter(OAuthConstants.TOKEN, requestToken.getToken());
request.addOAuthParameter(OAuthConstants.VERIFIER, verifier.getValue()); request.addOAuthParameter(OAuthConstants.VERIFIER, verifier.getValue());
config.log("setting token to: " + requestToken + " and verifier to: " + verifier);
addOAuthParams(request, requestToken); addOAuthParams(request, requestToken);
addSignature(request); appendSignature(request);
Response response = request.send(); Response response = request.send();
return api.getAccessTokenExtractor().extract(response.getBody()); return api.getAccessTokenExtractor().extract(response.getBody());
} }
@@ -72,9 +87,12 @@ public class OAuth10aServiceImpl implements OAuthService
*/ */
public void signRequest(Token token, OAuthRequest request) public void signRequest(Token token, OAuthRequest request)
{ {
config.log("signing request: " + request.getCompleteUrl());
request.addOAuthParameter(OAuthConstants.TOKEN, token.getToken()); request.addOAuthParameter(OAuthConstants.TOKEN, token.getToken());
config.log("setting token to: " + token);
addOAuthParams(request, token); addOAuthParams(request, token);
addSignature(request); appendSignature(request);
} }
/** /**
@@ -95,19 +113,28 @@ public class OAuth10aServiceImpl implements OAuthService
private String getSignature(OAuthRequest request, Token token) private String getSignature(OAuthRequest request, Token token)
{ {
config.log("generating signature...");
String baseString = api.getBaseStringExtractor().extract(request); String baseString = api.getBaseStringExtractor().extract(request);
return api.getSignatureService().getSignature(baseString, config.getApiSecret(), token.getSecret()); String signature = api.getSignatureService().getSignature(baseString, config.getApiSecret(), token.getSecret());
config.log("base string is: " + baseString);
config.log("signature is: " + signature);
return signature;
} }
private void addSignature(OAuthRequest request) private void appendSignature(OAuthRequest request)
{ {
switch (config.getSignatureType()) switch (config.getSignatureType())
{ {
case Header: case Header:
config.log("using Http Header signature");
String oauthHeader = api.getHeaderExtractor().extract(request); String oauthHeader = api.getHeaderExtractor().extract(request);
request.addHeader(OAuthConstants.HEADER, oauthHeader); request.addHeader(OAuthConstants.HEADER, oauthHeader);
break; break;
case QueryString: case QueryString:
config.log("using Querystring signature");
for (Map.Entry<String, String> entry : request.getOauthParameters().entrySet()) for (Map.Entry<String, String> entry : request.getOauthParameters().entrySet())
{ {
request.addQuerystringParameter(entry.getKey(), entry.getValue()); request.addQuerystringParameter(entry.getKey(), entry.getValue());
@@ -0,0 +1,22 @@
package org.scribe.utils;
import java.util.Map;
/**
* @author: Pablo Fernandez
*/
public class MapUtils
{
public static <K,V> String toString(Map<K,V> map)
{
if (map == null) return "";
if (map.isEmpty()) return "{}";
StringBuilder result = new StringBuilder();
for(Map.Entry<K,V> entry : map.entrySet())
{
result.append(String.format(", %s -> %s ", entry.getKey().toString(), entry.getValue().toString()));
}
return "{" + result.substring(1) + "}";
}
}
@@ -63,8 +63,7 @@ public class ServiceBuilderTest
public static class ApiMock implements Api public static class ApiMock implements Api
{ {
public static OAuthConfig config; public static OAuthConfig config;
@Override
public OAuthService createService(OAuthConfig config) public OAuthService createService(OAuthConfig config)
{ {
ApiMock.config = config; ApiMock.config = config;
@@ -1,28 +0,0 @@
package org.scribe.model;
import static org.junit.Assert.*;
import org.junit.*;
public class OAuthConfigTest
{
@Test
public void shouldReturnDefaultValuesIfNotSet()
{
OAuthConfig config = new OAuthConfig("key", "secret");
assertEquals(OAuthConstants.OUT_OF_BAND, config.getCallback());
assertEquals(SignatureType.Header, config.getSignatureType());
assertFalse(config.hasScope());
}
@Test
public void shouldOverrideDefaultsIfSet()
{
OAuthConfig config = new OAuthConfig("key", "secret", "http://callback", SignatureType.Header, "scope");
assertEquals("http://callback", config.getCallback());
assertEquals("key", config.getApiKey());
assertEquals("secret", config.getApiSecret());
}
}
@@ -0,0 +1,35 @@
package org.scribe.utils;
import java.util.*;
import org.junit.*;
/**
* @author: Pablo Fernandez
*/
public class MapUtilsTest
{
@Test
public void shouldPrettyPrintMap()
{
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
map.put(4, "four");
Assert.assertEquals("{ 1 -> one , 2 -> two , 3 -> three , 4 -> four }", MapUtils.toString(map));
}
@Test
public void shouldHandleEmptyMap()
{
Map<Integer, String> map = new HashMap<Integer, String>();
Assert.assertEquals("{}", MapUtils.toString(map));
}
@Test
public void shouldHandleNullInputs()
{
Assert.assertEquals("", MapUtils.toString(null));
}
}