Browse Source

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
master
Stefan Bodewig 16 years ago
parent
commit
c8232ed82c
8 changed files with 166 additions and 0 deletions
  1. +1
    -0
      CONTRIBUTORS
  2. +4
    -0
      WHATSNEW
  3. +1
    -0
      build.xml
  4. +4
    -0
      contributors.xml
  5. +9
    -0
      docs/manual/CoreTasks/conditions.html
  6. +57
    -0
      src/etc/testcases/taskdefs/conditions/http.xml
  7. +27
    -0
      src/main/org/apache/tools/ant/taskdefs/condition/Http.java
  8. +63
    -0
      src/tests/junit/org/apache/tools/ant/taskdefs/condition/HttpTest.java

+ 1
- 0
CONTRIBUTORS View File

@@ -248,6 +248,7 @@ Ray Waldin
Richard Evans
Rick Beton
Robert Anderson
Robert Clark
Robert Flaherty
Robert Shaw
Robert Streich


+ 4
- 0
WHATSNEW View File

@@ -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
=============================================



+ 1
- 0
build.xml View File

@@ -299,6 +299,7 @@

<patternset id="onlinetests">
<exclude name="**/GetTest.java" if="offline"/>
<exclude name="**/HttpTest.java" if="offline"/>
</patternset>

<patternset id="teststhatfail">


+ 4
- 0
contributors.xml View File

@@ -1012,6 +1012,10 @@
<first>Robert</first>
<last>Anderson</last>
</name>
<name>
<first>Robert</first>
<last>Clark</last>
</name>
<name>
<first>Robert</first>
<last>Flaherty</last>


+ 9
- 0
docs/manual/CoreTasks/conditions.html View File

@@ -213,6 +213,15 @@ of 400 or greater are viewed as invalid.</p>
are detected</td>
<td align="center">No</td>
</tr>
<tr>
<td valign="top">requestMethod</td>
<td valign="top">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 &quot;GET&quot;.<br/>
<em>since Ant 1.8.0</em></td>
<td align="center">No</td>
</tr>
</table>

<h4>socket</h4>


+ 57
- 0
src/etc/testcases/taskdefs/conditions/http.xml View File

@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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.
-->
<project default="nope">

<target name="nope">
<fail>This build file should be run by a testcase</fail>
</target>

<target name="basic-no-method">
<condition property="basic-no-method">
<http url="http://ant.apache.org/"/>
</condition>
<condition property="basic-no-method-bad-url">
<http url="http://ant.apache.org/this-page-does-not-exist"/>
</condition>
</target>

<target name="test-head-request">
<condition property="test-head-request">
<http url="http://ant.apache.org/" requestMethod="HEAD"/>
</condition>
<condition property="test-head-request-bad-url">
<http url="http://ant.apache.org/this-page-does-not-exist" requestMethod="HEAD"/>
</condition>
</target>

<target name="test-get-request">
<condition property="test-get-request">
<http url="http://ant.apache.org/" requestMethod="GET"/>
</condition>
<condition property="test-get-request-bad-url">
<http url="http://ant.apache.org/this-page-does-not-exist" requestMethod="GET"/>
</condition>
</target>

<target name="bad-request-method">
<condition property="bad-request-method">
<http url="http://ant.apache.org" requestMethod="UNKNOWN"/>
</condition>
</target>

</project>

+ 27
- 0
src/main/org/apache/tools/ant/taskdefs/condition/Http.java View File

@@ -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;
}


+ 63
- 0
src/tests/junit/org/apache/tools/ant/taskdefs/condition/HttpTest.java View File

@@ -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 &lt;http&gt; 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);
}

}

Loading…
Cancel
Save