Autopsy  4.20.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
TikaTextExtractor.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2018-2021 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.textextractors;
20 
21 import com.google.common.io.CharSource;
22 import com.google.common.util.concurrent.ThreadFactoryBuilder;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.PushbackReader;
29 import java.io.Reader;
30 import java.nio.file.Paths;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Objects;
34 import java.util.Map;
35 import java.util.concurrent.Callable;
36 import java.util.concurrent.ExecutorService;
37 import java.util.concurrent.Executors;
38 import java.util.concurrent.Future;
39 import java.util.concurrent.ThreadFactory;
40 import java.util.concurrent.TimeUnit;
41 import java.util.concurrent.TimeoutException;
42 import java.util.logging.Level;
43 import java.util.stream.Collectors;
44 import org.apache.tika.Tika;
45 import org.apache.tika.exception.TikaException;
46 import org.apache.tika.metadata.Metadata;
47 import org.apache.tika.parser.AutoDetectParser;
48 import org.apache.tika.parser.ParseContext;
49 import org.apache.tika.parser.Parser;
50 import org.apache.tika.parser.ParsingReader;
51 import org.apache.tika.parser.microsoft.OfficeParserConfig;
52 import org.apache.tika.parser.ocr.TesseractOCRConfig;
53 import org.apache.tika.parser.pdf.PDFParserConfig;
54 import org.openide.util.NbBundle;
55 import org.openide.modules.InstalledFileLocator;
56 import org.openide.util.Lookup;
66 import org.sleuthkit.datamodel.AbstractFile;
67 import org.sleuthkit.datamodel.Content;
68 import org.sleuthkit.datamodel.ReadContentInputStream;
69 import org.xml.sax.ContentHandler;
70 import org.xml.sax.SAXException;
71 import org.xml.sax.helpers.DefaultHandler;
72 import com.google.common.collect.ImmutableMap;
73 import com.google.common.collect.ImmutableSet;
74 import java.io.InputStreamReader;
75 import java.nio.charset.Charset;
76 import java.util.ArrayList;
77 import java.util.Set;
78 import org.apache.tika.mime.MimeTypes;
79 import org.apache.tika.parser.pdf.PDFParserConfig.OCR_STRATEGY;
82 
87 final class TikaTextExtractor implements TextExtractor {
88 
89  //Mimetype groups to aassist extractor implementations in ignoring binary and
90  //archive files.
91  private static final Set<String> BINARY_MIME_TYPES
92  = ImmutableSet.of(
93  //ignore binary blob data, for which string extraction will be used
94  "application/octet-stream", //NON-NLS
95  "application/x-msdownload"); //NON-NLS
96 
101  private static final Set<String> ARCHIVE_MIME_TYPES
102  = ImmutableSet.of(
103  //ignore unstructured binary and compressed data, for which string extraction or unzipper works better
104  "application/x-7z-compressed", //NON-NLS
105  "application/x-ace-compressed", //NON-NLS
106  "application/x-alz-compressed", //NON-NLS
107  "application/x-arj", //NON-NLS
108  "application/vnd.ms-cab-compressed", //NON-NLS
109  "application/x-cfs-compressed", //NON-NLS
110  "application/x-dgc-compressed", //NON-NLS
111  "application/x-apple-diskimage", //NON-NLS
112  "application/x-gca-compressed", //NON-NLS
113  "application/x-dar", //NON-NLS
114  "application/x-lzx", //NON-NLS
115  "application/x-lzh", //NON-NLS
116  "application/x-rar-compressed", //NON-NLS
117  "application/x-stuffit", //NON-NLS
118  "application/x-stuffitx", //NON-NLS
119  "application/x-gtar", //NON-NLS
120  "application/x-archive", //NON-NLS
121  "application/x-executable", //NON-NLS
122  "application/x-gzip", //NON-NLS
123  "application/zip", //NON-NLS
124  "application/x-zoo", //NON-NLS
125  "application/x-cpio", //NON-NLS
126  "application/x-shar", //NON-NLS
127  "application/x-tar", //NON-NLS
128  "application/x-bzip", //NON-NLS
129  "application/x-bzip2", //NON-NLS
130  "application/x-lzip", //NON-NLS
131  "application/x-lzma", //NON-NLS
132  "application/x-lzop", //NON-NLS
133  "application/x-z", //NON-NLS
134  "application/x-compress"); //NON-NLS
135 
136  // Used to log to the tika file that is why it uses the java.util.logging.logger class instead of the Autopsy one
137  private static final java.util.logging.Logger TIKA_LOGGER = java.util.logging.Logger.getLogger("Tika"); //NON-NLS
138  private static final Logger AUTOPSY_LOGGER = Logger.getLogger(TikaTextExtractor.class.getName());
139 
140  private final ThreadFactory tikaThreadFactory
141  = new ThreadFactoryBuilder().setNameFormat("tika-reader-%d").build();
142  private final ExecutorService executorService = Executors.newSingleThreadExecutor(tikaThreadFactory);
143  private static final String SQLITE_MIMETYPE = "application/x-sqlite3";
144 
145  private final AutoDetectParser parser = new AutoDetectParser();
146  private final FileTypeDetector fileTypeDetector;
147  private final Content content;
148 
149  private boolean tesseractOCREnabled;
150  private static final String TESSERACT_DIR_NAME = "Tesseract-OCR"; //NON-NLS
151  private static final String TESSERACT_EXECUTABLE = "tesseract.exe"; //NON-NLS
152  private static final File TESSERACT_PATH = locateTesseractExecutable();
153  private String languagePacks = formatLanguagePacks(PlatformUtil.getOcrLanguagePacks());
154  private static final String TESSERACT_OUTPUT_FILE_NAME = "tess_output"; //NON-NLS
155 
156  // documents where OCR is performed
157  private static final ImmutableSet<String> OCR_DOCUMENTS = ImmutableSet.of(
158  "application/pdf",
159  "application/msword",
160  "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
161  "application/vnd.ms-powerpoint",
162  "application/vnd.openxmlformats-officedocument.presentationml.presentation",
163  "application/vnd.ms-excel",
164  "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
165  );
166 
167  private static final String IMAGE_MIME_TYPE_PREFIX = "image/";
168 
169  private Map<String, String> metadataMap;
170 
171  private ProcessTerminator processTerminator;
172 
173  private static final List<String> TIKA_SUPPORTED_TYPES
174  = new Tika().getParser().getSupportedTypes(new ParseContext())
175  .stream()
176  .map(mt -> mt.getType() + "/" + mt.getSubtype())
177  .collect(Collectors.toList());
178 
179  TikaTextExtractor(Content content) {
180  this.content = content;
181 
182  FileTypeDetector detector = null;
183  try {
184  detector = new FileTypeDetector();
185  } catch (FileTypeDetector.FileTypeDetectorInitException ex) {
186  TIKA_LOGGER.log(Level.SEVERE, "Unable to instantiate a file type detector", ex);
187  }
188  this.fileTypeDetector = detector;
189  }
190 
201  private String getMimeType(AbstractFile file) {
202  String mimeType = MimeTypes.OCTET_STREAM;
203  if (fileTypeDetector != null) {
204  mimeType = fileTypeDetector.getMIMEType(file);
205  } else if (file.getMIMEType() != null) {
206  mimeType = file.getMIMEType();
207  }
208 
209  return mimeType.trim().toLowerCase();
210  }
211 
212  @Override
213  public boolean willUseOCR() {
214  if (!isOcrSupported() || (!(content instanceof AbstractFile))) {
215  return false;
216  }
217 
218  String mimeType = getMimeType((AbstractFile) content);
219  // in order to ocr, it needs to either be an image or a document with embedded content
220  return mimeType.startsWith(IMAGE_MIME_TYPE_PREFIX) || OCR_DOCUMENTS.contains(mimeType);
221  }
222 
228  private boolean isOcrSupported() {
229  // If Tesseract has been installed and is set to be used through
230  // configuration, then ocr is enabled. OCR can only currently be run on 64
231  // bit Windows OS.
232  return TESSERACT_PATH != null
233  && tesseractOCREnabled
234  && PlatformUtil.isWindowsOS()
235  && PlatformUtil.is64BitOS()
236  && isSupported();
237  }
238 
250  @Override
251  public Reader getReader() throws InitReaderException {
252  if (!this.isSupported()) {
253  throw new InitReaderException("Content is not supported");
254  }
255 
256  // Only abstract files are supported, see isSupported()
257  final AbstractFile file = ((AbstractFile) content);
258 
259  String mimeType = getMimeType(file);
260 
261  // Handle images seperately so the OCR task can be cancelled.
262  // See JIRA-4519 for the need to have cancellation in the UI and ingest.
263  if (isOcrSupported() && mimeType.startsWith(IMAGE_MIME_TYPE_PREFIX)) {
264  InputStream imageOcrStream = performOCR(file);
265  return new InputStreamReader(imageOcrStream, Charset.forName("UTF-8"));
266  }
267 
268  // Set up Tika
269  final InputStream stream = new ReadContentInputStream(content);
270 
271  final ParseContext parseContext = new ParseContext();
272  // Documents can contain other documents. By adding
273  // the parser back into the context, Tika will recursively
274  // parse embedded documents.
275  parseContext.set(Parser.class, parser);
276  // Use the more memory efficient Tika SAX parsers for DOCX and
277  // PPTX files (it already uses SAX for XLSX).
278  OfficeParserConfig officeParserConfig = new OfficeParserConfig();
279  officeParserConfig.setUseSAXPptxExtractor(true);
280  officeParserConfig.setUseSAXDocxExtractor(true);
281  parseContext.set(OfficeParserConfig.class, officeParserConfig);
282  if (isOcrSupported()) {
283  // Configure OCR for Tika if it chooses to run OCR
284  // during extraction
285  TesseractOCRConfig ocrConfig = new TesseractOCRConfig();
286  String tesseractFolder = TESSERACT_PATH.getParent();
287  ocrConfig.setTesseractPath(tesseractFolder);
288  ocrConfig.setLanguage(languagePacks);
289  ocrConfig.setTessdataPath(PlatformUtil.getOcrLanguagePacksPath());
290  parseContext.set(TesseractOCRConfig.class, ocrConfig);
291 
292  // Configure how Tika handles OCRing PDFs
293  PDFParserConfig pdfConfig = new PDFParserConfig();
294 
295  // This stategy tries to pick between OCRing a page in the
296  // PDF and doing text extraction. It makes this choice by
297  // first running text extraction and then counting characters.
298  // If there are too few characters or too many unmapped
299  // unicode characters, it'll run the entire page through OCR
300  // and take that output instead. See JIRA-6938
301  pdfConfig.setOcrStrategy(OCR_STRATEGY.AUTO);
302  parseContext.set(PDFParserConfig.class, pdfConfig);
303  }
304 
305  Metadata metadata = new Metadata();
306  //Make the creation of a TikaReader a cancellable future in case it takes too long
307  Future<Reader> future = executorService.submit(
308  new GetTikaReader(parser, stream, metadata, parseContext));
309  try {
310  final Reader tikaReader = future.get(getTimeout(content.getSize()), TimeUnit.SECONDS);
311  //check if the reader is empty
312  PushbackReader pushbackReader = new PushbackReader(tikaReader);
313  int read = pushbackReader.read();
314  if (read == -1) {
315  throw new InitReaderException("Unable to extract text: "
316  + "Tika returned empty reader for " + content);
317  }
318  pushbackReader.unread(read);
319 
320  //Save the metadata if it has not been fetched already.
321  if (metadataMap == null) {
322  metadataMap = new HashMap<>();
323  for (String mtdtKey : metadata.names()) {
324  metadataMap.put(mtdtKey, metadata.get(mtdtKey));
325  }
326  }
327 
328  return new ReaderCharSource(pushbackReader).openStream();
329  } catch (TimeoutException te) {
330  final String msg = NbBundle.getMessage(this.getClass(),
331  "AbstractFileTikaTextExtract.index.tikaParseTimeout.text",
332  content.getId(), content.getName());
333  throw new InitReaderException(msg, te);
334  } catch (InitReaderException ex) {
335  throw ex;
336  } catch (Exception ex) {
337  AUTOPSY_LOGGER.log(Level.WARNING, String.format("Error with file [id=%d] %s, see Tika log for details...",
338  content.getId(), content.getName()));
339  TIKA_LOGGER.log(Level.WARNING, "Exception: Unable to Tika parse the "
340  + "content" + content.getId() + ": " + content.getName(),
341  ex.getCause()); //NON-NLS
342  final String msg = NbBundle.getMessage(this.getClass(),
343  "AbstractFileTikaTextExtract.index.exception.tikaParse.msg",
344  content.getId(), content.getName());
345  throw new InitReaderException(msg, ex);
346  } finally {
347  future.cancel(true);
348  }
349  }
350 
361  private InputStream performOCR(AbstractFile file) throws InitReaderException {
362  File inputFile = null;
363  File outputFile = null;
364  try {
365  String tempDirectory = Case.getCurrentCaseThrows().getTempDirectory();
366 
367  //Appending file id makes the name unique
368  String tempFileName = FileUtil.escapeFileName(file.getId() + file.getName());
369  inputFile = Paths.get(tempDirectory, tempFileName).toFile();
370  ContentUtils.writeToFile(content, inputFile);
371 
372  String tempOutputName = FileUtil.escapeFileName(file.getId() + TESSERACT_OUTPUT_FILE_NAME);
373  String outputFilePath = Paths.get(tempDirectory, tempOutputName).toString();
374  String executeablePath = TESSERACT_PATH.toString();
375 
376  //Build tesseract commands
377  ProcessBuilder process = new ProcessBuilder();
378  process.command(executeablePath,
379  String.format("\"%s\"", inputFile.getAbsolutePath()),
380  String.format("\"%s\"", outputFilePath),
381  "--tessdata-dir", PlatformUtil.getOcrLanguagePacksPath(),
382  //language pack command flag
383  "-l", languagePacks);
384 
385  //If the ProcessTerminator was supplied during
386  //configuration apply it here.
387  if (processTerminator != null) {
388  ExecUtil.execute(process, 1, TimeUnit.SECONDS, processTerminator);
389  } else {
390  ExecUtil.execute(process);
391  }
392 
393  outputFile = new File(outputFilePath + ".txt");
394  //Open a stream of the Tesseract text file and send this to Tika
395  return new CleanUpStream(outputFile);
396  } catch (NoCurrentCaseException | IOException ex) {
397  if (outputFile != null) {
398  outputFile.delete();
399  }
400  throw new InitReaderException("Could not successfully run Tesseract", ex);
401  } finally {
402  if (inputFile != null) {
403  inputFile.delete();
404  }
405  }
406  }
407 
412  private class GetTikaReader implements Callable<Reader> {
413 
414  private final AutoDetectParser parser;
415  private final InputStream stream;
416  private final Metadata metadata;
417  private final ParseContext parseContext;
418 
419  GetTikaReader(AutoDetectParser parser, InputStream stream,
420  Metadata metadata, ParseContext parseContext) {
421  this.parser = parser;
422  this.stream = stream;
423  this.metadata = metadata;
424  this.parseContext = parseContext;
425  }
426 
427  @Override
428  public Reader call() throws Exception {
429  return new ParsingReader(parser, stream, metadata, parseContext);
430  }
431  }
432 
438  private class CleanUpStream extends FileInputStream {
439 
440  private File file;
441 
449  CleanUpStream(File file) throws FileNotFoundException {
450  super(file);
451  this.file = file;
452  }
453 
459  @Override
460  public void close() throws IOException {
461  try {
462  super.close();
463  } finally {
464  if (file != null) {
465  file.delete();
466  file = null;
467  }
468  }
469  }
470  }
471 
477  private static File locateTesseractExecutable() {
478  if (!PlatformUtil.isWindowsOS()) {
479  return null;
480  }
481 
482  String executableToFindName = Paths.get(TESSERACT_DIR_NAME, TESSERACT_EXECUTABLE).toString();
483  File exeFile = InstalledFileLocator.getDefault().locate(executableToFindName, TikaTextExtractor.class.getPackage().getName(), false);
484  if (null == exeFile) {
485  return null;
486  }
487 
488  if (!exeFile.canExecute()) {
489  return null;
490  }
491 
492  return exeFile;
493  }
494 
500  @Override
501  public Map<String, String> getMetadata() {
502  if (metadataMap != null) {
503  return ImmutableMap.copyOf(metadataMap);
504  }
505 
506  try {
507  metadataMap = new HashMap<>();
508  InputStream stream = new ReadContentInputStream(content);
509  ContentHandler doNothingContentHandler = new DefaultHandler();
510  Metadata mtdt = new Metadata();
511  parser.parse(stream, doNothingContentHandler, mtdt);
512  for (String mtdtKey : mtdt.names()) {
513  metadataMap.put(mtdtKey, mtdt.get(mtdtKey));
514  }
515  } catch (IOException | SAXException | TikaException ex) {
516  AUTOPSY_LOGGER.log(Level.WARNING, String.format("Error getting metadata for file [id=%d] %s, see Tika log for details...", //NON-NLS
517  content.getId(), content.getName()));
518  TIKA_LOGGER.log(Level.WARNING, "Exception: Unable to get metadata for " //NON-NLS
519  + "content" + content.getId() + ": " + content.getName(), ex); //NON-NLS
520  }
521 
522  return metadataMap;
523  }
524 
530  @Override
531  public boolean isSupported() {
532  if (!(content instanceof AbstractFile)) {
533  return false;
534  }
535 
536  String detectedType = ((AbstractFile) content).getMIMEType();
537  if (detectedType == null
538  || BINARY_MIME_TYPES.contains(detectedType) //any binary unstructured blobs (string extraction will be used)
539  || ARCHIVE_MIME_TYPES.contains(detectedType)
540  || (detectedType.startsWith("video/") && !detectedType.equals("video/x-flv")) //skip video other than flv (tika supports flv only) //NON-NLS
541  || detectedType.equals(SQLITE_MIMETYPE) //Skip sqlite files, Tika cannot handle virtual tables and will fail with an exception. //NON-NLS
542  ) {
543  return false;
544  }
545 
546  return TIKA_SUPPORTED_TYPES.contains(detectedType);
547  }
548 
554  private static String formatLanguagePacks(List<String> languagePacks) {
555  return String.join("+", languagePacks);
556  }
557 
565  private static int getTimeout(long size) {
566  if (size < 1024 * 1024L) //1MB
567  {
568  return 60;
569  } else if (size < 10 * 1024 * 1024L) //10MB
570  {
571  return 1200;
572  } else if (size < 100 * 1024 * 1024L) //100MB
573  {
574  return 3600;
575  } else {
576  return 3 * 3600;
577  }
578 
579  }
580 
590  @Override
591  public void setExtractionSettings(Lookup context) {
592  if (context != null) {
593  List<ProcessTerminator> terminators = new ArrayList<>();
594  ImageConfig configInstance = context.lookup(ImageConfig.class);
595  if (configInstance != null) {
596  this.tesseractOCREnabled = configInstance.getOCREnabled();
597 
598  if (Objects.nonNull(configInstance.getOCRLanguages())) {
599  this.languagePacks = formatLanguagePacks(configInstance.getOCRLanguages());
600  }
601 
602  terminators.add(configInstance.getOCRTimeoutTerminator());
603  }
604 
605  ProcessTerminator terminatorInstance = context.lookup(ProcessTerminator.class);
606  if (terminatorInstance != null) {
607  terminators.add(terminatorInstance);
608  }
609 
610  if (!terminators.isEmpty()) {
611  this.processTerminator = new HybridTerminator(terminators);
612  }
613  }
614  }
615 
620  private static class ReaderCharSource extends CharSource {
621 
622  private final Reader reader;
623 
624  ReaderCharSource(Reader reader) {
625  this.reader = reader;
626  }
627 
628  @Override
629  public Reader openStream() throws IOException {
630  return reader;
631  }
632  }
633 }

Copyright © 2012-2022 Basis Technology. Generated on: Tue Aug 1 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.