SOLR-15154: Close Reader used for credentials file

This commit is contained in:
Tomas Fernandez Lobbe 2021-03-05 21:38:33 -08:00
parent 895deb89e6
commit 03aec55f1e
2 changed files with 11 additions and 9 deletions

View File

@ -17,10 +17,11 @@
package org.apache.solr.client.solrj.impl; package org.apache.solr.client.solrj.impl;
import java.io.FileInputStream; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; 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))); 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) { } else if (configFile != null) {
Properties defaultProps = new Properties(); Properties defaultProps = new Properties();
try { try (BufferedReader reader = Files.newBufferedReader(Path.of(configFile), StandardCharsets.UTF_8)) {
defaultProps.load(new InputStreamReader(new FileInputStream(configFile), StandardCharsets.UTF_8)); defaultProps.load(reader);
} catch (IOException e) { } catch (IOException e) {
throw new IllegalArgumentException("Unable to read credentials file at " + configFile, e); throw new IllegalArgumentException("Unable to read credentials file at " + configFile, e);
} }

View File

@ -19,10 +19,11 @@ package org.apache.solr.client.solrj.impl;
import org.apache.solr.SolrTestCase; import org.apache.solr.SolrTestCase;
import java.io.File; import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties; import java.util.Properties;
public class PreemptiveBasicAuthClientBuilderFactoryTest extends SolrTestCase { public class PreemptiveBasicAuthClientBuilderFactoryTest extends SolrTestCase {
@ -66,11 +67,11 @@ public class PreemptiveBasicAuthClientBuilderFactoryTest extends SolrTestCase {
Properties p = new Properties(); Properties p = new Properties();
p.setProperty("httpBasicAuthUser", "foo"); p.setProperty("httpBasicAuthUser", "foo");
p.setProperty("httpBasicAuthPassword", "bar"); p.setProperty("httpBasicAuthPassword", "bar");
File f = createTempFile().toFile(); Path f = createTempFile();
try (FileWriter fw = new FileWriter(f, StandardCharsets.UTF_8)) { try (BufferedWriter fw = Files.newBufferedWriter(f, StandardCharsets.UTF_8)) {
p.store(fw, "tmp properties file for PreemptiveBasicAuthClientBuilderFactoryTest.testCredentialsFromConfigFile"); 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(); PreemptiveBasicAuthClientBuilderFactory.CredentialsResolver credentialsResolver = new PreemptiveBasicAuthClientBuilderFactory.CredentialsResolver();
assertEquals("foo", credentialsResolver.defaultParams.get(HttpClientUtil.PROP_BASIC_AUTH_USER)); assertEquals("foo", credentialsResolver.defaultParams.get(HttpClientUtil.PROP_BASIC_AUTH_USER));
assertEquals("bar", credentialsResolver.defaultParams.get(HttpClientUtil.PROP_BASIC_AUTH_PASS)); assertEquals("bar", credentialsResolver.defaultParams.get(HttpClientUtil.PROP_BASIC_AUTH_PASS));