Autopsy  4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
AddContentTagAction.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2016 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.actions;
20 
21 import java.util.Collection;
22 import java.util.HashSet;
23 import java.util.logging.Level;
24 import javax.swing.JOptionPane;
25 import javax.swing.SwingUtilities;
26 import org.openide.util.NbBundle;
27 import org.openide.util.Utilities;
30 import org.sleuthkit.datamodel.AbstractFile;
31 import org.sleuthkit.datamodel.Content;
32 import org.sleuthkit.datamodel.FsContent;
33 import org.sleuthkit.datamodel.LayoutFile;
34 import org.sleuthkit.datamodel.LocalFile;
35 import org.sleuthkit.datamodel.TagName;
36 import org.sleuthkit.datamodel.TskCoreException;
37 
41 public class AddContentTagAction extends AddTagAction {
42 
43  // This class is a singleton to support multi-selection of nodes, since
44  // org.openide.nodes.NodeOp.findActions(Node[] nodes) will only pick up an Action if every
45  // node in the array returns a reference to the same action object from Node.getActions(boolean).
46  private static AddContentTagAction instance;
47 
48  public static synchronized AddContentTagAction getInstance() {
49  if (null == instance) {
50  instance = new AddContentTagAction();
51  }
52  return instance;
53  }
54 
55  private AddContentTagAction() {
56  super("");
57  }
58 
59  @Override
60  protected String getActionDisplayName() {
61  String singularTagFile = NbBundle.getMessage(this.getClass(), "AddContentTagAction.singularTagFile");
62  String pluralTagFile = NbBundle.getMessage(this.getClass(), "AddContentTagAction.pluralTagFile");
63  return Utilities.actionsGlobalContext().lookupAll(AbstractFile.class).size() > 1 ? pluralTagFile : singularTagFile;
64  }
65 
66  @Override
67  protected void addTag(TagName tagName, String comment) {
68  /*
69  * The documentation for Lookup.lookupAll() explicitly says that the
70  * collection it returns may contain duplicates. Within this invocation
71  * of addTag(), we don't want to tag the same AbstractFile more than
72  * once, so we dedupe the AbstractFiles by stuffing them into a HashSet.
73  *
74  * We don't want VirtualFile and DerivedFile objects to be tagged.
75  */
76  final Collection<AbstractFile> selectedFiles = new HashSet<>(Utilities.actionsGlobalContext().lookupAll(AbstractFile.class));
77 
78  new Thread(() -> {
79  for (AbstractFile file : selectedFiles) {
80  try {
81  // Handle the special cases of current (".") and parent ("..") directory entries.
82  if (file.getName().equals(".")) {
83  Content parentFile = file.getParent();
84  if (parentFile instanceof AbstractFile) {
85  file = (AbstractFile) parentFile;
86  } else {
87  SwingUtilities.invokeLater(() -> {
88  JOptionPane.showMessageDialog(null,
89  NbBundle.getMessage(this.getClass(),
90  "AddContentTagAction.unableToTag.msg",
91  parentFile.getName()),
92  NbBundle.getMessage(this.getClass(),
93  "AddContentTagAction.cannotApplyTagErr"),
94  JOptionPane.WARNING_MESSAGE);
95  });
96  continue;
97  }
98  } else if (file.getName().equals("..")) {
99  Content parentFile = file.getParent();
100  if (parentFile instanceof AbstractFile) {
101  parentFile = (AbstractFile) ((AbstractFile) parentFile).getParent();
102  if (parentFile instanceof AbstractFile) {
103  file = (AbstractFile) parentFile;
104  } else {
105  final Content parentFileCopy = parentFile;
106  SwingUtilities.invokeLater(() -> {
107  JOptionPane.showMessageDialog(null,
108  NbBundle.getMessage(this.getClass(),
109  "AddContentTagAction.unableToTag.msg",
110  parentFileCopy.getName()),
111  NbBundle.getMessage(this.getClass(),
112  "AddContentTagAction.cannotApplyTagErr"),
113  JOptionPane.WARNING_MESSAGE);
114  });
115  continue;
116  }
117  } else {
118  final Content parentFileCopy = parentFile;
119  SwingUtilities.invokeLater(() -> {
120  JOptionPane.showMessageDialog(null,
121  NbBundle.getMessage(this.getClass(),
122  "AddContentTagAction.unableToTag.msg",
123  parentFileCopy.getName()),
124  NbBundle.getMessage(this.getClass(),
125  "AddContentTagAction.cannotApplyTagErr"),
126  JOptionPane.WARNING_MESSAGE);
127  });
128  continue;
129  }
130  }
131 
132  Case.getCurrentCase().getServices().getTagsManager().addContentTag(file, tagName, comment);
133  } catch (TskCoreException ex) {
134  Logger.getLogger(AddContentTagAction.class.getName()).log(Level.SEVERE, "Error tagging result", ex); //NON-NLS
135  AbstractFile fileCopy = file;
136  SwingUtilities.invokeLater(() -> {
137  JOptionPane.showMessageDialog(null,
138  NbBundle.getMessage(this.getClass(),
139  "AddContentTagAction.unableToTag.msg2",
140  fileCopy.getName()),
141  NbBundle.getMessage(this.getClass(), "AddContentTagAction.taggingErr"),
142  JOptionPane.ERROR_MESSAGE);
143  });
144  break;
145  }
146  }
147  }).start();
148  }
149 }
ContentTag addContentTag(Content content, TagName tagName)
void addTag(TagName tagName, String comment)
synchronized static Logger getLogger(String name)
Definition: Logger.java:161
static synchronized AddContentTagAction getInstance()

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