Autopsy  4.5.0
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-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.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 
35 @Messages("AddFileTypePanel.mimeFormatLabel.text=Form of MIME type should be: media type/media subtype")
36 
37 
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  }
83 
91  @Messages({"AddMimeTypePanel.emptySigList.message=Must have at least one signature.",
92  "AddMimeTypePanel.emptySigList.title=Invalid Signature List",
93  "AddMimeTypePanel.emptySetName.message=Interesting files set name is required if alert is requested.",
94  "AddMimeTypePanel.emptySetName.title=Missing Interesting Files Set Name",
95  "# {0} - media subtype",
96  "AddFileTypePanel.nonStandardMIMEType.message="
97  + "MIME type must be of form: media type/media subtype. Custom/{0} has been suggested instead.",
98  "# {0} - type name",
99  "AddFileTypePanel.containsIllegalCharacter.message=Invalid character in MIME type, {0} has been suggested instead",
100  "AddFileTypePanel.containsIllegalCharacter.title=Invalid Character in MIME Type",
101  "AddFileTypePanel.nonStandardMIMEType.title=Non-standard MIME Type"})
102 
103  FileType getFileType() {
104  String typeName = mimeTypeTextField.getText();
105 
106  //if typeName does not equal sanitized typeName display message saying this name will be used instead
107  if (typeName.isEmpty()) {
108  JOptionPane.showMessageDialog(null,
109  NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "FileTypeIdGlobalSettingsPanel.JOptionPane.invalidMIMEType.message"),
110  NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "FileTypeIdGlobalSettingsPanel.JOptionPane.invalidMIMEType.title"),
111  JOptionPane.ERROR_MESSAGE);
112  return null;
113  }
114  //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.
115  if (typeName.contains("\'")) { //remove single apostraphes as they are an easy way to accidently screw up PostgreSQL
116  typeName = typeName.replaceAll("[\\']", "");
117  JOptionPane.showMessageDialog(null,
118  NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "AddFileTypePanel.containsIllegalCharacter.message", typeName),
119  NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "AddFileTypePanel.containsIllegalCharacter.title"),
120  JOptionPane.WARNING_MESSAGE);
121  mimeTypeTextField.setText(typeName);
122  return null;
123  }
124  //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
125  //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
126  String[] splitName = typeName.split("/");
127  if (splitName.length < 2 || splitName[0].isEmpty()) {
128  JOptionPane.showMessageDialog(null,
129  NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "AddFileTypePanel.nonStandardMIMEType.message", typeName),
130  NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "AddFileTypePanel.nonStandardMIMEType.title"),
131  JOptionPane.WARNING_MESSAGE);
132  mimeTypeTextField.setText("custom/" + typeName);
133  return null;
134  }
135  //Make sure the mimetype will piece back together to be the same string it was entered
136  //trailing forward slashes will cause this mismatch to happen
137  //suggests a mime_type that will be the same after it is split appart and rejoined
138  if (!StringUtils.join(ArrayUtils.subarray(splitName, 0, splitName.length), "/").equals(typeName)) {
139  String rejoinedMimeType = StringUtils.join(ArrayUtils.subarray(splitName, 0, splitName.length), "/");
140  JOptionPane.showMessageDialog(null,
141  NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "AddFileTypePanel.nonStandardMIMEType.message", rejoinedMimeType),
142  NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "AddFileTypePanel.nonStandardMIMEType.title"),
143  JOptionPane.WARNING_MESSAGE);
144  mimeTypeTextField.setText(rejoinedMimeType);
145  return null;
146  }
147  if (this.signaturesListModel.isEmpty()) {
148  JOptionPane.showMessageDialog(null,
149  Bundle.AddMimeTypePanel_emptySigList_message(),
150  Bundle.AddMimeTypePanel_emptySigList_title(),
151  JOptionPane.ERROR_MESSAGE);
152  return null;
153  }
154  List<Signature> sigList = new ArrayList<>();
155  for (int i = 0; i < this.signaturesListModel.getSize(); i++) {
156  sigList.add(this.signaturesListModel.elementAt(i));
157  }
158 
159  String setName = "";
160  if (this.postHitCheckBox.isSelected()) {
161  if (this.setNameTextField.getText().isEmpty()) {
162  JOptionPane.showMessageDialog(null,
163  Bundle.AddMimeTypePanel_emptySetName_message(),
164  Bundle.AddMimeTypePanel_emptySetName_title(),
165  JOptionPane.ERROR_MESSAGE);
166 
167  return null;
168  }
169  setName = this.setNameTextField.getText();
170  }
171  return new FileType(typeName, sigList, this.postHitCheckBox.isSelected(), setName);
172  }
173 
178  private void addTypeListSelectionListener() {
179  this.signatureList.addListSelectionListener(new ListSelectionListener() {
180  @Override
181  public void valueChanged(ListSelectionEvent e) {
182  if (e.getValueIsAdjusting() == false) {
183  enableButtons();
184 
185  }
186  }
187  });
188  }
189 
194  private void enableButtons() {
195  if (signatureList.getSelectedIndex() == -1) {
196  editSigButton.setEnabled(false);
197  deleteSigButton.setEnabled(false);
198  } else {
199  editSigButton.setEnabled(true);
200  deleteSigButton.setEnabled(true);
201  }
202  }
203 
204  boolean hasSignature() {
205  return !this.signaturesListModel.isEmpty();
206  }
207 
213  @SuppressWarnings("unchecked")
214  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
215  private void initComponents() {
216 
217  editSigButton = new javax.swing.JButton();
218  deleteSigButton = new javax.swing.JButton();
219  mimeTypeLabel = new javax.swing.JLabel();
220  jScrollPane1 = new javax.swing.JScrollPane();
221  signatureList = new javax.swing.JList<>();
222  mimeTypeTextField = new javax.swing.JTextField();
223  addSigButton = new javax.swing.JButton();
224  jLabel1 = new javax.swing.JLabel();
225  postHitCheckBox = new javax.swing.JCheckBox();
226  setNameLabel = new javax.swing.JLabel();
227  setNameTextField = new javax.swing.JTextField();
228  mimeFormatLabel = new javax.swing.JLabel();
229 
230  editSigButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/images/edit16.png"))); // NOI18N
231  org.openide.awt.Mnemonics.setLocalizedText(editSigButton, org.openide.util.NbBundle.getMessage(AddFileTypePanel.class, "AddFileTypePanel.editSigButton.text")); // NOI18N
232  editSigButton.addActionListener(new java.awt.event.ActionListener() {
233  public void actionPerformed(java.awt.event.ActionEvent evt) {
234  editSigButtonActionPerformed(evt);
235  }
236  });
237 
238  deleteSigButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/images/delete16.png"))); // NOI18N
239  org.openide.awt.Mnemonics.setLocalizedText(deleteSigButton, org.openide.util.NbBundle.getMessage(AddFileTypePanel.class, "AddFileTypePanel.deleteSigButton.text")); // NOI18N
240  deleteSigButton.addActionListener(new java.awt.event.ActionListener() {
241  public void actionPerformed(java.awt.event.ActionEvent evt) {
242  deleteSigButtonActionPerformed(evt);
243  }
244  });
245 
246  mimeTypeLabel.setFont(mimeTypeLabel.getFont().deriveFont(mimeTypeLabel.getFont().getStyle() & ~java.awt.Font.BOLD, 11));
247  org.openide.awt.Mnemonics.setLocalizedText(mimeTypeLabel, org.openide.util.NbBundle.getMessage(AddFileTypePanel.class, "AddFileTypePanel.mimeTypeLabel.text")); // NOI18N
248 
249  signatureList.setModel(new javax.swing.AbstractListModel<FileType.Signature>() {
250  Signature[] signatures = {};
251  public int getSize() { return signatures.length; }
252  public Signature getElementAt(int i) { return signatures[i]; }
253  });
254  jScrollPane1.setViewportView(signatureList);
255 
256  mimeTypeTextField.setFont(mimeTypeTextField.getFont().deriveFont(mimeTypeTextField.getFont().getStyle() & ~java.awt.Font.BOLD, 11));
257  mimeTypeTextField.setText(org.openide.util.NbBundle.getMessage(AddFileTypePanel.class, "AddFileTypePanel.mimeTypeTextField.text")); // NOI18N
258 
259  addSigButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/images/add16.png"))); // NOI18N
260  org.openide.awt.Mnemonics.setLocalizedText(addSigButton, org.openide.util.NbBundle.getMessage(AddFileTypePanel.class, "AddFileTypePanel.addSigButton.text")); // NOI18N
261  addSigButton.addActionListener(new java.awt.event.ActionListener() {
262  public void actionPerformed(java.awt.event.ActionEvent evt) {
263  addSigButtonActionPerformed(evt);
264  }
265  });
266 
267  org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(AddFileTypePanel.class, "AddFileTypePanel.jLabel1.text")); // NOI18N
268 
269  org.openide.awt.Mnemonics.setLocalizedText(postHitCheckBox, org.openide.util.NbBundle.getMessage(AddFileTypePanel.class, "AddFileTypePanel.postHitCheckBox.text")); // NOI18N
270  postHitCheckBox.addActionListener(new java.awt.event.ActionListener() {
271  public void actionPerformed(java.awt.event.ActionEvent evt) {
272  postHitCheckBoxActionPerformed(evt);
273  }
274  });
275 
276  org.openide.awt.Mnemonics.setLocalizedText(setNameLabel, org.openide.util.NbBundle.getMessage(AddFileTypePanel.class, "AddFileTypePanel.setNameLabel.text")); // NOI18N
277  setNameLabel.setEnabled(postHitCheckBox.isSelected());
278 
279  setNameTextField.setText(org.openide.util.NbBundle.getMessage(AddFileTypePanel.class, "AddFileTypePanel.setNameTextField.text")); // NOI18N
280  setNameTextField.setEnabled(postHitCheckBox.isSelected());
281 
282  org.openide.awt.Mnemonics.setLocalizedText(mimeFormatLabel, org.openide.util.NbBundle.getMessage(AddFileTypePanel.class, "AddFileTypePanel.mimeFormatLabel.text")); // NOI18N
283 
284  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
285  this.setLayout(layout);
286  layout.setHorizontalGroup(
287  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
288  .addGroup(layout.createSequentialGroup()
289  .addContainerGap()
290  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
291  .addGroup(layout.createSequentialGroup()
292  .addComponent(mimeTypeLabel)
293  .addGap(18, 18, 18)
294  .addComponent(mimeTypeTextField))
295  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
296  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
297  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
298  .addComponent(addSigButton)
299  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
300  .addComponent(editSigButton)
301  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
302  .addComponent(deleteSigButton))
303  .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 393, javax.swing.GroupLayout.PREFERRED_SIZE))
304  .addGroup(layout.createSequentialGroup()
305  .addGap(28, 28, 28)
306  .addComponent(setNameLabel)
307  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
308  .addComponent(setNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)))
309  .addGroup(layout.createSequentialGroup()
310  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
311  .addComponent(jLabel1)
312  .addComponent(postHitCheckBox))
313  .addGap(0, 0, Short.MAX_VALUE))
314  .addGroup(layout.createSequentialGroup()
315  .addGap(71, 71, 71)
316  .addComponent(mimeFormatLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
317  .addContainerGap())
318  );
319  layout.setVerticalGroup(
320  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
321  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
322  .addContainerGap()
323  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
324  .addComponent(mimeTypeLabel)
325  .addComponent(mimeTypeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
326  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
327  .addComponent(mimeFormatLabel)
328  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
329  .addComponent(jLabel1)
330  .addGap(1, 1, 1)
331  .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
332  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
333  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
334  .addComponent(addSigButton)
335  .addComponent(editSigButton)
336  .addComponent(deleteSigButton))
337  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
338  .addComponent(postHitCheckBox)
339  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
340  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
341  .addComponent(setNameLabel)
342  .addComponent(setNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
343  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
344  );
345  }// </editor-fold>//GEN-END:initComponents
346 
347  private void editSigButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editSigButtonActionPerformed
348  if (evt.getSource().equals(this.editSigButton) && this.signatureList.getSelectedValue() != null) {
349  int selected = this.signatureList.getSelectedIndex();
350  this.addSigDialog = new AddFileTypeSignatureDialog(this.signatureList.getSelectedValue());
351  if (addSigDialog.getResult() == BUTTON_PRESSED.OK) {
352  signaturesListModel.removeElementAt(selected);
353  this.signaturesListModel.add(selected, this.addSigDialog.getSignature());
354  }
355  }
356  }//GEN-LAST:event_editSigButtonActionPerformed
357 
358  private void deleteSigButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteSigButtonActionPerformed
359  if (this.signatureList.getSelectedIndex() != -1) {
360  signaturesListModel.removeElementAt(this.signatureList.getSelectedIndex());
361  if (!this.signaturesListModel.isEmpty()) {
362  signatureList.setSelectedIndex(0);
363  }
364  firePropertyChange(SIG_LIST_CHANGED.toString(), null, null);
365  }
366  }//GEN-LAST:event_deleteSigButtonActionPerformed
367 
368  private void addSigButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addSigButtonActionPerformed
369  if (evt.getSource().equals(this.addSigButton)) {
370  this.addSigDialog = new AddFileTypeSignatureDialog();
371  if (addSigDialog.getResult() == AddFileTypeSignatureDialog.BUTTON_PRESSED.OK) {
372  signaturesListModel.addElement(this.addSigDialog.getSignature());
373  }
374  firePropertyChange(SIG_LIST_CHANGED.toString(), null, null);
375  }
376  }//GEN-LAST:event_addSigButtonActionPerformed
377 
378  private void postHitCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_postHitCheckBoxActionPerformed
379  if (evt.getSource().equals(postHitCheckBox)) {
380  this.setNameLabel.setEnabled(postHitCheckBox.isSelected());
381  this.setNameTextField.setEnabled(postHitCheckBox.isSelected());
382  }
383  }//GEN-LAST:event_postHitCheckBoxActionPerformed
384 
385 
386  // Variables declaration - do not modify//GEN-BEGIN:variables
387  private javax.swing.JButton addSigButton;
388  private javax.swing.JButton deleteSigButton;
389  private javax.swing.JButton editSigButton;
390  private javax.swing.JLabel jLabel1;
391  private javax.swing.JScrollPane jScrollPane1;
392  private javax.swing.JLabel mimeFormatLabel;
393  private javax.swing.JLabel mimeTypeLabel;
394  private javax.swing.JTextField mimeTypeTextField;
395  private javax.swing.JCheckBox postHitCheckBox;
396  private javax.swing.JLabel setNameLabel;
397  private javax.swing.JTextField setNameTextField;
398  private javax.swing.JList<FileType.Signature> signatureList;
399  // End of variables declaration//GEN-END:variables
400 }

Copyright © 2012-2016 Basis Technology. Generated on: Tue Feb 20 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.