6 package org.sleuthkit.autopsy.casemodule.services;
8 import com.google.gson.Gson;
9 import com.google.gson.GsonBuilder;
10 import com.google.gson.JsonArray;
11 import com.google.gson.JsonDeserializationContext;
12 import com.google.gson.JsonDeserializer;
13 import com.google.gson.JsonElement;
14 import com.google.gson.JsonObject;
15 import com.google.gson.JsonParseException;
16 import com.google.gson.JsonSyntaxException;
17 import java.lang.reflect.Type;
19 import java.io.FileWriter;
20 import java.io.IOException;
21 import java.nio.file.Path;
22 import java.nio.file.Paths;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 import javax.annotation.concurrent.Immutable;
27 import java.io.FileFilter;
28 import java.io.FileReader;
29 import java.util.logging.Level;
50 if (name == null || name.isEmpty()) {
51 throw new IllegalArgumentException(
"Invalid parameter passed to TagSetDefinition constructor. TagSet name was null or empty.");
54 if (tagNameDefinitionList == null || tagNameDefinitionList.isEmpty()) {
55 throw new IllegalArgumentException(
"Invalid parameter passed to TagSetDefinition constructor. TagNameDefinition list was null or empty.");
77 return Collections.unmodifiableList(tagNameDefinitionList);
88 static synchronized void writeTagSetDefinition(
TagSetDefinition tagSetDefinition)
throws IOException {
90 File dir = TAGS_USER_CONFIG_DIR.toFile();
95 File file = Paths.get(TAGS_USER_CONFIG_DIR.toString(), tagSetDefinition.getFileName()).toFile();
100 try (FileWriter writer =
new FileWriter(file)) {
101 (
new Gson()).toJson(tagSetDefinition, writer);
111 static synchronized List<TagSetDefinition> readTagSetDefinitions() throws IOException {
112 List<TagSetDefinition> tagSetList =
new ArrayList<>();
113 File dir = TAGS_USER_CONFIG_DIR.toFile();
119 File[] fileList = dir.listFiles(
new TagSetJsonFileFilter());
120 if (fileList == null) {
124 Gson gson =
new GsonBuilder()
125 .registerTypeAdapter(
TagSetDefinition.class,
new TagSetDefinitionDeserializer())
128 for (File file : fileList) {
129 try (FileReader reader =
new FileReader(file)) {
131 if (tagSet != null) {
132 tagSetList.add(tagSet);
134 }
catch (JsonSyntaxException e) {
135 LOGGER.log(Level.SEVERE,
"Skipping invalid JSON file: " + file.getName() +
" - " + e.getMessage());
136 }
catch (IOException e) {
137 LOGGER.log(Level.SEVERE,
"Error reading file: " + file.getName() +
" - " + e.getMessage());
150 return String.format(FILE_NAME_TEMPLATE, name.replace(
" ",
"-"));
160 return file.getName().endsWith(
"tag-set.json");
176 JsonObject jsonObject = json.getAsJsonObject();
178 String name = jsonObject.has(
"name") ? jsonObject.get(
"name").getAsString() : null;
179 JsonArray tagArray = jsonObject.has(
"tagNameDefinitionList") ? jsonObject.getAsJsonArray(
"tagNameDefinitionList") :
new JsonArray();
180 List<TagNameDefinition> tagNameDefinitions =
new ArrayList<>();
182 for (JsonElement element : tagArray) {
183 JsonObject tagObject = element.getAsJsonObject();
185 String displayName = tagObject.has(
"displayName") ? tagObject.get(
"displayName").getAsString() : null;
186 String description = tagObject.has(
"description") ? tagObject.get(
"description").getAsString() : null;
191 if (tagObject.has(
"tagType") && !tagObject.get(
"tagType").isJsonNull()) {
192 tagType = context.deserialize(tagObject.get(
"tagType"),
TskData.
TagType.class);
193 }
else if (tagObject.has(
"knownStatus") && !tagObject.get(
"knownStatus").isJsonNull()) {
201 tagType = legacyStatus;
205 if (tagType == null) {
206 LOGGER.log(Level.SEVERE,
"Failed to initialize tagType for tag: {0}. Skipping entry.", displayName);
210 tagNameDefinitions.add(
new TagNameDefinition(displayName, description, color, tagType));
boolean accept(File file)
TagSetDefinition deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
static final String FILE_NAME_TEMPLATE
static final Logger LOGGER
List< TagNameDefinition > getTagNameDefinitions()
TagSetDefinition(String name, List< TagNameDefinition > tagNameDefinitionList)
synchronized static Logger getLogger(String name)
static final Path TAGS_USER_CONFIG_DIR
final List< TagNameDefinition > tagNameDefinitionList