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