Apache Lucene open-source search software
SOLR-5960: Add support for basic authentication in post.jar tool
Uwe Schindler8674badf9d95
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex fb3abaa5864..d23c902e438 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -171,6 +171,10 @@ New Featurescapable of reporting its configuration, via REST API.(Tim Potter via Steve Rowe)+* SOLR-5960: Add support for basic authentication in post.jar tool, e.g.:+ java -Durl="http://username:password@hostname:8983/solr/update" -jar post.jar sample.xml+ (Sameer Maggon via Uwe Schindler)+Bug Fixes----------------------diff --git a/solr/core/src/java/org/apache/solr/util/SimplePostTool.java b/solr/core/src/java/org/apache/solr/util/SimplePostTool.javaindex a55f6228720..078aa55b15b 100644--- a/solr/core/src/java/org/apache/solr/util/SimplePostTool.java+++ b/solr/core/src/java/org/apache/solr/util/SimplePostTool.java@@ -50,6 +50,7 @@ import java.util.zip.GZIPInputStream;import java.util.zip.Inflater;import java.util.zip.InflaterInputStream;+import javax.xml.bind.DatatypeConverter;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import javax.xml.xpath.XPath;@@ -812,6 +813,10 @@ public class SimplePostTool {try {if(mockMode) return;HttpURLConnection urlc = (HttpURLConnection) url.openConnection();+ if (url.getUserInfo() != null) {+ String encoding = DatatypeConverter.printBase64Binary(url.getUserInfo().getBytes(StandardCharsets.US_ASCII));+ urlc.setRequestProperty("Authorization", "Basic " + encoding);+ }if (HttpURLConnection.HTTP_OK != urlc.getResponseCode()) {warn("Solr returned an error #" + urlc.getResponseCode() +" " + urlc.getResponseMessage() + " for url "+url);@@ -845,7 +850,10 @@ public class SimplePostTool {urlc.setUseCaches(false);urlc.setAllowUserInteraction(false);urlc.setRequestProperty("Content-type", type);-+ if (url.getUserInfo() != null) {+ String encoding = DatatypeConverter.printBase64Binary(url.getUserInfo().getBytes(StandardCharsets.US_ASCII));+ urlc.setRequestProperty("Authorization", "Basic " + encoding);+ }if (null != length) urlc.setFixedLengthStreamingMode(length);} catch (IOException e) {diff --git a/solr/core/src/test/org/apache/solr/util/SimplePostToolTest.java b/solr/core/src/test/org/apache/solr/util/SimplePostToolTest.javaindex 2b93cebd107..b0aa895c48e 100644--- a/solr/core/src/test/org/apache/solr/util/SimplePostToolTest.java+++ b/solr/core/src/test/org/apache/solr/util/SimplePostToolTest.java@@ -21,7 +21,6 @@ import java.io.ByteArrayInputStream;import java.io.File;import java.io.IOException;import java.io.InputStream;-import java.io.UnsupportedEncodingException;import java.net.MalformedURLException;import java.net.URL;import java.nio.charset.StandardCharsets;@@ -62,7 +61,7 @@ public class SimplePostToolTest extends SolrTestCaseJ4 {t_web = SimplePostTool.parseArgsAndInit(args);System.setProperty("params", "param1=foo¶m2=bar");- System.setProperty("url", "http://localhost:5150/solr/update");+ System.setProperty("url", "http://user:password@localhost:5150/solr/update");t_test = SimplePostTool.parseArgsAndInit(args);pf = new MockPageFetcher();@@ -83,7 +82,7 @@ public class SimplePostToolTest extends SolrTestCaseJ4 {assertEquals(1, t_web.recursive);assertEquals(10, t_web.delay);- assertEquals("http://localhost:5150/solr/update?param1=foo¶m2=bar",t_test.solrUrl.toExternalForm());+ assertEquals("http://user:password@localhost:5150/solr/update?param1=foo¶m2=bar",t_test.solrUrl.toExternalForm());}@Test