SOLR-15154: Close Reader used for credentials file
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/PreemptiveBasicAuthClientBuilderFactory.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/PreemptiveBasicAuthClientBuilderFactory.java
index 2b4f6c2..38274b8 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/PreemptiveBasicAuthClientBuilderFactory.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/PreemptiveBasicAuthClientBuilderFactory.java
@@ -17,10 +17,11 @@
package org.apache.solr.client.solrj.impl;
-import java.io.FileInputStream;
+import java.io.BufferedReader;
import java.io.IOException;
-import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -139,8 +140,8 @@
defaultParams = new MapSolrParams(Map.of(HttpClientUtil.PROP_BASIC_AUTH_USER, ss.get(0), HttpClientUtil.PROP_BASIC_AUTH_PASS, ss.get(1)));
} else if (configFile != null) {
Properties defaultProps = new Properties();
- try {
- defaultProps.load(new InputStreamReader(new FileInputStream(configFile), StandardCharsets.UTF_8));
+ try (BufferedReader reader = Files.newBufferedReader(Path.of(configFile), StandardCharsets.UTF_8)) {
+ defaultProps.load(reader);
} catch (IOException e) {
throw new IllegalArgumentException("Unable to read credentials file at " + configFile, e);
}
diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/PreemptiveBasicAuthClientBuilderFactoryTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/PreemptiveBasicAuthClientBuilderFactoryTest.java
index 2da0def..6a385d7 100644
--- a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/PreemptiveBasicAuthClientBuilderFactoryTest.java
+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/PreemptiveBasicAuthClientBuilderFactoryTest.java
@@ -19,10 +19,11 @@
import org.apache.solr.SolrTestCase;
-import java.io.File;
-import java.io.FileWriter;
+import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.Properties;
public class PreemptiveBasicAuthClientBuilderFactoryTest extends SolrTestCase {
@@ -66,11 +67,11 @@
Properties p = new Properties();
p.setProperty("httpBasicAuthUser", "foo");
p.setProperty("httpBasicAuthPassword", "bar");
- File f = createTempFile().toFile();
- try (FileWriter fw = new FileWriter(f, StandardCharsets.UTF_8)) {
+ Path f = createTempFile();
+ try (BufferedWriter fw = Files.newBufferedWriter(f, StandardCharsets.UTF_8)) {
p.store(fw, "tmp properties file for PreemptiveBasicAuthClientBuilderFactoryTest.testCredentialsFromConfigFile");
}
- System.setProperty(PreemptiveBasicAuthClientBuilderFactory.SYS_PROP_HTTP_CLIENT_CONFIG, f.getAbsolutePath());
+ System.setProperty(PreemptiveBasicAuthClientBuilderFactory.SYS_PROP_HTTP_CLIENT_CONFIG, f.toFile().getAbsolutePath());
PreemptiveBasicAuthClientBuilderFactory.CredentialsResolver credentialsResolver = new PreemptiveBasicAuthClientBuilderFactory.CredentialsResolver();
assertEquals("foo", credentialsResolver.defaultParams.get(HttpClientUtil.PROP_BASIC_AUTH_USER));
assertEquals("bar", credentialsResolver.defaultParams.get(HttpClientUtil.PROP_BASIC_AUTH_PASS));