1 /*
 2  *
 3  * Autopsy Forensic Browser
 4  * 
 5  * Copyright 2012-2014 Basis Technology Corp.
 6  * 
 7  * Copyright 2012 42six Solutions.
 8  * Contact: aebadirad <at> 42six <dot> com
 9  * Project Contact/Architect: carrier <at> sleuthkit <dot> org
 10  * 
 11  * Licensed under the Apache License, Version 2.0 (the "License");
 12  * you may not use this file except in compliance with the License.
 13  * You may obtain a copy of the License at
 14  * 
 15  * http://www.apache.org/licenses/LICENSE-2.0
 16  * 
 17  * Unless required by applicable law or agreed to in writing, software
 18  * distributed under the License is distributed on an "AS IS" BASIS,
 19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 20  * See the License for the specific language governing permissions and
 21  * limitations under the License.
 22  */
 23 package org.sleuthkit.autopsy.recentactivity;
 24 
 25 import java.sql.ResultSet;
 26 import java.sql.ResultSetMetaData;
 27 import java.sql.SQLException;
 28 import java.util.*;
 29 import java.util.logging.Level;
 30 import org.openide.util.NbBundle;
 
 31 import org.openide.util.NbBundle.Messages;
 
 40 
 41 abstract class Extract {
 42 
 43  protected Case currentCase = Case.getCurrentCase();
 44  protected SleuthkitCase tskCase = currentCase.getSleuthkitCase();
 45  private final Logger logger = Logger.getLogger(this.getClass().getName());
 46  private final ArrayList<String> errorMessages = new ArrayList<>();
 47  String moduleName = "";
 48  boolean dataFound = false;
 49 
 50  Extract() {
 51  }
 52 
 53  void init() throws IngestModuleException {
 54  }
 55 
 56  abstract void process(Content dataSource, IngestJobContext context);
 57 
 58  void complete() {
 59  }
 60 
 66  List<String> getErrorMessages() {
 67  return errorMessages;
 68  }
 69 
 75  protected void addErrorMessage(String message) {
 76  errorMessages.add(message);
 77  }
 78 
 90  protected void addArtifact(BlackboardArtifact.ARTIFACT_TYPE type, AbstractFile content, Collection<BlackboardAttribute> bbattributes) {
 91  try {
 92  BlackboardArtifact bbart = content.newArtifact(type);
 93  bbart.addAttributes(bbattributes);
 94  // index the artifact for keyword search
 95  this.indexArtifact(bbart);
 96  } catch (TskException ex) {
 97  logger.log(Level.SEVERE, "Error while trying to add an artifact", ex); //NON-NLS
 98  }
 99  }
 100  
 106  @Messages({"Extract.indexError.message=Failed to index artifact for keyword search."})
 107  void indexArtifact(BlackboardArtifact bbart) {
 108  Blackboard blackboard = Case.getCurrentCase().getServices().getBlackboard();
 109  try {
 110  // index the artifact for keyword search
 111  blackboard.indexArtifact(bbart);
 112  } catch (Blackboard.BlackboardException ex) {
 113  logger.log(Level.SEVERE, "Unable to index blackboard artifact " + bbart.getDisplayName(), ex); //NON-NLS
 114  MessageNotifyUtil.Notify.error(Bundle.Extract_indexError_message(), bbart.getDisplayName());
 115  }
 116  }
 117 
 129  protected List<HashMap<String, Object>> dbConnect(String path, String query) {
 130  ResultSet temprs;
 131  List<HashMap<String, Object>> list;
 132  String connectionString = "jdbc:sqlite:" + path; //NON-NLS
 133  try {
 134  SQLiteDBConnect tempdbconnect = new SQLiteDBConnect("org.sqlite.JDBC", connectionString); //NON-NLS
 135  temprs = tempdbconnect.executeQry(query);
 136  list = this.resultSetToArrayList(temprs);
 137  tempdbconnect.closeConnection();
 138  } catch (SQLException ex) {
 139  logger.log(Level.SEVERE, "Error while trying to read into a sqlite db." + connectionString, ex); //NON-NLS
 140  errorMessages.add(NbBundle.getMessage(this.getClass(), "Extract.dbConn.errMsg.failedToQueryDb", getName()));
 141  return Collections.<HashMap<String, Object>>emptyList();
 142  }
 143  return list;
 144  }
 145 
 153  private List<HashMap<String, Object>> resultSetToArrayList(ResultSet rs) throws SQLException {
 154  ResultSetMetaData md = rs.getMetaData();
 155  int columns = md.getColumnCount();
 156  List<HashMap<String, Object>> list = new ArrayList<>(50);
 157  while (rs.next()) {
 158  HashMap<String, Object> row = new HashMap<>(columns);
 159  for (int i = 1; i <= columns; ++i) {
 160  if (rs.getObject(i) == null) {
 161  row.put(md.getColumnName(i), "");
 162  } else {
 163  row.put(md.getColumnName(i), rs.getObject(i));
 164  }
 165  }
 166  list.add(row);
 167  }
 168 
 169  return list;
 170  }
 171 
 177  protected String getName() {
 178  return moduleName;
 179  }
 180 
 181  public boolean foundData() {
 182  return dataFound;
 183  }
 184 }