diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 172058b3b..1b1d8698f 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -16,6 +16,7 @@ Andrew Eisenberg Andrew Everitt Andrew Stevens Andrey Urazov +André-John Mas Andy Wood Anil K. Vijendran Anli Shundi diff --git a/contributors.xml b/contributors.xml index 9b663e7b6..88cd45221 100644 --- a/contributors.xml +++ b/contributors.xml @@ -90,6 +90,10 @@ Andrey Urazov + + André-John + Mas + Andy Wood diff --git a/src/main/org/apache/tools/ant/MagicNames.java b/src/main/org/apache/tools/ant/MagicNames.java index 01668b11b..bc39a2578 100644 --- a/src/main/org/apache/tools/ant/MagicNames.java +++ b/src/main/org/apache/tools/ant/MagicNames.java @@ -282,5 +282,12 @@ public final class MagicNames { * @since Ant 1.9.1 */ public static final String ATTRIBUTE_NAMESPACE = "attribute namespace"; + + /** + * Name of the property which can provide an override of the + * User-Agent used in <get> tasks. + * Value {@value} + */ + public static final String HTTP_AGENT_PROPERTY = "ant.http.agent"; } diff --git a/src/main/org/apache/tools/ant/Main.java b/src/main/org/apache/tools/ant/Main.java index debfb8843..6be945d2f 100644 --- a/src/main/org/apache/tools/ant/Main.java +++ b/src/main/org/apache/tools/ant/Main.java @@ -1060,6 +1060,11 @@ public class Main implements AntMain { */ private static String antVersion = null; + /** + * Cache of the short Ant version information when it has been loaded. + */ + private static String shortAntVersion = null; + /** * Returns the Ant version information, if available. Once the information * has been loaded once, it's cached and returned from the cache on future @@ -1078,10 +1083,11 @@ public class Main implements AntMain { Main.class.getResourceAsStream("/org/apache/tools/ant/version.txt"); props.load(in); in.close(); + shortAntVersion = props.getProperty("VERSION"); StringBuffer msg = new StringBuffer(); msg.append("Apache Ant(TM) version "); - msg.append(props.getProperty("VERSION")); + msg.append(shortAntVersion); msg.append(" compiled on "); msg.append(props.getProperty("DATE")); antVersion = msg.toString(); @@ -1094,6 +1100,24 @@ public class Main implements AntMain { } return antVersion; } + + /** + * Returns the short Ant version information, if available. Once the information + * has been loaded once, it's cached and returned from the cache on future + * calls. + * + * @return the short Ant version information as a String + * (always non-null) + * + * @throws BuildException BuildException if the version information is unavailable + * @since Ant 1.9.3 + */ + public static String getShortAntVersion() throws BuildException { + if (shortAntVersion == null) { + getAntVersion(); + } + return shortAntVersion; + } /** * Prints the description of a project (if there is one) to diff --git a/src/main/org/apache/tools/ant/taskdefs/Get.java b/src/main/org/apache/tools/ant/taskdefs/Get.java index 70529e69d..d1125ae26 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Get.java +++ b/src/main/org/apache/tools/ant/taskdefs/Get.java @@ -31,6 +31,7 @@ import java.net.URLConnection; import java.util.Date; import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.MagicNames; import org.apache.tools.ant.Main; import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; @@ -80,7 +81,10 @@ public class Get extends Task { private boolean skipExisting = false; private boolean httpUseCaches = true; // on by default private Mapper mapperElement = null; - private String userAgent = System.getProperty("http.agent", DEFAULT_AGENT_PREFIX + "/" + Main.getAntVersion()); + private String userAgent = + System.getProperty(MagicNames.HTTP_AGENT_PROPERTY, + DEFAULT_AGENT_PREFIX + "/" + + Main.getShortAntVersion()); /** * Does the work. @@ -406,6 +410,18 @@ public class Get extends Task { public void setSkipExisting(boolean s) { this.skipExisting = s; } + + /** + * HTTP connections only - set the user-agent to be used + * when communicating with remote server. if null, then + * the value is considered unset and the behaviour falls + * back to the default of the http API. + * + * @since Ant 1.9.3 + */ + public void setUserAgent(String userAgent) { + this.userAgent = userAgent; + } /** * HTTP connections only - control caching on the @@ -421,16 +437,6 @@ public class Get extends Task { this.httpUseCaches = httpUseCache; } - /** - * HTTP connections only - set the user-agent to be used - * when communicating with remote server. if null, then - * the value is considered unset and the behaviour falls - * back to the default of the http API. - */ - public void setUserAgent(String userAgent) { - this.userAgent = userAgent; - } - /** * Define the mapper to map source to destination files. * @return a mapper to be configured. @@ -657,6 +663,9 @@ public class Get extends Task { if (hasTimestamp) { connection.setIfModifiedSince(timestamp); } + // Set the user agent + connection.addRequestProperty("User-Agent", this.userAgent); + // prepare Java 1.1 style credentials if (uname != null || pword != null) { String up = uname + ":" + pword;