From c8232ed82c53175729e226f4bc2dd464136a1e1b Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Wed, 22 Jul 2009 15:17:00 +0000 Subject: [PATCH] allow methods other than GET in http condition. Submitted by Robert Clark. PR 30244 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@796762 13f79535-47bb-0310-9956-ffa450edef68 --- CONTRIBUTORS | 1 + WHATSNEW | 4 ++ build.xml | 1 + contributors.xml | 4 ++ docs/manual/CoreTasks/conditions.html | 9 +++ .../testcases/taskdefs/conditions/http.xml | 57 +++++++++++++++++ .../tools/ant/taskdefs/condition/Http.java | 27 ++++++++ .../ant/taskdefs/condition/HttpTest.java | 63 +++++++++++++++++++ 8 files changed, 166 insertions(+) create mode 100644 src/etc/testcases/taskdefs/conditions/http.xml create mode 100644 src/tests/junit/org/apache/tools/ant/taskdefs/condition/HttpTest.java diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 8f17ceb85..abc1d3df8 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -248,6 +248,7 @@ Ray Waldin Richard Evans Rick Beton Robert Anderson +Robert Clark Robert Flaherty Robert Shaw Robert Streich diff --git a/WHATSNEW b/WHATSNEW index 3f4a8d7f0..42623f167 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -800,6 +800,10 @@ Other changes: and Ant is running on Java5 or more recent. Bugzilla Issue 46752. + * a new attributes can chose a different request method than GET for + the http condition. + Bugzilla Report 30244 + Changes from Ant 1.7.0 TO Ant 1.7.1 ============================================= diff --git a/build.xml b/build.xml index 47cceb369..2b05f0623 100644 --- a/build.xml +++ b/build.xml @@ -299,6 +299,7 @@ + diff --git a/contributors.xml b/contributors.xml index bced4787d..14c6f3852 100644 --- a/contributors.xml +++ b/contributors.xml @@ -1012,6 +1012,10 @@ Robert Anderson + + Robert + Clark + Robert Flaherty diff --git a/docs/manual/CoreTasks/conditions.html b/docs/manual/CoreTasks/conditions.html index 4856dac38..e49c07f92 100644 --- a/docs/manual/CoreTasks/conditions.html +++ b/docs/manual/CoreTasks/conditions.html @@ -213,6 +213,15 @@ of 400 or greater are viewed as invalid.

are detected No + + requestMethod + The HTTP method to be used when issuing the request. + Any of GET, POST, HEAD, OPTIONS, PUT, DELETEm and TRACE + are valid, subject to protocol restrictions. The default if not + specified is "GET".
+ since Ant 1.8.0 + No +

socket

diff --git a/src/etc/testcases/taskdefs/conditions/http.xml b/src/etc/testcases/taskdefs/conditions/http.xml new file mode 100644 index 000000000..76e87cbe6 --- /dev/null +++ b/src/etc/testcases/taskdefs/conditions/http.xml @@ -0,0 +1,57 @@ + + + + + + This build file should be run by a testcase + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 e0ef3ce55..fffb12666 100644 --- a/src/main/org/apache/tools/ant/taskdefs/condition/Http.java +++ b/src/main/org/apache/tools/ant/taskdefs/condition/Http.java @@ -18,6 +18,7 @@ package org.apache.tools.ant.taskdefs.condition; +import java.util.Locale; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; @@ -30,11 +31,16 @@ import org.apache.tools.ant.ProjectComponent; * Condition to wait for a HTTP request to succeed. Its attribute(s) are: * 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 * @since Ant 1.5 */ public class Http extends ProjectComponent implements Condition { private static final int ERROR_BEGINS = 400; + private static final String DEFAULT_REQUEST_METHOD = "GET"; + private String spec = null; + private String requestMethod = DEFAULT_REQUEST_METHOD; + /** * Set the url attribute @@ -55,6 +61,23 @@ public class Http extends ProjectComponent implements Condition { this.errorsBeginAt = errorsBeginAt; } + /** + * Sets the method to be used when issuing the HTTP request. + * + * @param method The HTTP request method to use. Valid values are + * the same as those accepted by the + * HttpURLConnection.setRequestMetho d() method, + * such as "GET", "HEAD", "TRACE", etc. The default + * if not specified is "GET". + * + * @see java.net.HttpURLConnection.setRequestMethod() + * @since Ant 1.8.0 + */ + public void setRequestMethod(String method) { + this.requestMethod = method == null ? DEFAULT_REQUEST_METHOD + : method.toUpperCase(Locale.US); + } + /** * @return true if the HTTP request succeeds * @exception BuildException if an error occurs @@ -70,6 +93,7 @@ public class Http extends ProjectComponent implements Condition { URLConnection conn = url.openConnection(); if (conn instanceof HttpURLConnection) { HttpURLConnection http = (HttpURLConnection) conn; + http.setRequestMethod(requestMethod); int code = http.getResponseCode(); log("Result code for " + spec + " was " + code, Project.MSG_VERBOSE); @@ -78,6 +102,9 @@ public class Http extends ProjectComponent implements Condition { } return false; } + } catch (java.net.ProtocolException pe) { + throw new BuildException("Invalid HTTP protocol: " + + requestMethod, pe); } catch (java.io.IOException e) { return false; } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/condition/HttpTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/condition/HttpTest.java new file mode 100644 index 000000000..afa8bb3c2 --- /dev/null +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/condition/HttpTest.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.tools.ant.taskdefs.condition; + +import org.apache.tools.ant.Project; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.BuildFileTest; + +/** + * Testcases for the <http> condition. All these tests require + * us to be online as they attempt to get the status of various pages + * on the Ant Apache web site. + */ +public class HttpTest extends BuildFileTest { + + public HttpTest(String name) { + super(name); + } + + /** + * The JUnit setup method + */ + public void setUp() { + configureProject("src/etc/testcases/taskdefs/conditions/http.xml"); + } + + public void testNoMethod() { + expectPropertySet("basic-no-method", "basic-no-method"); + assertPropertyUnset("basic-no-method-bad-url"); + } + + public void testHeadRequest() { + expectPropertySet("test-head-request", "test-head-request"); + assertPropertyUnset("test-head-request-bad-url"); + } + + public void testGetRequest() { + expectPropertySet("test-get-request", "test-get-request"); + assertPropertyUnset("test-get-request-bad-url"); + } + + public void testBadRequestMethod() { + expectSpecificBuildException("bad-request-method", + "invalid HTTP request method specified", + null); + } + +}