Initial commit
This commit is contained in:
commit
bfbe536886
59 files changed
+2540
No files matched your search
@@ -0,0 +1,26 @@
|
||||
package org.scribe.utils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class MapUtils
|
||||
{
|
||||
|
||||
public static final Map<String, String> sort(Map<String, String> map)
|
||||
{
|
||||
Preconditions.checkNotNull(map, "Cannot sort a null object.");
|
||||
|
||||
Map<String, String> sorted = new LinkedHashMap<String, String>();
|
||||
for (String key : getSortedKeys(map))
|
||||
{
|
||||
sorted.put(key, map.get(key));
|
||||
}
|
||||
return sorted;
|
||||
}
|
||||
|
||||
private static List<String> getSortedKeys(Map<String, String> map)
|
||||
{
|
||||
List<String> keys = new ArrayList<String>(map.keySet());
|
||||
Collections.sort(keys);
|
||||
return keys;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.scribe.utils;
|
||||
|
||||
import org.scribe.model.OAuthConstants;
|
||||
|
||||
public class Preconditions
|
||||
{
|
||||
private static final String DEFAULT_MESSAGE = "Received an invalid parameter";
|
||||
|
||||
public static void checkNotNull(Object object, String errorMsg)
|
||||
{
|
||||
check(object != null, errorMsg);
|
||||
}
|
||||
|
||||
public static void checkEmptyString(String string, String errorMsg)
|
||||
{
|
||||
check(string != null && !string.trim().isEmpty(), errorMsg);
|
||||
}
|
||||
|
||||
public static void checkValidUrl(String url, String errorMsg)
|
||||
{
|
||||
checkEmptyString(url, errorMsg);
|
||||
check(isUrl(url), errorMsg);
|
||||
}
|
||||
|
||||
public static void checkValidOAuthCallback(String url, String errorMsg)
|
||||
{
|
||||
checkEmptyString(url, errorMsg);
|
||||
if(url.toLowerCase().compareToIgnoreCase(OAuthConstants.OUT_OF_BAND) != 0)
|
||||
{
|
||||
check(isUrl(url), errorMsg);
|
||||
}
|
||||
}
|
||||
|
||||
static boolean isUrl(String url)
|
||||
{
|
||||
return url.startsWith("http://") || url.startsWith("https://");
|
||||
}
|
||||
|
||||
private static void check(boolean requirements, String error)
|
||||
{
|
||||
String message = (error == null || error.trim().isEmpty()) ? DEFAULT_MESSAGE : error;
|
||||
if (!requirements)
|
||||
{
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.scribe.utils;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class StreamUtils
|
||||
{
|
||||
|
||||
public static String getStreamContents(InputStream is)
|
||||
{
|
||||
Preconditions.checkNotNull(is, "Cannot get String from a null object");
|
||||
try
|
||||
{
|
||||
final char[] buffer = new char[0x10000];
|
||||
StringBuilder out = new StringBuilder();
|
||||
Reader in = new InputStreamReader(is, "UTF-8");
|
||||
int read;
|
||||
do
|
||||
{
|
||||
read = in.read(buffer, 0, buffer.length);
|
||||
if (read > 0)
|
||||
{
|
||||
out.append(buffer, 0, read);
|
||||
}
|
||||
} while (read >= 0);
|
||||
in.close();
|
||||
return out.toString();
|
||||
} catch (IOException ioe)
|
||||
{
|
||||
throw new RuntimeException("Error while reading response body", ioe);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package org.scribe.utils;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
|
||||
import org.scribe.exceptions.*;
|
||||
|
||||
public class URLUtils
|
||||
{
|
||||
|
||||
private static final String EMPTY_STRING = "";
|
||||
private static final String UTF_8 = "UTF-8";
|
||||
private static final char PAIR_SEPARATOR = '=';
|
||||
private static final char PARAM_SEPARATOR = '&';
|
||||
|
||||
private static final String ERROR_MSG = String.format("Cannot find specified encoding: %s", UTF_8);
|
||||
|
||||
public static String formURLEncodeMap(Map<String, String> map)
|
||||
{
|
||||
Preconditions.checkNotNull(map, "Cannot url-encode a null object");
|
||||
return (map.size() <= 0) ? EMPTY_STRING : doFormUrlEncode(map);
|
||||
}
|
||||
|
||||
private static String doFormUrlEncode(Map<String, String> map)
|
||||
{
|
||||
StringBuffer encodedString = new StringBuffer();
|
||||
for (String key : map.keySet())
|
||||
{
|
||||
encodedString.append(percentEncode(key)).append(PAIR_SEPARATOR).append(percentEncode(map.get(key))).append(PARAM_SEPARATOR);
|
||||
}
|
||||
return removeTrailingSeparator(encodedString);
|
||||
}
|
||||
|
||||
private static String removeTrailingSeparator(StringBuffer buffer)
|
||||
{
|
||||
return buffer.toString().substring(0, buffer.length() - 1);
|
||||
}
|
||||
|
||||
public static String percentEncode(String string)
|
||||
{
|
||||
Preconditions.checkNotNull(string, "Cannot encode null string");
|
||||
try
|
||||
{
|
||||
return URLEncoder.encode(string, UTF_8);
|
||||
} catch (UnsupportedEncodingException uee)
|
||||
{
|
||||
throw new OAuthException(ERROR_MSG, uee);
|
||||
}
|
||||
}
|
||||
|
||||
public static String percentDecode(String string)
|
||||
{
|
||||
Preconditions.checkNotNull(string, "Cannot decode null string");
|
||||
try
|
||||
{
|
||||
return URLDecoder.decode(string, UTF_8);
|
||||
} catch (UnsupportedEncodingException uee)
|
||||
{
|
||||
throw new OAuthException(ERROR_MSG, uee);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user