Skip to content

Commit 5dfa1cf

Browse files
Added timeout to Request
1 parent bf79459 commit 5dfa1cf

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

src/main/java/org/scribe/model/Request.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,15 @@ public class Request
2222
private Map<String, String> bodyParams;
2323
private Map<String, String> headers;
2424
private String payload = null;
25+
private Integer timeout = null;
2526
private HttpURLConnection connection;
2627

28+
/**
29+
* Creates a new Http Request
30+
*
31+
* @param verb Http Verb (GET, POST, etc)
32+
* @param url url with optional querystring parameters.
33+
*/
2734
public Request(Verb verb, String url)
2835
{
2936
this.verb = verb;
@@ -32,7 +39,8 @@ public Request(Verb verb, String url)
3239
this.headers = new HashMap<String, String>();
3340
try
3441
{
35-
this.connection = (HttpURLConnection) new URL(url).openConnection();
42+
connection = (HttpURLConnection) new URL(url).openConnection();
43+
connection.setConnectTimeout(timeout);
3644
} catch (IOException ioe)
3745
{
3846
throw new OAuthException("Could not open connection to: " + url, ioe);
@@ -174,6 +182,11 @@ public String getSanitizedUrl()
174182
return url.replaceAll("\\?.*", "").replace("\\:\\d{4}", "");
175183
}
176184

185+
/**
186+
* Returns the body of the request
187+
*
188+
* @return form encoded string
189+
*/
177190
public String getBodyContents()
178191
{
179192
return (payload != null) ? payload : URLUtils.formURLEncodeMap(bodyParams);
@@ -188,12 +201,30 @@ public Verb getVerb()
188201
{
189202
return verb;
190203
}
191-
204+
205+
/**
206+
* Returns the connection headers as a {@link Map}
207+
*
208+
* @return map of headers
209+
*/
192210
public Map<String, String> getHeaders()
193211
{
194212
return headers;
195213
}
196214

215+
/**
216+
* Sets the connection timeout in milliseconds for the underlying {@link HttpURLConnection}
217+
*
218+
* @param timeout in milliseconds
219+
*/
220+
public void setTimeout(int timeout)
221+
{
222+
this.timeout = timeout;
223+
}
224+
225+
/*
226+
* We need this in order to stub the connection object for test cases
227+
*/
197228
void setConnection(HttpURLConnection connection)
198229
{
199230
this.connection = connection;

0 commit comments

Comments
 (0)