Autopsy  4.7.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
TagNameDefinition.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2018 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.casemodule.services;
20 
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.LinkedHashSet;
24 import java.util.List;
25 import java.util.Objects;
26 import java.util.Set;
27 import java.util.logging.Level;
28 import javax.annotation.concurrent.Immutable;
29 import org.openide.util.NbBundle;
34 import org.sleuthkit.datamodel.TagName;
36 import org.sleuthkit.datamodel.SleuthkitCase;
37 import org.sleuthkit.datamodel.TskCoreException;
38 import org.sleuthkit.datamodel.TskData;
39 
43 @Immutable
44 final class TagNameDefinition implements Comparable<TagNameDefinition> {
45 
46  private static final Logger LOGGER = Logger.getLogger(TagNameDefinition.class.getName());
47  @NbBundle.Messages({"TagNameDefinition.predefTagNames.bookmark.text=Bookmark",
48  "TagNameDefinition.predefTagNames.followUp.text=Follow Up",
49  "TagNameDefinition.predefTagNames.notableItem.text=Notable Item"})
50  private static final String TAGS_SETTINGS_NAME = "Tags"; //NON-NLS
51  private static final String TAG_NAMES_SETTING_KEY = "TagNames"; //NON-NLS
52 
53  private static final List<String> STANDARD_NOTABLE_TAG_DISPLAY_NAMES = Arrays.asList(Bundle.TagNameDefinition_predefTagNames_notableItem_text(), DhsImageCategory.ONE.getDisplayName(), DhsImageCategory.TWO.getDisplayName(), DhsImageCategory.THREE.getDisplayName()); // NON-NLS
54  private static final List<String> STANDARD_TAG_DISPLAY_NAMES = Arrays.asList(Bundle.TagNameDefinition_predefTagNames_bookmark_text(), Bundle.TagNameDefinition_predefTagNames_followUp_text(),
55  Bundle.TagNameDefinition_predefTagNames_notableItem_text(), DhsImageCategory.ONE.getDisplayName(),
56  DhsImageCategory.TWO.getDisplayName(), DhsImageCategory.THREE.getDisplayName(),
57  DhsImageCategory.FOUR.getDisplayName(), DhsImageCategory.FIVE.getDisplayName());
58  private final String displayName;
59  private final String description;
60  private final TagName.HTML_COLOR color;
61  private final TskData.FileKnown knownStatus;
62 
72  TagNameDefinition(String displayName, String description, TagName.HTML_COLOR color, TskData.FileKnown status) {
73  this.displayName = displayName;
74  this.description = description;
75  this.color = color;
76  this.knownStatus = status;
77  }
78 
79  static List<String> getStandardTagNames() {
80  return STANDARD_TAG_DISPLAY_NAMES;
81  }
82 
88  String getDisplayName() {
89  return displayName;
90  }
91 
97  String getDescription() {
98  return description;
99  }
100 
106  TagName.HTML_COLOR getColor() {
107  return color;
108  }
109 
115  TskData.FileKnown getKnownStatus() {
116  return knownStatus;
117  }
118 
130  @Override
131  public int compareTo(TagNameDefinition other) {
132  return this.getDisplayName().toLowerCase().compareTo(other.getDisplayName().toLowerCase());
133  }
134 
140  @Override
141  public int hashCode() {
142  int hash = 7;
143  hash = 83 * hash + Objects.hashCode(this.displayName);
144  return hash;
145  }
146 
155  @Override
156  public boolean equals(Object obj) {
157  if (!(obj instanceof TagNameDefinition)) {
158  return false;
159  }
160  boolean sameName = this.getDisplayName().equals(((TagNameDefinition) obj).getDisplayName());
161  boolean sameStatus = this.getKnownStatus().equals(((TagNameDefinition) obj).getKnownStatus());
162  return sameName && sameStatus;
163  }
164 
170  @Override
171  public String toString() {
172  return displayName;
173  }
174 
179  private String toSettingsFormat() {
180  return displayName + "," + description + "," + color.name() + "," + knownStatus.toString();
181  }
182 
183  TagName saveToCase(SleuthkitCase caseDb) {
184  TagName tagName = null;
185  try {
186  tagName = caseDb.addOrUpdateTagName(displayName, description, color, knownStatus);
187  } catch (TskCoreException ex) {
188  LOGGER.log(Level.SEVERE, "Error updating non-file object ", ex);
189  }
190  return tagName;
191  }
192 
199  static synchronized Set<TagNameDefinition> getTagNameDefinitions() {
200  Set<TagNameDefinition> tagNames = new LinkedHashSet<>();
201  //modifiable copy of default tags list for us to keep track of which default tags have already been created
202  Set<String> standardTags = new LinkedHashSet<>(STANDARD_TAG_DISPLAY_NAMES);
203  String setting = ModuleSettings.getConfigSetting(TAGS_SETTINGS_NAME, TAG_NAMES_SETTING_KEY);
204  if (null != setting && !setting.isEmpty()) {
205  List<String> tagNameTuples = Arrays.asList(setting.split(";"));
206  int numberOfAttributes = 0;
207  if (tagNameTuples.size() > 0) {
208  // Determine if Tags.properties file needs to be upgraded
209  numberOfAttributes = tagNameTuples.get(0).split(",").length;
210  }
211  if (numberOfAttributes == 3) {
212  // Upgrade Tags.Properties with the settings in Central Repository Settings if necessary
213  tagNames.addAll(upgradeTagPropertiesFile(tagNameTuples, standardTags));
214  } else if (numberOfAttributes == 4) {
215  // if the Tags.Properties file is up to date parse it
216  tagNames.addAll(readCurrentTagPropertiesFile(tagNameTuples, standardTags));
217  }
218  }
219  //create standard tags which should always exist which were not already created for whatever reason, such as upgrade
220  for (String standardTagName : standardTags) {
221  if (STANDARD_NOTABLE_TAG_DISPLAY_NAMES.contains(standardTagName)) {
222  tagNames.add(new TagNameDefinition(standardTagName, "", TagName.HTML_COLOR.NONE, TskData.FileKnown.BAD));
223  } else {
224  tagNames.add(new TagNameDefinition(standardTagName, "", TagName.HTML_COLOR.NONE, TskData.FileKnown.UNKNOWN));
225  }
226  }
227  return tagNames;
228 
229  }
230 
242  private static Set<TagNameDefinition> upgradeTagPropertiesFile(List<String> tagProperties, Set<String> standardTagsToBeCreated) {
243  Set<TagNameDefinition> tagNames = new LinkedHashSet<>();
244  List<String> legacyNotableTags = new ArrayList<>();
245  String badTagsStr = ModuleSettings.getConfigSetting("CentralRepository", "db.badTags"); // NON-NLS
246  if (badTagsStr == null || badTagsStr.isEmpty()) { //if there were no bad tags in the central repo properties file use the default list
247  legacyNotableTags.addAll(STANDARD_NOTABLE_TAG_DISPLAY_NAMES);
248  } else { //otherwise use the list that was in the central repository properties file
249  legacyNotableTags.addAll(Arrays.asList(badTagsStr.split(",")));
250  }
251  for (String tagNameTuple : tagProperties) {
252  String[] tagNameAttributes = tagNameTuple.split(","); //get the attributes
253  standardTagsToBeCreated.remove(tagNameAttributes[0]); //remove the tag from the list of standard tags which have not been created
254  if (legacyNotableTags.contains(tagNameAttributes[0])) { //if tag should be notable mark create it as such
255  tagNames.add(new TagNameDefinition(tagNameAttributes[0], tagNameAttributes[1],
256  TagName.HTML_COLOR.valueOf(tagNameAttributes[2]), TskData.FileKnown.BAD));
257  } else { //otherwise create it as unknown
258  tagNames.add(new TagNameDefinition(tagNameAttributes[0], tagNameAttributes[1],
259  TagName.HTML_COLOR.valueOf(tagNameAttributes[2]), TskData.FileKnown.UNKNOWN)); //add the default value for that tag
260  }
261  }
262  return tagNames;
263  }
264 
276  private static Set<TagNameDefinition> readCurrentTagPropertiesFile(List<String> tagProperties, Set<String> standardTagsToBeCreated) {
277  Set<TagNameDefinition> tagNames = new LinkedHashSet<>();
278  for (String tagNameTuple : tagProperties) {
279  String[] tagNameAttributes = tagNameTuple.split(","); //get the attributes
280  standardTagsToBeCreated.remove(tagNameAttributes[0]); //remove the tag from the list of standard tags which have not been created
281  tagNames.add(new TagNameDefinition(tagNameAttributes[0], tagNameAttributes[1],
282  TagName.HTML_COLOR.valueOf(tagNameAttributes[2]), TskData.FileKnown.valueOf(tagNameAttributes[3])));
283  }
284  return tagNames;
285  }
286 
292  static synchronized void setTagNameDefinitions(Set<TagNameDefinition> tagNames) {
293  StringBuilder setting = new StringBuilder();
294  for (TagNameDefinition tagName : tagNames) {
295  if (setting.length() != 0) {
296  setting.append(";");
297  }
298  setting.append(tagName.toSettingsFormat());
299  try {
300  SleuthkitCase caseDb = Case.getCurrentCaseThrows().getSleuthkitCase();
301  tagName.saveToCase(caseDb);
302  } catch (NoCurrentCaseException ex) {
303  LOGGER.log(Level.SEVERE, "Exception while getting open case.", ex);
304  }
305  }
306  ModuleSettings.setConfigSetting(TAGS_SETTINGS_NAME, TAG_NAMES_SETTING_KEY, setting.toString());
307  }
308 
309 }

Copyright © 2012-2016 Basis Technology. Generated on: Mon Jun 18 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.