19 package org.sleuthkit.autopsy.keywordsearch;
21 import java.io.BufferedReader;
22 import java.io.Reader;
23 import java.util.Collections;
24 import java.util.HashMap;
26 import java.util.Optional;
27 import java.util.logging.Level;
28 import org.apache.commons.lang3.math.NumberUtils;
29 import org.apache.solr.client.solrj.SolrServerException;
30 import org.apache.solr.common.SolrInputDocument;
31 import org.openide.util.NbBundle;
59 private static final Logger logger = Logger.getLogger(Ingester.class.getName());
60 private volatile boolean uncommitedIngests =
false;
61 private final Server solrServer = KeywordSearch.getServer();
62 private static final SolrFieldsVisitor SOLR_FIELDS_VISITOR =
new SolrFieldsVisitor();
63 private static Ingester instance;
64 private final LanguageSpecificContentIndexingHelper languageSpecificContentIndexingHelper
65 =
new LanguageSpecificContentIndexingHelper();
70 public static synchronized Ingester getDefault() {
71 if (instance == null) {
72 instance =
new Ingester();
79 @SuppressWarnings(
"FinalizeDeclaration")
80 protected
void finalize() throws Throwable {
84 if (uncommitedIngests) {
85 logger.warning(
"Ingester was used to add files that it never committed.");
99 void indexMetaDataOnly(AbstractFile file)
throws IngesterException {
100 indexChunk(
"",
"", file.getName().toLowerCase(),
new HashMap<>(getContentFields(file)));
113 void indexMetaDataOnly(BlackboardArtifact artifact, String sourceName)
throws IngesterException {
114 indexChunk(
"",
"", sourceName,
new HashMap<>(getContentFields(artifact)));
125 private Map<String, String> getContentFields(SleuthkitVisitableItem item) {
126 return item.accept(SOLR_FIELDS_VISITOR);
147 < T extends SleuthkitVisitableItem>
boolean indexText(Reader sourceReader,
long sourceID, String sourceName, T source, IngestJobContext context)
throws Ingester.IngesterException {
150 Map<String, String> contentFields = Collections.unmodifiableMap(getContentFields(source));
152 try (BufferedReader reader =
new BufferedReader(sourceReader)) {
153 Chunker chunker =
new Chunker(reader);
154 while (chunker.hasNext()) {
155 if (context != null && context.fileIngestIsCancelled()) {
156 logger.log(Level.INFO,
"File ingest cancelled. Cancelling keyword search indexing of {0}", sourceName);
160 Chunk chunk = chunker.next();
161 Map<String, Object> fields =
new HashMap<>(contentFields);
162 String chunkId = Server.getChunkIdString(sourceID, numChunks + 1);
163 fields.put(Server.Schema.ID.toString(), chunkId);
164 fields.put(Server.Schema.CHUNK_SIZE.toString(), String.valueOf(chunk.getBaseChunkLength()));
165 Optional<Language> language = languageSpecificContentIndexingHelper.detectLanguageIfNeeded(chunk);
166 language.ifPresent(lang -> languageSpecificContentIndexingHelper.updateLanguageSpecificFields(fields, chunk, lang));
169 indexChunk(chunk.toString(), chunk.geLowerCasedChunk(), sourceName, fields);
171 if (chunker.hasNext() && language.isPresent()) {
172 languageSpecificContentIndexingHelper.indexMiniChunk(chunk, sourceName,
new HashMap<>(contentFields), chunkId, language.get());
175 }
catch (Ingester.IngesterException ingEx) {
176 logger.log(Level.WARNING,
"Ingester had a problem with extracted string from file '"
177 + sourceName +
"' (id: " + sourceID +
").", ingEx);
182 if (chunker.hasException()) {
183 logger.log(Level.WARNING,
"Error chunking content from " + sourceID +
": " + sourceName, chunker.getException());
186 }
catch (Exception ex) {
187 logger.log(Level.WARNING,
"Unexpected error, can't read content stream from " + sourceID +
": " + sourceName, ex);
190 if (context != null && context.fileIngestIsCancelled()) {
193 Map<String, Object> fields =
new HashMap<>(contentFields);
195 fields.put(Server.Schema.NUM_CHUNKS.toString(), Integer.toString(numChunks));
197 fields.put(Server.Schema.ID.toString(), Long.toString(sourceID));
199 fields.remove(Server.Schema.CHUNK_SIZE.toString());
200 indexChunk(null, null, sourceName, fields);
220 private void indexChunk(String chunk, String lowerCasedChunk, String sourceName, Map<String, Object> fields)
throws IngesterException {
221 if (fields.get(Server.Schema.IMAGE_ID.toString()) == null) {
226 String msg = NbBundle.getMessage(Ingester.class,
227 "Ingester.ingest.exception.unknownImgId.msg", sourceName);
228 logger.log(Level.SEVERE, msg);
229 throw new IngesterException(msg);
233 SolrInputDocument updateDoc =
new SolrInputDocument();
234 for (String key : fields.keySet()) {
235 updateDoc.addField(key, fields.get(key));
243 updateDoc.addField(Server.Schema.CONTENT.toString(), chunk);
247 double indexSchemaVersion = NumberUtils.toDouble(solrServer.getIndexInfo().getSchemaVersion());
248 if (indexSchemaVersion >= 2.1) {
249 updateDoc.addField(Server.Schema.CONTENT_STR.toString(), ((chunk == null) ?
"" : lowerCasedChunk));
252 TimingMetric metric = HealthMonitor.getTimingMetric(
"Solr: Index chunk");
254 solrServer.addDocument(updateDoc);
255 HealthMonitor.submitTimingMetric(metric);
256 uncommitedIngests =
true;
258 }
catch (KeywordSearchModuleException | NoOpenCoreException ex) {
260 throw new IngesterException(
261 NbBundle.getMessage(Ingester.class,
"Ingester.ingest.exception.err.msg", sourceName), ex);
272 uncommitedIngests =
false;
273 }
catch (NoOpenCoreException | SolrServerException ex) {
274 logger.log(Level.WARNING,
"Error commiting index", ex);
282 static private class SolrFieldsVisitor extends SleuthkitItemVisitor.Default<Map<String, String>> {
285 protected Map<String, String>
defaultVisit(SleuthkitVisitableItem svi) {
286 return new HashMap<>();
290 public Map<String, String>
visit(File f) {
295 public Map<String, String>
visit(DerivedFile df) {
300 public Map<String, String>
visit(Directory d) {
305 public Map<String, String>
visit(LocalDirectory ld) {
310 public Map<String, String>
visit(LayoutFile lf) {
316 public Map<String, String>
visit(LocalFile lf) {
321 public Map<String, String>
visit(SlackFile f) {
352 Map<String, String> params =
new HashMap<>();
353 params.put(
Server.
Schema.ID.toString(), Long.toString(file.getId()));
355 params.put(
Server.
Schema.IMAGE_ID.toString(), Long.toString(file.getDataSource().getId()));
356 }
catch (TskCoreException ex) {
357 logger.log(Level.SEVERE,
"Could not get data source id to properly index the file " + file.getId(), ex);
358 params.put(
Server.
Schema.IMAGE_ID.toString(), Long.toString(-1));
360 params.put(
Server.
Schema.FILE_NAME.toString(), file.getName().toLowerCase());
372 public Map<String, String>
visit(BlackboardArtifact artifact) {
373 Map<String, String> params =
new HashMap<>();
374 params.put(
Server.
Schema.ID.toString(), Long.toString(artifact.getArtifactID()));
376 params.put(
Server.
Schema.IMAGE_ID.toString(), Long.toString(artifact.getDataSource().getId()));
377 }
catch (TskCoreException ex) {
378 logger.log(Level.SEVERE,
"Could not get data source id to properly index the artifact " + artifact.getArtifactID(), ex);
379 params.put(
Server.
Schema.IMAGE_ID.toString(), Long.toString(-1));
392 public Map<String, String>
visit(Report report) {
393 Map<String, String> params =
new HashMap<>();
394 params.put(
Server.
Schema.ID.toString(), Long.toString(report.getId()));
396 Content dataSource = report.getDataSource();
397 if (null == dataSource) {
398 params.put(
Server.
Schema.IMAGE_ID.toString(), Long.toString(-1));
400 params.put(
Server.
Schema.IMAGE_ID.toString(), Long.toString(dataSource.getId()));
402 }
catch (TskCoreException ex) {
403 logger.log(Level.SEVERE,
"Could not get data source id to properly index the report, using default value. Id: " + report.getId(), ex);
404 params.put(
Server.
Schema.IMAGE_ID.toString(), Long.toString(-1));
414 static class IngesterException
extends Exception {
416 private static final long serialVersionUID = 1L;
418 IngesterException(String message, Throwable ex) {
422 IngesterException(String message) {
Map< String, String > visit(Report report)
Map< String, String > visit(LayoutFile lf)
Map< String, String > visit(File f)
Map< String, String > visit(LocalDirectory ld)
Map< String, String > getCommonAndMACTimeFields(AbstractFile file)
Map< String, String > visit(SlackFile f)
Map< String, String > visit(Directory d)
static String getStringTimeISO8601(long epochSeconds, TimeZone tzone)
Map< String, String > getCommonFields(AbstractFile file)
Map< String, String > visit(DerivedFile df)
Map< String, String > visit(BlackboardArtifact artifact)
Map< String, String > visit(LocalFile lf)
Map< String, String > defaultVisit(SleuthkitVisitableItem svi)