Permalink
Browse files

added optional commons-codec dependency

there are some platforms (e.g. android) that do not have by default the java.xml.bind package. The solution is to use the commons-codec implementation if it's on the classpath and fallback to java.xml.bind
1 parent c9401eb commit 86f12eb5bcc1059833c0b8b19d5eb0c2a1726eaa @fernandezpablo85 fernandezpablo85 committed Feb 28, 2013
View
@@ -38,14 +38,19 @@
</scm>
<dependencies>
-
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
-
+ <dependency>
+ <groupId>commons-codec</groupId>
+ <artifactId>commons-codec</artifactId>
+ <version>1.4</version>
+ <scope>compile</scope>
+ <optional>true</optional>
+ </dependency>
</dependencies>
<build>
<plugins>
@@ -4,6 +4,7 @@
import org.scribe.builder.api.*;
import org.scribe.model.*;
+import org.scribe.services.*;
import org.scribe.utils.*;
import java.util.concurrent.TimeUnit;
@@ -139,6 +140,7 @@ public String getAuthorizationUrl(Token requestToken)
private String getSignature(OAuthRequest request, Token token)
{
config.log("generating signature...");
+ config.log("using base64 encoder: " + Base64Encoder.type());
String baseString = api.getBaseStringExtractor().extract(request);
String signature = api.getSignatureService().getSignature(baseString, config.getApiSecret(), token.getSecret());
@@ -0,0 +1,36 @@
+package org.scribe.services;
+
+public abstract class Base64Encoder
+{
+ private static Base64Encoder instance;
+
+ public static synchronized Base64Encoder getInstance()
+ {
+ if (instance == null)
+ {
+ instance = createEncoderInstance();
+ }
+ return instance;
+ }
+
+ private static Base64Encoder createEncoderInstance()
+ {
+ if (CommonsEncoder.isPresent())
+ {
+ return new CommonsEncoder();
+ }
+ else
+ {
+ return new DatatypeConverterEncoder();
+ }
+ }
+
+ public static String type()
+ {
+ return getInstance().getType();
+ }
+
+ public abstract String encode(byte[] bytes);
+
+ public abstract String getType();
+}
@@ -0,0 +1,42 @@
+package org.scribe.services;
+
+import org.apache.commons.codec.binary.*;
+import org.scribe.exceptions.*;
+
+import java.io.UnsupportedEncodingException;
+
+public class CommonsEncoder extends Base64Encoder
+{
+
+ @Override
+ public String encode(byte[] bytes)
+ {
+ try
+ {
+ return new String(Base64.encodeBase64(bytes), "UTF-8");
+ }
+ catch (UnsupportedEncodingException e)
+ {
+ throw new OAuthSignatureException("Can't perform base64 encoding", e);
+ }
+ }
+
+ @Override
+ public String getType()
+ {
+ return "CommonsCodec";
+ }
+
+ public static boolean isPresent()
+ {
+ try
+ {
+ Class.forName("org.apache.commons.codec.binary.Base64");
+ return true;
+ }
+ catch (ClassNotFoundException e)
+ {
+ return false;
+ }
+ }
+}
@@ -0,0 +1,18 @@
+package org.scribe.services;
+
+import javax.xml.bind.*;
+
+public class DatatypeConverterEncoder extends Base64Encoder
+{
+ @Override
+ public String encode(byte[] bytes)
+ {
+ return DatatypeConverter.printBase64Binary(bytes);
+ }
+
+ @Override
+ public String getType()
+ {
+ return "DatatypeConverter";
+ }
+}
@@ -6,9 +6,6 @@
import org.scribe.exceptions.*;
import org.scribe.utils.*;
-// implementation of base64 encoding lives here.
-import javax.xml.bind.DatatypeConverter;
-
/**
* HMAC-SHA1 implementation of {@SignatureService}
*
@@ -51,7 +48,7 @@ private String doSign(String toSign, String keyString) throws Exception
private String bytesToBase64String(byte[] bytes)
{
- return DatatypeConverter.printBase64Binary(bytes);
+ return Base64Encoder.getInstance().encode(bytes);
}
/**
@@ -3,9 +3,6 @@
import org.scribe.exceptions.*;
import java.security.*;
-// implementation of base64 encoding lives here.
-import javax.xml.bind.DatatypeConverter;
-
/**
* A signature service that uses the RSA-SHA1 algorithm.
*/
@@ -42,7 +39,7 @@ public String getSignature(String baseString, String apiSecret, String tokenSecr
private String bytesToBase64String(Signature signature) throws SignatureException
{
- return DatatypeConverter.printBase64Binary(signature.sign());
+ return Base64Encoder.getInstance().encode(signature.sign());
}
/**

0 comments on commit 86f12eb

Please sign in to comment.