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 COMMENT_PREFIX =
"#";
60 private static final String CSV_DELIMITER =
",";
61 private static final String DOMAIN_TYPE_CSV =
"default_domain_categories.csv";
71 private static Map<String, String>
loadMapping() throws IOException {
72 try (InputStream is = DomainCategoryRunner.class.getResourceAsStream(DOMAIN_TYPE_CSV);
73 InputStreamReader isReader =
new InputStreamReader(is, StandardCharsets.UTF_8);
74 BufferedReader reader =
new BufferedReader(isReader)) {
76 Map<String, String> mapping =
new HashMap<>();
78 while (reader.ready()) {
79 String line = reader.readLine();
80 if (!StringUtils.isBlank(line) && !line.startsWith(COMMENT_PREFIX)) {
81 addItem(mapping, line.trim(), lineNum);
98 private static void addItem(Map<String, String> mapping, String line,
int lineNumber) {
100 if (StringUtils.isBlank(line)) {
104 String[] csvItems = line.split(CSV_DELIMITER);
106 if (csvItems.length < 2) {
107 logger.log(Level.WARNING, String.format(
"Unable to properly parse line of \"%s\" at line %d", line, lineNumber));
112 String domainTypeStr = csvItems[1].trim();
113 if (StringUtils.isBlank(domainTypeStr)) {
114 logger.log(Level.WARNING, String.format(
"No category specified for this line: \"%s\" at line %d", line, lineNumber));
119 String hostSuffix = csvItems[0];
120 if (StringUtils.isBlank(hostSuffix)) {
121 logger.log(Level.WARNING, String.format(
"Could not determine host suffix for this line: \"%s\" at line %d", line, lineNumber));
125 mapping.put(hostSuffix.toLowerCase(), domainTypeStr);
129 private Map<String, String> mapping = null;
133 if (isInitialized()) {
138 this.mapping = loadMapping();
139 }
catch (IOException ex) {
140 throw new DomainCategorizerException(
"Unable to load domain type csv for domain category analysis", ex);
150 return this.mapping != null;
155 if (!isInitialized()) {
160 String hostToUse = StringUtils.isBlank(host) ? domain : host;
162 if (StringUtils.isBlank(hostToUse)) {
168 List<String> tokens = Arrays.asList(hostToUse.split(
"\\."));
169 for (
int i = 0; i < tokens.size(); i++) {
170 String searchString = String.join(
".", tokens.subList(i, tokens.size()));
171 String category = mapping.get(searchString);
172 if (StringUtils.isNotBlank(category)) {
181 public synchronized void close() throws Exception {
synchronized void initialize()
static void addItem(Map< String, String > mapping, String line, int lineNumber)
synchronized void close()
synchronized boolean isInitialized()
static Map< String, String > loadMapping()
synchronized DomainCategory getCategory(String domain, String host)
synchronized static Logger getLogger(String name)