Autopsy  4.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
TagsManager.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-16 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.io.Closeable;
22 import java.io.IOException;
23 import java.util.Arrays;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.logging.Level;
27 import org.openide.util.NbBundle;
31 import org.sleuthkit.datamodel.BlackboardArtifact;
32 import org.sleuthkit.datamodel.BlackboardArtifactTag;
33 import org.sleuthkit.datamodel.Content;
34 import org.sleuthkit.datamodel.ContentTag;
35 import org.sleuthkit.datamodel.SleuthkitCase;
36 import org.sleuthkit.datamodel.TagName;
37 import org.sleuthkit.datamodel.TskCoreException;
38 
43 public class TagsManager implements Closeable {
44 
45  private static final Logger logger = Logger.getLogger(TagsManager.class.getName());
46  private static final String TAGS_SETTINGS_NAME = "Tags"; //NON-NLS
47  private static final String TAG_NAMES_SETTING_KEY = "TagNames"; //NON-NLS
48  private final SleuthkitCase caseDb;
49  private final HashMap<String, TagName> uniqueTagNames = new HashMap<>();
50  private boolean tagNamesLoaded = false;
51 
59  TagsManager(SleuthkitCase caseDb) {
60  this.caseDb = caseDb;
61  }
62 
72  public synchronized List<TagName> getAllTagNames() throws TskCoreException {
74  return caseDb.getAllTagNames();
75  }
76 
86  public synchronized List<TagName> getTagNamesInUse() throws TskCoreException {
88  return caseDb.getTagNamesInUse();
89  }
90 
98  public synchronized boolean tagNameExists(String tagDisplayName) {
100  return uniqueTagNames.containsKey(tagDisplayName);
101  }
102 
116  public TagName addTagName(String displayName) throws TagNameAlreadyExistsException, TskCoreException {
117  return addTagName(displayName, "", TagName.HTML_COLOR.NONE);
118  }
119 
134  public TagName addTagName(String displayName, String description) throws TagNameAlreadyExistsException, TskCoreException {
135  return addTagName(displayName, description, TagName.HTML_COLOR.NONE);
136  }
137 
153  public synchronized TagName addTagName(String displayName, String description, TagName.HTML_COLOR color) throws TagNameAlreadyExistsException, TskCoreException {
155  if (uniqueTagNames.containsKey(displayName)) {
156  throw new TagNameAlreadyExistsException();
157  }
158 
159  /*
160  * Add the tag name to the case.
161  */
162  TagName newTagName = caseDb.addTagName(displayName, description, color);
163 
164  /*
165  * Add the tag name to the tags settings.
166  */
167  uniqueTagNames.put(newTagName.getDisplayName(), newTagName);
169 
170  return newTagName;
171  }
172 
184  public ContentTag addContentTag(Content content, TagName tagName) throws TskCoreException {
185  return addContentTag(content, tagName, "", -1, -1);
186  }
187 
200  public ContentTag addContentTag(Content content, TagName tagName, String comment) throws TskCoreException {
201  return addContentTag(content, tagName, comment, -1, -1);
202  }
203 
220  public ContentTag addContentTag(Content content, TagName tagName, String comment, long beginByteOffset, long endByteOffset) throws IllegalArgumentException, TskCoreException {
221  ContentTag tag;
222  synchronized (this) {
224 
225  if (beginByteOffset >= 0 && endByteOffset >= 1) {
226  if (beginByteOffset > content.getSize() - 1) {
227  throw new IllegalArgumentException(NbBundle.getMessage(this.getClass(),
228  "TagsManager.addContentTag.exception.beginByteOffsetOOR.msg",
229  beginByteOffset, content.getSize() - 1));
230  }
231 
232  if (endByteOffset > content.getSize() - 1) {
233  throw new IllegalArgumentException(
234  NbBundle.getMessage(this.getClass(), "TagsManager.addContentTag.exception.endByteOffsetOOR.msg",
235  endByteOffset, content.getSize() - 1));
236  }
237 
238  if (endByteOffset < beginByteOffset) {
239  throw new IllegalArgumentException(
240  NbBundle.getMessage(this.getClass(), "TagsManager.addContentTag.exception.endLTbegin.msg"));
241  }
242  }
243 
244  tag = caseDb.addContentTag(content, tagName, comment, beginByteOffset, endByteOffset);
245  }
246 
247  try {
249  } catch (IllegalStateException ex) {
250  logger.log(Level.SEVERE, NbBundle.getMessage(TagsManager.class, "TagsManager.addContentTag.noCaseWarning"), ex);
251  }
252  return tag;
253  }
254 
263  public void deleteContentTag(ContentTag tag) throws TskCoreException {
264  synchronized (this) {
266  caseDb.deleteContentTag(tag);
267  }
268 
269  try {
271  } catch (IllegalStateException ex) {
272  logger.log(Level.SEVERE, NbBundle.getMessage(TagsManager.class, "TagsManager.deleteContentTag.noCaseWarning"), ex);
273  }
274  }
275 
284  public synchronized List<ContentTag> getAllContentTags() throws TskCoreException {
286  return caseDb.getAllContentTags();
287  }
288 
299  public synchronized long getContentTagsCountByTagName(TagName tagName) throws TskCoreException {
301  return caseDb.getContentTagsCountByTagName(tagName);
302  }
303 
314  public synchronized ContentTag getContentTagByTagID(long tagID) throws TskCoreException {
316  return caseDb.getContentTagByID(tagID);
317  }
318 
330  public synchronized List<ContentTag> getContentTagsByTagName(TagName tagName) throws TskCoreException {
332  return caseDb.getContentTagsByTagName(tagName);
333  }
334 
346  public synchronized List<ContentTag> getContentTagsByContent(Content content) throws TskCoreException {
348  return caseDb.getContentTagsByContent(content);
349  }
350 
363  public BlackboardArtifactTag addBlackboardArtifactTag(BlackboardArtifact artifact, TagName tagName) throws TskCoreException {
364  return addBlackboardArtifactTag(artifact, tagName, "");
365  }
366 
380  public BlackboardArtifactTag addBlackboardArtifactTag(BlackboardArtifact artifact, TagName tagName, String comment) throws TskCoreException {
381  BlackboardArtifactTag tag;
382  synchronized (this) {
384  tag = caseDb.addBlackboardArtifactTag(artifact, tagName, comment);
385  }
386 
387  try {
389  } catch (IllegalStateException ex) {
390  logger.log(Level.SEVERE, NbBundle.getMessage(TagsManager.class, "TagsManager.addBlackboardArtifactTag.noCaseWarning"), ex);
391  }
392  return tag;
393  }
394 
403  public void deleteBlackboardArtifactTag(BlackboardArtifactTag tag) throws TskCoreException {
404  synchronized (this) {
406  caseDb.deleteBlackboardArtifactTag(tag);
407  }
408 
409  try {
411  } catch (IllegalStateException ex) {
412  logger.log(Level.WARNING, NbBundle.getMessage(TagsManager.class, "TagsManager.deleteBlackboardArtifactTag.noCaseWarning"), ex);
413  }
414  }
415 
424  public synchronized List<BlackboardArtifactTag> getAllBlackboardArtifactTags() throws TskCoreException {
426  return caseDb.getAllBlackboardArtifactTags();
427  }
428 
440  public synchronized long getBlackboardArtifactTagsCountByTagName(TagName tagName) throws TskCoreException {
442  return caseDb.getBlackboardArtifactTagsCountByTagName(tagName);
443  }
444 
455  public synchronized BlackboardArtifactTag getBlackboardArtifactTagByTagID(long tagID) throws TskCoreException {
457  return caseDb.getBlackboardArtifactTagByID(tagID);
458  }
459 
471  public synchronized List<BlackboardArtifactTag> getBlackboardArtifactTagsByTagName(TagName tagName) throws TskCoreException {
473  return caseDb.getBlackboardArtifactTagsByTagName(tagName);
474  }
475 
487  public synchronized List<BlackboardArtifactTag> getBlackboardArtifactTagsByArtifact(BlackboardArtifact artifact) throws TskCoreException {
489  return caseDb.getBlackboardArtifactTagsByArtifact(artifact);
490  }
491 
495  @Override
496  public synchronized void close() throws IOException {
498  }
499 
504  private void lazyLoadExistingTagNames() {
505  if (!tagNamesLoaded) {
510  tagNamesLoaded = true;
511  }
512  }
513 
518  private void addTagNamesFromCurrentCase() {
519  try {
520  List<TagName> currentTagNames = caseDb.getAllTagNames();
521  for (TagName tagName : currentTagNames) {
522  uniqueTagNames.put(tagName.getDisplayName(), tagName);
523  }
524  } catch (TskCoreException ex) {
525  Logger.getLogger(TagsManager.class.getName()).log(Level.SEVERE, "Failed to get tag types from the current case", ex); //NON-NLS
526  }
527  }
528 
535  String setting = ModuleSettings.getConfigSetting(TAGS_SETTINGS_NAME, TAG_NAMES_SETTING_KEY);
536  if (null != setting && !setting.isEmpty()) {
537  // Read the tag name setting and break it into tag name tuples.
538  List<String> tagNameTuples = Arrays.asList(setting.split(";"));
539 
540  // Parse each tuple and add the tag names to the current case, one
541  // at a time to gracefully discard any duplicates or corrupt tuples.
542  for (String tagNameTuple : tagNameTuples) {
543  String[] tagNameAttributes = tagNameTuple.split(",");
544  if (!uniqueTagNames.containsKey(tagNameAttributes[0])) {
545  try {
546  TagName tagName = caseDb.addTagName(tagNameAttributes[0], tagNameAttributes[1], TagName.HTML_COLOR.getColorByName(tagNameAttributes[2]));
547  uniqueTagNames.put(tagName.getDisplayName(), tagName);
548  } catch (TskCoreException ex) {
549  Logger.getLogger(TagsManager.class.getName()).log(Level.SEVERE, "Failed to add saved tag name " + tagNameAttributes[0], ex); //NON-NLS
550  }
551  }
552  }
553  }
554  }
555 
559  private void addPredefinedTagNames() {
560  if (!uniqueTagNames.containsKey(NbBundle.getMessage(this.getClass(), "TagsManager.predefTagNames.bookmark.text"))) {
561  try {
562  TagName tagName = caseDb.addTagName(
563  NbBundle.getMessage(this.getClass(), "TagsManager.predefTagNames.bookmark.text"), "", TagName.HTML_COLOR.NONE);
564  uniqueTagNames.put(tagName.getDisplayName(), tagName);
565  } catch (TskCoreException ex) {
566  Logger.getLogger(TagsManager.class.getName()).log(Level.SEVERE, "Failed to add standard 'Bookmark' tag name to case database", ex); //NON-NLS
567  }
568  }
569  }
570 
575  private void saveTagNamesToTagsSettings() {
576  if (!uniqueTagNames.isEmpty()) {
577  StringBuilder setting = new StringBuilder();
578  for (TagName tagName : uniqueTagNames.values()) {
579  if (setting.length() != 0) {
580  setting.append(";");
581  }
582  setting.append(tagName.getDisplayName()).append(",");
583  setting.append(tagName.getDescription()).append(",");
584  setting.append(tagName.getColor().name());
585  }
586  ModuleSettings.setConfigSetting(TAGS_SETTINGS_NAME, TAG_NAMES_SETTING_KEY, setting.toString());
587  }
588  }
589 
593  public static class TagNameAlreadyExistsException extends Exception {
594 
595  private static final long serialVersionUID = 1L;
596  }
597 
598 }
void notifyContentTagDeleted(ContentTag deletedTag)
Definition: Case.java:812
synchronized ContentTag getContentTagByTagID(long tagID)
void notifyBlackBoardArtifactTagDeleted(BlackboardArtifactTag deletedTag)
Definition: Case.java:834
BlackboardArtifactTag addBlackboardArtifactTag(BlackboardArtifact artifact, TagName tagName)
synchronized List< ContentTag > getContentTagsByTagName(TagName tagName)
synchronized List< BlackboardArtifactTag > getAllBlackboardArtifactTags()
TagName addTagName(String displayName, String description)
synchronized TagName addTagName(String displayName, String description, TagName.HTML_COLOR color)
synchronized List< BlackboardArtifactTag > getBlackboardArtifactTagsByArtifact(BlackboardArtifact artifact)
synchronized List< BlackboardArtifactTag > getBlackboardArtifactTagsByTagName(TagName tagName)
ContentTag addContentTag(Content content, TagName tagName)
synchronized boolean tagNameExists(String tagDisplayName)
void deleteBlackboardArtifactTag(BlackboardArtifactTag tag)
ContentTag addContentTag(Content content, TagName tagName, String comment, long beginByteOffset, long endByteOffset)
final HashMap< String, TagName > uniqueTagNames
static synchronized void setConfigSetting(String moduleName, String settingName, String settingVal)
synchronized long getBlackboardArtifactTagsCountByTagName(TagName tagName)
void notifyBlackBoardArtifactTagAdded(BlackboardArtifactTag newTag)
Definition: Case.java:823
static String getConfigSetting(String moduleName, String settingName)
synchronized BlackboardArtifactTag getBlackboardArtifactTagByTagID(long tagID)
synchronized static Logger getLogger(String name)
Definition: Logger.java:166
void notifyContentTagAdded(ContentTag newTag)
Definition: Case.java:801
synchronized long getContentTagsCountByTagName(TagName tagName)
synchronized List< ContentTag > getContentTagsByContent(Content content)
synchronized List< ContentTag > getAllContentTags()
ContentTag addContentTag(Content content, TagName tagName, String comment)
BlackboardArtifactTag addBlackboardArtifactTag(BlackboardArtifact artifact, TagName tagName, String comment)

Copyright © 2012-2015 Basis Technology. Generated on: Wed Apr 6 2016
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.