Initial commit
This commit is contained in:
commit
bfbe536886
59 files changed
+2540
No files matched your search
@@ -0,0 +1,8 @@
|
||||
package org.scribe.extractors;
|
||||
|
||||
import org.scribe.model.*;
|
||||
|
||||
public interface AccessTokenExtractor
|
||||
{
|
||||
public Token extract(String response);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package org.scribe.extractors;
|
||||
|
||||
import org.scribe.model.*;
|
||||
|
||||
public interface BaseStringExtractor
|
||||
{
|
||||
String extract(OAuthRequest request);
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package org.scribe.extractors;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import org.scribe.exceptions.*;
|
||||
import org.scribe.model.*;
|
||||
import org.scribe.utils.*;
|
||||
|
||||
public class BaseStringExtractorImpl implements BaseStringExtractor
|
||||
{
|
||||
|
||||
private static final String AMPERSAND_SEPARATED_STRING = "%s&%s&%s";
|
||||
|
||||
public String extract(OAuthRequest request)
|
||||
{
|
||||
checkPreconditions(request);
|
||||
String verb = URLUtils.percentEncode(request.getVerb().name());
|
||||
String url = URLUtils.percentEncode(request.getSanitizedUrl());
|
||||
String params = getSortedAndEncodedParams(request);
|
||||
return String.format(AMPERSAND_SEPARATED_STRING, verb, url, params);
|
||||
}
|
||||
|
||||
private String getSortedAndEncodedParams(OAuthRequest request)
|
||||
{
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.putAll(request.getQueryStringParams());
|
||||
params.putAll(request.getBodyParams());
|
||||
params.putAll(request.getOauthParameters());
|
||||
params = MapUtils.sort(params);
|
||||
return URLUtils.percentEncode(URLUtils.formURLEncodeMap(params));
|
||||
}
|
||||
|
||||
private void checkPreconditions(OAuthRequest request)
|
||||
{
|
||||
Preconditions.checkNotNull(request, "Cannot extract base string from null object");
|
||||
|
||||
if (request.getOauthParameters() == null || request.getOauthParameters().size() <= 0)
|
||||
{
|
||||
throw new OAuthParametersMissingException(request);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package org.scribe.extractors;
|
||||
|
||||
import org.scribe.model.*;
|
||||
|
||||
public interface HeaderExtractor
|
||||
{
|
||||
String extract(OAuthRequest request);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package org.scribe.extractors;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import org.scribe.exceptions.*;
|
||||
import org.scribe.model.*;
|
||||
import org.scribe.utils.*;
|
||||
|
||||
public class HeaderExtractorImpl implements HeaderExtractor
|
||||
{
|
||||
|
||||
private static final String PREAMBLE = "OAuth ";
|
||||
|
||||
public String extract(OAuthRequest request)
|
||||
{
|
||||
checkPreconditions(request);
|
||||
Map<String, String> parameters = request.getOauthParameters();
|
||||
StringBuffer header = new StringBuffer();
|
||||
header.append(PREAMBLE);
|
||||
for (String key : parameters.keySet())
|
||||
{
|
||||
addToHeader(header, key, parameters.get(key));
|
||||
}
|
||||
return removeTrail(header);
|
||||
}
|
||||
|
||||
private void checkPreconditions(OAuthRequest request)
|
||||
{
|
||||
Preconditions.checkNotNull(request, "Cannot extract a header from a null object");
|
||||
|
||||
if (request.getOauthParameters() == null || request.getOauthParameters().size() <= 0)
|
||||
{
|
||||
throw new OAuthParametersMissingException(request);
|
||||
}
|
||||
}
|
||||
|
||||
private void addToHeader(StringBuffer header, String name, String value)
|
||||
{
|
||||
header.append(String.format("%s=\"%s\", ", name, URLUtils.percentEncode(value)));
|
||||
}
|
||||
|
||||
private String removeTrail(StringBuffer header)
|
||||
{
|
||||
return header.toString().substring(0, header.length() - 2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.scribe.extractors;
|
||||
|
||||
import org.scribe.model.*;
|
||||
|
||||
public interface RequestTokenExtractor
|
||||
{
|
||||
|
||||
public Token extract(String response);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.scribe.extractors;
|
||||
|
||||
import java.util.regex.*;
|
||||
|
||||
import org.scribe.exceptions.*;
|
||||
import org.scribe.model.*;
|
||||
import org.scribe.utils.*;
|
||||
|
||||
public class TokenExtractorImpl implements RequestTokenExtractor, AccessTokenExtractor
|
||||
{
|
||||
|
||||
private static final String TOKEN_REGEX = "oauth_token=(\\S*)&oauth_token_secret=(\\S*?)(&(.*))?";
|
||||
|
||||
public Token extract(String response)
|
||||
{
|
||||
Preconditions.checkEmptyString(response, "Cant extract a token from null object or an empty string.");
|
||||
|
||||
Matcher matcher = Pattern.compile(TOKEN_REGEX).matcher(response);
|
||||
if (matcher.matches())
|
||||
{
|
||||
String token = URLUtils.percentDecode(matcher.group(1));
|
||||
String secret = URLUtils.percentDecode(matcher.group(2));
|
||||
return new Token(token, secret);
|
||||
} else
|
||||
{
|
||||
throw new OAuthException("Could not find request token or secret in response: " + response, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user