Skip to content

Commit

Permalink
[playframework#446] Implement timeout for WS
Browse files Browse the repository at this point in the history
  • Loading branch information
erwan committed Dec 13, 2010
1 parent 109e427 commit 51c63e3
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion framework/src/play/Play.java
Expand Up @@ -347,7 +347,7 @@ static void readConfiguration() {
configuration.setProperty(key.toString(), newValue.toString());
}
// Include
Map toInclude = new HashMap(16);
Map<Object, Object> toInclude = new HashMap<Object, Object>(16);
for (Object key : configuration.keySet()) {
if (key.toString().startsWith("@include.")) {
try {
Expand Down
2 changes: 1 addition & 1 deletion framework/src/play/db/jpa/GenericModel.java
Expand Up @@ -116,7 +116,7 @@ public static <T extends JPABase> T edit(Object o, String name, Map<String, Stri
try {
String localName = name + "." + field.getName();
Object to = q.getSingleResult();
edit(to, localName, params, field.getAnnotations());
edit(to, localName, params, field.getAnnotations());
params = Utils.filterMap(params, localName);
bw.set(field.getName(), o, to);
} catch (NoResultException e) {
Expand Down
11 changes: 11 additions & 0 deletions framework/src/play/libs/WS.java
Expand Up @@ -192,6 +192,17 @@ public WSRequest followRedirects(boolean value) {
return this;
}

/**
* Set the value of the request timeout, i.e. the number of seconds before cutting the
* connection - default to 60 seconds
* @param timeout the timeout value, e.g. "30s", "1min"
* @return the WSRequest for chaining
*/
public WSRequest timeout(String timeout) {
this.timeout = Time.parseDuration(timeout);
return this;
}

/**
* Add files to request. This will only work with POST or PUT.
* @param files
Expand Down
8 changes: 5 additions & 3 deletions framework/src/play/libs/XML.java
@@ -1,6 +1,5 @@
package play.libs;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
Expand Down Expand Up @@ -121,8 +120,11 @@ public static boolean validSignature(Document document, Key publicKey) {
}

/**
* Sign the XML document using xmldsig. The input document will be modified, it is returned
* only for convience so you can chain a call if needed.
* Sign the XML document using xmldsig.
* @param document the document to sign; it will be modified by the method.
* @param publicKey the public key from the key pair to sign the document.
* @param privateKey the private key from the key pair to sign the document.
* @return the signed document for chaining.
*/
public static Document sign(Document document, RSAPublicKey publicKey, RSAPrivateKey privateKey) {
XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
Expand Down
4 changes: 4 additions & 0 deletions framework/src/play/libs/ws/WSAsync.java
Expand Up @@ -33,6 +33,7 @@
import com.ning.http.client.AsyncHttpClientConfig;
import com.ning.http.client.AsyncHttpClientConfig.Builder;
import com.ning.http.client.FilePart;
import com.ning.http.client.PerRequestConfig;
import com.ning.http.client.ProxyServer;
import com.ning.http.client.Response;
import com.ning.http.client.StringPart;
Expand Down Expand Up @@ -236,6 +237,9 @@ private BoundRequestBuilder prepare(BoundRequestBuilder builder) {
builder.addHeader(key, headers.get(key));
}
builder.setFollowRedirects(this.followRedirects);
PerRequestConfig perRequestConfig = new PerRequestConfig();
perRequestConfig.setRequestTimeoutInMs(this.timeout * 1000);
builder.setPerRequestConfig(perRequestConfig);
return builder;
}

Expand Down
1 change: 1 addition & 0 deletions framework/src/play/libs/ws/WSUrlFetch.java
Expand Up @@ -116,6 +116,7 @@ private HttpURLConnection prepare(URL url, String method) {
connection.setRequestMethod(method);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(this.followRedirects);
connection.setReadTimeout(this.timeout * 1000);
for (String key: this.headers.keySet()) {
connection.setRequestProperty(key, headers.get(key));
}
Expand Down
3 changes: 1 addition & 2 deletions framework/src/play/utils/Java.java
Expand Up @@ -5,7 +5,6 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
Expand Down Expand Up @@ -45,7 +44,7 @@ public static String[] extractInfosFromByteCode(byte[] code) {
/**
* Try to discover what is hidden under a FutureTask (hack)
*/
public static Object extractUnderlyingCallable(FutureTask futureTask) {
public static Object extractUnderlyingCallable(FutureTask<?> futureTask) {
try {
Field syncField = FutureTask.class.getDeclaredField("sync");
syncField.setAccessible(true);
Expand Down

0 comments on commit 51c63e3

Please sign in to comment.