Autopsy  4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
EmailExtracted.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2012-2014 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.datamodel;
20 
21 import java.beans.PropertyChangeEvent;
22 import java.beans.PropertyChangeListener;
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Observable;
31 import java.util.Observer;
32 import java.util.Set;
33 import java.util.logging.Level;
34 import org.openide.nodes.ChildFactory;
35 import org.openide.nodes.Children;
36 import org.openide.nodes.Node;
37 import org.openide.nodes.Sheet;
38 import org.openide.util.NbBundle;
39 import org.openide.util.lookup.Lookups;
44 import org.sleuthkit.datamodel.BlackboardArtifact;
45 import org.sleuthkit.datamodel.BlackboardAttribute;
46 import org.sleuthkit.datamodel.SleuthkitCase;
47 import org.sleuthkit.datamodel.SleuthkitCase.CaseDbQuery;
48 import org.sleuthkit.datamodel.TskCoreException;
49 import org.sleuthkit.datamodel.TskException;
50 
57 public class EmailExtracted implements AutopsyVisitableItem {
58 
59  private static final String LABEL_NAME = BlackboardArtifact.ARTIFACT_TYPE.TSK_EMAIL_MSG.getLabel();
60  private static final String DISPLAY_NAME = BlackboardArtifact.ARTIFACT_TYPE.TSK_EMAIL_MSG.getDisplayName();
61  private static final Logger logger = Logger.getLogger(EmailExtracted.class.getName());
62  private static final String MAIL_ACCOUNT = NbBundle.getMessage(EmailExtracted.class, "EmailExtracted.mailAccount.text");
63  private static final String MAIL_FOLDER = NbBundle.getMessage(EmailExtracted.class, "EmailExtracted.mailFolder.text");
64  private static final String MAIL_PATH_SEPARATOR = "/";
65  private SleuthkitCase skCase;
66  private final EmailResults emailResults;
67 
68  public EmailExtracted(SleuthkitCase skCase) {
69  this.skCase = skCase;
70  emailResults = new EmailResults();
71  }
72 
73  private final class EmailResults extends Observable {
74 
75  // NOTE: the map can be accessed by multiple worker threads and needs to be synchronized
76  private final Map<String, Map<String, List<Long>>> accounts = new LinkedHashMap<>();
77 
78  EmailResults() {
79  update();
80  }
81 
82  public Set<String> getAccounts() {
83  synchronized (accounts) {
84  return accounts.keySet();
85  }
86  }
87 
88  public Set<String> getFolders(String account) {
89  synchronized (accounts) {
90  return accounts.get(account).keySet();
91  }
92  }
93 
94  public List<Long> getArtifactIds(String account, String folder) {
95  synchronized (accounts) {
96  return accounts.get(account).get(folder);
97  }
98  }
99 
100  @SuppressWarnings("deprecation")
101  public void update() {
102  synchronized (accounts) {
103  accounts.clear();
104  }
105  if (skCase == null) {
106  return;
107  }
108 
109  int artId = BlackboardArtifact.ARTIFACT_TYPE.TSK_EMAIL_MSG.getTypeID();
110  int pathAttrId = BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PATH.getTypeID();
111  String query = "SELECT value_text,blackboard_attributes.artifact_id,attribute_type_id " //NON-NLS
112  + "FROM blackboard_attributes,blackboard_artifacts WHERE " //NON-NLS
113  + "attribute_type_id=" + pathAttrId //NON-NLS
114  + " AND blackboard_attributes.artifact_id=blackboard_artifacts.artifact_id" //NON-NLS
115  + " AND blackboard_artifacts.artifact_type_id=" + artId; //NON-NLS
116 
117  try (CaseDbQuery dbQuery = skCase.executeQuery(query)) {
118  ResultSet resultSet = dbQuery.getResultSet();
119  synchronized (accounts) {
120  while (resultSet.next()) {
121  final String path = resultSet.getString("value_text"); //NON-NLS
122  final long artifactId = resultSet.getLong("artifact_id"); //NON-NLS
123  final Map<String, String> parsedPath = parsePath(path);
124  final String account = parsedPath.get(MAIL_ACCOUNT);
125  final String folder = parsedPath.get(MAIL_FOLDER);
126 
127  Map<String, List<Long>> folders = accounts.get(account);
128  if (folders == null) {
129  folders = new LinkedHashMap<>();
130  accounts.put(account, folders);
131  }
132  List<Long> messages = folders.get(folder);
133  if (messages == null) {
134  messages = new ArrayList<>();
135  folders.put(folder, messages);
136  }
137  messages.add(artifactId);
138  }
139  }
140  } catch (TskCoreException | SQLException ex) {
141  logger.log(Level.WARNING, "Cannot initialize email extraction: ", ex); //NON-NLS
142  }
143  setChanged();
144  notifyObservers();
145  }
146 
147  private Map<String, String> parsePath(String path) {
148  Map<String, String> parsed = new HashMap<>();
149  String[] split = path.split(MAIL_PATH_SEPARATOR);
150  if (split.length < 4) {
151  parsed.put(MAIL_ACCOUNT, NbBundle.getMessage(EmailExtracted.class, "EmailExtracted.defaultAcct.text"));
152  parsed.put(MAIL_FOLDER, NbBundle.getMessage(EmailExtracted.class, "EmailExtracted.defaultFolder.text"));
153  return parsed;
154  }
155  parsed.put(MAIL_ACCOUNT, split[2]);
156  parsed.put(MAIL_FOLDER, split[3]);
157  return parsed;
158  }
159  }
160 
161  @Override
162  public <T> T accept(AutopsyItemVisitor<T> v) {
163  return v.visit(this);
164  }
165 
170  public class RootNode extends DisplayableItemNode {
171 
172  public RootNode() {
173  super(Children.create(new AccountFactory(), true), Lookups.singleton(DISPLAY_NAME));
174  super.setName(LABEL_NAME);
175  super.setDisplayName(DISPLAY_NAME);
176  this.setIconBaseWithExtension("org/sleuthkit/autopsy/images/mail-icon-16.png"); //NON-NLS
177  emailResults.update();
178  }
179 
180  @Override
181  public boolean isLeafTypeNode() {
182  return false;
183  }
184 
185  @Override
186  public <T> T accept(DisplayableItemNodeVisitor<T> v) {
187  return v.visit(this);
188  }
189 
190  @Override
191  protected Sheet createSheet() {
192  Sheet s = super.createSheet();
193  Sheet.Set ss = s.get(Sheet.PROPERTIES);
194  if (ss == null) {
195  ss = Sheet.createPropertiesSet();
196  s.put(ss);
197  }
198 
199  ss.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "EmailExtracted.createSheet.name.name"),
200  NbBundle.getMessage(this.getClass(), "EmailExtracted.createSheet.name.displayName"),
201  NbBundle.getMessage(this.getClass(), "EmailExtracted.createSheet.name.desc"),
202  getName()));
203 
204  return s;
205  }
206 
207  @Override
208  public String getItemType() {
209  return getClass().getName();
210  }
211  }
212 
216  private class AccountFactory extends ChildFactory.Detachable<String> implements Observer {
217 
218  /*
219  * The pcl is in the class because it has the easiest mechanisms to add
220  * and remove itself during its life cycles.
221  */
222  private final PropertyChangeListener pcl = new PropertyChangeListener() {
223  @Override
224  public void propertyChange(PropertyChangeEvent evt) {
225  String eventType = evt.getPropertyName();
226  if (eventType.equals(IngestManager.IngestModuleEvent.DATA_ADDED.toString())) {
233  try {
241  ModuleDataEvent eventData = (ModuleDataEvent) evt.getOldValue();
242  if (null != eventData && eventData.getBlackboardArtifactType().getTypeID() == BlackboardArtifact.ARTIFACT_TYPE.TSK_EMAIL_MSG.getTypeID()) {
243  emailResults.update();
244  }
245  } catch (IllegalStateException notUsed) {
249  }
250  } else if (eventType.equals(IngestManager.IngestJobEvent.COMPLETED.toString())
251  || eventType.equals(IngestManager.IngestJobEvent.CANCELLED.toString())) {
258  try {
260  emailResults.update();
261  } catch (IllegalStateException notUsed) {
265  }
266  } else if (eventType.equals(Case.Events.CURRENT_CASE.toString())) {
267  // case was closed. Remove listeners so that we don't get called with a stale case handle
268  if (evt.getNewValue() == null) {
269  removeNotify();
270  skCase = null;
271  }
272  }
273  }
274  };
275 
276  @Override
277  protected void addNotify() {
281  emailResults.update();
282  emailResults.addObserver(this);
283  }
284 
285  @Override
286  protected void removeNotify() {
290  emailResults.deleteObserver(this);
291  }
292 
293  @Override
294  protected boolean createKeys(List<String> list) {
295  list.addAll(emailResults.getAccounts());
296  return true;
297  }
298 
299  @Override
300  protected Node createNodeForKey(String key) {
301  return new AccountNode(key);
302  }
303 
304  @Override
305  public void update(Observable o, Object arg) {
306  refresh(true);
307  }
308  }
309 
313  public class AccountNode extends DisplayableItemNode implements Observer {
314 
315  private final String accountName;
316 
317  public AccountNode(String accountName) {
318  super(Children.create(new FolderFactory(accountName), true), Lookups.singleton(accountName));
319  super.setName(accountName);
320  this.accountName = accountName;
321  this.setIconBaseWithExtension("org/sleuthkit/autopsy/images/account-icon-16.png"); //NON-NLS
323  emailResults.addObserver(this);
324  }
325 
326  private void updateDisplayName() {
327  super.setDisplayName(accountName + " (" + emailResults.getFolders(accountName) + ")");
328  }
329 
330  @Override
331  protected Sheet createSheet() {
332  Sheet s = super.createSheet();
333  Sheet.Set ss = s.get(Sheet.PROPERTIES);
334  if (ss == null) {
335  ss = Sheet.createPropertiesSet();
336  s.put(ss);
337  }
338 
339  ss.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "EmailExtracted.createSheet.name.name"),
340  NbBundle.getMessage(this.getClass(), "EmailExtracted.createSheet.name.displayName"),
341  NbBundle.getMessage(this.getClass(), "EmailExtracted.createSheet.name.desc"),
342  getName()));
343 
344  return s;
345  }
346 
347  @Override
348  public boolean isLeafTypeNode() {
349  return false;
350  }
351 
352  @Override
353  public <T> T accept(DisplayableItemNodeVisitor<T> v) {
354  return v.visit(this);
355  }
356 
357  @Override
358  public void update(Observable o, Object arg) {
360  }
361 
362  @Override
363  public String getItemType() {
364  return getClass().getName();
365  }
366  }
367 
371  private class FolderFactory extends ChildFactory<String> implements Observer {
372 
373  private final String accountName;
374 
375  private FolderFactory(String accountName) {
376  super();
377  this.accountName = accountName;
378  emailResults.addObserver(this);
379  }
380 
381  @Override
382  protected boolean createKeys(List<String> list) {
383  list.addAll(emailResults.getFolders(accountName));
384  return true;
385  }
386 
387  @Override
388  protected Node createNodeForKey(String folderName) {
389  return new FolderNode(accountName, folderName);
390  }
391 
392  @Override
393  public void update(Observable o, Object arg) {
394  refresh(true);
395  }
396  }
397 
401  public class FolderNode extends DisplayableItemNode implements Observer {
402 
403  private final String accountName;
404  private final String folderName;
405 
406  public FolderNode(String accountName, String folderName) {
407  super(Children.create(new MessageFactory(accountName, folderName), true), Lookups.singleton(accountName));
408  super.setName(folderName);
409  this.setIconBaseWithExtension("org/sleuthkit/autopsy/images/folder-icon-16.png"); //NON-NLS
410  this.accountName = accountName;
411  this.folderName = folderName;
413  emailResults.addObserver(this);
414  }
415 
416  private void updateDisplayName() {
417  super.setDisplayName(folderName + " (" + emailResults.getArtifactIds(accountName, folderName).size() + ")");
418 
419  }
420 
421  @Override
422  public boolean isLeafTypeNode() {
423  return true;
424  }
425 
426  @Override
427  protected Sheet createSheet() {
428  Sheet s = super.createSheet();
429  Sheet.Set ss = s.get(Sheet.PROPERTIES);
430  if (ss == null) {
431  ss = Sheet.createPropertiesSet();
432  s.put(ss);
433  }
434 
435  ss.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "EmailExtracted.createSheet.name.name"),
436  NbBundle.getMessage(this.getClass(), "EmailExtracted.createSheet.name.displayName"),
437  NbBundle.getMessage(this.getClass(), "EmailExtracted.createSheet.name.desc"),
438  getName()));
439 
440  return s;
441  }
442 
443  @Override
444  public <T> T accept(DisplayableItemNodeVisitor<T> v) {
445  return v.visit(this);
446  }
447 
448  @Override
449  public void update(Observable o, Object arg) {
451  }
452 
453  @Override
454  public String getItemType() {
455  return getClass().getName();
456  }
457  }
458 
462  private class MessageFactory extends ChildFactory<Long> implements Observer {
463 
464  private final String accountName;
465  private final String folderName;
466 
467  private MessageFactory(String accountName, String folderName) {
468  super();
469  this.accountName = accountName;
470  this.folderName = folderName;
471  emailResults.addObserver(this);
472  }
473 
474  @Override
475  protected boolean createKeys(List<Long> list) {
476  list.addAll(emailResults.getArtifactIds(accountName, folderName));
477  return true;
478  }
479 
480  @Override
481  protected Node createNodeForKey(Long artifactId) {
482  if (skCase == null) {
483  return null;
484  }
485  try {
486  BlackboardArtifact artifact = skCase.getBlackboardArtifact(artifactId);
487  return new BlackboardArtifactNode(artifact);
488  } catch (TskException ex) {
489  logger.log(Level.WARNING, "Error creating mail messages nodes", ex); //NON-NLS
490  }
491  return null;
492  }
493 
494  @Override
495  public void update(Observable o, Object arg) {
496  refresh(true);
497  }
498  }
499 }
BlackboardArtifact.Type getBlackboardArtifactType()
void removeIngestModuleEventListener(final PropertyChangeListener listener)
static synchronized IngestManager getInstance()
static void removePropertyChangeListener(PropertyChangeListener listener)
Definition: Case.java:318
void removeIngestJobEventListener(final PropertyChangeListener listener)
void addIngestJobEventListener(final PropertyChangeListener listener)
List< Long > getArtifactIds(String account, String folder)
static void addPropertyChangeListener(PropertyChangeListener listener)
Definition: Case.java:306
FolderNode(String accountName, String folderName)
void addIngestModuleEventListener(final PropertyChangeListener listener)
synchronized static Logger getLogger(String name)
Definition: Logger.java:161
final Map< String, Map< String, List< Long > > > accounts

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.