19 package org.sleuthkit.autopsy.keywordsearch;
21 import java.io.BufferedReader;
22 import java.util.HashMap;
24 import java.util.logging.Level;
25 import org.apache.solr.client.solrj.SolrServerException;
26 import org.apache.solr.common.SolrInputDocument;
27 import org.openide.util.NbBundle;
55 private static final Logger logger = Logger.getLogger(Ingester.class.getName());
56 private volatile boolean uncommitedIngests =
false;
57 private final Server solrServer = KeywordSearch.getServer();
58 private static final SolrFieldsVisitor SOLR_FIELDS_VISITOR =
new SolrFieldsVisitor();
59 private static Ingester instance;
60 private static final int SINGLE_READ_CHARS = 512;
65 public static synchronized Ingester getDefault() {
66 if (instance == null) {
67 instance =
new Ingester();
74 @SuppressWarnings(
"FinalizeDeclaration")
75 protected
void finalize() throws Throwable {
79 if (uncommitedIngests) {
80 logger.warning(
"Ingester was used to add files that it never committed.");
94 void indexMetaDataOnly(AbstractFile file)
throws IngesterException {
95 indexChunk(
"", file.getName(), getContentFields(file));
108 void indexMetaDataOnly(BlackboardArtifact artifact)
throws IngesterException {
109 indexChunk(
"",
new ArtifactTextExtractor().getName(artifact), getContentFields(artifact));
120 private Map<String, String> getContentFields(SleuthkitVisitableItem item) {
121 return item.accept(SOLR_FIELDS_VISITOR);
144 < T extends SleuthkitVisitableItem>
boolean indexText(TextExtractor< T> extractor, T source, IngestJobContext context)
throws Ingester.IngesterException {
145 final long sourceID = extractor.getID(source);
146 final String sourceName = extractor.getName(source);
150 if (extractor.isDisabled()) {
158 Map<String, String> fields = getContentFields(source);
160 try (BufferedReader reader =
new BufferedReader(extractor.getReader(source));) {
161 Chunker chunker =
new Chunker(reader);
162 for (Chunk chunk : chunker) {
163 if (context != null && context.fileIngestIsCancelled()) {
164 logger.log(Level.INFO,
"File ingest cancelled. Cancelling keyword search indexing of {0}", sourceName);
167 String chunkId = Server.getChunkIdString(sourceID, numChunks + 1);
168 fields.put(Server.Schema.ID.toString(), chunkId);
169 fields.put(Server.Schema.CHUNK_SIZE.toString(), String.valueOf(chunk.getBaseChunkLength()));
172 indexChunk(chunk.toString(), sourceName, fields);
174 }
catch (Ingester.IngesterException ingEx) {
175 extractor.logWarning(
"Ingester had a problem with extracted string from file '"
176 + sourceName +
"' (id: " + sourceID +
").", ingEx);
181 if (chunker.hasException()) {
182 extractor.logWarning(
"Error chunking content from " + sourceID +
": " + sourceName, chunker.getException());
185 }
catch (Exception ex) {
186 extractor.logWarning(
"Unexpected error, can't read content stream from " + sourceID +
": " + sourceName, ex);
189 if (context != null && context.fileIngestIsCancelled()) {
193 fields.put(Server.Schema.NUM_CHUNKS.toString(), Integer.toString(numChunks));
195 fields.put(Server.Schema.ID.toString(), Long.toString(sourceID));
197 fields.remove(Server.Schema.CHUNK_SIZE.toString());
198 indexChunk(null, sourceName, fields);
217 private void indexChunk(String chunk, String sourceName, Map<String, String> fields)
throws IngesterException {
218 if (fields.get(Server.Schema.IMAGE_ID.toString()) == null) {
223 String msg = NbBundle.getMessage(Ingester.class,
224 "Ingester.ingest.exception.unknownImgId.msg", sourceName);
225 logger.log(Level.SEVERE, msg);
226 throw new IngesterException(msg);
230 SolrInputDocument updateDoc =
new SolrInputDocument();
231 for (String key : fields.keySet()) {
232 updateDoc.addField(key, fields.get(key));
236 updateDoc.addField(Server.Schema.CONTENT.toString(), chunk);
240 TimingMetric metric = EnterpriseHealthMonitor.getTimingMetric(
"Solr: Index chunk");
241 solrServer.addDocument(updateDoc);
242 EnterpriseHealthMonitor.submitTimingMetric(metric);
243 uncommitedIngests =
true;
245 }
catch (KeywordSearchModuleException | NoOpenCoreException ex) {
247 throw new IngesterException(
248 NbBundle.getMessage(Ingester.class,
"Ingester.ingest.exception.err.msg", sourceName), ex);
259 uncommitedIngests =
false;
260 }
catch (NoOpenCoreException | SolrServerException ex) {
261 logger.log(Level.WARNING,
"Error commiting index", ex);
269 static private class SolrFieldsVisitor extends SleuthkitItemVisitor.Default<Map<String, String>> {
272 protected Map<String, String>
defaultVisit(SleuthkitVisitableItem svi) {
273 return new HashMap<>();
277 public Map<String, String>
visit(File f) {
282 public Map<String, String>
visit(DerivedFile df) {
287 public Map<String, String>
visit(Directory d) {
292 public Map<String, String>
visit(LocalDirectory ld) {
297 public Map<String, String>
visit(LayoutFile lf) {
303 public Map<String, String>
visit(LocalFile lf) {
308 public Map<String, String>
visit(SlackFile f) {
339 Map<String, String> params =
new HashMap<>();
340 params.put(
Server.
Schema.ID.toString(), Long.toString(file.getId()));
342 params.put(
Server.
Schema.IMAGE_ID.toString(), Long.toString(file.getDataSource().getId()));
343 }
catch (TskCoreException ex) {
344 logger.log(Level.SEVERE,
"Could not get data source id to properly index the file " + file.getId(), ex);
345 params.put(
Server.
Schema.IMAGE_ID.toString(), Long.toString(-1));
347 params.put(
Server.
Schema.FILE_NAME.toString(), file.getName());
359 public Map<String, String>
visit(BlackboardArtifact artifact) {
360 Map<String, String> params =
new HashMap<>();
361 params.put(
Server.
Schema.ID.toString(), Long.toString(artifact.getArtifactID()));
363 params.put(
Server.
Schema.IMAGE_ID.toString(), Long.toString(ArtifactTextExtractor.getDataSource(artifact).getId()));
364 }
catch (TskCoreException ex) {
365 logger.log(Level.SEVERE,
"Could not get data source id to properly index the artifact " + artifact.getArtifactID(), ex);
366 params.put(
Server.
Schema.IMAGE_ID.toString(), Long.toString(-1));
379 public Map<String, String>
visit(Report report) {
380 Map<String, String> params =
new HashMap<>();
381 params.put(
Server.
Schema.ID.toString(), Long.toString(report.getId()));
383 Content dataSource = report.getDataSource();
384 if (null == dataSource) {
385 params.put(
Server.
Schema.IMAGE_ID.toString(), Long.toString(-1));
387 params.put(
Server.
Schema.IMAGE_ID.toString(), Long.toString(dataSource.getId()));
389 }
catch (TskCoreException ex) {
390 logger.log(Level.SEVERE,
"Could not get data source id to properly index the report, using default value. Id: " + report.getId(), ex);
391 params.put(
Server.
Schema.IMAGE_ID.toString(), Long.toString(-1));
401 static class IngesterException
extends Exception {
403 private static final long serialVersionUID = 1L;
405 IngesterException(String message, Throwable ex) {
409 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)