Autopsy  4.16.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
TagSetDefinition.java
Go to the documentation of this file.
1 /*
2  * To change this license header, choose License Headers in Project Properties.
3  * To change this template file, choose Tools | Templates
4  * and open the template in the editor.
5  */
6 package org.sleuthkit.autopsy.casemodule.services;
7 
8 import com.google.gson.Gson;
9 import java.io.File;
10 import java.io.FileWriter;
11 import java.io.IOException;
12 import java.nio.file.Path;
13 import java.nio.file.Paths;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
17 import javax.annotation.concurrent.Immutable;
18 import java.io.FileFilter;
19 import java.io.FileReader;
21 
25 @Immutable
26 final public class TagSetDefinition {
27 
28  private final static String FILE_NAME_TEMPLATE = "%s-tag-set.json";
29  private final static Path TAGS_USER_CONFIG_DIR = Paths.get(PlatformUtil.getUserConfigDirectory(), "tags");
30 
31  private final String name;
32  private final List<TagNameDefinition> tagNameDefinitionList;
33 
34  public TagSetDefinition(String name, List<TagNameDefinition> tagNameDefinitionList) {
35  if (name == null || name.isEmpty()) {
36  throw new IllegalArgumentException("Invalid parameter passed to TagSetDefinition constructor. TagSet name was null or empty.");
37  }
38 
39  if (tagNameDefinitionList == null || tagNameDefinitionList.isEmpty()) {
40  throw new IllegalArgumentException("Invalid parameter passed to TagSetDefinition constructor. TagNameDefinition list was null or empty.");
41  }
42 
43  this.name = name;
44  this.tagNameDefinitionList = tagNameDefinitionList;
45  }
46 
52  public String getName() {
53  return name;
54  }
55 
61  public List<TagNameDefinition> getTagNameDefinitions() {
62  return Collections.unmodifiableList(tagNameDefinitionList);
63  }
64 
73  static synchronized void writeTagSetDefinition(TagSetDefinition tagSetDefinition) throws IOException {
74  // Create the tags directory if it doesn't exist.
75  File dir = TAGS_USER_CONFIG_DIR.toFile();
76  if (!dir.exists()) {
77  dir.mkdirs();
78  }
79 
80  File file = Paths.get(TAGS_USER_CONFIG_DIR.toString(), tagSetDefinition.getFileName()).toFile();
81  if (file.exists()) {
82  file.delete();
83  }
84 
85  try (FileWriter writer = new FileWriter(file)) {
86  (new Gson()).toJson(tagSetDefinition, writer);
87  }
88  }
89 
96  static synchronized List<TagSetDefinition> readTagSetDefinitions() throws IOException {
97  List<TagSetDefinition> tagSetList = new ArrayList<>();
98  File dir = TAGS_USER_CONFIG_DIR.toFile();
99 
100  if (!dir.exists()) {
101  return tagSetList;
102  }
103 
104  File[] fileList = dir.listFiles(new TagSetJsonFileFilter());
105  Gson gson = new Gson();
106  for (File file : fileList) {
107  try (FileReader reader = new FileReader(file)) {
108  tagSetList.add(gson.fromJson(reader, TagSetDefinition.class));
109  }
110  }
111 
112  return tagSetList;
113  }
114 
120  private String getFileName() {
121  return String.format(FILE_NAME_TEMPLATE, name.replace(" ", "-"));
122  }
123 
127  private static final class TagSetJsonFileFilter implements FileFilter {
128 
129  @Override
130  public boolean accept(File file) {
131  return file.getName().endsWith("tag-set.json");
132  }
133 
134  }
135 }
TagSetDefinition(String name, List< TagNameDefinition > tagNameDefinitionList)

Copyright © 2012-2020 Basis Technology. Generated on: Tue Sep 22 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.