Formatted file, & minor cleanup

This commit is contained in:
Raman 2018-01-11 10:17:33 -05:00
parent 30793eb51f
commit 1f44f08783
2 changed files with 86 additions and 80 deletions

View File

@ -45,7 +45,7 @@ public class FileViewer extends javax.swing.JPanel implements DataContentViewer
// TBD: This hardcoded list of viewers should be replaced with a dynamic lookup // TBD: This hardcoded list of viewers should be replaced with a dynamic lookup
private static final FileTypeViewer[] KNOWN_VIEWERS = new FileTypeViewer[]{ private static final FileTypeViewer[] KNOWN_VIEWERS = new FileTypeViewer[]{
new JPEGViewerDummy(), // this if for testing only // new JPEGViewerDummy(), // this if for testing only
new SQLiteViewer() new SQLiteViewer()
}; };

View File

@ -1,7 +1,20 @@
/* /*
* To change this license header, choose License Headers in Project Properties. * Autopsy Forensic Browser
* To change this template file, choose Tools | Templates *
* and open the template in the editor. * Copyright 2018 Basis Technology Corp.
* Contact: carrier <at> sleuthkit <dot> org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ */
package org.sleuthkit.autopsy.contentviewers; package org.sleuthkit.autopsy.contentviewers;
@ -22,30 +35,28 @@ import java.util.concurrent.ExecutionException;
import javax.swing.JComboBox; import javax.swing.JComboBox;
import javax.swing.SwingWorker; import javax.swing.SwingWorker;
import org.sleuthkit.autopsy.casemodule.Case; import org.sleuthkit.autopsy.casemodule.Case;
import org.sleuthkit.autopsy.centralrepository.datamodel.EamOrganization;
import org.sleuthkit.autopsy.coreutils.Logger; import org.sleuthkit.autopsy.coreutils.Logger;
import org.sleuthkit.autopsy.datamodel.ContentUtils; import org.sleuthkit.autopsy.datamodel.ContentUtils;
import org.sleuthkit.datamodel.AbstractFile; import org.sleuthkit.datamodel.AbstractFile;
public class SQLiteViewer extends javax.swing.JPanel implements FileTypeViewer { public class SQLiteViewer extends javax.swing.JPanel implements FileTypeViewer {
public static final String[] SUPPORTED_MIMETYPES = new String[]{"application/x-sqlite3"}; public static final String[] SUPPORTED_MIMETYPES = new String[]{"application/x-sqlite3"};
private static final Logger LOGGER = Logger.getLogger(FileViewer.class.getName()); private static final Logger LOGGER = Logger.getLogger(FileViewer.class.getName());
private Connection connection = null; private Connection connection = null;
private String tmpDBPathName = null; private String tmpDBPathName = null;
private File tmpDBFile = null; private File tmpDBFile = null;
// TBD: Change the value to be a Array of ColDefs // TBD: Change the value to be a Array of ColDefs
Map<String, String> dbTablesMap = new TreeMap<>(); Map<String, String> dbTablesMap = new TreeMap<>();
/** /**
* Creates new form SQLiteViewer * Creates new form SQLiteViewer
*/ */
public SQLiteViewer() { public SQLiteViewer() {
initComponents(); initComponents();
customizeComponents(); customizeComponents();
} }
@ -164,7 +175,7 @@ public class SQLiteViewer extends javax.swing.JPanel implements FileTypeViewer {
if (null == tableName) { if (null == tableName) {
return; return;
} }
readTable(tableName); readTable(tableName);
}//GEN-LAST:event_tablesDropdownListActionPerformed }//GEN-LAST:event_tablesDropdownListActionPerformed
@ -181,12 +192,12 @@ public class SQLiteViewer extends javax.swing.JPanel implements FileTypeViewer {
@Override @Override
public List<String> getSupportedMIMETypes() { public List<String> getSupportedMIMETypes() {
return Arrays.asList(SUPPORTED_MIMETYPES); return Arrays.asList(SUPPORTED_MIMETYPES);
} }
@Override @Override
public void setFile(AbstractFile file) { public void setFile(AbstractFile file) {
processSQLiteFile( file); processSQLiteFile(file);
} }
@Override @Override
@ -196,13 +207,13 @@ public class SQLiteViewer extends javax.swing.JPanel implements FileTypeViewer {
@Override @Override
public void resetComponent() { public void resetComponent() {
dbTablesMap.clear(); dbTablesMap.clear();
tablesDropdownList.setEnabled(true); tablesDropdownList.setEnabled(true);
tablesDropdownList.removeAllItems(); tablesDropdownList.removeAllItems();
numEntriesField.setText(""); numEntriesField.setText("");
// close DB connection to file // close DB connection to file
if (null != connection) { if (null != connection) {
try { try {
@ -212,23 +223,23 @@ public class SQLiteViewer extends javax.swing.JPanel implements FileTypeViewer {
LOGGER.log(Level.SEVERE, "Failed to close DB connection to file.", ex); //NON-NLS LOGGER.log(Level.SEVERE, "Failed to close DB connection to file.", ex); //NON-NLS
} }
} }
// delete last temp file // delete last temp file
if (null != tmpDBFile) { if (null != tmpDBFile) {
tmpDBFile.delete(); tmpDBFile.delete();
tmpDBFile = null; tmpDBFile = null;
} }
} }
private void customizeComponents() { private void customizeComponents() {
// add a actionListener to jTablesComboBox // add a actionListener to jTablesComboBox
} }
/** /**
* Process the given SQLite DB file * Process the given SQLite DB file
* *
* @param sqliteFile - * @param sqliteFile -
* *
* @return none * @return none
*/ */
@ -239,15 +250,13 @@ public class SQLiteViewer extends javax.swing.JPanel implements FileTypeViewer {
new SwingWorker<Boolean, Void>() { new SwingWorker<Boolean, Void>() {
@Override @Override
protected Boolean doInBackground() throws Exception { protected Boolean doInBackground() throws Exception {
try { try {
// Copy the file to temp folder
tmpDBPathName = Case.getCurrentCase().getTempDirectory() + File.separator + sqliteFile.getName() + "-" + sqliteFile.getId(); tmpDBPathName = Case.getCurrentCase().getTempDirectory() + File.separator + sqliteFile.getName() + "-" + sqliteFile.getId();
tmpDBFile = new File(tmpDBPathName); tmpDBFile = new File(tmpDBPathName);
// Copy the file to temp folder
ContentUtils.writeToFile(sqliteFile, tmpDBFile); ContentUtils.writeToFile(sqliteFile, tmpDBFile);
System.out.println("RAMAN: SQLite file copied to: " + tmpDBPathName);
// Open copy using JDBC // Open copy using JDBC
Class.forName("org.sqlite.JDBC"); //NON-NLS //load JDBC driver Class.forName("org.sqlite.JDBC"); //NON-NLS //load JDBC driver
connection = DriverManager.getConnection("jdbc:sqlite:" + tmpDBPathName); //NON-NLS connection = DriverManager.getConnection("jdbc:sqlite:" + tmpDBPathName); //NON-NLS
@ -266,18 +275,17 @@ public class SQLiteViewer extends javax.swing.JPanel implements FileTypeViewer {
@Override @Override
protected void done() { protected void done() {
super.done(); super.done();
try { try {
boolean status = get(); boolean status = get();
if ( (status == true) && (dbTablesMap.size() > 0) ) { if ((status == true) && (dbTablesMap.size() > 0)) {
dbTablesMap.keySet().forEach((tableName) -> { dbTablesMap.keySet().forEach((tableName) -> {
tablesDropdownList.addItem(tableName); tablesDropdownList.addItem(tableName);
}); });
} } else {
else {
// Populate error message // Populate error message
tablesDropdownList.addItem("No tables found"); tablesDropdownList.addItem("No tables found");
tablesDropdownList.setEnabled(false); tablesDropdownList.setEnabled(false);
} }
} catch (InterruptedException | ExecutionException ex) { } catch (InterruptedException | ExecutionException ex) {
LOGGER.log(Level.SEVERE, "Unexpected exception while opening DB file", ex); //NON-NLS LOGGER.log(Level.SEVERE, "Unexpected exception while opening DB file", ex); //NON-NLS
@ -286,68 +294,66 @@ public class SQLiteViewer extends javax.swing.JPanel implements FileTypeViewer {
}.execute(); }.execute();
} }
/** /**
* Gets the table names and their schema from loaded SQLite db file * Gets the table names and their schema from loaded SQLite db file
* *
* @return true if success, false otherwise * @return true if success, false otherwise
*/ */
private boolean getTables() { private boolean getTables() {
try { try {
Statement statement = connection.createStatement(); Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(
"SELECT name, sql FROM sqlite_master "
+ " WHERE type= 'table' "
+ " ORDER BY name;"); //NON-NLS
while (resultSet.next()) { ResultSet resultSet = statement.executeQuery(
String tableName = resultSet.getString("name"); //NON-NLS "SELECT name, sql FROM sqlite_master "
String tableSQL = resultSet.getString("sql"); //NON-NLS + " WHERE type= 'table' "
+ " ORDER BY name;"); //NON-NLS
dbTablesMap.put(tableName, tableSQL);
String query = "PRAGMA table_info(" + tableName + ")"; //NON-NLS while (resultSet.next()) {
ResultSet rs2; String tableName = resultSet.getString("name"); //NON-NLS
try { String tableSQL = resultSet.getString("sql"); //NON-NLS
Statement statement2 = connection.createStatement();
rs2 = statement2.executeQuery(query); dbTablesMap.put(tableName, tableSQL);
while (rs2.next()) { String query = "PRAGMA table_info(" + tableName + ")"; //NON-NLS
ResultSet rs2;
System.out.println("RAMAN: Col Name = " + rs2.getString("name") ); try {
System.out.println("RAMAN: Col Type = " + rs2.getString("type") ); Statement statement2 = connection.createStatement();
rs2 = statement2.executeQuery(query);
// RAMAN TBD: parse and save the table schema while (rs2.next()) {
}
} catch (Exception ex) { // System.out.println("RAMAN: Col Name = " + rs2.getString("name"));
LOGGER.log(Level.WARNING, "Error while trying to get columns from sqlite db." + connection, ex); //NON-NLS // System.out.println("RAMAN: Col Type = " + rs2.getString("type"));
// RAMAN TBD: parse and save the table schema
} }
} catch (Exception ex) {
LOGGER.log(Level.WARNING, "Error while trying to get columns from sqlite db." + connection, ex); //NON-NLS
} }
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error getting table names from the DB", e); //NON-NLS
} }
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error getting table names from the DB", e); //NON-NLS
}
return true; return true;
} }
private void readTable(String tableName) { private void readTable(String tableName) {
System.out.println("RAMAN: selected table = " + tableName);
// TBD: need to handle cancelling if one is already in progress // TBD: need to handle cancelling if one is already in progress
new SwingWorker<Integer, Void>() { new SwingWorker<Integer, Void>() {
@Override @Override
protected Integer doInBackground() throws Exception { protected Integer doInBackground() throws Exception {
try { try {
Statement statement = connection.createStatement(); Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery( ResultSet resultSet = statement.executeQuery(
"SELECT COUNT(*) as count FROM " + tableName ); //NON-NLS "SELECT COUNT(*) as count FROM " + tableName); //NON-NLS
System.out.println("Row count = " + resultSet.getInt("count") ); // TBD: read the rows here and popluate the ExplorerManager.
// Read all table names and schema
return resultSet.getInt("count"); return resultSet.getInt("count");
}catch (SQLException ex) { } catch (SQLException ex) {
LOGGER.log(Level.SEVERE, "Failed to Open DB.", ex); //NON-NLS LOGGER.log(Level.SEVERE, "Failed to get data for table.", ex); //NON-NLS
} }
//NON-NLS //NON-NLS
return 0; return 0;
@ -355,35 +361,35 @@ public class SQLiteViewer extends javax.swing.JPanel implements FileTypeViewer {
@Override @Override
protected void done() { protected void done() {
super.done(); super.done();
try { try {
int numRows = get(); int numRows = get();
numEntriesField.setText( numRows + " entries"); numEntriesField.setText(numRows + " entries");
} catch (InterruptedException | ExecutionException ex) { } catch (InterruptedException | ExecutionException ex) {
LOGGER.log(Level.SEVERE, "Unexpected exception while reading table.", ex); //NON-NLS LOGGER.log(Level.SEVERE, "Unexpected exception while reading table.", ex); //NON-NLS
} }
} }
}.execute(); }.execute();
} }
enum SQLStorageClass { enum SQLStorageClass {
NULL, NULL,
INTEGER, INTEGER,
REAL, REAL,
TEXT, TEXT,
BLOB BLOB
}; };
private class SQLColDef { private class SQLColDef {
private final String colName; private final String colName;
private final SQLStorageClass storageClass; private final SQLStorageClass storageClass;
SQLColDef(String colName, SQLStorageClass sc ) { SQLColDef(String colName, SQLStorageClass sc) {
this.colName = colName; this.colName = colName;
this.storageClass = sc; this.storageClass = sc;
} }
} }
} }