Autopsy  4.10.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
DeleteFileContentTagAction.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2017-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.actions;
20 
21 import java.awt.event.ActionEvent;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.TreeMap;
28 import java.util.concurrent.ExecutionException;
29 import java.util.logging.Level;
30 import javafx.application.Platform;
31 import javafx.scene.control.Alert;
32 import javax.swing.AbstractAction;
33 import javax.swing.JMenu;
34 import javax.swing.JMenuItem;
35 import javax.swing.SwingWorker;
36 import org.openide.util.NbBundle;
37 import org.openide.util.Utilities;
38 import org.openide.util.actions.Presenter;
43 import org.sleuthkit.datamodel.AbstractFile;
44 import org.sleuthkit.datamodel.ContentTag;
45 import org.sleuthkit.datamodel.TagName;
46 import org.sleuthkit.datamodel.TskCoreException;
47 import org.sleuthkit.datamodel.TskData;
48 
52 @NbBundle.Messages({
53  "DeleteFileContentTagAction.deleteTag=Remove File Tag"
54 })
55 public class DeleteFileContentTagAction extends AbstractAction implements Presenter.Popup {
56 
57  private static final Logger logger = Logger.getLogger(DeleteFileContentTagAction.class.getName());
58 
59  private static final long serialVersionUID = 1L;
60  private static final String MENU_TEXT = NbBundle.getMessage(DeleteFileContentTagAction.class,
61  "DeleteFileContentTagAction.deleteTag");
62 
63  // This class is a singleton to support multi-selection of nodes, since
64  // org.openide.nodes.NodeOp.findActions(Node[] nodes) will only pick up an Action if every
65  // node in the array returns a reference to the same action object from Node.getActions(boolean).
67 
68  public static synchronized DeleteFileContentTagAction getInstance() {
69  if (null == instance) {
70  instance = new DeleteFileContentTagAction();
71  }
72  return instance;
73  }
74 
76  super(MENU_TEXT);
77  }
78 
79  @Override
80  public JMenuItem getPopupPresenter() {
81  return new TagMenu();
82  }
83 
84  @Override
85  public void actionPerformed(ActionEvent e) {
86  }
87 
88  protected String getActionDisplayName() {
89  return MENU_TEXT;
90  }
91 
92  @NbBundle.Messages({
93  "# {0} - fileID",
94  "DeleteFileContentTagAction.deleteTag.alert=Unable to untag file {0}."})
95  protected void deleteTag(TagName tagName, ContentTag contentTag, long fileId) {
96  new SwingWorker<Void, Void>() {
97 
98  @Override
99  protected Void doInBackground() throws Exception {
100  TagsManager tagsManager;
101  try {
103  } catch (NoCurrentCaseException ex) {
104  logger.log(Level.SEVERE, "Error untagging file. No open case found.", ex); //NON-NLS
105  Platform.runLater(() ->
106  new Alert(Alert.AlertType.ERROR, Bundle.DeleteFileContentTagAction_deleteTag_alert(fileId)).show()
107  );
108  return null;
109  }
110 
111  try {
112  logger.log(Level.INFO, "Removing tag {0} from {1}", new Object[]{tagName.getDisplayName(), contentTag.getContent().getName()}); //NON-NLS
113  tagsManager.deleteContentTag(contentTag);
114  } catch (TskCoreException tskCoreException) {
115  logger.log(Level.SEVERE, "Error untagging file", tskCoreException); //NON-NLS
116  Platform.runLater(() ->
117  new Alert(Alert.AlertType.ERROR, Bundle.DeleteFileContentTagAction_deleteTag_alert(fileId)).show()
118  );
119  }
120  return null;
121  }
122 
123  @Override
124  protected void done() {
125  super.done();
126  try {
127  get();
128  } catch (InterruptedException | ExecutionException ex) {
129  logger.log(Level.SEVERE, "Unexpected exception while untagging file", ex); //NON-NLS
130  }
131  }
132  }.execute();
133  }
134 
140  private final class TagMenu extends JMenu {
141 
142  private static final long serialVersionUID = 1L;
143 
144  TagMenu() {
145  super(getActionDisplayName());
146 
147  final Collection<AbstractFile> selectedAbstractFilesList =
148  new HashSet<>(Utilities.actionsGlobalContext().lookupAll(AbstractFile.class));
149 
150  if(!selectedAbstractFilesList.isEmpty()) {
151  AbstractFile file = selectedAbstractFilesList.iterator().next();
152 
153  Map<String, TagName> tagNamesMap = null;
154  List<String> standardTagNames = TagsManager.getStandardTagNames();
155  List<JMenuItem> standardTagMenuitems = new ArrayList<>();
156  try {
157  // Get the current set of tag names.
159 
160  tagNamesMap = new TreeMap<>(tagsManager.getDisplayNamesToTagNamesMap());
161  } catch (TskCoreException | NoCurrentCaseException ex) {
162  Logger.getLogger(TagsManager.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
163  }
164 
165  // Each tag name in the current set of tags gets its own menu item in
166  // the "Quick Tags" sub-menu. Selecting one of these menu items adds
167  // a tag with the associated tag name.
168  if (null != tagNamesMap && !tagNamesMap.isEmpty()) {
169  try {
170  List<ContentTag> existingTagsList =
173 
174  for (Map.Entry<String, TagName> entry : tagNamesMap.entrySet()) {
175  String tagDisplayName = entry.getKey();
176 
177  TagName tagName = entry.getValue();
178  for(ContentTag contentTag : existingTagsList) {
179  if(tagDisplayName.equals(contentTag.getName().getDisplayName())) {
180  String notableString = tagName.getKnownStatus() == TskData.FileKnown.BAD ? TagsManager.getNotableTagLabel() : "";
181  JMenuItem tagNameItem = new JMenuItem(tagDisplayName + notableString);
182  tagNameItem.addActionListener((ActionEvent e) -> {
183  deleteTag(tagName, contentTag, file.getId());
184  });
185 
186  // Show custom tags before predefined tags in the menu
187  if (standardTagNames.contains(tagDisplayName)) {
188  standardTagMenuitems.add(tagNameItem);
189  } else {
190  add(tagNameItem);
191  }
192  }
193  }
194  }
195  } catch (TskCoreException | NoCurrentCaseException ex) {
196  Logger.getLogger(TagMenu.class.getName())
197  .log(Level.SEVERE, "Error retrieving tags for TagMenu", ex); //NON-NLS
198  }
199  }
200 
201  if ((getItemCount() > 0) && !standardTagMenuitems.isEmpty() ){
202  addSeparator();
203  }
204  standardTagMenuitems.forEach((menuItem) -> {
205  add(menuItem);
206  });
207 
208  if(getItemCount() == 0) {
209  setEnabled(false);
210  }
211  }
212  }
213  }
214 
215 }
List< ContentTag > getContentTagsByContent(Content content)
static synchronized DeleteFileContentTagAction getInstance()
void deleteTag(TagName tagName, ContentTag contentTag, long fileId)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2018 Basis Technology. Generated on: Fri Mar 22 2019
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.