Autopsy  4.6.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-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.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=Tag File",
42  "AddContentTagAction.pluralTagFile=Tag Files",
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  /*
80  * The documentation for Lookup.lookupAll() explicitly says that the
81  * collection it returns may contain duplicates. Within this invocation
82  * of addTag(), we don't want to tag the same AbstractFile more than
83  * once, so we dedupe the AbstractFiles by stuffing them into a HashSet.
84  *
85  * We don't want VirtualFile and DerivedFile objects to be tagged.
86  */
87  final Collection<AbstractFile> selectedFiles = new HashSet<>(Utilities.actionsGlobalContext().lookupAll(AbstractFile.class));
88 
89  new Thread(() -> {
90  for (AbstractFile file : selectedFiles) {
91  try {
92  // Handle the special cases of current (".") and parent ("..") directory entries.
93  if (file.getName().equals(".")) {
94  Content parentFile = file.getParent();
95  if (parentFile instanceof AbstractFile) {
96  file = (AbstractFile) parentFile;
97  } else {
98  SwingUtilities.invokeLater(() -> {
99  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
100  NbBundle.getMessage(this.getClass(),
101  "AddContentTagAction.unableToTag.msg",
102  parentFile.getName()),
103  NbBundle.getMessage(this.getClass(),
104  "AddContentTagAction.cannotApplyTagErr"),
105  JOptionPane.WARNING_MESSAGE);
106  });
107  continue;
108  }
109  } else if (file.getName().equals("..")) {
110  Content parentFile = file.getParent();
111  if (parentFile instanceof AbstractFile) {
112  parentFile = (AbstractFile) ((AbstractFile) parentFile).getParent();
113  if (parentFile instanceof AbstractFile) {
114  file = (AbstractFile) parentFile;
115  } else {
116  final Content parentFileCopy = parentFile;
117  SwingUtilities.invokeLater(() -> {
118  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
119  NbBundle.getMessage(this.getClass(),
120  "AddContentTagAction.unableToTag.msg",
121  parentFileCopy.getName()),
122  NbBundle.getMessage(this.getClass(),
123  "AddContentTagAction.cannotApplyTagErr"),
124  JOptionPane.WARNING_MESSAGE);
125  });
126  continue;
127  }
128  } else {
129  final Content parentFileCopy = parentFile;
130  SwingUtilities.invokeLater(() -> {
131  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
132  NbBundle.getMessage(this.getClass(),
133  "AddContentTagAction.unableToTag.msg",
134  parentFileCopy.getName()),
135  NbBundle.getMessage(this.getClass(),
136  "AddContentTagAction.cannotApplyTagErr"),
137  JOptionPane.WARNING_MESSAGE);
138  });
139  continue;
140  }
141  }
142 
143  Case.getOpenCase().getServices().getTagsManager().addContentTag(file, tagName, comment);
144  } catch (TskCoreException | NoCurrentCaseException ex) {
145  Logger.getLogger(AddContentTagAction.class.getName()).log(Level.SEVERE, "Error tagging result", ex); //NON-NLS
146  AbstractFile fileCopy = file;
147  SwingUtilities.invokeLater(() -> {
148  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
149  NbBundle.getMessage(this.getClass(),
150  "AddContentTagAction.unableToTag.msg2",
151  fileCopy.getName()),
152  NbBundle.getMessage(this.getClass(), "AddContentTagAction.taggingErr"),
153  JOptionPane.ERROR_MESSAGE);
154  });
155  break;
156  }
157  }
158  }).start();
159  }
160 }
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-2016 Basis Technology. Generated on: Mon May 7 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.