19 package org.sleuthkit.autopsy.keywordsearch;
21 import com.google.common.io.CharSource;
22 import java.io.IOException;
23 import java.io.Reader;
24 import java.sql.Connection;
25 import java.sql.DriverManager;
26 import java.sql.ResultSet;
27 import java.sql.ResultSetMetaData;
28 import java.sql.SQLException;
29 import java.sql.Statement;
30 import java.util.Collection;
31 import java.util.Iterator;
32 import java.util.LinkedList;
33 import java.util.logging.Level;
49 class SqliteTextExtractor
extends ContentTextExtractor {
51 private static final String SQLITE_MIMETYPE =
"application/x-sqlite3";
52 private static final Logger logger = Logger.getLogger(SqliteTextExtractor.class.getName());
53 private static final CharSequence EMPTY_CHARACTER_SEQUENCE =
"";
56 boolean isContentTypeSpecific() {
61 public boolean isDisabled() {
66 public void logWarning(String msg, Exception exception) {
67 logger.log(Level.WARNING, msg, exception);
79 boolean isSupported(Content file, String detectedFormat) {
80 return SQLITE_MIMETYPE.equals(detectedFormat);
94 public Reader getReader(Content source)
throws TextExtractorException {
97 if (!AbstractFile.class.isInstance(source)) {
98 return CharSource.wrap(EMPTY_CHARACTER_SEQUENCE).openStream();
100 return new SQLiteTableReader((AbstractFile) source);
101 }
catch (NoCurrentCaseException | IOException | TskCoreException
102 | ClassNotFoundException | SQLException ex) {
103 throw new TextExtractorException(
104 String.format(
"Encountered an issue while trying to initialize "
105 +
"a sqlite table steamer for abstract file with id: [%s], name: "
106 +
"[%s].", source.getId(), source.getName()), ex);
135 IOException, TskCoreException, ClassNotFoundException, SQLException {
140 Class.forName(
"org.sqlite.JDBC");
141 connection = DriverManager.getConnection(
"jdbc:sqlite:" + localDiskPath);
150 private Collection<String>
getTables() throws SQLException {
151 Collection<String> tableNames =
new LinkedList<>();
152 try (Statement statement = connection.createStatement();
153 ResultSet resultSet = statement.executeQuery(
154 "SELECT name FROM sqlite_master "
155 +
" WHERE type= 'table' ")) {
156 while (resultSet.next()) {
157 tableNames.add(resultSet.getString(
"name"));
172 String quotedTableName =
"\"" + tableName +
"\"";
174 try (Statement statement = connection.createStatement();
175 ResultSet resultSet = statement.executeQuery(
176 "SELECT * FROM " + quotedTableName)) {
177 ResultSetMetaData metaData = resultSet.getMetaData();
178 int columnCount = resultSet.getMetaData().getColumnCount();
179 Collection<String> row =
new LinkedList<>();
182 for (
int i = 1; i <= columnCount; i++) {
183 row.add(metaData.getColumnName(i));
187 while (resultSet.next()) {
188 row =
new LinkedList<>();
189 for (
int i = 1; i <= columnCount; i++) {
190 Object result = resultSet.getObject(i);
191 String type = metaData.getColumnTypeName(i);
193 row.add(resultSet.getObject(i).toString());
199 }
catch (SQLException ex) {
200 logger.log(Level.WARNING, String.format(
201 "Error attempting to read file table: [%s]"
202 +
" for file: [%s] (id=%d).", tableName,
203 source.getName(), source.getId()), ex);
221 return result != null && type.compareToIgnoreCase(
"blob") != 0;
238 public int read(
char[] cbuf,
int off,
int len)
throws IOException {
239 if (currentTableReader == null) {
241 if (tableResults == null) {
244 currentTableReader = CharSource.wrap(tableResults).openStream();
247 int charactersRead = currentTableReader.read(cbuf, off, len);
248 while (charactersRead == -1) {
250 if (tableResults == null) {
253 currentTableReader = CharSource.wrap(tableResults).openStream();
254 charactersRead = currentTableReader.read(cbuf, off, len);
257 return charactersRead;
269 if (tableIterator.hasNext()) {
283 public void close() throws IOException {
286 }
catch (SQLException ex) {
289 logger.log(Level.WARNING,
"Could not close JDBC connection", ex);
302 private final StringBuilder
table =
new StringBuilder(DEFAULT_CAPACITY);
304 private static final String
TAB =
"\t";
306 private static final String
SPACE =
" ";
315 table.append(tableName)
334 public void addRow(Collection<String> vals) {
336 vals.forEach((val) -> {
340 table.append(NEW_LINE);
355 return table.toString();
static void findAndCopySQLiteMetaFile(AbstractFile sqliteFile)
static String writeAbstractFileToLocalDisk(AbstractFile file)