Autopsy  4.13.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
ConfigVisualPanel3.java
Go to the documentation of this file.
1 /*
2  * Autopsy
3  *
4  * Copyright 2019 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.logicalimager.configuration;
20 
21 import com.google.gson.Gson;
22 import com.google.gson.GsonBuilder;
23 import com.google.gson.JsonIOException;
24 import java.awt.Color;
25 import java.awt.Cursor;
26 import java.io.File;
27 import java.io.IOException;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.Arrays;
31 import java.util.List;
32 import java.util.logging.Level;
33 import javax.swing.JOptionPane;
34 import org.apache.commons.io.FileUtils;
35 import org.apache.commons.io.FilenameUtils;
36 import org.openide.util.NbBundle;
39 
43 class ConfigVisualPanel3 extends javax.swing.JPanel {
44 
45  private static final Logger logger = Logger.getLogger(ConfigVisualPanel3.class.getName());
46  private static final String SAVED_LOGICAL_IMAGER = "SAVED_LOGICAL_IMAGER";
47  private static final long serialVersionUID = 1L;
48  private boolean hasBeenSaved = false;
49  private String configFilename;
50  private LogicalImagerConfig config;
51 
55  @NbBundle.Messages({"ConfigVisualPanel3.copyStatus.notSaved=File has not been saved",
56  "ConfigVisualPanel3.copyStatus.savingInProgress=Saving file, please wait",
57  "ConfigVisualPanel3.copyStatus.saved=Saved",
58  "ConfigVisualPanel3.copyStatus.error=Unable to save file"})
59  ConfigVisualPanel3() {
60  initComponents();
61  }
62 
63  final void resetPanel() {
64  hasBeenSaved = false;
65  configStatusLabel.setText(Bundle.ConfigVisualPanel3_copyStatus_notSaved());
66  executableStatusLabel.setText(Bundle.ConfigVisualPanel3_copyStatus_notSaved());
67  }
68 
69  @NbBundle.Messages({
70  "ConfigVisualPanel3.saveConfigurationFile=Save imager"
71  })
72  @Override
73  public String getName() {
74  return Bundle.ConfigVisualPanel3_saveConfigurationFile();
75  }
76 
82  boolean isSaved() {
83  return hasBeenSaved;
84  }
85 
90  @NbBundle.Messages({
91  "# {0} - configFilename",
92  "ConfigVisualPanel3.failedToSaveConfigMsg=Failed to save configuration file: {0}",
93  "# {0} - reason",
94  "ConfigVisualPanel3.reason=\nReason: {0}",
95  "ConfigVisualPanel3.failedToSaveExeMsg=Failed to save tsk_logical_imager.exe file"
96  })
97  void saveConfigFile() {
98  boolean saveSuccess = true;
99  executableStatusLabel.setForeground(Color.BLACK);
100  configStatusLabel.setText(Bundle.ConfigVisualPanel3_copyStatus_savingInProgress());
101  executableStatusLabel.setText(Bundle.ConfigVisualPanel3_copyStatus_savingInProgress());
102  setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
103  GsonBuilder gsonBuilder = new GsonBuilder()
104  .setPrettyPrinting()
105  .excludeFieldsWithoutExposeAnnotation()
106  .disableHtmlEscaping();
107  Gson gson = gsonBuilder.create();
108  String toJson = gson.toJson(config);
109  try {
110  List<String> lines = Arrays.asList(toJson.split("\\n"));
111  FileUtils.writeLines(new File(configFilename), "UTF-8", lines, System.getProperty("line.separator")); // NON-NLS
112  configStatusLabel.setText(Bundle.ConfigVisualPanel3_copyStatus_saved());
113  } catch (IOException ex) {
114  saveSuccess = false;
115  configStatusLabel.setText(Bundle.ConfigVisualPanel3_copyStatus_error());
116  configStatusLabel.setForeground(Color.RED);
117  JOptionPane.showMessageDialog(this, Bundle.ConfigVisualPanel3_failedToSaveConfigMsg(configFilename)
118  + Bundle.ConfigVisualPanel3_reason(ex.getMessage()));
119  } catch (JsonIOException jioe) {
120  saveSuccess = false;
121  executableStatusLabel.setText(Bundle.ConfigVisualPanel3_copyStatus_error());
122  executableStatusLabel.setForeground(Color.RED);
123  logger.log(Level.SEVERE, "Failed to save configuration file: " + configFilename, jioe); // NON-NLS
124  JOptionPane.showMessageDialog(this, Bundle.ConfigVisualPanel3_failedToSaveConfigMsg(configFilename)
125  + Bundle.ConfigVisualPanel3_reason(jioe.getMessage()));
126  }
127  try {
128  writeTskLogicalImagerExe(Paths.get(configFilename).getParent());
129  executableStatusLabel.setText(Bundle.ConfigVisualPanel3_copyStatus_saved());
130  } catch (IOException ex) {
131  saveSuccess = false;
132  executableStatusLabel.setText(Bundle.ConfigVisualPanel3_copyStatus_error());
133  executableStatusLabel.setForeground(Color.RED);
134  logger.log(Level.SEVERE, "Failed to save tsk_logical_imager.exe file", ex); // NON-NLS
135  JOptionPane.showMessageDialog(this, Bundle.ConfigVisualPanel3_failedToSaveExeMsg()
136  + Bundle.ConfigVisualPanel3_reason(ex.getMessage()));
137  }
138  if (saveSuccess) {
139  hasBeenSaved = true;
140  firePropertyChange(SAVED_LOGICAL_IMAGER, false, true); // NON-NLS
141  }
142  setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
143  }
144 
152  @NbBundle.Messages({
153  "ConfigVisualPanel3.errorMsg.cannotFindLogicalImager=Cannot locate logical imager, cannot copy to destination"
154  })
155  private void writeTskLogicalImagerExe(Path destDir) throws IOException {
156  File logicalImagerExe = getLogicalImagerExe();
157  if (logicalImagerExe != null && logicalImagerExe.exists()) {
158  FileUtils.copyFileToDirectory(getLogicalImagerExe(), destDir.toFile());
159  } else {
160  throw new IOException(Bundle.ConfigVisualPanel3_errorMsg_cannotFindLogicalImager());
161  }
162  }
163 
170  static String getSavedEventName() {
171  return SAVED_LOGICAL_IMAGER;
172  }
173 
179  @SuppressWarnings("unchecked")
180  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
181  private void initComponents() {
182 
183  javax.swing.JButton saveButton = new javax.swing.JButton();
184  descriptionScrollPane = new javax.swing.JScrollPane();
185  descriptionTextArea = new javax.swing.JTextArea();
186  javax.swing.JLabel configLabel = new javax.swing.JLabel();
187  configStatusLabel = new javax.swing.JLabel();
188  javax.swing.JLabel executableLabel = new javax.swing.JLabel();
189  executableStatusLabel = new javax.swing.JLabel();
190 
191  org.openide.awt.Mnemonics.setLocalizedText(saveButton, org.openide.util.NbBundle.getMessage(ConfigVisualPanel3.class, "ConfigVisualPanel3.saveButton.text")); // NOI18N
192  saveButton.addActionListener(new java.awt.event.ActionListener() {
193  public void actionPerformed(java.awt.event.ActionEvent evt) {
194  saveButtonActionPerformed(evt);
195  }
196  });
197 
198  descriptionTextArea.setEditable(false);
199  descriptionTextArea.setBackground(new java.awt.Color(240, 240, 240));
200  descriptionTextArea.setColumns(20);
201  descriptionTextArea.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
202  descriptionTextArea.setLineWrap(true);
203  descriptionTextArea.setRows(5);
204  descriptionTextArea.setWrapStyleWord(true);
205  descriptionTextArea.setEnabled(false);
206  descriptionScrollPane.setViewportView(descriptionTextArea);
207 
208  org.openide.awt.Mnemonics.setLocalizedText(configLabel, org.openide.util.NbBundle.getMessage(ConfigVisualPanel3.class, "ConfigVisualPanel3.configLabel.text")); // NOI18N
209 
210  org.openide.awt.Mnemonics.setLocalizedText(executableLabel, org.openide.util.NbBundle.getMessage(ConfigVisualPanel3.class, "ConfigVisualPanel3.executableLabel.text")); // NOI18N
211 
212  org.openide.awt.Mnemonics.setLocalizedText(executableStatusLabel, org.openide.util.NbBundle.getMessage(ConfigVisualPanel3.class, "ConfigVisualPanel3.executableStatusLabel.text")); // NOI18N
213 
214  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
215  this.setLayout(layout);
216  layout.setHorizontalGroup(
217  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
218  .addGroup(layout.createSequentialGroup()
219  .addContainerGap()
220  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
221  .addGroup(layout.createSequentialGroup()
222  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
223  .addComponent(configLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
224  .addComponent(executableLabel))
225  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
226  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
227  .addComponent(configStatusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE)
228  .addComponent(executableStatusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE))
229  .addGap(0, 10, Short.MAX_VALUE))
230  .addGroup(layout.createSequentialGroup()
231  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
232  .addComponent(descriptionScrollPane)
233  .addGroup(layout.createSequentialGroup()
234  .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
235  .addGap(0, 0, Short.MAX_VALUE)))
236  .addContainerGap())))
237  );
238  layout.setVerticalGroup(
239  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
240  .addGroup(layout.createSequentialGroup()
241  .addContainerGap()
242  .addComponent(descriptionScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
243  .addGap(18, 18, 18)
244  .addComponent(saveButton)
245  .addGap(18, 18, 18)
246  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
247  .addComponent(configLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
248  .addComponent(configStatusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
249  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
250  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
251  .addComponent(executableLabel)
252  .addComponent(executableStatusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
253  .addContainerGap(120, Short.MAX_VALUE))
254  );
255  }// </editor-fold>//GEN-END:initComponents
256 
257  private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
258  saveConfigFile();
259  }//GEN-LAST:event_saveButtonActionPerformed
260 
261 
262  // Variables declaration - do not modify//GEN-BEGIN:variables
263  private javax.swing.JLabel configStatusLabel;
264  private javax.swing.JScrollPane descriptionScrollPane;
265  private javax.swing.JTextArea descriptionTextArea;
266  private javax.swing.JLabel executableStatusLabel;
267  // End of variables declaration//GEN-END:variables
268 
275  @NbBundle.Messages({
276  "# {0} - configurationLocation",
277  "ConfigVisualPanel3.description.text=Press Save to write the imaging tool and configuration file to the destination.\nDestination: {0}"
278  })
279  void setConfigInfoForSaving(String configFile, LogicalImagerConfig config) {
280  this.configFilename = configFile;
281  this.config = config;
282  descriptionTextArea.setText(Bundle.ConfigVisualPanel3_description_text(FilenameUtils.getFullPath(configFilename)));
283  }
284 }

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.