Autopsy  4.15.0
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-2019 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;
28 import org.openide.windows.WindowManager;
32 import org.sleuthkit.datamodel.AbstractFile;
33 import org.sleuthkit.datamodel.Content;
34 import org.sleuthkit.datamodel.TagName;
35 import org.sleuthkit.datamodel.TskCoreException;
36 
40 @NbBundle.Messages({
41  "AddContentTagAction.singularTagFile=Add File Tag",
42  "AddContentTagAction.pluralTagFile=Add File Tags",
43  "# {0} - fileName",
44  "AddContentTagAction.unableToTag.msg=Unable to tag {0}, not a regular file.",
45  "AddContentTagAction.cannotApplyTagErr=Cannot Apply Tag",
46  "# {0} - fileName",
47  "AddContentTagAction.unableToTag.msg2=Unable to tag {0}.",
48  "AddContentTagAction.taggingErr=Tagging Error",
49  "# {0} - fileName", "# {1} - tagName",
50  "AddContentTagAction.tagExists={0} has been tagged as {1}. Cannot reapply the same tag."
51 })
52 public class AddContentTagAction extends AddTagAction {
53 
54  // This class is a singleton to support multi-selection of nodes, since
55  // org.openide.nodes.NodeOp.findActions(Node[] nodes) will only pick up an Action if every
56  // node in the array returns a reference to the same action object from Node.getActions(boolean).
57  private static AddContentTagAction instance;
58 
59  public static synchronized AddContentTagAction getInstance() {
60  if (null == instance) {
61  instance = new AddContentTagAction();
62  }
63  return instance;
64  }
65 
66  private AddContentTagAction() {
67  super("");
68  }
69 
70  @Override
71  protected String getActionDisplayName() {
72  String singularTagFile = NbBundle.getMessage(this.getClass(), "AddContentTagAction.singularTagFile");
73  String pluralTagFile = NbBundle.getMessage(this.getClass(), "AddContentTagAction.pluralTagFile");
74  return Utilities.actionsGlobalContext().lookupAll(AbstractFile.class).size() > 1 ? pluralTagFile : singularTagFile;
75  }
76 
77  @Override
78  protected void addTag(TagName tagName, String comment) {
79  final Collection<AbstractFile> selectedFiles = new HashSet<>();
80  //If the contentToTag is empty look up the selected content
81  if (getContentToTag().isEmpty()) {
82  /*
83  * The documentation for Lookup.lookupAll() explicitly says that the
84  * collection it returns may contain duplicates. Within this
85  * invocation of addTag(), we don't want to tag the same
86  * AbstractFile more than once, so we dedupe the AbstractFiles by
87  * stuffing them into a HashSet.
88  *
89  * We don't want VirtualFile and DerivedFile objects to be tagged.
90  */
91  selectedFiles.addAll(Utilities.actionsGlobalContext().lookupAll(AbstractFile.class));
92  } else {
93  for (Content content : getContentToTag()) {
94  if (content instanceof AbstractFile) {
95  selectedFiles.add((AbstractFile) content);
96  }
97  }
98  }
99  new Thread(() -> {
100  for (AbstractFile file : selectedFiles) {
101  try {
102  // Handle the special cases of current (".") and parent ("..") directory entries.
103  if (file.getName().equals(".")) {
104  Content parentFile = file.getParent();
105  if (parentFile instanceof AbstractFile) {
106  file = (AbstractFile) parentFile;
107  } else {
108  SwingUtilities.invokeLater(() -> {
109  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
110  NbBundle.getMessage(this.getClass(),
111  "AddContentTagAction.unableToTag.msg",
112  parentFile.getName()),
113  NbBundle.getMessage(this.getClass(),
114  "AddContentTagAction.cannotApplyTagErr"),
115  JOptionPane.WARNING_MESSAGE);
116  });
117  continue;
118  }
119  } else if (file.getName().equals("..")) {
120  Content parentFile = file.getParent();
121  if (parentFile instanceof AbstractFile) {
122  parentFile = (AbstractFile) ((AbstractFile) parentFile).getParent();
123  if (parentFile instanceof AbstractFile) {
124  file = (AbstractFile) parentFile;
125  } else {
126  final Content parentFileCopy = parentFile;
127  SwingUtilities.invokeLater(() -> {
128  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
129  NbBundle.getMessage(this.getClass(),
130  "AddContentTagAction.unableToTag.msg",
131  parentFileCopy.getName()),
132  NbBundle.getMessage(this.getClass(),
133  "AddContentTagAction.cannotApplyTagErr"),
134  JOptionPane.WARNING_MESSAGE);
135  });
136  continue;
137  }
138  } else {
139  final Content parentFileCopy = parentFile;
140  SwingUtilities.invokeLater(() -> {
141  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
142  NbBundle.getMessage(this.getClass(),
143  "AddContentTagAction.unableToTag.msg",
144  parentFileCopy.getName()),
145  NbBundle.getMessage(this.getClass(),
146  "AddContentTagAction.cannotApplyTagErr"),
147  JOptionPane.WARNING_MESSAGE);
148  });
149  continue;
150  }
151  }
152 
153  Case.getCurrentCaseThrows().getServices().getTagsManager().addContentTag(file, tagName, comment);
154  } catch (TskCoreException | NoCurrentCaseException ex) {
155  Logger.getLogger(AddContentTagAction.class.getName()).log(Level.SEVERE, "Error tagging result", ex); //NON-NLS
156  AbstractFile fileCopy = file;
157  SwingUtilities.invokeLater(() -> {
158  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
159  NbBundle.getMessage(this.getClass(),
160  "AddContentTagAction.unableToTag.msg2",
161  fileCopy.getName()),
162  NbBundle.getMessage(this.getClass(), "AddContentTagAction.taggingErr"),
163  JOptionPane.ERROR_MESSAGE);
164  });
165  break;
166  }
167  }
168  }).start();
169  }
170 }
ContentTag addContentTag(Content content, TagName tagName)
void addTag(TagName tagName, String comment)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static synchronized AddContentTagAction getInstance()

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