From 03aec55f1e28fcd9a95b29e5beaf769ca0d023c7 Mon Sep 17 00:00:00 2001 From: Tomas Fernandez Lobbe Date: Fri, 5 Mar 2021 21:38:33 -0800 Subject: [PATCH] SOLR-15154: Close Reader used for credentials file --- .../impl/PreemptiveBasicAuthClientBuilderFactory.java | 9 +++++---- .../PreemptiveBasicAuthClientBuilderFactoryTest.java | 11 ++++++----- 2 files changed, 11 insertions(+), 9 deletions(-) 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 2b4f6c26c89..38274b8800e 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 @@ public class PreemptiveBasicAuthClientBuilderFactory implements HttpClientBuilde 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 2da0defdf36..6a385d7a06c 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 @@ package org.apache.solr.client.solrj.impl; 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 @@ public class PreemptiveBasicAuthClientBuilderFactoryTest extends SolrTestCase { 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));