19 package org.sleuthkit.autopsy.casemodule;
21 import java.awt.Color;
22 import java.beans.PropertyChangeEvent;
23 import java.beans.PropertyChangeListener;
24 import java.io.BufferedReader;
26 import java.io.IOException;
27 import java.io.InputStreamReader;
28 import java.nio.file.Paths;
30 import java.util.concurrent.atomic.AtomicBoolean;
31 import java.util.logging.Level;
32 import javax.swing.JFrame;
33 import javax.swing.SwingWorker;
34 import org.apache.commons.io.FilenameUtils;
35 import org.apache.commons.lang3.StringUtils;
36 import org.openide.modules.InstalledFileLocator;
37 import org.openide.util.NbBundle;
38 import org.openide.windows.WindowManager;
49 @SuppressWarnings(
"PMD.SingularField")
50 class UnpackagePortableCaseProgressDialog extends javax.swing.JDialog implements PropertyChangeListener {
52 private UnpackageWorker worker;
53 private final static String CASES_OPENED_LOG_FILE =
"portable_cases_opened";
54 private final static String PORTABLE_CASE_NAME =
"portable_case_name";
55 private final static String PORTABLE_CASE_DIR =
"portable_case_dir_opened";
60 @NbBundle.Messages({
"UnpackagePortableCaseProgressDialog.title.text=Unpackage Portable Case Progress",})
61 UnpackagePortableCaseProgressDialog() {
62 super((JFrame) WindowManager.getDefault().getMainWindow(),
63 Bundle.UnpackagePortableCaseProgressDialog_title_text(),
66 customizeComponents();
69 private void customizeComponents() {
70 cancelButton.setEnabled(
true);
71 okButton.setEnabled(
false);
72 progressBar.setIndeterminate(
true);
73 resultLabel.setText(
"");
82 void unpackageCase(String packagedCase, String outputFolder) {
84 worker =
new UnpackageWorker(packagedCase, outputFolder);
85 worker.addPropertyChangeListener(
this);
88 setLocationRelativeTo((JFrame) WindowManager.getDefault().getMainWindow());
89 this.setVisible(
true);
102 return worker.isSuccess();
106 @NbBundle.Messages({
"UnpackagePortableCaseProgressDialog.propertyChange.success=Successfully unpacked case",})
108 public void propertyChange(PropertyChangeEvent evt) {
110 if (
"state".equals(evt.getPropertyName())
111 && (SwingWorker.StateValue.DONE.equals(evt.getNewValue()))) {
115 cancelButton.setEnabled(
false);
116 okButton.setEnabled(
true);
118 if (worker.isSuccess()) {
119 progressBar.setIndeterminate(
false);
120 progressBar.setValue(progressBar.getMaximum());
121 resultLabel.setText(Bundle.UnpackagePortableCaseProgressDialog_propertyChange_success());
124 progressBar.setIndeterminate(
false);
125 progressBar.setValue(0);
126 resultLabel.setForeground(Color.red);
127 resultLabel.setText(worker.getDisplayError());
140 private final AtomicBoolean success =
new AtomicBoolean();
141 private String lastError =
"";
144 this.packagedCase = packagedCase;
145 this.outputFolder = outputFolder;
146 this.success.set(
false);
150 "UnpackageWorker.doInBackground.errorFinding7zip=Could not locate 7-Zip executable",
151 "UnpackageWorker.doInBackground.errorCompressingCase=Error unpackaging case",
152 "UnpackageWorker.doInBackground.canceled=Unpackaging canceled by user",
153 "UnpackageWorker.doInBackground.previouslySeenCase=Case has been previously opened. Open it again?",})
158 String caseUnpackedBefore = getCaseIfUnpackedBefore(packagedCase);
159 if ((!caseUnpackedBefore.isEmpty())
166 throw new TskCoreException(
"Error opening case after unpacking it.", ex);
170 File sevenZipExe = locate7ZipExecutable();
171 if (sevenZipExe == null) {
172 setDisplayError(Bundle.UnpackageWorker_doInBackground_errorFinding7zip());
173 throw new TskCoreException(
"Error finding 7-Zip executable");
176 String outputFolderSwitch =
"-o" + outputFolder;
177 ProcessBuilder procBuilder =
new ProcessBuilder();
179 sevenZipExe.getAbsolutePath(),
186 Process process = procBuilder.start();
188 while (process.isAlive()) {
189 if (this.isCancelled()) {
190 setDisplayError(Bundle.UnpackageWorker_doInBackground_canceled());
196 int exitCode = process.exitValue();
199 StringBuilder sb =
new StringBuilder();
200 try (BufferedReader br =
new BufferedReader(
new InputStreamReader(process.getErrorStream()))) {
202 while ((line = br.readLine()) != null) {
203 sb.append(line).append(System.getProperty(
"line.separator"));
207 setDisplayError(Bundle.UnpackageWorker_doInBackground_errorCompressingCase());
208 throw new TskCoreException(
"Error unpackaging case. 7-Zip output: " + sb.toString());
210 }
catch (IOException | InterruptedException ex) {
211 setDisplayError(Bundle.UnpackageWorker_doInBackground_errorCompressingCase());
212 throw new TskCoreException(
"Error unpackaging case", ex);
216 String caseFileDirectory = FilenameUtils.getBaseName(packagedCase);
217 String caseDirectory = StringUtils.substringBefore(caseFileDirectory,
".zip");
218 String caseFileToOpen = outputFolder + File.separator + caseDirectory + File.separator + caseDirectory +
".aut";
230 throw new TskCoreException(
"Error opening case after unpacking it.", ex);
239 synchronized protected void done() {
240 if (this.isCancelled()) {
246 }
catch (Exception ex) {
247 Logger.
getLogger(UnpackagePortableCaseProgressDialog.class.getName()).log(Level.SEVERE,
"Error unpackaging portable case", ex);
257 lastError = errorStr;
270 return success.get();
283 String executableToFindName = Paths.get(
"7-Zip",
"7z.exe").toString();
284 File exeFile = InstalledFileLocator.getDefault().locate(executableToFindName, UnpackagePortableCaseProgressDialog.class.getPackage().getName(),
false);
285 if (null == exeFile) {
289 if (!exeFile.canExecute()) {
306 for (Map.Entry<String, String> entries : configEntries.entrySet()) {
307 if (entries.getValue().contains(packedCaseName)) {
308 String entryFound = entries.getKey().substring(0, entries.getKey().indexOf(
'-'));
310 File caseFile =
new File(caseFileName);
311 if (caseFile.exists()) {
329 @SuppressWarnings(
"unchecked")
331 private
void initComponents() {
333 progressBar =
new javax.swing.JProgressBar();
334 cancelButton =
new javax.swing.JButton();
335 okButton =
new javax.swing.JButton();
336 resultLabel =
new javax.swing.JLabel();
338 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
340 org.openide.awt.Mnemonics.setLocalizedText(cancelButton,
org.openide.util.NbBundle.getMessage(UnpackagePortableCaseProgressDialog.class,
"UnpackagePortableCaseProgressDialog.cancelButton.text"));
341 cancelButton.addActionListener(
new java.awt.event.ActionListener() {
342 public void actionPerformed(java.awt.event.ActionEvent evt) {
343 cancelButtonActionPerformed(evt);
347 org.openide.awt.Mnemonics.setLocalizedText(okButton,
org.openide.util.NbBundle.getMessage(UnpackagePortableCaseProgressDialog.class,
"UnpackagePortableCaseProgressDialog.okButton.text"));
348 okButton.addActionListener(
new java.awt.event.ActionListener() {
349 public void actionPerformed(java.awt.event.ActionEvent evt) {
350 okButtonActionPerformed(evt);
354 org.openide.awt.Mnemonics.setLocalizedText(resultLabel,
org.openide.util.NbBundle.getMessage(UnpackagePortableCaseProgressDialog.class,
"UnpackagePortableCaseProgressDialog.resultLabel.text"));
356 javax.swing.GroupLayout layout =
new javax.swing.GroupLayout(getContentPane());
357 getContentPane().setLayout(layout);
358 layout.setHorizontalGroup(
359 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
360 .addGroup(layout.createSequentialGroup()
362 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
363 .addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
364 .addGroup(layout.createSequentialGroup()
365 .addComponent(resultLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
366 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
367 .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
368 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
369 .addComponent(cancelButton)))
372 layout.setVerticalGroup(
373 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
374 .addGroup(layout.createSequentialGroup()
376 .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
377 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
378 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
379 .addComponent(cancelButton)
380 .addComponent(okButton)
381 .addComponent(resultLabel))
382 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
388 private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
393 private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
398 private javax.swing.JButton cancelButton;
399 private javax.swing.JButton okButton;
400 private javax.swing.JProgressBar progressBar;
401 private javax.swing.JLabel resultLabel;
static synchronized String getConfigSetting(String moduleName, String settingName)
static synchronized boolean makeConfigFile(String moduleName)
static String createTimeStamp()
synchronized String getDisplayError()
static boolean confirm(String message)
File locate7ZipExecutable()
static synchronized void setConfigSetting(String moduleName, String settingName, String settingVal)
final String packagedCase
synchronized void setDisplayError(String errorStr)
static void openAsCurrentCase(String caseMetadataFilePath)
static synchronized boolean configExists(String moduleName)
synchronized static Logger getLogger(String name)
final String outputFolder
String getCaseIfUnpackedBefore(String packedCaseName)
static synchronized Map< String, String > getConfigSettings(String moduleName)