Autopsy  4.13.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
DataContentViewerArtifact.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.corecomponents;
20 
21 import java.awt.Component;
22 import java.awt.Cursor;
23 import java.awt.Toolkit;
24 import java.awt.event.ActionEvent;
25 import java.awt.event.ActionListener;
26 import java.awt.datatransfer.StringSelection;
27 import java.text.SimpleDateFormat;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.Enumeration;
31 import java.util.List;
32 import java.util.concurrent.ExecutionException;
33 import java.util.logging.Level;
34 import javax.swing.JMenuItem;
35 import javax.swing.JTextArea;
36 import javax.swing.SwingWorker;
37 import javax.swing.event.ChangeEvent;
38 import javax.swing.event.ListSelectionEvent;
39 import javax.swing.event.TableColumnModelEvent;
40 import javax.swing.table.DefaultTableModel;
41 import javax.swing.table.TableColumn;
42 import javax.swing.event.TableColumnModelListener;
43 import javax.swing.text.View;
44 import org.apache.commons.lang.StringUtils;
45 import org.openide.nodes.Node;
46 import org.openide.util.Lookup;
47 import org.openide.util.NbBundle;
48 import org.openide.util.lookup.ServiceProvider;
52 import org.sleuthkit.datamodel.BlackboardArtifact;
53 import org.sleuthkit.datamodel.BlackboardArtifact.ARTIFACT_TYPE;
54 import org.sleuthkit.datamodel.BlackboardAttribute;
55 import org.sleuthkit.datamodel.Content;
56 import org.sleuthkit.datamodel.TskCoreException;
57 import org.sleuthkit.datamodel.TskException;
58 import org.netbeans.swing.etable.ETable;
59 import com.google.gson.JsonElement;
60 import com.google.gson.JsonObject;
61 import com.google.gson.JsonParser;
62 import com.google.gson.JsonArray;
63 import java.util.Map;
64 
70 @ServiceProvider(service = DataContentViewer.class, position = 7)
71 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
72 public class DataContentViewerArtifact extends javax.swing.JPanel implements DataContentViewer {
73 
74  @NbBundle.Messages({
75  "DataContentViewerArtifact.attrsTableHeader.type=Type",
76  "DataContentViewerArtifact.attrsTableHeader.value=Value",
77  "DataContentViewerArtifact.attrsTableHeader.sources=Source(s)",
78  "DataContentViewerArtifact.failedToGetSourcePath.message=Failed to get source file path from case database",
79  "DataContentViewerArtifact.failedToGetAttributes.message=Failed to get some or all attributes from case database"
80  })
81  private final static Logger logger = Logger.getLogger(DataContentViewerArtifact.class.getName());
82  private final static String WAIT_TEXT = NbBundle.getMessage(DataContentViewerArtifact.class, "DataContentViewerArtifact.waitText");
83  private final static String ERROR_TEXT = NbBundle.getMessage(DataContentViewerArtifact.class, "DataContentViewerArtifact.errorText");
84  private Node currentNode; // @@@ Remove this when the redundant setNode() calls problem is fixed.
85  private int currentPage = 1;
86  private final Object lock = new Object();
87  private List<ResultsTableArtifact> artifactTableContents; // Accessed by multiple threads, use getArtifactContents() and setArtifactContents()
88  SwingWorker<ViewUpdate, Void> currentTask; // Accessed by multiple threads, use startNewTask()
89  private static final String[] COLUMN_HEADERS = {
90  Bundle.DataContentViewerArtifact_attrsTableHeader_type(),
91  Bundle.DataContentViewerArtifact_attrsTableHeader_value(),
92  Bundle.DataContentViewerArtifact_attrsTableHeader_sources()};
93  private static final int[] COLUMN_WIDTHS = {100, 800, 100};
94  private static final int CELL_BOTTOM_MARGIN = 5;
95  private static final int CELL_RIGHT_MARGIN = 1;
96 
98  initResultsTable();
99  initComponents();
100  resultsTableScrollPane.setViewportView(resultsTable);
101  customizeComponents();
102  resetComponents();
103  resultsTable.setDefaultRenderer(Object.class, new MultiLineTableCellRenderer());
104  }
105 
106  private void initResultsTable() {
107  resultsTable = new ETable();
108  resultsTable.setModel(new javax.swing.table.DefaultTableModel() {
109  private static final long serialVersionUID = 1L;
110 
111  public boolean isCellEditable(int rowIndex, int columnIndex) {
112  return false;
113  }
114  });
115  resultsTable.setCellSelectionEnabled(true);
116  resultsTable.getTableHeader().setReorderingAllowed(false);
117  resultsTable.setColumnHidingAllowed(false);
118  resultsTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
119  resultsTable.getColumnModel().addColumnModelListener(new TableColumnModelListener() {
120 
121  @Override
122  public void columnAdded(TableColumnModelEvent e) {
123  }
124 
125  @Override
126  public void columnRemoved(TableColumnModelEvent e) {
127  }
128 
129  @Override
130  public void columnMoved(TableColumnModelEvent e) {
131 
132  }
133 
134  @Override
135  public void columnMarginChanged(ChangeEvent e) {
136  updateRowHeights(); //When the user changes column width we may need to resize row height
137  }
138 
139  @Override
140  public void columnSelectionChanged(ListSelectionEvent e) {
141  }
142  });
143  resultsTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_NEXT_COLUMN);
144 
145  }
146 
150  private void updateRowHeights() {
151  int valueColIndex = -1;
152  for (int col = 0; col < resultsTable.getColumnCount(); col++) {
153  if (resultsTable.getColumnName(col).equals(COLUMN_HEADERS[1])) {
154  valueColIndex = col;
155  }
156  }
157  if (valueColIndex != -1) {
158  for (int row = 0; row < resultsTable.getRowCount(); row++) {
159  Component comp = resultsTable.prepareRenderer(
160  resultsTable.getCellRenderer(row, valueColIndex), row, valueColIndex);
161  final int rowHeight;
162  if (comp instanceof JTextArea) {
163  final JTextArea tc = (JTextArea) comp;
164  final View rootView = tc.getUI().getRootView(tc);
165  java.awt.Insets i = tc.getInsets();
166  rootView.setSize(resultsTable.getColumnModel().getColumn(valueColIndex)
167  .getWidth() - (i.left + i.right +CELL_RIGHT_MARGIN), //current width minus borders
168  Integer.MAX_VALUE);
169  rowHeight = (int) rootView.getPreferredSpan(View.Y_AXIS);
170  } else {
171  rowHeight = comp.getPreferredSize().height;
172  }
173  if (rowHeight > 0) {
174  resultsTable.setRowHeight(row, rowHeight + CELL_BOTTOM_MARGIN);
175  }
176  }
177  }
178  }
179 
183  private void updateColumnSizes() {
184  Enumeration<TableColumn> columns = resultsTable.getColumnModel().getColumns();
185  while (columns.hasMoreElements()) {
186  TableColumn col = columns.nextElement();
187  if (col.getHeaderValue().equals(COLUMN_HEADERS[0])) {
188  col.setPreferredWidth(COLUMN_WIDTHS[0]);
189  } else if (col.getHeaderValue().equals(COLUMN_HEADERS[1])) {
190  col.setPreferredWidth(COLUMN_WIDTHS[1]);
191  } else if (col.getHeaderValue().equals(COLUMN_HEADERS[2])) {
192  col.setPreferredWidth(COLUMN_WIDTHS[2]);
193  }
194  }
195  }
196 
202  @SuppressWarnings("unchecked")
203  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
204  private void initComponents() {
205 
206  rightClickMenu = new javax.swing.JPopupMenu();
207  copyMenuItem = new javax.swing.JMenuItem();
208  selectAllMenuItem = new javax.swing.JMenuItem();
209  jScrollPane1 = new javax.swing.JScrollPane();
210  jPanel1 = new javax.swing.JPanel();
211  totalPageLabel = new javax.swing.JLabel();
212  ofLabel = new javax.swing.JLabel();
213  currentPageLabel = new javax.swing.JLabel();
214  pageLabel = new javax.swing.JLabel();
215  nextPageButton = new javax.swing.JButton();
216  pageLabel2 = new javax.swing.JLabel();
217  prevPageButton = new javax.swing.JButton();
218  artifactLabel = new javax.swing.JLabel();
219  resultsTableScrollPane = new javax.swing.JScrollPane();
220 
221  copyMenuItem.setText(org.openide.util.NbBundle.getMessage(DataContentViewerArtifact.class, "DataContentViewerArtifact.copyMenuItem.text")); // NOI18N
222  rightClickMenu.add(copyMenuItem);
223 
224  selectAllMenuItem.setText(org.openide.util.NbBundle.getMessage(DataContentViewerArtifact.class, "DataContentViewerArtifact.selectAllMenuItem.text")); // NOI18N
225  rightClickMenu.add(selectAllMenuItem);
226 
227  setPreferredSize(new java.awt.Dimension(100, 58));
228 
229  jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
230  jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
231 
232  jPanel1.setPreferredSize(new java.awt.Dimension(620, 58));
233 
234  totalPageLabel.setText(org.openide.util.NbBundle.getMessage(DataContentViewerArtifact.class, "DataContentViewerArtifact.totalPageLabel.text")); // NOI18N
235 
236  ofLabel.setText(org.openide.util.NbBundle.getMessage(DataContentViewerArtifact.class, "DataContentViewerArtifact.ofLabel.text")); // NOI18N
237 
238  currentPageLabel.setText(org.openide.util.NbBundle.getMessage(DataContentViewerArtifact.class, "DataContentViewerArtifact.currentPageLabel.text")); // NOI18N
239  currentPageLabel.setMaximumSize(new java.awt.Dimension(18, 14));
240  currentPageLabel.setMinimumSize(new java.awt.Dimension(18, 14));
241  currentPageLabel.setPreferredSize(new java.awt.Dimension(18, 14));
242 
243  pageLabel.setText(org.openide.util.NbBundle.getMessage(DataContentViewerArtifact.class, "DataContentViewerArtifact.pageLabel.text")); // NOI18N
244 
245  nextPageButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/corecomponents/btn_step_forward.png"))); // NOI18N
246  nextPageButton.setText(org.openide.util.NbBundle.getMessage(DataContentViewerArtifact.class, "DataContentViewerArtifact.nextPageButton.text")); // NOI18N
247  nextPageButton.setBorderPainted(false);
248  nextPageButton.setContentAreaFilled(false);
249  nextPageButton.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/corecomponents/btn_step_forward_disabled.png"))); // NOI18N
250  nextPageButton.setMargin(new java.awt.Insets(2, 0, 2, 0));
251  nextPageButton.setPreferredSize(new java.awt.Dimension(23, 23));
252  nextPageButton.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/corecomponents/btn_step_forward_hover.png"))); // NOI18N
253  nextPageButton.addActionListener(new java.awt.event.ActionListener() {
254  public void actionPerformed(java.awt.event.ActionEvent evt) {
255  nextPageButtonActionPerformed(evt);
256  }
257  });
258 
259  pageLabel2.setText(org.openide.util.NbBundle.getMessage(DataContentViewerArtifact.class, "DataContentViewerArtifact.pageLabel2.text")); // NOI18N
260  pageLabel2.setMaximumSize(new java.awt.Dimension(29, 14));
261  pageLabel2.setMinimumSize(new java.awt.Dimension(29, 14));
262 
263  prevPageButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/corecomponents/btn_step_back.png"))); // NOI18N
264  prevPageButton.setText(org.openide.util.NbBundle.getMessage(DataContentViewerArtifact.class, "DataContentViewerArtifact.prevPageButton.text")); // NOI18N
265  prevPageButton.setBorderPainted(false);
266  prevPageButton.setContentAreaFilled(false);
267  prevPageButton.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/corecomponents/btn_step_back_disabled.png"))); // NOI18N
268  prevPageButton.setMargin(new java.awt.Insets(2, 0, 2, 0));
269  prevPageButton.setPreferredSize(new java.awt.Dimension(23, 23));
270  prevPageButton.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/corecomponents/btn_step_back_hover.png"))); // NOI18N
271  prevPageButton.addActionListener(new java.awt.event.ActionListener() {
272  public void actionPerformed(java.awt.event.ActionEvent evt) {
273  prevPageButtonActionPerformed(evt);
274  }
275  });
276 
277  javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
278  jPanel1.setLayout(jPanel1Layout);
279  jPanel1Layout.setHorizontalGroup(
280  jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
281  .addGroup(jPanel1Layout.createSequentialGroup()
282  .addContainerGap()
283  .addComponent(pageLabel)
284  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
285  .addComponent(currentPageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
286  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
287  .addComponent(ofLabel)
288  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
289  .addComponent(totalPageLabel)
290  .addGap(41, 41, 41)
291  .addComponent(pageLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
292  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
293  .addComponent(prevPageButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
294  .addGap(0, 0, 0)
295  .addComponent(nextPageButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
296  .addContainerGap(383, Short.MAX_VALUE))
297  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
298  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
299  .addContainerGap(280, Short.MAX_VALUE)
300  .addComponent(artifactLabel)
301  .addContainerGap(84, Short.MAX_VALUE)))
302  );
303  jPanel1Layout.setVerticalGroup(
304  jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
305  .addGroup(jPanel1Layout.createSequentialGroup()
306  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
307  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
308  .addComponent(pageLabel)
309  .addComponent(currentPageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
310  .addComponent(ofLabel)
311  .addComponent(totalPageLabel))
312  .addComponent(nextPageButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
313  .addComponent(prevPageButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
314  .addComponent(pageLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
315  .addContainerGap(35, Short.MAX_VALUE))
316  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
317  .addGroup(jPanel1Layout.createSequentialGroup()
318  .addComponent(artifactLabel)
319  .addGap(0, 58, Short.MAX_VALUE)))
320  );
321 
322  jScrollPane1.setViewportView(jPanel1);
323 
324  resultsTableScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
325  resultsTableScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
326  resultsTableScrollPane.setPreferredSize(new java.awt.Dimension(620, 34));
327 
328  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
329  this.setLayout(layout);
330  layout.setHorizontalGroup(
331  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
332  .addComponent(jScrollPane1)
333  .addComponent(resultsTableScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
334  );
335  layout.setVerticalGroup(
336  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
337  .addGroup(layout.createSequentialGroup()
338  .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
339  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
340  .addComponent(resultsTableScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
341  );
342  }// </editor-fold>//GEN-END:initComponents
343 
344  private void nextPageButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextPageButtonActionPerformed
345  currentPage = currentPage + 1;
346  currentPageLabel.setText(Integer.toString(currentPage));
347  artifactLabel.setText(artifactTableContents.get(currentPage - 1).getArtifactDisplayName());
348  startNewTask(new SelectedArtifactChangedTask(currentPage));
349  }//GEN-LAST:event_nextPageButtonActionPerformed
350 
351  private void prevPageButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_prevPageButtonActionPerformed
352  currentPage = currentPage - 1;
353  currentPageLabel.setText(Integer.toString(currentPage));
354  artifactLabel.setText(artifactTableContents.get(currentPage - 1).getArtifactDisplayName());
355  startNewTask(new SelectedArtifactChangedTask(currentPage));
356  }//GEN-LAST:event_prevPageButtonActionPerformed
357 
358  // Variables declaration - do not modify//GEN-BEGIN:variables
359  private javax.swing.JLabel artifactLabel;
360  private javax.swing.JMenuItem copyMenuItem;
361  private javax.swing.JLabel currentPageLabel;
362  private javax.swing.JPanel jPanel1;
363  private javax.swing.JScrollPane jScrollPane1;
364  private javax.swing.JButton nextPageButton;
365  private javax.swing.JLabel ofLabel;
366  private javax.swing.JLabel pageLabel;
367  private javax.swing.JLabel pageLabel2;
368  private javax.swing.JButton prevPageButton;
369  private javax.swing.JScrollPane resultsTableScrollPane;
370  private javax.swing.JPopupMenu rightClickMenu;
371  private javax.swing.JMenuItem selectAllMenuItem;
372  private javax.swing.JLabel totalPageLabel;
373  // End of variables declaration//GEN-END:variables
374  private ETable resultsTable;
375 
376  private void customizeComponents() {
377  resultsTable.setComponentPopupMenu(rightClickMenu);
378  ActionListener actList = new ActionListener() {
379  @Override
380  public void actionPerformed(ActionEvent e) {
381  JMenuItem jmi = (JMenuItem) e.getSource();
382  if (jmi.equals(copyMenuItem)) {
383  StringBuilder selectedText = new StringBuilder(512);
384  for (int row : resultsTable.getSelectedRows()) {
385  for (int col : resultsTable.getSelectedColumns()) {
386  selectedText.append((String) resultsTable.getValueAt(row, col));
387  selectedText.append("\t");
388  }
389  //if its the last row selected don't add a new line
390  if (row != resultsTable.getSelectedRows()[resultsTable.getSelectedRows().length - 1]) {
391  selectedText.append(System.lineSeparator());
392  }
393  }
394  Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(selectedText.toString()), null);
395  } else if (jmi.equals(selectAllMenuItem)) {
396  resultsTable.selectAll();
397  }
398  }
399  };
400  copyMenuItem.addActionListener(actList);
401 
402  selectAllMenuItem.addActionListener(actList);
403  }
404 
408  private void resetComponents() {
409  currentPage = 1;
410  currentPageLabel.setText("");
411  artifactLabel.setText("");
412  totalPageLabel.setText("");
413  ((DefaultTableModel) resultsTable.getModel()).setRowCount(0);
414  prevPageButton.setEnabled(false);
415  nextPageButton.setEnabled(false);
416  currentNode = null;
417  }
418 
419  @Override
420  public void setNode(Node selectedNode) {
421  if (currentNode == selectedNode) {
422  return;
423  }
424  currentNode = selectedNode;
425 
426  // Make sure there is a node. Null might be passed to reset the viewer.
427  if (selectedNode == null) {
428  return;
429  }
430 
431  // Make sure the node is of the correct type.
432  Lookup lookup = selectedNode.getLookup();
433  Content content = lookup.lookup(Content.class);
434  if (content == null) {
435  return;
436  }
437 
438  startNewTask(new SelectedNodeChangedTask(selectedNode));
439  }
440 
441  @Override
442  public String getTitle() {
443  return NbBundle.getMessage(this.getClass(), "DataContentViewerArtifact.title");
444  }
445 
446  @Override
447  public String getToolTip() {
448  return NbBundle.getMessage(this.getClass(), "DataContentViewerArtifact.toolTip");
449  }
450 
451  @Override
452  public DataContentViewer createInstance() {
453  return new DataContentViewerArtifact();
454  }
455 
456  @Override
457  public Component getComponent() {
458  return this;
459  }
460 
461  @Override
462  public void resetComponent() {
463  resetComponents();
464  }
465 
466  @Override
467  public boolean isSupported(Node node) {
468  if (node == null) {
469  return false;
470  }
471 
472  for (Content content : node.getLookup().lookupAll(Content.class)) {
473  if ( (content != null) && (!(content instanceof BlackboardArtifact)) ){
474  try {
475  return content.getAllArtifactsCount() > 0;
476  } catch (TskException ex) {
477  logger.log(Level.SEVERE, "Couldn't get count of BlackboardArtifacts for content", ex); //NON-NLS
478  }
479  }
480  }
481  return false;
482  }
483 
484  @Override
485  public int isPreferred(Node node) {
486  BlackboardArtifact artifact = node.getLookup().lookup(BlackboardArtifact.class);
487  // low priority if node doesn't have an artifact (meaning it was found from normal directory
488  // browsing, or if the artifact is something that means the user really wants to see the original
489  // file and not more details about the artifact
490  if ((artifact == null)
491  || (artifact.getArtifactTypeID() == ARTIFACT_TYPE.TSK_HASHSET_HIT.getTypeID())
492  || (artifact.getArtifactTypeID() == ARTIFACT_TYPE.TSK_KEYWORD_HIT.getTypeID())
493  || (artifact.getArtifactTypeID() == ARTIFACT_TYPE.TSK_INTERESTING_FILE_HIT.getTypeID())
494  || (artifact.getArtifactTypeID() == ARTIFACT_TYPE.TSK_OBJECT_DETECTED.getTypeID())
495  || (artifact.getArtifactTypeID() == ARTIFACT_TYPE.TSK_METADATA_EXIF.getTypeID())
496  || (artifact.getArtifactTypeID() == ARTIFACT_TYPE.TSK_EXT_MISMATCH_DETECTED.getTypeID())) {
497  return 3;
498  } else {
499  return 6;
500  }
501  }
502 
507  private class ResultsTableArtifact {
508 
509  private final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
510  private String[][] rowData = null;
511  private final String artifactDisplayName;
512  private final Content content;
513 
514  ResultsTableArtifact(BlackboardArtifact artifact, Content content) {
515  artifactDisplayName = artifact.getDisplayName();
516  this.content = content;
517  addRows(artifact);
518  }
519 
520  ResultsTableArtifact(String errorMsg) {
521  artifactDisplayName = errorMsg;
522  rowData = new String[1][3];
523  rowData[0] = new String[]{"", errorMsg, ""};
524  content = null;
525  }
526 
527  private String[][] getRows() {
528  return rowData;
529  }
530 
531  private void addRows(BlackboardArtifact artifact) {
532  List<String[]> rowsToAdd = new ArrayList<>();
533  try {
534  /*
535  * Add rows for each attribute.
536  */
537  for (BlackboardAttribute attr : artifact.getAttributes()) {
538  /*
539  * Attribute value column.
540  */
541  String value = "";
542  switch (attr.getAttributeType().getValueType()) {
543  case STRING:
544  case INTEGER:
545  case LONG:
546  case DOUBLE:
547  case BYTE:
548  default:
549  value = attr.getDisplayString();
550  break;
551  // Use Autopsy date formatting settings, not TSK defaults
552  case DATETIME:
553  long epoch = attr.getValueLong();
554  value = "0000-00-00 00:00:00";
555  if (null != content && 0 != epoch) {
556  dateFormatter.setTimeZone(ContentUtils.getTimeZone(content));
557  value = dateFormatter.format(new java.util.Date(epoch * 1000));
558  }
559  break;
560  case JSON:
561  // Get the attribute's JSON value and convert to indented multiline display string
562  String jsonVal = attr.getValueString();
563  JsonParser parser = new JsonParser();
564  JsonObject json = parser.parse(jsonVal).getAsJsonObject();
565 
566  value = toJsonDisplayString(json, "");
567  break;
568  }
569  /*
570  * Attribute sources column.
571  */
572  String sources = StringUtils.join(attr.getSources(), ", ");
573  rowsToAdd.add(new String[]{attr.getAttributeType().getDisplayName(), value, sources});
574  }
575  /*
576  * Add a row for the source content path.
577  */
578  String path = "";
579  try {
580  if (null != content) {
581  path = content.getUniquePath();
582  }
583  } catch (TskCoreException ex) {
584  logger.log(Level.SEVERE, String.format("Error getting source content path for artifact (artifact_id=%d, obj_id=%d)", artifact.getArtifactID(), artifact.getObjectID()), ex);
585  path = Bundle.DataContentViewerArtifact_failedToGetSourcePath_message();
586  }
587  rowsToAdd.add(new String[]{"Source File Path", path, ""});
588  /*
589  * Add a row for the artifact id.
590  */
591  rowsToAdd.add(new String[]{"Artifact ID", Long.toString(artifact.getArtifactID()), ""});
592  } catch (TskCoreException ex) {
593  rowsToAdd.add(new String[]{"", Bundle.DataContentViewerArtifact_failedToGetAttributes_message(), ""});
594  }
595  rowData = rowsToAdd.toArray(new String[0][0]);
596  }
597 
601  String getArtifactDisplayName() {
602  return artifactDisplayName;
603  }
604 
605  private static final String INDENT_RIGHT = " ";
606  private static final String NEW_LINE = "\n";
607 
617  private String toJsonDisplayString(JsonElement element, String startIndent) {
618 
619  StringBuilder sb = new StringBuilder("");
620  JsonObject obj = element.getAsJsonObject();
621 
622  for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
623  appendJsonElementToString(entry.getKey(), entry.getValue(), startIndent, sb );
624  }
625 
626  String returnString = sb.toString();
627  if (startIndent.length() == 0 && returnString.startsWith(NEW_LINE)) {
628  returnString = returnString.substring(NEW_LINE.length());
629  }
630  return returnString;
631  }
632 
633 
642  private void appendJsonElementToString(String jsonKey, JsonElement jsonElement, String startIndent, StringBuilder sb) {
643  if (jsonElement.isJsonArray()) {
644  JsonArray jsonArray = jsonElement.getAsJsonArray();
645  if (jsonArray.size() > 0) {
646  int count = 1;
647  sb.append(NEW_LINE).append(String.format("%s%s", startIndent, jsonKey));
648  for (JsonElement arrayMember : jsonArray) {
649  sb.append(NEW_LINE).append(String.format("%s%d", startIndent.concat(INDENT_RIGHT), count));
650  sb.append(toJsonDisplayString(arrayMember, startIndent.concat(INDENT_RIGHT).concat(INDENT_RIGHT)));
651  count++;
652  }
653  }
654  } else if (jsonElement.isJsonObject()) {
655  sb.append(NEW_LINE).append(String.format("%s%s %s", startIndent, jsonKey, toJsonDisplayString(jsonElement.getAsJsonObject(), startIndent + INDENT_RIGHT)));
656  } else if (jsonElement.isJsonPrimitive()) {
657  sb.append(NEW_LINE).append(String.format("%s%s = %s", startIndent, jsonKey, jsonElement.getAsString()));
658  } else if (jsonElement.isJsonNull()) {
659  sb.append(NEW_LINE).append(String.format("%s%s = null", startIndent, jsonKey));
660  }
661  }
662  }
663 
668  private class ViewUpdate {
669 
670  int numberOfPages;
671  int currentPage;
672  ResultsTableArtifact tableContents;
673 
674  ViewUpdate(int numberOfPages, int currentPage, ResultsTableArtifact contents) {
675  this.currentPage = currentPage;
676  this.numberOfPages = numberOfPages;
677  this.tableContents = contents;
678  }
679 
680  ViewUpdate(int numberOfPages, int currentPage, String errorMsg) {
681  this.currentPage = currentPage;
682  this.numberOfPages = numberOfPages;
683  this.tableContents = new ResultsTableArtifact(errorMsg);
684  }
685  }
686 
694  private void updateView(ViewUpdate viewUpdate) {
695  this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
696 
697  nextPageButton.setEnabled(viewUpdate.currentPage < viewUpdate.numberOfPages);
698  prevPageButton.setEnabled(viewUpdate.currentPage > 1);
699  currentPage = viewUpdate.currentPage;
700  totalPageLabel.setText(Integer.toString(viewUpdate.numberOfPages));
701  currentPageLabel.setText(Integer.toString(currentPage));
702  artifactLabel.setText(viewUpdate.tableContents.getArtifactDisplayName());
703  DefaultTableModel tModel = ((DefaultTableModel) resultsTable.getModel());
704  tModel.setDataVector(viewUpdate.tableContents.getRows(), COLUMN_HEADERS);
705  updateColumnSizes();
706  updateRowHeights();
707  resultsTable.clearSelection();
708 
709  this.setCursor(null);
710  }
711 
718  private synchronized void startNewTask(SwingWorker<ViewUpdate, Void> task) {
719  String[][] waitRow = new String[1][3];
720  waitRow[0] = new String[]{"", WAIT_TEXT, ""};
721  DefaultTableModel tModel = ((DefaultTableModel) resultsTable.getModel());
722  tModel.setDataVector(waitRow, COLUMN_HEADERS);
723  updateColumnSizes();
724  updateRowHeights();
725  resultsTable.clearSelection();
726  // The output of the previous task is no longer relevant.
727  if (currentTask != null) {
728  // This call sets a cancellation flag. It does not terminate the background thread running the task.
729  // The task must check the cancellation flag and react appropriately.
730  currentTask.cancel(false);
731  }
732 
733  // Start the new task.
734  currentTask = task;
735  currentTask.execute();
736  }
737 
744  private void setArtifactContents(List<ResultsTableArtifact> artifactList) {
745  synchronized (lock) {
746  this.artifactTableContents = artifactList;
747  }
748  }
749 
755  private List<ResultsTableArtifact> getArtifactContents() {
756  synchronized (lock) {
757  return artifactTableContents;
758  }
759  }
760 
766  private class SelectedNodeChangedTask extends SwingWorker<ViewUpdate, Void> {
767 
768  private final Node selectedNode;
769 
770  SelectedNodeChangedTask(Node selectedNode) {
771  this.selectedNode = selectedNode;
772  }
773 
774  @Override
776  // Get the lookup for the node for access to its underlying content and
777  // blackboard artifact, if any.
778  Lookup lookup = selectedNode.getLookup();
779 
780  // Get the content. We may get BlackboardArtifacts, ignore those here.
781  ArrayList<BlackboardArtifact> artifacts = new ArrayList<>();
782  Collection<? extends Content> contents = lookup.lookupAll(Content.class);
783  if (contents.isEmpty()) {
784  return new ViewUpdate(getArtifactContents().size(), currentPage, ERROR_TEXT);
785  }
786  Content underlyingContent = null;
787  for (Content content : contents) {
788  if ( (content != null) && (!(content instanceof BlackboardArtifact)) ) {
789  // Get all of the blackboard artifacts associated with the content. These are what this
790  // viewer displays.
791  try {
792  artifacts = content.getAllArtifacts();
793  underlyingContent = content;
794  break;
795  } catch (TskException ex) {
796  logger.log(Level.SEVERE, "Couldn't get artifacts", ex); //NON-NLS
797  return new ViewUpdate(getArtifactContents().size(), currentPage, ERROR_TEXT);
798  }
799  }
800  }
801 
802  if (isCancelled()) {
803  return null;
804  }
805 
806  // Build the new artifact contents cache.
807  ArrayList<ResultsTableArtifact> artifactContents = new ArrayList<>();
808  for (BlackboardArtifact artifact : artifacts) {
809  artifactContents.add(new ResultsTableArtifact(artifact, underlyingContent));
810  }
811 
812  // If the node has an underlying blackboard artifact, show it. If not,
813  // show the first artifact.
814  int index = 0;
815  BlackboardArtifact artifact = lookup.lookup(BlackboardArtifact.class);
816  if (artifact != null) {
817  index = artifacts.indexOf(artifact);
818  if (index == -1) {
819  index = 0;
820  } else {
821  // if the artifact has an ASSOCIATED ARTIFACT, then we display the associated artifact instead
822  try {
823  for (BlackboardAttribute attr : artifact.getAttributes()) {
824  if (attr.getAttributeType().getTypeID() == BlackboardAttribute.ATTRIBUTE_TYPE.TSK_ASSOCIATED_ARTIFACT.getTypeID()) {
825  long assocArtifactId = attr.getValueLong();
826  int assocArtifactIndex = -1;
827  for (BlackboardArtifact art : artifacts) {
828  if (assocArtifactId == art.getArtifactID()) {
829  assocArtifactIndex = artifacts.indexOf(art);
830  break;
831  }
832  }
833  if (assocArtifactIndex >= 0) {
834  index = assocArtifactIndex;
835  }
836  break;
837  }
838  }
839  } catch (TskCoreException ex) {
840  logger.log(Level.WARNING, "Couldn't get associated artifact to display in Content Viewer.", ex); //NON-NLS
841  }
842  }
843 
844  }
845 
846  if (isCancelled()) {
847  return null;
848  }
849 
850  // Add one to the index of the artifact content for the corresponding page index.
851  ViewUpdate viewUpdate = new ViewUpdate(artifactContents.size(), index + 1, artifactContents.get(index));
852 
853  // It may take a considerable amount of time to fetch the attributes of the selected artifact
854  if (isCancelled()) {
855  return null;
856  }
857 
858  // Update the artifact contents cache.
859  setArtifactContents(artifactContents);
860 
861  return viewUpdate;
862  }
863 
864  @Override
865  protected void done() {
866  if (!isCancelled()) {
867  try {
868  ViewUpdate viewUpdate = get();
869  if (viewUpdate != null) {
870  updateView(viewUpdate);
871  }
872  } catch (InterruptedException | ExecutionException ex) {
873  logger.log(Level.WARNING, "Artifact display task unexpectedly interrupted or failed", ex); //NON-NLS
874  }
875  }
876  }
877  }
878 
884  private class SelectedArtifactChangedTask extends SwingWorker<ViewUpdate, Void> {
885 
886  private final int pageIndex;
887 
888  SelectedArtifactChangedTask(final int pageIndex) {
889  this.pageIndex = pageIndex;
890  }
891 
892  @Override
894  // Get the artifact content to display from the cache. Note that one must be subtracted from the
895  // page index to get the corresponding artifact content index.
896  List<ResultsTableArtifact> artifactContents = getArtifactContents();
897  ResultsTableArtifact artifactContent = artifactContents.get(pageIndex - 1);
898 
899  // It may take a considerable amount of time to fetch the attributes of the selected artifact so check for cancellation.
900  if (isCancelled()) {
901  return null;
902  }
903 
904  return new ViewUpdate(artifactContents.size(), pageIndex, artifactContent);
905  }
906 
907  @Override
908  protected void done() {
909  if (!isCancelled()) {
910  try {
911  ViewUpdate viewUpdate = get();
912  if (viewUpdate != null) {
913  updateView(viewUpdate);
914  }
915  } catch (InterruptedException | ExecutionException ex) {
916  logger.log(Level.WARNING, "Artifact display task unexpectedly interrupted or failed", ex); //NON-NLS
917  }
918  }
919  }
920  }
921 
925  private class MultiLineTableCellRenderer implements javax.swing.table.TableCellRenderer {
926 
927  @Override
928  public Component getTableCellRendererComponent(javax.swing.JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
929  javax.swing.JTextArea jtex = new javax.swing.JTextArea();
930  if (value instanceof String) {
931  jtex.setText((String) value);
932  jtex.setLineWrap(true);
933  jtex.setWrapStyleWord(false);
934  }
935  //cell backgroud color when selected
936  if (isSelected) {
937  jtex.setBackground(javax.swing.UIManager.getColor("Table.selectionBackground"));
938  } else {
939  jtex.setBackground(javax.swing.UIManager.getColor("Table.background"));
940  }
941  return jtex;
942  }
943  }
944 }
synchronized void startNewTask(SwingWorker< ViewUpdate, Void > task)
void appendJsonElementToString(String jsonKey, JsonElement jsonElement, String startIndent, StringBuilder sb)
void setArtifactContents(List< ResultsTableArtifact > artifactList)
static TimeZone getTimeZone(Content content)
Component getTableCellRendererComponent(javax.swing.JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2019 Basis Technology. Generated on: Tue Jan 7 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.