Autopsy  4.14.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-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.io.Closeable;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.EnumSet;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.logging.Level;
31 import org.openide.util.NbBundle;
36 import org.sleuthkit.datamodel.BlackboardArtifact;
37 import org.sleuthkit.datamodel.BlackboardArtifactTag;
38 import org.sleuthkit.datamodel.CaseDbAccessManager;
39 import org.sleuthkit.datamodel.Content;
40 import org.sleuthkit.datamodel.ContentTag;
41 import org.sleuthkit.datamodel.SleuthkitCase;
42 import org.sleuthkit.datamodel.TagName;
43 import org.sleuthkit.datamodel.TskCoreException;
44 import org.sleuthkit.datamodel.TskData;
45 import org.sleuthkit.datamodel.TskData.DbType;
46 
51 public class TagsManager implements Closeable {
52 
53  private static final Logger LOGGER = Logger.getLogger(TagsManager.class.getName());
54  private final SleuthkitCase caseDb;
55 
56  static {
57  //Create the contentviewer tags table (beta) if the current case does not
58  //have the table present
59  Case.addEventTypeSubscriber(EnumSet.of(Case.Events.CURRENT_CASE), evt -> {
60  if (evt.getNewValue() != null) {
61  Case currentCase = (Case) evt.getNewValue();
62  try {
63  CaseDbAccessManager caseDb = currentCase.getSleuthkitCase().getCaseDbAccessManager();
64  if (caseDb.tableExists(ContentViewerTagManager.TABLE_NAME)) {
65  return;
66  }
67 
68  if (currentCase.getSleuthkitCase().getDatabaseType().equals(DbType.SQLITE)) {
70  } else if (currentCase.getSleuthkitCase().getDatabaseType().equals(DbType.POSTGRESQL)) {
72  }
73  } catch (TskCoreException ex) {
74  LOGGER.log(Level.SEVERE,
75  String.format("Unable to create the %s table for image tag storage.",
77  }
78  }
79  });
80  }
81 
90  public static boolean containsIllegalCharacters(String tagDisplayName) {
91  return (tagDisplayName.contains("\\")
92  || tagDisplayName.contains(":")
93  || tagDisplayName.contains("*")
94  || tagDisplayName.contains("?")
95  || tagDisplayName.contains("\"")
96  || tagDisplayName.contains("<")
97  || tagDisplayName.contains(">")
98  || tagDisplayName.contains("|")
99  || tagDisplayName.contains(",")
100  || tagDisplayName.contains(";"));
101 
102  }
103 
104  @NbBundle.Messages({"TagsManager.notableTagEnding.text= (Notable)"})
110  public static String getNotableTagLabel() {
111  return Bundle.TagsManager_notableTagEnding_text();
112  }
113 
125  public static Set<String> getTagDisplayNames() throws TskCoreException {
126  Set<String> tagDisplayNames = new HashSet<>();
127  Set<TagNameDefinition> customNames = TagNameDefinition.getTagNameDefinitions();
128  customNames.forEach((tagType) -> {
129  tagDisplayNames.add(tagType.getDisplayName());
130  });
131  try {
133  for (TagName tagName : tagsManager.getAllTagNames()) {
134  tagDisplayNames.add(tagName.getDisplayName());
135  }
136  } catch (NoCurrentCaseException ignored) {
137  /*
138  * No current case, nothing more to add to the set.
139  */
140  }
141  return tagDisplayNames;
142  }
143 
144  public static List<String> getNotableTagDisplayNames() {
145  List<String> tagDisplayNames = new ArrayList<>();
146  for (TagNameDefinition tagDef : TagNameDefinition.getTagNameDefinitions()) {
147  if (tagDef.getKnownStatus() == TskData.FileKnown.BAD) {
148  tagDisplayNames.add(tagDef.getDisplayName());
149  }
150  }
151  return tagDisplayNames;
152  }
153 
159  public static List<String> getStandardTagNames() {
160  return TagNameDefinition.getStandardTagNames();
161  }
162 
169  TagsManager(SleuthkitCase caseDb) {
170  this.caseDb = caseDb;
171  for (TagNameDefinition tagName : TagNameDefinition.getTagNameDefinitions()) {
172  tagName.saveToCase(caseDb);
173  }
174  }
175 
183  public List<TagName> getAllTagNames() throws TskCoreException {
184  return caseDb.getAllTagNames();
185  }
186 
195  public List<TagName> getTagNamesInUse() throws TskCoreException {
196  return caseDb.getTagNamesInUse();
197  }
198 
209  public List<TagName> getTagNamesInUseForUser(String userName) throws TskCoreException {
210  Set<TagName> tagNameSet = new HashSet<>();
211  List<BlackboardArtifactTag> artifactTags = caseDb.getAllBlackboardArtifactTags();
212  for (BlackboardArtifactTag tag : artifactTags) {
213  if (tag.getUserName().equals(userName)) {
214  tagNameSet.add(tag.getName());
215  }
216  }
217  List<ContentTag> contentTags = caseDb.getAllContentTags();
218  for (ContentTag tag : contentTags) {
219  if (tag.getUserName().equals(userName)) {
220  tagNameSet.add(tag.getName());
221  }
222  }
223  return new ArrayList<>(tagNameSet);
224  }
225 
238  public List<TagName> getTagNamesInUse(long dsObjId) throws TskCoreException {
239  return caseDb.getTagNamesInUse(dsObjId);
240  }
241 
255  public List<TagName> getTagNamesInUseForUser(long dsObjId, String userName) throws TskCoreException {
256  Set<TagName> tagNameSet = new HashSet<>();
257  List<BlackboardArtifactTag> artifactTags = caseDb.getAllBlackboardArtifactTags();
258  for (BlackboardArtifactTag tag : artifactTags) {
259  if (tag.getUserName().equals(userName) && tag.getArtifact().getDataSource().getId() == dsObjId) {
260  tagNameSet.add(tag.getName());
261  }
262  }
263  List<ContentTag> contentTags = caseDb.getAllContentTags();
264  for (ContentTag tag : contentTags) {
265  if (tag.getUserName().equals(userName) && tag.getContent().getDataSource().getId() == dsObjId) {
266  tagNameSet.add(tag.getName());
267  }
268  }
269  return new ArrayList<>(tagNameSet);
270  }
271 
285  public Map<String, TagName> getDisplayNamesToTagNamesMap() throws TskCoreException {
298  Map<String, TagName> tagNames = new HashMap<>();
299  Set<TagNameDefinition> customTypes = TagNameDefinition.getTagNameDefinitions();
300  for (TagNameDefinition tagType : customTypes) {
301  tagNames.put(tagType.getDisplayName(), null);
302  }
303  for (TagName tagName : caseDb.getAllTagNames()) {
304  tagNames.put(tagName.getDisplayName(), tagName);
305  }
306  return new HashMap<>(tagNames);
307  }
308 
323  public synchronized TagName addTagName(String displayName) throws TagNameAlreadyExistsException, TskCoreException {
324  return addTagName(displayName, "", TagName.HTML_COLOR.NONE, TskData.FileKnown.UNKNOWN);
325  }
326 
342  public synchronized TagName addTagName(String displayName, String description) throws TagNameAlreadyExistsException, TskCoreException {
343  return addTagName(displayName, description, TagName.HTML_COLOR.NONE, TskData.FileKnown.UNKNOWN);
344  }
345 
361  public synchronized TagName addTagName(String displayName, String description, TagName.HTML_COLOR color) throws TagNameAlreadyExistsException, TskCoreException {
362  return addTagName(displayName, description, color, TskData.FileKnown.UNKNOWN);
363  }
364 
382  public synchronized TagName addTagName(String displayName, String description, TagName.HTML_COLOR color, TskData.FileKnown knownStatus) throws TagNameAlreadyExistsException, TskCoreException {
383  try {
384  TagName tagName = caseDb.addOrUpdateTagName(displayName, description, color, knownStatus);
385  Set<TagNameDefinition> customTypes = TagNameDefinition.getTagNameDefinitions();
386  customTypes.add(new TagNameDefinition(displayName, description, color, knownStatus));
387  TagNameDefinition.setTagNameDefinitions(customTypes);
388  return tagName;
389  } catch (TskCoreException ex) {
390  List<TagName> existingTagNames = caseDb.getAllTagNames();
391  for (TagName tagName : existingTagNames) {
392  if (tagName.getDisplayName().equals(displayName)) {
393  throw new TagNameAlreadyExistsException();
394  }
395  }
396  throw ex;
397  }
398  }
399 
413  public ContentTag addContentTag(Content content, TagName tagName) throws TskCoreException {
414  return addContentTag(content, tagName, "", -1, -1);
415  }
416 
431  public ContentTag addContentTag(Content content, TagName tagName, String comment) throws TskCoreException {
432  return addContentTag(content, tagName, comment, -1, -1);
433  }
434 
451  public ContentTag addContentTag(Content content, TagName tagName, String comment, long beginByteOffset, long endByteOffset) throws TskCoreException {
452  ContentTag tag;
453  tag = caseDb.addContentTag(content, tagName, comment, beginByteOffset, endByteOffset);
454  try {
456  } catch (NoCurrentCaseException ex) {
457  throw new TskCoreException("Added a tag to a closed case", ex);
458  }
459  return tag;
460  }
461 
470  public void deleteContentTag(ContentTag tag) throws TskCoreException {
471  caseDb.deleteContentTag(tag);
472  try {
474  } catch (NoCurrentCaseException ex) {
475  throw new TskCoreException("Deleted a tag from a closed case", ex);
476  }
477  }
478 
487  public List<ContentTag> getAllContentTags() throws TskCoreException {
488  return caseDb.getAllContentTags();
489  }
490 
503  public long getContentTagsCountByTagName(TagName tagName) throws TskCoreException {
504  return caseDb.getContentTagsCountByTagName(tagName);
505  }
506 
521  public long getContentTagsCountByTagNameForUser(TagName tagName, String userName) throws TskCoreException {
522  long count = 0;
523  List<ContentTag> contentTags = getContentTagsByTagName(tagName);
524  for (ContentTag tag : contentTags) {
525  if (userName.equals(tag.getUserName())) {
526  count++;
527  }
528  }
529  return count;
530  }
531 
547  public long getContentTagsCountByTagName(TagName tagName, long dsObjId) throws TskCoreException {
548  return caseDb.getContentTagsCountByTagName(tagName, dsObjId);
549  }
550 
567  public long getContentTagsCountByTagNameForUser(TagName tagName, long dsObjId, String userName) throws TskCoreException {
568  long count = 0;
569  List<ContentTag> contentTags = getContentTagsByTagName(tagName, dsObjId);
570  for (ContentTag tag : contentTags) {
571  if (userName.equals(tag.getUserName())) {
572  count++;
573  }
574  }
575  return count;
576  }
577 
588  public ContentTag getContentTagByTagID(long tagId) throws TskCoreException {
589  return caseDb.getContentTagByID(tagId);
590  }
591 
603  public List<ContentTag> getContentTagsByTagName(TagName tagName) throws TskCoreException {
604  return caseDb.getContentTagsByTagName(tagName);
605  }
606 
620  public List<ContentTag> getContentTagsByTagName(TagName tagName, long dsObjId) throws TskCoreException {
621  return caseDb.getContentTagsByTagName(tagName, dsObjId);
622  }
623 
635  public List<ContentTag> getContentTagsByContent(Content content) throws TskCoreException {
636  return caseDb.getContentTagsByContent(content);
637  }
638 
652  public BlackboardArtifactTag addBlackboardArtifactTag(BlackboardArtifact artifact, TagName tagName) throws TskCoreException {
653  return addBlackboardArtifactTag(artifact, tagName, "");
654  }
655 
670  public BlackboardArtifactTag addBlackboardArtifactTag(BlackboardArtifact artifact, TagName tagName, String comment) throws TskCoreException {
671  BlackboardArtifactTag tag = caseDb.addBlackboardArtifactTag(artifact, tagName, comment);
672  try {
674  } catch (NoCurrentCaseException ex) {
675  throw new TskCoreException("Added a tag to a closed case", ex);
676  }
677  return tag;
678  }
679 
688  public void deleteBlackboardArtifactTag(BlackboardArtifactTag tag) throws TskCoreException {
689  caseDb.deleteBlackboardArtifactTag(tag);
690  try {
692  } catch (NoCurrentCaseException ex) {
693  throw new TskCoreException("Deleted a tag from a closed case", ex);
694  }
695  }
696 
705  public List<BlackboardArtifactTag> getAllBlackboardArtifactTags() throws TskCoreException {
706  return caseDb.getAllBlackboardArtifactTags();
707  }
708 
721  public long getBlackboardArtifactTagsCountByTagName(TagName tagName) throws TskCoreException {
722  return caseDb.getBlackboardArtifactTagsCountByTagName(tagName);
723  }
724 
739  public long getBlackboardArtifactTagsCountByTagNameForUser(TagName tagName, String userName) throws TskCoreException {
740  long count = 0;
741  List<BlackboardArtifactTag> artifactTags = getBlackboardArtifactTagsByTagName(tagName);
742  for (BlackboardArtifactTag tag : artifactTags) {
743  if (userName.equals(tag.getUserName())) {
744  count++;
745  }
746  }
747  return count;
748  }
749 
764  public long getBlackboardArtifactTagsCountByTagName(TagName tagName, long dsObjId) throws TskCoreException {
765  return caseDb.getBlackboardArtifactTagsCountByTagName(tagName, dsObjId);
766  }
767 
784  public long getBlackboardArtifactTagsCountByTagNameForUser(TagName tagName, long dsObjId, String userName) throws TskCoreException {
785  long count = 0;
786  List<BlackboardArtifactTag> artifactTags = getBlackboardArtifactTagsByTagName(tagName, dsObjId);
787  for (BlackboardArtifactTag tag : artifactTags) {
788  if (userName.equals(tag.getUserName())) {
789  count++;
790  }
791  }
792  return count;
793  }
794 
805  public BlackboardArtifactTag getBlackboardArtifactTagByTagID(long tagId) throws TskCoreException {
806  return caseDb.getBlackboardArtifactTagByID(tagId);
807  }
808 
822  public List<BlackboardArtifactTag> getBlackboardArtifactTagsByTagName(TagName tagName) throws TskCoreException {
823  return caseDb.getBlackboardArtifactTagsByTagName(tagName);
824  }
825 
840  public List<BlackboardArtifactTag> getBlackboardArtifactTagsByTagName(TagName tagName, long dsObjId) throws TskCoreException {
841  return caseDb.getBlackboardArtifactTagsByTagName(tagName, dsObjId);
842  }
843 
855  public List<BlackboardArtifactTag> getBlackboardArtifactTagsByArtifact(BlackboardArtifact artifact) throws TskCoreException {
856  return caseDb.getBlackboardArtifactTagsByArtifact(artifact);
857  }
858 
862  public static class TagNameAlreadyExistsException extends Exception {
863 
864  private static final long serialVersionUID = 1L;
865  }
866 
877  @Deprecated
878  public boolean tagNameExists(String tagDisplayName) {
879  try {
880  Map<String, TagName> tagNames = getDisplayNamesToTagNamesMap();
881  return tagNames.containsKey(tagDisplayName) && (tagNames.get(tagDisplayName) != null);
882  } catch (TskCoreException ex) {
883  LOGGER.log(Level.SEVERE, "Error querying case database for tag names", ex);
884  return false;
885  }
886  }
887 
894  @Deprecated
895  @Override
896  public void close() throws IOException {
897  }
898 
899 }
void notifyContentTagDeleted(ContentTag deletedTag)
Definition: Case.java:1563
void notifyBlackBoardArtifactTagDeleted(BlackboardArtifactTag deletedTag)
Definition: Case.java:1615
synchronized TagName addTagName(String displayName)
List< TagName > getTagNamesInUseForUser(String userName)
long getContentTagsCountByTagName(TagName tagName, long dsObjId)
List< ContentTag > getContentTagsByContent(Content content)
BlackboardArtifactTag addBlackboardArtifactTag(BlackboardArtifact artifact, TagName tagName)
List< BlackboardArtifactTag > getBlackboardArtifactTagsByTagName(TagName tagName, long dsObjId)
long getContentTagsCountByTagNameForUser(TagName tagName, long dsObjId, String userName)
synchronized TagName addTagName(String displayName, String description, TagName.HTML_COLOR color)
static boolean containsIllegalCharacters(String tagDisplayName)
List< ContentTag > getContentTagsByTagName(TagName tagName, long dsObjId)
long getBlackboardArtifactTagsCountByTagNameForUser(TagName tagName, long dsObjId, String userName)
ContentTag addContentTag(Content content, TagName tagName)
void deleteBlackboardArtifactTag(BlackboardArtifactTag tag)
BlackboardArtifactTag getBlackboardArtifactTagByTagID(long tagId)
ContentTag addContentTag(Content content, TagName tagName, String comment, long beginByteOffset, long endByteOffset)
synchronized TagName addTagName(String displayName, String description)
List< BlackboardArtifactTag > getAllBlackboardArtifactTags()
void notifyBlackBoardArtifactTagAdded(BlackboardArtifactTag newTag)
Definition: Case.java:1604
List< BlackboardArtifactTag > getBlackboardArtifactTagsByTagName(TagName tagName)
List< TagName > getTagNamesInUseForUser(long dsObjId, String userName)
long getBlackboardArtifactTagsCountByTagName(TagName tagName, long dsObjId)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static void addEventTypeSubscriber(Set< Events > eventTypes, PropertyChangeListener subscriber)
Definition: Case.java:486
void notifyContentTagAdded(ContentTag newTag)
Definition: Case.java:1552
List< ContentTag > getContentTagsByTagName(TagName tagName)
long getContentTagsCountByTagNameForUser(TagName tagName, String userName)
long getBlackboardArtifactTagsCountByTagNameForUser(TagName tagName, String userName)
synchronized TagName addTagName(String displayName, String description, TagName.HTML_COLOR color, TskData.FileKnown knownStatus)
ContentTag addContentTag(Content content, TagName tagName, String comment)
BlackboardArtifactTag addBlackboardArtifactTag(BlackboardArtifact artifact, TagName tagName, String comment)
List< BlackboardArtifactTag > getBlackboardArtifactTagsByArtifact(BlackboardArtifact artifact)

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