SOLR-12021: Fixed a bug in ApiSpec and other JSON resource loading that was causing unclosed file handles

This commit is contained in:
Chris Hostetter 2018-02-23 10:43:22 -07:00
parent df0f141907
commit 9e0e301f9f
3 changed files with 27 additions and 10 deletions

View File

@ -223,6 +223,8 @@ Bug Fixes
* SOLR-11971: Don't allow referal to external resources in DataImportHandler's dataConfig request parameter.
(麦 香浓郁, Uwe Schindler)
* SOLR-12021: Fixed a bug in ApiSpec and other JSON resource loading that was causing unclosed file handles (hossman)
Optimizations
----------------------

View File

@ -23,6 +23,7 @@ import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.lang.invoke.MethodHandles;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
@ -200,9 +201,17 @@ public class Utils {
}
}
public static Object fromJSONResource(String resourceName){
return fromJSON(Utils.class.getClassLoader().getResourceAsStream(resourceName));
public static Object fromJSONResource(String resourceName) {
final URL resource = Utils.class.getClassLoader().getResource(resourceName);
if (null == resource) {
throw new IllegalArgumentException("invalid resource name: " + resourceName);
}
try (InputStream stream = resource.openStream()) {
return fromJSON(stream);
} catch (IOException e) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
"Resource error: " + e.getMessage(), e);
}
}
public static JSONParser getJSONParser(Reader reader){
JSONParser parser = new JSONParser(reader);

View File

@ -21,6 +21,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@ -313,17 +314,22 @@ public class ValidatingJsonMap implements Map<String, Object> {
}
public static ValidatingJsonMap parse(String resourceName, String includeLocation) {
InputStream is = ValidatingJsonMap.class.getClassLoader().getResourceAsStream(resourceName);
if (is == null)
final URL resource = ValidatingJsonMap.class.getClassLoader().getResource(resourceName);
if (null == resource) {
throw new RuntimeException("invalid API spec: " + resourceName);
}
ValidatingJsonMap map = null;
try {
map = fromJSON(is, includeLocation);
} catch (Exception e) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error in JSON : " + resourceName, e);
try (InputStream is = resource.openStream()) {
try {
map = fromJSON(is, includeLocation);
} catch (Exception e) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error in JSON : " + resourceName, e);
}
} catch (IOException ioe) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
"Unable to read resource: " + resourceName, ioe);
}
if (map == null) throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Empty value for " + resourceName);
return getDeepCopy(map, 5, false);
}