Autopsy  3.1
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 2013 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;
28 
29 import org.openide.util.NbBundle;
38 
44 public class TagsManager implements Closeable {
45  private static final String TAGS_SETTINGS_NAME = "Tags"; //NON-NLS
46  private static final String TAG_NAMES_SETTING_KEY = "TagNames"; //NON-NLS
47  private final SleuthkitCase tskCase;
48  private final HashMap<String, TagName> uniqueTagNames = new HashMap<>();
49  private boolean tagNamesInitialized = false; // @@@ This is part of a work around to be removed when database access on the EDT is correctly synchronized.
50 
51  // Use this exception and the member hash map to manage uniqueness of hash
52  // names. This is deemed more proactive and informative than leaving this to
53  // the UNIQUE constraint on the display_name field of the tag_names table in
54  // the case database.
55  public class TagNameAlreadyExistsException extends Exception {
56  }
57 
63  TagsManager(SleuthkitCase tskCase) {
64  this.tskCase = tskCase;
65  // @@@ The removal of this call is a work around until database access on the EDT is correctly synchronized.
66  // getExistingTagNames();
67  }
68 
75  public synchronized List<TagName> getAllTagNames() throws TskCoreException {
76  // @@@ This is a work around to be removed when database access on the EDT is correctly synchronized.
77  if (!tagNamesInitialized) {
79  }
80 
81  return tskCase.getAllTagNames();
82  }
83 
90  public synchronized List<TagName> getTagNamesInUse() throws TskCoreException {
91  // @@@ This is a work around to be removed when database access on the EDT is correctly synchronized.
92  if (!tagNamesInitialized) {
94  }
95 
96  return tskCase.getTagNamesInUse();
97  }
98 
104  public synchronized boolean tagNameExists(String tagDisplayName) {
105  // @@@ This is a work around to be removed when database access on the EDT is correctly synchronized.
106  if (!tagNamesInitialized) {
108  }
109 
110  return uniqueTagNames.containsKey(tagDisplayName);
111  }
112 
120  return addTagName(displayName, "", TagName.HTML_COLOR.NONE);
121  }
122 
130  public TagName addTagName(String displayName, String description) throws TagNameAlreadyExistsException, TskCoreException {
131  return addTagName(displayName, description, TagName.HTML_COLOR.NONE);
132  }
133 
142  public synchronized TagName addTagName(String displayName, String description, TagName.HTML_COLOR color) throws TagNameAlreadyExistsException, TskCoreException {
143  // @@@ This is a work around to be removed when database access on the EDT is correctly synchronized.
144  if (!tagNamesInitialized) {
146  }
147 
148  if (uniqueTagNames.containsKey(displayName)) {
149  throw new TagNameAlreadyExistsException();
150  }
151 
152  // Add the tag name to the case.
153  TagName newTagName = tskCase.addTagName(displayName, description, color);
154 
155  // Add the tag name to the tags settings.
156  uniqueTagNames.put(newTagName.getDisplayName(), newTagName);
158 
159  return newTagName;
160  }
161 
169  public ContentTag addContentTag(Content content, TagName tagName) throws TskCoreException {
170  return addContentTag(content, tagName, "", -1, -1);
171  }
172 
181  public ContentTag addContentTag(Content content, TagName tagName, String comment) throws TskCoreException {
182  return addContentTag(content, tagName, comment, -1, -1);
183  }
184 
195  public synchronized ContentTag addContentTag(Content content, TagName tagName, String comment, long beginByteOffset, long endByteOffset) throws IllegalArgumentException, TskCoreException {
196  // @@@ This is a work around to be removed when database access on the EDT is correctly synchronized.
197  if (!tagNamesInitialized) {
199  }
200 
201  if (beginByteOffset >= 0 && endByteOffset >= 1) {
202  if (beginByteOffset > content.getSize() - 1) {
203  throw new IllegalArgumentException(NbBundle.getMessage(this.getClass(),
204  "TagsManager.addContentTag.exception.beginByteOffsetOOR.msg",
205  beginByteOffset, content.getSize() - 1));
206  }
207 
208  if (endByteOffset > content.getSize() - 1) {
209  throw new IllegalArgumentException(
210  NbBundle.getMessage(this.getClass(), "TagsManager.addContentTag.exception.endByteOffsetOOR.msg",
211  endByteOffset, content.getSize() - 1));
212  }
213 
214  if (endByteOffset < beginByteOffset) {
215  throw new IllegalArgumentException(
216  NbBundle.getMessage(this.getClass(), "TagsManager.addContentTag.exception.endLTbegin.msg"));
217  }
218  }
219 
220  return tskCase.addContentTag(content, tagName, comment, beginByteOffset, endByteOffset);
221  }
222 
228  public synchronized void deleteContentTag(ContentTag tag) throws TskCoreException {
229  // @@@ This is a work around to be removed when database access on the EDT is correctly synchronized.
230  if (!tagNamesInitialized) {
232  }
233 
234  tskCase.deleteContentTag(tag);
235  }
236 
242  public List<ContentTag> getAllContentTags() throws TskCoreException {
243  // @@@ This is a work around to be removed when database access on the EDT is correctly synchronized.
244  if (!tagNamesInitialized) {
246  }
247 
248  return tskCase.getAllContentTags();
249  }
250 
257  public synchronized long getContentTagsCountByTagName(TagName tagName) throws TskCoreException {
258  // @@@ This is a work around to be removed when database access on the EDT is correctly synchronized.
259  if (!tagNamesInitialized) {
261  }
262 
263  return tskCase.getContentTagsCountByTagName(tagName);
264  }
265 
272  public synchronized List<ContentTag> getContentTagsByTagName(TagName tagName) throws TskCoreException {
273  // @@@ This is a work around to be removed when database access on the EDT is correctly synchronized.
274  if (!tagNamesInitialized) {
276  }
277 
278  return tskCase.getContentTagsByTagName(tagName);
279  }
280 
287  public synchronized List<ContentTag> getContentTagsByContent(Content content) throws TskCoreException {
288  // @@@ This is a work around to be removed when database access on the EDT is correctly synchronized.
289  if (!tagNamesInitialized) {
291  }
292 
293  return tskCase.getContentTagsByContent(content);
294  }
295 
304  return addBlackboardArtifactTag(artifact, tagName, "");
305  }
306 
315  public synchronized BlackboardArtifactTag addBlackboardArtifactTag(BlackboardArtifact artifact, TagName tagName, String comment) throws TskCoreException {
316  // @@@ This is a work around to be removed when database access on the EDT is correctly synchronized.
317  if (!tagNamesInitialized) {
319  }
320 
321  return tskCase.addBlackboardArtifactTag(artifact, tagName, comment);
322  }
323 
330  // @@@ This is a work around to be removed when database access on the EDT is correctly synchronized.
331  if (!tagNamesInitialized) {
333  }
334 
335  tskCase.deleteBlackboardArtifactTag(tag);
336  }
337 
343  public List<BlackboardArtifactTag> getAllBlackboardArtifactTags() throws TskCoreException {
344  // @@@ This is a work around to be removed when database access on the EDT is correctly synchronized.
345  if (!tagNamesInitialized) {
347  }
348 
349  return tskCase.getAllBlackboardArtifactTags();
350  }
351 
358  public synchronized long getBlackboardArtifactTagsCountByTagName(TagName tagName) throws TskCoreException {
359  // @@@ This is a work around to be removed when database access on the EDT is correctly synchronized.
360  if (!tagNamesInitialized) {
362  }
363 
364  return tskCase.getBlackboardArtifactTagsCountByTagName(tagName);
365  }
366 
373  public synchronized List<BlackboardArtifactTag> getBlackboardArtifactTagsByTagName(TagName tagName) throws TskCoreException {
374  // @@@ This is a work around to be removed when database access on the EDT is correctly synchronized.
375  if (!tagNamesInitialized) {
377  }
378 
379  return tskCase.getBlackboardArtifactTagsByTagName(tagName);
380  }
381 
388  public synchronized List<BlackboardArtifactTag> getBlackboardArtifactTagsByArtifact(BlackboardArtifact artifact) throws TskCoreException {
389  // @@@ This is a work around to be removed when database access on the EDT is correctly synchronized.
390  if (!tagNamesInitialized) {
392  }
393 
394  return tskCase.getBlackboardArtifactTagsByArtifact(artifact);
395  }
396 
397  @Override
398  public void close() throws IOException {
400  }
401 
402  private void getExistingTagNames() {
407  tagNamesInitialized = true; // @@@ This is part of a work around to be removed when database access on the EDT is correctly synchronized.
408  }
409 
410  private void getTagNamesFromCurrentCase() {
411  try {
412  List<TagName> currentTagNames = tskCase.getAllTagNames();
413  for (TagName tagName : currentTagNames) {
414  uniqueTagNames.put(tagName.getDisplayName(), tagName);
415  }
416  }
417  catch (TskCoreException ex) {
418  Logger.getLogger(TagsManager.class.getName()).log(Level.SEVERE, "Failed to get tag types from the current case", ex); //NON-NLS
419  }
420  }
421 
423  String setting = ModuleSettings.getConfigSetting(TAGS_SETTINGS_NAME, TAG_NAMES_SETTING_KEY);
424  if (null != setting && !setting.isEmpty()) {
425  // Read the tag name setting and break it into tag name tuples.
426  List<String> tagNameTuples = Arrays.asList(setting.split(";"));
427 
428  // Parse each tuple and add the tag names to the current case, one
429  // at a time to gracefully discard any duplicates or corrupt tuples.
430  for (String tagNameTuple : tagNameTuples) {
431  String[] tagNameAttributes = tagNameTuple.split(",");
432  if (!uniqueTagNames.containsKey(tagNameAttributes[0])) {
433  try {
434  TagName tagName = tskCase.addTagName(tagNameAttributes[0], tagNameAttributes[1], TagName.HTML_COLOR.getColorByName(tagNameAttributes[2]));
435  uniqueTagNames.put(tagName.getDisplayName(), tagName);
436  }
437  catch (TskCoreException ex) {
438  Logger.getLogger(TagsManager.class.getName()).log(Level.SEVERE, "Failed to add saved tag name " + tagNameAttributes[0], ex); //NON-NLS
439  }
440  }
441  }
442  }
443  }
444 
445  private void getPredefinedTagNames() {
446  if (!uniqueTagNames.containsKey(NbBundle.getMessage(this.getClass(), "TagsManager.predefTagNames.bookmark.text"))) {
447  try {
448  TagName tagName = tskCase.addTagName(
449  NbBundle.getMessage(this.getClass(), "TagsManager.predefTagNames.bookmark.text"), "", TagName.HTML_COLOR.NONE);
450  uniqueTagNames.put(tagName.getDisplayName(), tagName);
451  }
452  catch (TskCoreException ex) {
453  Logger.getLogger(TagsManager.class.getName()).log(Level.SEVERE, "Failed to add predefined 'Bookmark' tag name", ex); //NON-NLS
454  }
455  }
456  }
457 
458  private void saveTagNamesToTagsSettings() {
459  if (!uniqueTagNames.isEmpty()) {
460  StringBuilder setting = new StringBuilder();
461  for (TagName tagName : uniqueTagNames.values()) {
462  if (setting.length() != 0) {
463  setting.append(";");
464  }
465  setting.append(tagName.getDisplayName()).append(",");
466  setting.append(tagName.getDescription()).append(",");
467  setting.append(tagName.getColor().name());
468  }
469  ModuleSettings.setConfigSetting(TAGS_SETTINGS_NAME, TAG_NAMES_SETTING_KEY, setting.toString());
470  }
471  }
472 }
long getBlackboardArtifactTagsCountByTagName(TagName tagName)
List< BlackboardArtifactTag > getBlackboardArtifactTagsByArtifact(BlackboardArtifact artifact)
synchronized BlackboardArtifactTag addBlackboardArtifactTag(BlackboardArtifact artifact, TagName tagName, String comment)
BlackboardArtifactTag addBlackboardArtifactTag(BlackboardArtifact artifact, TagName tagName)
synchronized List< ContentTag > getContentTagsByTagName(TagName tagName)
synchronized void deleteBlackboardArtifactTag(BlackboardArtifactTag tag)
TagName addTagName(String displayName, String description)
synchronized TagName addTagName(String displayName, String description, TagName.HTML_COLOR color)
synchronized List< BlackboardArtifactTag > getBlackboardArtifactTagsByArtifact(BlackboardArtifact artifact)
synchronized void deleteContentTag(ContentTag tag)
synchronized List< BlackboardArtifactTag > getBlackboardArtifactTagsByTagName(TagName tagName)
ContentTag addContentTag(Content content, TagName tagName)
synchronized boolean tagNameExists(String tagDisplayName)
BlackboardArtifactTag addBlackboardArtifactTag(BlackboardArtifact artifact, TagName tagName, String comment)
List< BlackboardArtifactTag > getAllBlackboardArtifactTags()
void deleteBlackboardArtifactTag(BlackboardArtifactTag tag)
List< ContentTag > getContentTagsByTagName(TagName tagName)
final HashMap< String, TagName > uniqueTagNames
static synchronized void setConfigSetting(String moduleName, String settingName, String settingVal)
static HTML_COLOR getColorByName(String colorName)
synchronized long getBlackboardArtifactTagsCountByTagName(TagName tagName)
List< BlackboardArtifactTag > getBlackboardArtifactTagsByTagName(TagName tagName)
List< BlackboardArtifactTag > getAllBlackboardArtifactTags()
static String getConfigSetting(String moduleName, String settingName)
long getContentTagsCountByTagName(TagName tagName)
synchronized ContentTag addContentTag(Content content, TagName tagName, String comment, long beginByteOffset, long endByteOffset)
List< ContentTag > getContentTagsByContent(Content content)
TagName addTagName(String displayName, String description, TagName.HTML_COLOR color)
ContentTag addContentTag(Content content, TagName tagName, String comment, long beginByteOffset, long endByteOffset)
synchronized long getContentTagsCountByTagName(TagName tagName)
synchronized List< ContentTag > getContentTagsByContent(Content content)
static Logger getLogger(String name)
Definition: Logger.java:131
ContentTag addContentTag(Content content, TagName tagName, String comment)

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.