19 package org.sleuthkit.autopsy.recentactivity;
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.nio.charset.StandardCharsets;
26 import java.util.Arrays;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.logging.Level;
31 import org.apache.commons.lang.StringUtils;
56 @SuppressWarnings(
"try")
59 private static final String CSV_DELIMITER =
",";
60 private static final String DOMAIN_TYPE_CSV =
"default_domain_categories.csv";
70 private static Map<String, String>
loadMapping() throws IOException {
71 try (InputStream is = DomainCategoryRunner.class.getResourceAsStream(DOMAIN_TYPE_CSV);
72 InputStreamReader isReader =
new InputStreamReader(is, StandardCharsets.UTF_8);
73 BufferedReader reader =
new BufferedReader(isReader)) {
75 Map<String, String> mapping =
new HashMap<>();
77 while (reader.ready()) {
78 String line = reader.readLine();
79 if (!StringUtils.isBlank(line)) {
80 addItem(mapping, line.trim(), lineNum);
97 private static void addItem(Map<String, String> mapping, String line,
int lineNumber) {
99 if (StringUtils.isBlank(line)) {
103 String[] csvItems = line.split(CSV_DELIMITER);
105 if (csvItems.length < 2) {
106 logger.log(Level.WARNING, String.format(
"Unable to properly parse line of \"%s\" at line %d", line, lineNumber));
111 String domainTypeStr = csvItems[1].trim();
112 if (StringUtils.isBlank(domainTypeStr)) {
113 logger.log(Level.WARNING, String.format(
"No category specified for this line: \"%s\" at line %d", line, lineNumber));
118 String hostSuffix = csvItems[0];
119 if (StringUtils.isBlank(hostSuffix)) {
120 logger.log(Level.WARNING, String.format(
"Could not determine host suffix for this line: \"%s\" at line %d", line, lineNumber));
124 mapping.put(hostSuffix.toLowerCase(), domainTypeStr);
128 private Map<String, String> mapping = null;
132 if (this.mapping == null) {
134 this.mapping = loadMapping();
135 }
catch (IOException ex) {
136 throw new DomainCategorizerException(
"Unable to load domain type csv for domain category analysis", ex);
144 String hostToUse = StringUtils.isBlank(host) ? domain : host;
146 if (StringUtils.isBlank(hostToUse)) {
152 List<String> tokens = Arrays.asList(hostToUse.split(
"\\."));
153 for (
int i = 0; i < tokens.size(); i++) {
154 String searchString = String.join(
".", tokens.subList(i, tokens.size()));
155 String category = mapping.get(searchString);
156 if (StringUtils.isNotBlank(category)) {
165 public void close() throws Exception {
DomainCategory getCategory(String domain, String host)
static void addItem(Map< String, String > mapping, String line, int lineNumber)
static Map< String, String > loadMapping()
synchronized static Logger getLogger(String name)