From da9ca6ef0126310ea8d9d36268fe5050941e5325 Mon Sep 17 00:00:00 2001 From: Jaikiran Pai Date: Sun, 24 Feb 2019 09:38:09 +0530 Subject: [PATCH] bz-63193 Introduce a readTimeout attribute for the "http" condition task --- CONTRIBUTORS | 1 + WHATSNEW | 5 +++++ contributors.xml | 4 ++++ manual/Tasks/conditions.html | 9 +++++++++ .../tools/ant/taskdefs/condition/Http.java | 20 +++++++++++++++++++ 5 files changed, 39 insertions(+) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index c9a3f0897..56fb51130 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -128,6 +128,7 @@ Eduard Wirch Edwin Woudt Eli Tucker Emmanuel Bourg +Eugène Adell Eric Barboni Eric Olsen Eric Pugh diff --git a/WHATSNEW b/WHATSNEW index 6a6f302da..0ec0b5114 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -96,6 +96,11 @@ Other changes: the library behind the sshexec and scp Ant tasks. Github Pull Request #84 + * The "http" condition, now has a "readTimeout" attribute which can be + used to control the amount of time to wait for the read to complete. + Bugzilla Report 63193 + + Changes from Ant 1.10.4 TO Ant 1.10.5 ===================================== diff --git a/contributors.xml b/contributors.xml index 960f2db6f..a8e53ab94 100644 --- a/contributors.xml +++ b/contributors.xml @@ -547,6 +547,10 @@ Emmanuel Bourg + + Eugène + Adell + Eric Olsen diff --git a/manual/Tasks/conditions.html b/manual/Tasks/conditions.html index 92799551c..d1d2e4519 100644 --- a/manual/Tasks/conditions.html +++ b/manual/Tasks/conditions.html @@ -215,6 +215,15 @@ URL. By default, HTTP responses errors of 400 or greater are viewed as invalid.< Whether redirects should be followed.
since Ant 1.9.7 No; default is true + + readTimeout + Read timeout, in milli second, that will be used while reading from the target URL. + Accepts any value >= 0. Value of 0 implies wait indefinitely. Value < 0 will be silently + ignored.
+ since Ant 1.10.6 + + No; defaults to 0 +

socket

diff --git a/src/main/org/apache/tools/ant/taskdefs/condition/Http.java b/src/main/org/apache/tools/ant/taskdefs/condition/Http.java index de9317475..7ce6aa129 100644 --- a/src/main/org/apache/tools/ant/taskdefs/condition/Http.java +++ b/src/main/org/apache/tools/ant/taskdefs/condition/Http.java @@ -24,6 +24,7 @@ import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; import java.net.URLConnection; +import java.net.SocketTimeoutException; import java.util.Locale; import org.apache.tools.ant.BuildException; @@ -35,6 +36,7 @@ import org.apache.tools.ant.ProjectComponent; * url - the URL of the request. * errorsBeginAt - number at which errors begin at; default=400. * requestMethod - HTTP request method to use; GET, HEAD, etc. default=GET + * readTimeout - The read timeout in ms. default=0 * @since Ant 1.5 */ public class Http extends ProjectComponent implements Condition { @@ -46,6 +48,7 @@ public class Http extends ProjectComponent implements Condition { private boolean followRedirects = true; private int errorsBeginAt = ERROR_BEGINS; + private int readTimeout = 0; /** * Set the url attribute @@ -92,6 +95,20 @@ public class Http extends ProjectComponent implements Condition { followRedirects = f; } + /** + * Sets the read timeout. Any value < 0 will be ignored + * + * @param t the timeout value in milli seconds + * + * @see java.net.HttpURLConnection#setReadTimeout + * @since Ant 1.10.6 + */ + public void setReadTimeout(int t) { + if(t >= 0) { + this.readTimeout = t; + } + } + /** * @return true if the HTTP request succeeds * @exception BuildException if an error occurs @@ -110,6 +127,7 @@ public class Http extends ProjectComponent implements Condition { HttpURLConnection http = (HttpURLConnection) conn; http.setRequestMethod(requestMethod); http.setInstanceFollowRedirects(followRedirects); + http.setReadTimeout(readTimeout); int code = http.getResponseCode(); log("Result code for " + spec + " was " + code, Project.MSG_VERBOSE); @@ -118,6 +136,8 @@ public class Http extends ProjectComponent implements Condition { } catch (ProtocolException pe) { throw new BuildException("Invalid HTTP protocol: " + requestMethod, pe); + } catch (SocketTimeoutException ste) { + return false; } catch (IOException e) { return false; }