Skip to content

Commit

Permalink
Added timeout to Request
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandezpablo85 committed Sep 6, 2010
1 parent bf79459 commit 5dfa1cf
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/main/java/org/scribe/model/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@ public class Request
private Map<String, String> bodyParams;
private Map<String, String> headers;
private String payload = null;
private Integer timeout = null;
private HttpURLConnection connection;

/**
* Creates a new Http Request
*
* @param verb Http Verb (GET, POST, etc)
* @param url url with optional querystring parameters.
*/
public Request(Verb verb, String url)
{
this.verb = verb;
Expand All @@ -32,7 +39,8 @@ public Request(Verb verb, String url)
this.headers = new HashMap<String, String>();
try
{
this.connection = (HttpURLConnection) new URL(url).openConnection();
connection = (HttpURLConnection) new URL(url).openConnection();
connection.setConnectTimeout(timeout);
} catch (IOException ioe)
{
throw new OAuthException("Could not open connection to: " + url, ioe);
Expand Down Expand Up @@ -174,6 +182,11 @@ public String getSanitizedUrl()
return url.replaceAll("\\?.*", "").replace("\\:\\d{4}", "");
}

/**
* Returns the body of the request
*
* @return form encoded string
*/
public String getBodyContents()
{
return (payload != null) ? payload : URLUtils.formURLEncodeMap(bodyParams);
Expand All @@ -188,12 +201,30 @@ public Verb getVerb()
{
return verb;
}


/**
* Returns the connection headers as a {@link Map}
*
* @return map of headers
*/
public Map<String, String> getHeaders()
{
return headers;
}

/**
* Sets the connection timeout in milliseconds for the underlying {@link HttpURLConnection}
*
* @param timeout in milliseconds
*/
public void setTimeout(int timeout)
{
this.timeout = timeout;
}

/*
* We need this in order to stub the connection object for test cases
*/
void setConnection(HttpURLConnection connection)
{
this.connection = connection;
Expand Down

0 comments on commit 5dfa1cf

Please sign in to comment.