Initial commit

This commit is contained in:
Pablo Fernandez committed 2010-09-05 17:01:30 -03:00
commit bfbe536886
59 files changed
+2540

No files matched your search

@@ -0,0 +1,85 @@
package org.scribe.model;
import org.scribe.model.Request.*;
public class OAuthConfig
{
private String apiKey;
private String apiSecret;
private String callback;
private String requestTokenEndpoint;
private String accessTokenEndpoint;
private Verb requestTokenVerb;
private Verb accessTokenVerb;
public String getApiKey()
{
return apiKey;
}
public void setApiKey(String apiKey)
{
this.apiKey = apiKey;
}
public String getApiSecret()
{
return apiSecret;
}
public void setApiSecret(String apiSecret)
{
this.apiSecret = apiSecret;
}
public String getCallback()
{
return callback != null ? callback : OAuthConstants.OUT_OF_BAND;
}
public void setCallback(String callback)
{
this.callback = callback;
}
public String getRequestTokenEndpoint()
{
return requestTokenEndpoint;
}
public void setRequestTokenEndpoint(String requestTokenEndpoint)
{
this.requestTokenEndpoint = requestTokenEndpoint;
}
public String getAccessTokenEndpoint()
{
return accessTokenEndpoint;
}
public void setAccessTokenEndpoint(String accessTokenEndpoint)
{
this.accessTokenEndpoint = accessTokenEndpoint;
}
public Verb getRequestTokenVerb()
{
return requestTokenVerb != null ? requestTokenVerb : Verb.GET;
}
public void setRequestTokenVerb(Verb requestTokenVerb)
{
this.requestTokenVerb = requestTokenVerb;
}
public Verb getAccessTokenVerb()
{
return accessTokenVerb != null ? accessTokenVerb : Verb.GET;
}
public void setAccessTokenVerb(Verb accessTokenVerb)
{
this.accessTokenVerb = accessTokenVerb;
}
}
@@ -0,0 +1,41 @@
/*
Copyright 2010 Pablo Fernandez
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.scribe.model;
/**
* This class contains OAuth constants, used project-wide
*
* @author Pablo Fernandez
*
*/
public class OAuthConstants
{
public static final String TIMESTAMP = "oauth_timestamp";
public static final String SIGN_METHOD = "oauth_signature_method";
public static final String SIGNATURE = "oauth_signature";
public static final String CONSUMER_SECRET = "oauth_consumer_secret";
public static final String CONSUMER_KEY = "oauth_consumer_key";
public static final String CALLBACK = "oauth_callback";
public static final String VERSION = "oauth_version";
public static final String NONCE = "oauth_nonce";
public static final String PARAM_PREFIX = "oauth_";
public static final String TOKEN = "oauth_token";
public static final String TOKEN_SECRET = "oauth_token_secret";
public static final String OUT_OF_BAND = "oob";
public static final String VERIFIER = "oauth_verifier";
public static final String HEADER = "Authorization";
public static final Token EMPTY_TOKEN = new Token("", "");
}
@@ -0,0 +1,6 @@
package org.scribe.model;
public class OAuthParams
{
}
@@ -0,0 +1,43 @@
package org.scribe.model;
import java.util.*;
public class OAuthRequest extends Request
{
private static final String OAUTH_PREFIX = "oauth_";
private Map<String, String> oauthParameters;
public OAuthRequest(Verb verb, String url)
{
super(verb, url);
this.oauthParameters = new HashMap<String, String>();
}
public void addOAuthParameter(String key, String value)
{
oauthParameters.put(checkKey(key), value);
}
private String checkKey(String key)
{
if (!key.startsWith(OAUTH_PREFIX))
{
throw new IllegalArgumentException(String.format("OAuth parameters must start with '%s'", OAUTH_PREFIX));
} else
{
return key;
}
}
public Map<String, String> getOauthParameters()
{
return oauthParameters;
}
@Override
public String toString()
{
return String.format("@OAuthRequest(%s, %s)", getVerb(), getUrl());
}
}
+217
View File
@@ -0,0 +1,217 @@
package org.scribe.model;
import java.io.*;
import java.net.*;
import java.util.*;
import org.scribe.exceptions.*;
import org.scribe.utils.*;
/**
* Represents an HTTP Request object
*
* @author Pablo Fernandez
*/
public class Request
{
private static final String CONTENT_LENGTH = "Content-Length";
private String url;
private Verb verb;
private Map<String, String> bodyParams;
private Map<String, String> headers;
private String payload = null;
private HttpURLConnection connection;
public Request(Verb verb, String url)
{
this.verb = verb;
this.url = url;
this.bodyParams = new HashMap<String, String>();
this.headers = new HashMap<String, String>();
try
{
this.connection = (HttpURLConnection) new URL(url).openConnection();
} catch (IOException ioe)
{
throw new OAuthException("Could not open connection to: " + url, ioe);
}
}
/**
* Execute the request and return a {@link Response}
*
* @return Http Response
* @throws RuntimeException
* if the connection cannot be created.
*/
public Response send()
{
try
{
return doSend();
} catch (IOException ioe)
{
throw new OAuthException("Problems while creating connection", ioe);
}
}
Response doSend() throws IOException
{
connection.setRequestMethod(this.verb.name());
addHeaders(connection);
if (verb.equals(Verb.PUT) || verb.equals(Verb.POST))
{
addBody(connection, getBodyContents());
}
return new Response(connection);
}
void addHeaders(HttpURLConnection conn)
{
for (String key : headers.keySet())
conn.setRequestProperty(key, headers.get(key));
}
void addBody(HttpURLConnection conn, String content) throws IOException
{
conn.setRequestProperty(CONTENT_LENGTH, String.valueOf(content.getBytes().length));
conn.setDoOutput(true);
conn.getOutputStream().write(content.getBytes());
}
/**
* Add an HTTP Header to the Request
*
* @param name
* @param value
*/
public void addHeader(String key, String value)
{
this.headers.put(key, value);
}
/**
* Add a body Parameter (for POST/ PUT Requests)
*
* @param name
* @param value
*/
public void addBodyParameter(String key, String value)
{
this.bodyParams.put(key, value);
}
/**
* Add body payload.
*
* This method is used when the HTTP body is not a form-url-encoded string,
* but another thing. Like for example XML.
*
* Note: The contents are not part of the OAuth signature
*
* @param payload
*/
public void addPayload(String payload)
{
this.payload = payload;
}
/**
* Get a {@link Map} of the query string parameters.
*
* @return a map containing the query string parameters
*/
public Map<String, String> getQueryStringParams()
{
try
{
Map<String, String> params = new HashMap<String, String>();
String query = new URL(url).getQuery();
if (query != null)
{
for (String param : query.split("&"))
{
String pair[] = param.split("=");
params.put(pair[0], pair[1]);
}
}
return params;
} catch (MalformedURLException mue)
{
throw new OAuthException("Malformed URL", mue);
}
}
/**
* Obtains a {@link Map} of the body parameters.
*
* @return a map containing the body parameters.
*/
public Map<String, String> getBodyParams()
{
return bodyParams;
}
/**
* Obtains the URL of the HTTP Request.
*
* @return the original URL of the HTTP Request
*/
public String getUrl()
{
return url;
}
/**
* Returns the URL without the port and the query string part.
*
* @return the OAuth-sanitized URL
*/
public String getSanitizedUrl()
{
return url.replaceAll("\\?.*", "").replace("\\:\\d{4}", "");
}
public String getBodyContents()
{
return (payload != null) ? payload : URLUtils.formURLEncodeMap(bodyParams);
}
/**
* Returns the HTTP Verb
*
* @return the verb
*/
public Verb getVerb()
{
return verb;
}
public Map<String, String> getHeaders()
{
return headers;
}
void setConnection(HttpURLConnection connection)
{
this.connection = connection;
}
@Override
public String toString()
{
return String.format("@Request(%s %s)", getVerb(), getUrl());
}
/**
* An enumeration containing the most common HTTP Verbs.
*
* @author Pablo Fernandez
*/
public static enum Verb
{
GET, POST, PUT, DELETE
}
}
@@ -0,0 +1,119 @@
package org.scribe.model;
import java.io.*;
import java.net.*;
import java.util.*;
import org.scribe.utils.*;
/**
* Represents an HTTP Response.
*
* @author Pablo Fernandez
*/
public class Response
{
private static final String EMPTY = "";
private int code;
private String body;
private InputStream stream;
private Map<String, String> headers;
Response(HttpURLConnection connection) throws IOException
{
try
{
connection.connect();
code = connection.getResponseCode();
headers = parseHeaders(connection);
stream = wasSuccessful() ? connection.getInputStream() : connection.getErrorStream();
} catch (UnknownHostException e)
{
code = 404;
body = EMPTY;
}
}
private String parseBodyContents()
{
body = StreamUtils.getStreamContents(getStream());
return body;
}
private Map<String, String> parseHeaders(HttpURLConnection conn)
{
Map<String, String> headers = new HashMap<String, String>();
for (String key : conn.getHeaderFields().keySet())
{
headers.put(key, conn.getHeaderFields().get(key).get(0));
}
return headers;
}
/**
* Checks if the code is in the successful or redirect range.
*
* @return true if successful
*/
private boolean wasSuccessful()
{
return getCode() >= 200 && getCode() < 400;
}
/**
* Obtains the HTTP Response body
*
* @return response body
*/
public String getBody()
{
return body != null ? body : parseBodyContents();
}
/**
* Obtains the meaningful stream of the HttpUrlConnection, either inputStream
* or errorInputStream, depending on the status code
*
* @return input stream / error stream
*/
public InputStream getStream()
{
return stream;
}
/**
* Obtains the HTTP status code
*
* @return the status code
*/
public int getCode()
{
return code;
}
/**
* Obtains a {@link Map} containing the HTTP Response Headers
*
* @return headers
*/
public Map<String, String> getHeaders()
{
return headers;
}
/**
* Obtains a single HTTP Header value, or null if undefined
*
* @param header
* name
*
* @return header value or null
*/
public String getHeader(String name)
{
return headers.get(name);
}
}
+30
View File
@@ -0,0 +1,30 @@
package org.scribe.model;
public class Token
{
private final String token;
private final String secret;
public Token(String token, String secret)
{
this.token = token;
this.secret = secret;
}
public String getToken()
{
return token;
}
public String getSecret()
{
return secret;
}
@Override
public String toString()
{
return String.format("Token[%s , %s]", token, secret);
}
}
@@ -0,0 +1,17 @@
package org.scribe.model;
public class Verifier
{
private final String value;
public Verifier(String value)
{
this.value = value;
}
public String getValue()
{
return value;
}
}