From b83bdcebf0395250b3ae5e7e4af94ff1400493f0 Mon Sep 17 00:00:00 2001
From: Arcadius Ahouansou
Date: Mon, 23 Jan 2017 22:46:06 +0000
Subject: [PATCH] [AA] Bugzilla Bug 60628 - Ant Get Task To Accept Arbitrary
Header
---
manual/Tasks/get.html | 34 +++++++++++++++
src/etc/testcases/taskdefs/get.xml | 28 ++++++++++++
.../org/apache/tools/ant/taskdefs/Get.java | 43 +++++++++++++++++++
.../tools/ant/taskdefs/email/Header.java | 4 +-
.../apache/tools/ant/taskdefs/GetTest.java | 36 ++++++++++++++++
5 files changed, 144 insertions(+), 1 deletion(-)
diff --git a/manual/Tasks/get.html b/manual/Tasks/get.html
index a2c9b7e59..a6d89e9dd 100644
--- a/manual/Tasks/get.html
+++ b/manual/Tasks/get.html
@@ -170,6 +170,29 @@ plain text' authentication is used. This is only secure over an HTTPS link.
name will be skipped. If the returned name is a relative path, it
will be considered relative to the dest attribute.
+header
+Any arbitrary number of HTTP headers can be added to a request.
+ The attributes of a nested
<header/>
node are as follow:
+
+
+
+
+ Attribute |
+ Description |
+ Required |
+
+
+ name |
+ The name or key of this header. |
+ Yes |
+
+
+ value |
+ The value to assign to the. |
+ Yes |
+
+
+
Examples
<get src="http://ant.apache.org/" dest="help/index.html"/>
Gets the index page of http://ant.apache.org/, and stores it in the file help/index.html
.
@@ -234,6 +257,17 @@ the input task to query for a password.
<url url="http://ant.apache.org/faq.html"/>
</get>
+
+With custom HTTP headers
+
+<get src="http://ant.apache.org/index.html" dest="downloads">
+ <header name="header1" value=="headerValue1" />
+ <header name="header2" value=="headerValue2" />
+ <header name="header3" value=="headerValue3" />
+
+</get>
+
+
Gets the index and FAQ pages of http://ant.apache.org/, and stores
them in the directory downloads
which will be created if
necessary.
diff --git a/src/etc/testcases/taskdefs/get.xml b/src/etc/testcases/taskdefs/get.xml
index b74e92ab2..569d8333e 100644
--- a/src/etc/testcases/taskdefs/get.xml
+++ b/src/etc/testcases/taskdefs/get.xml
@@ -98,6 +98,34 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/main/org/apache/tools/ant/taskdefs/Get.java b/src/main/org/apache/tools/ant/taskdefs/Get.java
index 83f3b6bf0..2200bd89f 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Get.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Get.java
@@ -36,6 +36,7 @@ import org.apache.tools.ant.MagicNames;
import org.apache.tools.ant.Main;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
+import org.apache.tools.ant.taskdefs.email.Header;
import org.apache.tools.ant.types.Mapper;
import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.ResourceCollection;
@@ -44,6 +45,8 @@ import org.apache.tools.ant.types.resources.URLProvider;
import org.apache.tools.ant.types.resources.URLResource;
import org.apache.tools.ant.util.FileNameMapper;
import org.apache.tools.ant.util.FileUtils;
+import java.util.LinkedHashMap;
+import java.util.Map;
/**
* Gets a particular file from a URL source.
@@ -90,6 +93,9 @@ public class Get extends Task {
DEFAULT_AGENT_PREFIX + "/"
+ Main.getShortAntVersion());
+ // Store headers as key/value pair without duplicate in keyz
+ private Map headers = new LinkedHashMap();
+
/**
* Does the work.
*
@@ -482,6 +488,35 @@ public class Get extends Task {
tryGzipEncoding = b;
}
+ /**
+ * Add a nested header
+ * @param header to be added
+ *
+ */
+ public void addConfiguredHeader(Header header) {
+ if (header != null) {
+ String key = trimToNull(header.getName());
+ String value = trimToNull(header.getValue());
+ if (key != null && value != null) {
+ this.headers.put(key, value);
+ }
+ }
+ }
+
+ private String trimToNull(String inputString) {
+
+ if (inputString == null) {
+ return null;
+ }
+
+ inputString = inputString.trim();
+ if ("".equals(inputString)) {
+ return null;
+ }
+ return inputString;
+ }
+
+
/**
* Define the mapper to map source to destination files.
* @return a mapper to be configured.
@@ -726,6 +761,14 @@ public class Get extends Task {
connection.setRequestProperty("Accept-Encoding", GZIP_CONTENT_ENCODING);
}
+ if (!headers.isEmpty()) {
+ for (final Map.Entry header : headers.entrySet()) {
+ //we do not log the header value as it may contain sensitive data like passwords
+ log(String.format("Adding header '%s' ", header.getKey()));
+ connection.setRequestProperty(header.getKey(), header.getValue());
+ }
+ }
+
if (connection instanceof HttpURLConnection) {
((HttpURLConnection) connection)
.setInstanceFollowRedirects(false);
diff --git a/src/main/org/apache/tools/ant/taskdefs/email/Header.java b/src/main/org/apache/tools/ant/taskdefs/email/Header.java
index 6bcfb66ff..aaee69352 100644
--- a/src/main/org/apache/tools/ant/taskdefs/email/Header.java
+++ b/src/main/org/apache/tools/ant/taskdefs/email/Header.java
@@ -19,7 +19,9 @@
package org.apache.tools.ant.taskdefs.email;
/**
- * Class representing a generic e-mail header.
+ * Class representing a generic key-value header.
+ * TODO: This should be moved out of the email package
+ *
* @since Ant 1.7
*/
public class Header {
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/GetTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/GetTest.java
index 3e1157d8b..52950c911 100644
--- a/src/tests/junit/org/apache/tools/ant/taskdefs/GetTest.java
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/GetTest.java
@@ -18,6 +18,7 @@
package org.apache.tools.ant.taskdefs;
+import org.apache.tools.ant.AntAssert;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildFileRule;
import org.junit.After;
@@ -25,6 +26,7 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
/**
@@ -103,6 +105,8 @@ public class GetTest {
public void test7() {
try {
buildRule.executeTarget("test7");
+ AntAssert.assertNotContains("Adding header", buildRule.getLog());
+
fail("userAgent may not be null or empty");
} catch (BuildException ex) {
//TODO assert value
@@ -119,4 +123,36 @@ public class GetTest {
buildRule.executeTarget("testUseTomorrow");
}
+ @Test
+ public void testTwoHeaders() {
+ buildRule.executeTarget("testTwoHeaders");
+ String log = buildRule.getLog();
+ AntAssert.assertContains("Adding header 'header1'", log);
+ AntAssert.assertContains("Adding header 'header2'", log);
+ }
+
+ @Test
+ public void testEmptyHeadersAreNeverAdded() {
+ buildRule.executeTarget("testEmptyHeaders");
+ AntAssert.assertNotContains("Adding header", buildRule.getLog());
+ }
+
+ @Test
+ public void testThatWhenMoreThanOneHeaderHaveSameNameOnlyLastOneIsAdded() {
+ buildRule.executeTarget("testDuplicateHeaderNames");
+ String log = buildRule.getLog();
+ AntAssert.assertContains("Adding header 'header1'", log);
+
+ String firstHeaderLogTrimmed = log.replaceFirst("Adding header ", "");
+ String allHeaderLogsTrimmed = log.replaceAll("Adding header ", "");
+
+ assertEquals("Only one header has been added", firstHeaderLogTrimmed, allHeaderLogsTrimmed);
+ }
+
+ @Test
+ public void testHeaderSpaceTrimmed() {
+ buildRule.executeTarget("testHeaderSpacesTrimmed");
+ AntAssert.assertContains("Adding header 'header1'", buildRule.getLog());
+ }
+
}