naming updates, close resources, NPE fix

This commit is contained in:
Greg DiCristofaro 2020-11-30 14:48:49 -05:00
parent 95b9e51362
commit 7736629de1
2 changed files with 21 additions and 18 deletions

View File

@ -32,7 +32,7 @@ import org.apache.commons.lang3.StringUtils;
* Attempts to get the domain from a url/domain provided removing the * Attempts to get the domain from a url/domain provided removing the
* subdomain(s). * subdomain(s).
*/ */
class DomainCategorizer { class DomainTokenizer {
/** /**
* This is a node in the trie. Children in the hashmap are identified by * This is a node in the trie. Children in the hashmap are identified by
@ -65,7 +65,7 @@ class DomainCategorizer {
private static final String COMMENT_TOKEN = "//"; private static final String COMMENT_TOKEN = "//";
// singleton instance of this class. // singleton instance of this class.
private static DomainCategorizer categorizer = null; private static DomainTokenizer categorizer = null;
/** /**
* Returns the singleton instance of this class. * Returns the singleton instance of this class.
@ -73,7 +73,7 @@ class DomainCategorizer {
* @return The DomainCategorizer instance. * @return The DomainCategorizer instance.
* @throws IOException * @throws IOException
*/ */
static DomainCategorizer getInstance() throws IOException { static DomainTokenizer getInstance() throws IOException {
if (categorizer == null) { if (categorizer == null) {
categorizer = load(); categorizer = load();
} }
@ -87,22 +87,25 @@ class DomainCategorizer {
* @return The DomainCategorizer instance. * @return The DomainCategorizer instance.
* @throws IOException If there is an error reading the file. * @throws IOException If there is an error reading the file.
*/ */
private static DomainCategorizer load() throws IOException { private static DomainTokenizer load() throws IOException {
InputStream is = DomainCategorizer.class.getResourceAsStream(DOMAIN_LIST); try (InputStream is = DomainTokenizer.class.getResourceAsStream(DOMAIN_LIST);
BufferedReader reader = new BufferedReader(new InputStreamReader(is)); InputStreamReader isReader = new InputStreamReader(is);
DomainCategorizer categorizer = new DomainCategorizer(); BufferedReader reader = new BufferedReader(isReader)) {
while (reader.ready()) {
String line = reader.readLine();
String trimmed = line.trim();
if (!StringUtils.isBlank(trimmed) && !trimmed.startsWith(COMMENT_TOKEN)) {
categorizer.addDomainSuffix(trimmed);
}
}
return categorizer; DomainTokenizer categorizer = new DomainTokenizer();
while (reader.ready()) {
String line = reader.readLine();
String trimmed = line.trim();
if (!StringUtils.isBlank(trimmed) && !trimmed.startsWith(COMMENT_TOKEN)) {
categorizer.addDomainSuffix(trimmed);
}
}
return categorizer;
}
} }
private DomainCategorizer() { private DomainTokenizer() {
} }
// The top-level trie node. // The top-level trie node.
@ -144,7 +147,7 @@ class DomainCategorizer {
*/ */
String getDomain(String domain) { String getDomain(String domain) {
if (StringUtils.isBlank(domain)) { if (StringUtils.isBlank(domain)) {
return null; return "";
} }
List<String> tokens = Stream.of(domain.split(DELIMITER)) List<String> tokens = Stream.of(domain.split(DELIMITER))

View File

@ -75,7 +75,7 @@ public class NetworkUtils {
String base = host; String base = host;
try { try {
base = DomainCategorizer.getInstance().getDomain(host); base = DomainTokenizer.getInstance().getDomain(host);
} catch (IOException ex) { } catch (IOException ex) {
logger.log(Level.WARNING, "Unable to load resources for domain categorization.", ex); logger.log(Level.WARNING, "Unable to load resources for domain categorization.", ex);
} }