Autopsy  4.19.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
AddFileTypePanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2020 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.modules.filetypeid;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 import javax.swing.DefaultListModel;
24 import javax.swing.JOptionPane;
25 import javax.swing.event.ListSelectionEvent;
26 import javax.swing.event.ListSelectionListener;
27 import org.apache.commons.lang.ArrayUtils;
28 import org.apache.commons.lang3.StringUtils;
29 import org.openide.util.NbBundle;
30 import org.openide.util.NbBundle.Messages;
31 import static org.sleuthkit.autopsy.modules.filetypeid.AddFileTypePanel.EVENT.SIG_LIST_CHANGED;
34 
38 @Messages("AddFileTypePanel.mimeFormatLabel.text=Form of MIME type should be: media type/media subtype")
39 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
40 class AddFileTypePanel extends javax.swing.JPanel {
41 
42  private static final long serialVersionUID = 1L;
43 
44  private AddFileTypeSignatureDialog addSigDialog;
45  private DefaultListModel<FileType.Signature> signaturesListModel;
46 
50  AddFileTypePanel() {
51  initComponents();
52  this.signaturesListModel = new DefaultListModel<>();
53  this.signatureList.setModel(signaturesListModel);
54  this.addTypeListSelectionListener();
55  this.enableButtons();
56  }
57 
58  enum EVENT {
59  SIG_LIST_CHANGED
60  }
61 
67  AddFileTypePanel(FileType toEdit) {
68  this();
69  this.setComponentValues(toEdit);
70  }
71 
77  private void setComponentValues(FileType toEdit) {
78  this.mimeTypeTextField.setText(toEdit.getMimeType());
79  for (Signature sig : toEdit.getSignatures()) {
80  this.signaturesListModel.addElement(sig);
81  }
82  this.postHitCheckBox.setSelected(toEdit.shouldCreateInterestingFileHit());
83  this.setNameTextField.setEnabled(toEdit.shouldCreateInterestingFileHit());
84  this.setNameTextField.setText(toEdit.getInterestingFilesSetName());
85 
86  }
87 
95  @Messages({"AddMimeTypePanel.emptySigList.message=Must have at least one signature.",
96  "AddMimeTypePanel.emptySigList.title=Invalid Signature List",
97  "AddMimeTypePanel.emptySetName.message=Interesting files set name is required if alert is requested.",
98  "AddMimeTypePanel.emptySetName.title=Missing Interesting Files Set Name",
99  "# {0} - media subtype",
100  "AddFileTypePanel.nonStandardMIMEType.message="
101  + "MIME type must be of form: media type/media subtype. Custom/{0} has been suggested instead.",
102  "# {0} - type name",
103  "AddFileTypePanel.containsIllegalCharacter.message=Invalid character in MIME type, {0} has been suggested instead",
104  "AddFileTypePanel.containsIllegalCharacter.title=Invalid Character in MIME Type",
105  "AddFileTypePanel.nonStandardMIMEType.title=Non-standard MIME Type"})
106 
107  FileType getFileType() {
108  String typeName = mimeTypeTextField.getText();
109 
110  //if typeName does not equal sanitized typeName display message saying this name will be used instead
111  if (typeName.isEmpty()) {
112  JOptionPane.showMessageDialog(this,
113  NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "FileTypeIdGlobalSettingsPanel.JOptionPane.invalidMIMEType.message"),
114  NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "FileTypeIdGlobalSettingsPanel.JOptionPane.invalidMIMEType.title"),
115  JOptionPane.ERROR_MESSAGE);
116  return null;
117  }
118  //if we need to remove more characters could use matches instead of contains and regex "[^\\w\s\\-\\/] to remove everything that isnt a letter, number, underscore, whitespace, dash, or forward slash.
119  if (typeName.contains("\'")) { //remove single apostraphes as they are an easy way to accidently screw up PostgreSQL
120  typeName = typeName.replaceAll("[\\']", "");
121  JOptionPane.showMessageDialog(this,
122  NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "AddFileTypePanel.containsIllegalCharacter.message", typeName),
123  NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "AddFileTypePanel.containsIllegalCharacter.title"),
124  JOptionPane.WARNING_MESSAGE);
125  mimeTypeTextField.setText(typeName);
126  return null;
127  }
128  //if the MIME type is lacking two parts or the first part is empty ask if they want to use 'custom' as the first part
129  //if the MIME type has more than 2 parts the first part will be used as a media type and the remainder of the string as the sub-type
130  String[] splitName = typeName.split("/");
131  if (splitName.length < 2 || splitName[0].isEmpty()) {
132  JOptionPane.showMessageDialog(this,
133  NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "AddFileTypePanel.nonStandardMIMEType.message", typeName),
134  NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "AddFileTypePanel.nonStandardMIMEType.title"),
135  JOptionPane.WARNING_MESSAGE);
136  mimeTypeTextField.setText("custom/" + typeName);
137  return null;
138  }
139  //Make sure the mimetype will piece back together to be the same string it was entered
140  //trailing forward slashes will cause this mismatch to happen
141  //suggests a mime_type that will be the same after it is split appart and rejoined
142  if (!StringUtils.join(ArrayUtils.subarray(splitName, 0, splitName.length), "/").equals(typeName)) {
143  String rejoinedMimeType = StringUtils.join(ArrayUtils.subarray(splitName, 0, splitName.length), "/");
144  JOptionPane.showMessageDialog(this,
145  NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "AddFileTypePanel.nonStandardMIMEType.message", rejoinedMimeType),
146  NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "AddFileTypePanel.nonStandardMIMEType.title"),
147  JOptionPane.WARNING_MESSAGE);
148  mimeTypeTextField.setText(rejoinedMimeType);
149  return null;
150  }
151  if (this.signaturesListModel.isEmpty()) {
152  JOptionPane.showMessageDialog(this,
153  Bundle.AddMimeTypePanel_emptySigList_message(),
154  Bundle.AddMimeTypePanel_emptySigList_title(),
155  JOptionPane.ERROR_MESSAGE);
156  return null;
157  }
158  List<Signature> sigList = new ArrayList<>();
159  for (int i = 0; i < this.signaturesListModel.getSize(); i++) {
160  sigList.add(this.signaturesListModel.elementAt(i));
161  }
162 
163  String setName = "";
164  if (this.postHitCheckBox.isSelected()) {
165  if (this.setNameTextField.getText().isEmpty()) {
166  JOptionPane.showMessageDialog(this,
167  Bundle.AddMimeTypePanel_emptySetName_message(),
168  Bundle.AddMimeTypePanel_emptySetName_title(),
169  JOptionPane.ERROR_MESSAGE);
170 
171  return null;
172  }
173  setName = this.setNameTextField.getText();
174  }
175  return new FileType(typeName, sigList, this.postHitCheckBox.isSelected(), setName);
176  }
177 
182  private void addTypeListSelectionListener() {
183  this.signatureList.addListSelectionListener(new ListSelectionListener() {
184  @Override
185  public void valueChanged(ListSelectionEvent e) {
186  if (e.getValueIsAdjusting() == false) {
187  enableButtons();
188 
189  }
190  }
191  });
192  }
193 
198  private void enableButtons() {
199  if (signatureList.getSelectedIndex() == -1) {
200  editSigButton.setEnabled(false);
201  deleteSigButton.setEnabled(false);
202  } else {
203  editSigButton.setEnabled(true);
204  deleteSigButton.setEnabled(true);
205  }
206  }
207 
208  boolean hasSignature() {
209  return !this.signaturesListModel.isEmpty();
210  }
211 
217  @SuppressWarnings("unchecked")
218  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
219  private void initComponents() {
220 
221  editSigButton = new javax.swing.JButton();
222  deleteSigButton = new javax.swing.JButton();
223  mimeTypeLabel = new javax.swing.JLabel();
224  jScrollPane1 = new javax.swing.JScrollPane();
225  signatureList = new javax.swing.JList<>();
226  mimeTypeTextField = new javax.swing.JTextField();
227  addSigButton = new javax.swing.JButton();
228  jLabel1 = new javax.swing.JLabel();
229  postHitCheckBox = new javax.swing.JCheckBox();
230  setNameLabel = new javax.swing.JLabel();
231  setNameTextField = new javax.swing.JTextField();
232  mimeFormatLabel = new javax.swing.JLabel();
233 
234  editSigButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/images/edit16.png"))); // NOI18N
235  org.openide.awt.Mnemonics.setLocalizedText(editSigButton, org.openide.util.NbBundle.getMessage(AddFileTypePanel.class, "AddFileTypePanel.editSigButton.text")); // NOI18N
236  editSigButton.addActionListener(new java.awt.event.ActionListener() {
237  public void actionPerformed(java.awt.event.ActionEvent evt) {
238  editSigButtonActionPerformed(evt);
239  }
240  });
241 
242  deleteSigButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/images/delete16.png"))); // NOI18N
243  org.openide.awt.Mnemonics.setLocalizedText(deleteSigButton, org.openide.util.NbBundle.getMessage(AddFileTypePanel.class, "AddFileTypePanel.deleteSigButton.text")); // NOI18N
244  deleteSigButton.addActionListener(new java.awt.event.ActionListener() {
245  public void actionPerformed(java.awt.event.ActionEvent evt) {
246  deleteSigButtonActionPerformed(evt);
247  }
248  });
249 
250  org.openide.awt.Mnemonics.setLocalizedText(mimeTypeLabel, org.openide.util.NbBundle.getMessage(AddFileTypePanel.class, "AddFileTypePanel.mimeTypeLabel.text")); // NOI18N
251 
252  signatureList.setModel(new javax.swing.AbstractListModel<FileType.Signature>() {
253  Signature[] signatures = {};
254  public int getSize() { return signatures.length; }
255  public Signature getElementAt(int i) { return signatures[i]; }
256  });
257  jScrollPane1.setViewportView(signatureList);
258 
259  mimeTypeTextField.setText(org.openide.util.NbBundle.getMessage(AddFileTypePanel.class, "AddFileTypePanel.mimeTypeTextField.text")); // NOI18N
260 
261  addSigButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/images/add16.png"))); // NOI18N
262  org.openide.awt.Mnemonics.setLocalizedText(addSigButton, org.openide.util.NbBundle.getMessage(AddFileTypePanel.class, "AddFileTypePanel.addSigButton.text")); // NOI18N
263  addSigButton.addActionListener(new java.awt.event.ActionListener() {
264  public void actionPerformed(java.awt.event.ActionEvent evt) {
265  addSigButtonActionPerformed(evt);
266  }
267  });
268 
269  org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(AddFileTypePanel.class, "AddFileTypePanel.jLabel1.text")); // NOI18N
270 
271  org.openide.awt.Mnemonics.setLocalizedText(postHitCheckBox, org.openide.util.NbBundle.getMessage(AddFileTypePanel.class, "AddFileTypePanel.postHitCheckBox.text")); // NOI18N
272  postHitCheckBox.addActionListener(new java.awt.event.ActionListener() {
273  public void actionPerformed(java.awt.event.ActionEvent evt) {
274  postHitCheckBoxActionPerformed(evt);
275  }
276  });
277 
278  org.openide.awt.Mnemonics.setLocalizedText(setNameLabel, org.openide.util.NbBundle.getMessage(AddFileTypePanel.class, "AddFileTypePanel.setNameLabel.text")); // NOI18N
279  setNameLabel.setEnabled(postHitCheckBox.isSelected());
280 
281  setNameTextField.setText(org.openide.util.NbBundle.getMessage(AddFileTypePanel.class, "AddFileTypePanel.setNameTextField.text")); // NOI18N
282  setNameTextField.setEnabled(postHitCheckBox.isSelected());
283 
284  org.openide.awt.Mnemonics.setLocalizedText(mimeFormatLabel, org.openide.util.NbBundle.getMessage(AddFileTypePanel.class, "AddFileTypePanel.mimeFormatLabel.text")); // NOI18N
285 
286  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
287  this.setLayout(layout);
288  layout.setHorizontalGroup(
289  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
290  .addGroup(layout.createSequentialGroup()
291  .addContainerGap()
292  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
293  .addGroup(layout.createSequentialGroup()
294  .addComponent(mimeTypeLabel)
295  .addGap(18, 18, 18)
296  .addComponent(mimeTypeTextField))
297  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
298  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
299  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
300  .addComponent(addSigButton)
301  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
302  .addComponent(editSigButton)
303  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
304  .addComponent(deleteSigButton))
305  .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 393, javax.swing.GroupLayout.PREFERRED_SIZE))
306  .addGroup(layout.createSequentialGroup()
307  .addGap(28, 28, 28)
308  .addComponent(setNameLabel)
309  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
310  .addComponent(setNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)))
311  .addGroup(layout.createSequentialGroup()
312  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
313  .addComponent(jLabel1)
314  .addComponent(postHitCheckBox))
315  .addGap(0, 0, 0))
316  .addGroup(layout.createSequentialGroup()
317  .addGap(71, 71, 71)
318  .addComponent(mimeFormatLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
319  .addContainerGap())
320  );
321  layout.setVerticalGroup(
322  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
323  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
324  .addContainerGap()
325  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
326  .addComponent(mimeTypeLabel)
327  .addComponent(mimeTypeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
328  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
329  .addComponent(mimeFormatLabel)
330  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
331  .addComponent(jLabel1)
332  .addGap(1, 1, 1)
333  .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
334  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
335  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
336  .addComponent(addSigButton)
337  .addComponent(editSigButton)
338  .addComponent(deleteSigButton))
339  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
340  .addComponent(postHitCheckBox)
341  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
342  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
343  .addComponent(setNameLabel)
344  .addComponent(setNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
345  .addContainerGap())
346  );
347  }// </editor-fold>//GEN-END:initComponents
348 
349  private void editSigButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editSigButtonActionPerformed
350  if (evt.getSource().equals(this.editSigButton) && this.signatureList.getSelectedValue() != null) {
351  int selected = this.signatureList.getSelectedIndex();
352  this.addSigDialog = new AddFileTypeSignatureDialog(this.signatureList.getSelectedValue());
353  this.addSigDialog.display(false);
354  if (addSigDialog.getResult() == BUTTON_PRESSED.OK) {
355  signaturesListModel.removeElementAt(selected);
356  this.signaturesListModel.add(selected, this.addSigDialog.getSignature());
357  }
358  }
359  }//GEN-LAST:event_editSigButtonActionPerformed
360 
361  private void deleteSigButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteSigButtonActionPerformed
362  if (this.signatureList.getSelectedIndex() != -1) {
363  signaturesListModel.removeElementAt(this.signatureList.getSelectedIndex());
364  if (!this.signaturesListModel.isEmpty()) {
365  signatureList.setSelectedIndex(0);
366  }
367  firePropertyChange(SIG_LIST_CHANGED.toString(), null, null);
368  }
369  }//GEN-LAST:event_deleteSigButtonActionPerformed
370 
371  private void addSigButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addSigButtonActionPerformed
372  if (evt.getSource().equals(this.addSigButton)) {
373  this.addSigDialog = new AddFileTypeSignatureDialog();
374  this.addSigDialog.display(true);
375  if (addSigDialog.getResult() == AddFileTypeSignatureDialog.BUTTON_PRESSED.OK) {
376  signaturesListModel.addElement(this.addSigDialog.getSignature());
377  }
378  firePropertyChange(SIG_LIST_CHANGED.toString(), null, null);
379  }
380  }//GEN-LAST:event_addSigButtonActionPerformed
381 
382  private void postHitCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_postHitCheckBoxActionPerformed
383  if (evt.getSource().equals(postHitCheckBox)) {
384  this.setNameLabel.setEnabled(postHitCheckBox.isSelected());
385  this.setNameTextField.setEnabled(postHitCheckBox.isSelected());
386  }
387  }//GEN-LAST:event_postHitCheckBoxActionPerformed
388 
389 
390  // Variables declaration - do not modify//GEN-BEGIN:variables
391  private javax.swing.JButton addSigButton;
392  private javax.swing.JButton deleteSigButton;
393  private javax.swing.JButton editSigButton;
394  private javax.swing.JLabel jLabel1;
395  private javax.swing.JScrollPane jScrollPane1;
396  private javax.swing.JLabel mimeFormatLabel;
397  private javax.swing.JLabel mimeTypeLabel;
398  private javax.swing.JTextField mimeTypeTextField;
399  private javax.swing.JCheckBox postHitCheckBox;
400  private javax.swing.JLabel setNameLabel;
401  private javax.swing.JTextField setNameTextField;
402  private javax.swing.JList<FileType.Signature> signatureList;
403  // End of variables declaration//GEN-END:variables
404 }

Copyright © 2012-2021 Basis Technology. Generated on: Thu Sep 30 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.