Autopsy  4.11.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 2011-2019 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.collect.ImmutableList;
22 import com.google.common.io.CharSource;
23 import com.google.common.util.concurrent.ThreadFactoryBuilder;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.PushbackReader;
30 import java.io.Reader;
31 import java.nio.file.Paths;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Objects;
35 import java.util.Map;
36 import java.util.concurrent.Callable;
37 import java.util.concurrent.ExecutorService;
38 import java.util.concurrent.Executors;
39 import java.util.concurrent.Future;
40 import java.util.concurrent.ThreadFactory;
41 import java.util.concurrent.TimeUnit;
42 import java.util.concurrent.TimeoutException;
43 import java.util.logging.Level;
44 import java.util.stream.Collectors;
45 import org.apache.tika.Tika;
46 import org.apache.tika.exception.TikaException;
47 import org.apache.tika.metadata.Metadata;
48 import org.apache.tika.parser.AutoDetectParser;
49 import org.apache.tika.parser.EmptyParser;
50 import org.apache.tika.parser.ParseContext;
51 import org.apache.tika.parser.Parser;
52 import org.apache.tika.parser.ParsingReader;
53 import org.apache.tika.parser.microsoft.OfficeParserConfig;
54 import org.apache.tika.parser.ocr.TesseractOCRConfig;
55 import org.apache.tika.parser.pdf.PDFParserConfig;
56 import org.openide.util.NbBundle;
57 import org.openide.modules.InstalledFileLocator;
58 import org.openide.util.Lookup;
68 import org.sleuthkit.datamodel.AbstractFile;
69 import org.sleuthkit.datamodel.Content;
70 import org.sleuthkit.datamodel.ReadContentInputStream;
71 import org.xml.sax.ContentHandler;
72 import org.xml.sax.SAXException;
73 import org.xml.sax.helpers.DefaultHandler;
74 import com.google.common.collect.ImmutableMap;
75 
80 final class TikaTextExtractor implements TextExtractor {
81 
82  //Mimetype groups to aassist extractor implementations in ignoring binary and
83  //archive files.
84  private static final List<String> BINARY_MIME_TYPES
85  = ImmutableList.of(
86  //ignore binary blob data, for which string extraction will be used
87  "application/octet-stream", //NON-NLS
88  "application/x-msdownload"); //NON-NLS
89 
94  private static final List<String> ARCHIVE_MIME_TYPES
95  = ImmutableList.of(
96  //ignore unstructured binary and compressed data, for which string extraction or unzipper works better
97  "application/x-7z-compressed", //NON-NLS
98  "application/x-ace-compressed", //NON-NLS
99  "application/x-alz-compressed", //NON-NLS
100  "application/x-arj", //NON-NLS
101  "application/vnd.ms-cab-compressed", //NON-NLS
102  "application/x-cfs-compressed", //NON-NLS
103  "application/x-dgc-compressed", //NON-NLS
104  "application/x-apple-diskimage", //NON-NLS
105  "application/x-gca-compressed", //NON-NLS
106  "application/x-dar", //NON-NLS
107  "application/x-lzx", //NON-NLS
108  "application/x-lzh", //NON-NLS
109  "application/x-rar-compressed", //NON-NLS
110  "application/x-stuffit", //NON-NLS
111  "application/x-stuffitx", //NON-NLS
112  "application/x-gtar", //NON-NLS
113  "application/x-archive", //NON-NLS
114  "application/x-executable", //NON-NLS
115  "application/x-gzip", //NON-NLS
116  "application/zip", //NON-NLS
117  "application/x-zoo", //NON-NLS
118  "application/x-cpio", //NON-NLS
119  "application/x-shar", //NON-NLS
120  "application/x-tar", //NON-NLS
121  "application/x-bzip", //NON-NLS
122  "application/x-bzip2", //NON-NLS
123  "application/x-lzip", //NON-NLS
124  "application/x-lzma", //NON-NLS
125  "application/x-lzop", //NON-NLS
126  "application/x-z", //NON-NLS
127  "application/x-compress"); //NON-NLS
128 
129  //Tika should ignore types with embedded files that can be handled by the unpacking modules
130  private static final List<String> EMBEDDED_FILE_MIME_TYPES
131  = ImmutableList.of("application/msword", //NON-NLS
132  "application/vnd.openxmlformats-officedocument.wordprocessingml.document", //NON-NLS
133  "application/vnd.ms-powerpoint", //NON-NLS
134  "application/vnd.openxmlformats-officedocument.presentationml.presentation", //NON-NLS
135  "application/vnd.ms-excel", //NON-NLS
136  "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", //NON-NLS
137  "application/pdf"); //NON-NLS
138 
139  private static final java.util.logging.Logger TIKA_LOGGER = java.util.logging.Logger.getLogger("Tika"); //NON-NLS
140  private static final Logger AUTOPSY_LOGGER = Logger.getLogger(TikaTextExtractor.class.getName());
141 
142  private final ThreadFactory tikaThreadFactory
143  = new ThreadFactoryBuilder().setNameFormat("tika-reader-%d").build();
144  private final ExecutorService executorService = Executors.newSingleThreadExecutor(tikaThreadFactory);
145  private static final String SQLITE_MIMETYPE = "application/x-sqlite3";
146 
147  private final AutoDetectParser parser = new AutoDetectParser();
148  private final Content content;
149 
150  private boolean tesseractOCREnabled;
151  private static final String TESSERACT_DIR_NAME = "Tesseract-OCR"; //NON-NLS
152  private static final String TESSERACT_EXECUTABLE = "tesseract.exe"; //NON-NLS
153  private static final File TESSERACT_PATH = locateTesseractExecutable();
154  private String languagePacks = formatLanguagePacks(PlatformUtil.getOcrLanguagePacks());
155  private static final String TESSERACT_OUTPUT_FILE_NAME = "tess_output"; //NON-NLS
156  private Map<String, String> metadataMap;
157 
158  private ProcessTerminator processTerminator;
159 
160  private static final List<String> TIKA_SUPPORTED_TYPES
161  = new Tika().getParser().getSupportedTypes(new ParseContext())
162  .stream()
163  .map(mt -> mt.getType() + "/" + mt.getSubtype())
164  .collect(Collectors.toList());
165 
166  public TikaTextExtractor(Content content) {
167  this.content = content;
168  }
169 
177  private boolean ocrEnabled() {
178  return TESSERACT_PATH != null && tesseractOCREnabled
179  && PlatformUtil.isWindowsOS() == true && PlatformUtil.is64BitOS();
180  }
181 
193  @Override
194  public Reader getReader() throws InitReaderException {
195  InputStream stream = null;
196 
197  ParseContext parseContext = new ParseContext();
198 
199  //Disable appending embedded file text to output for EFE supported types
200  //JIRA-4975
201  if(content instanceof AbstractFile && EMBEDDED_FILE_MIME_TYPES.contains(((AbstractFile)content).getMIMEType())) {
202  parseContext.set(Parser.class, new EmptyParser());
203  } else {
204  parseContext.set(Parser.class, parser);
205  }
206 
207  if (ocrEnabled() && content instanceof AbstractFile) {
208  AbstractFile file = ((AbstractFile) content);
209  //Run OCR on images with Tesseract directly.
210  if (file.getMIMEType().toLowerCase().startsWith("image/")) {
211  stream = performOCR(file);
212  } else {
213  //Otherwise, go through Tika for PDFs so that it can
214  //extract images and run Tesseract on them.
215  PDFParserConfig pdfConfig = new PDFParserConfig();
216 
217  // Extracting the inline images and letting Tesseract run on each inline image.
218  // https://wiki.apache.org/tika/PDFParser%20%28Apache%20PDFBox%29
219  // https://tika.apache.org/1.7/api/org/apache/tika/parser/pdf/PDFParserConfig.html
220  pdfConfig.setExtractInlineImages(true);
221  // Multiple pages within a PDF file might refer to the same underlying image.
222  pdfConfig.setExtractUniqueInlineImagesOnly(true);
223  parseContext.set(PDFParserConfig.class, pdfConfig);
224 
225  // Configure Tesseract parser to perform OCR
226  TesseractOCRConfig ocrConfig = new TesseractOCRConfig();
227  String tesseractFolder = TESSERACT_PATH.getParent();
228  ocrConfig.setTesseractPath(tesseractFolder);
229 
230  ocrConfig.setLanguage(languagePacks);
231  ocrConfig.setTessdataPath(PlatformUtil.getOcrLanguagePacksPath());
232  parseContext.set(TesseractOCRConfig.class, ocrConfig);
233 
234  stream = new ReadContentInputStream(content);
235  }
236  } else {
237  stream = new ReadContentInputStream(content);
238  }
239 
240  Metadata metadata = new Metadata();
241  // Use the more memory efficient Tika SAX parsers for DOCX and
242  // PPTX files (it already uses SAX for XLSX).
243  OfficeParserConfig officeParserConfig = new OfficeParserConfig();
244  officeParserConfig.setUseSAXPptxExtractor(true);
245  officeParserConfig.setUseSAXDocxExtractor(true);
246  parseContext.set(OfficeParserConfig.class, officeParserConfig);
247 
248  //Make the creation of a TikaReader a cancellable future in case it takes too long
249  Future<Reader> future = executorService.submit(
250  new GetTikaReader(parser, stream, metadata, parseContext));
251  try {
252  final Reader tikaReader = future.get(getTimeout(content.getSize()), TimeUnit.SECONDS);
253  //check if the reader is empty
254  PushbackReader pushbackReader = new PushbackReader(tikaReader);
255  int read = pushbackReader.read();
256  if (read == -1) {
257  throw new InitReaderException("Unable to extract text: "
258  + "Tika returned empty reader for " + content);
259  }
260  pushbackReader.unread(read);
261 
262  //Save the metadata if it has not been fetched already.
263  if (metadataMap == null) {
264  metadataMap = new HashMap<>();
265  for (String mtdtKey : metadata.names()) {
266  metadataMap.put(mtdtKey, metadata.get(mtdtKey));
267  }
268  }
269 
270  return new ReaderCharSource(pushbackReader).openStream();
271  } catch (TimeoutException te) {
272  final String msg = NbBundle.getMessage(this.getClass(),
273  "AbstractFileTikaTextExtract.index.tikaParseTimeout.text",
274  content.getId(), content.getName());
275  throw new InitReaderException(msg, te);
276  } catch (InitReaderException ex) {
277  throw ex;
278  } catch (Exception ex) {
279  AUTOPSY_LOGGER.log(Level.WARNING, String.format("Error with file [id=%d] %s, see Tika log for details...",
280  content.getId(), content.getName()));
281  TIKA_LOGGER.log(Level.WARNING, "Exception: Unable to Tika parse the "
282  + "content" + content.getId() + ": " + content.getName(),
283  ex.getCause()); //NON-NLS
284  final String msg = NbBundle.getMessage(this.getClass(),
285  "AbstractFileTikaTextExtract.index.exception.tikaParse.msg",
286  content.getId(), content.getName());
287  throw new InitReaderException(msg, ex);
288  } finally {
289  future.cancel(true);
290  }
291  }
292 
303  private InputStream performOCR(AbstractFile file) throws InitReaderException {
304  File inputFile = null;
305  File outputFile = null;
306  try {
307  String tempDirectory = Case.getCurrentCaseThrows().getTempDirectory();
308 
309  //Appending file id makes the name unique
310  String tempFileName = FileUtil.escapeFileName(file.getId() + file.getName());
311  inputFile = Paths.get(tempDirectory, tempFileName).toFile();
312  ContentUtils.writeToFile(content, inputFile);
313 
314  String tempOutputName = FileUtil.escapeFileName(file.getId() + TESSERACT_OUTPUT_FILE_NAME);
315  String outputFilePath = Paths.get(tempDirectory, tempOutputName).toString();
316  String executeablePath = TESSERACT_PATH.toString();
317 
318  //Build tesseract commands
319  ProcessBuilder process = new ProcessBuilder();
320  process.command(executeablePath,
321  String.format("\"%s\"", inputFile.getAbsolutePath()),
322  String.format("\"%s\"", outputFilePath),
323  "--tessdata-dir", PlatformUtil.getOcrLanguagePacksPath(),
324  //language pack command flag
325  "-l", languagePacks);
326 
327  //If the ProcessTerminator was supplied during
328  //configuration apply it here.
329  if (processTerminator != null) {
330  ExecUtil.execute(process, 1, TimeUnit.SECONDS, processTerminator);
331  } else {
332  ExecUtil.execute(process);
333  }
334 
335  outputFile = new File(outputFilePath + ".txt");
336  //Open a stream of the Tesseract text file and send this to Tika
337  return new CleanUpStream(outputFile);
338  } catch (NoCurrentCaseException | IOException ex) {
339  if (outputFile != null) {
340  outputFile.delete();
341  }
342  throw new InitReaderException("Could not successfully run Tesseract", ex);
343  } finally {
344  if (inputFile != null) {
345  inputFile.delete();
346  }
347  }
348  }
349 
354  private class GetTikaReader implements Callable<Reader> {
355 
356  private final AutoDetectParser parser;
357  private final InputStream stream;
358  private final Metadata metadata;
359  private final ParseContext parseContext;
360 
361  public GetTikaReader(AutoDetectParser parser, InputStream stream,
362  Metadata metadata, ParseContext parseContext) {
363  this.parser = parser;
364  this.stream = stream;
365  this.metadata = metadata;
366  this.parseContext = parseContext;
367  }
368 
369  @Override
370  public Reader call() throws Exception {
371  return new ParsingReader(parser, stream, metadata, parseContext);
372  }
373  }
374 
380  private class CleanUpStream extends FileInputStream {
381 
382  private File file;
383 
391  public CleanUpStream(File file) throws FileNotFoundException {
392  super(file);
393  this.file = file;
394  }
395 
401  @Override
402  public void close() throws IOException {
403  try {
404  super.close();
405  } finally {
406  if (file != null) {
407  file.delete();
408  file = null;
409  }
410  }
411  }
412  }
413 
419  private static File locateTesseractExecutable() {
420  if (!PlatformUtil.isWindowsOS()) {
421  return null;
422  }
423 
424  String executableToFindName = Paths.get(TESSERACT_DIR_NAME, TESSERACT_EXECUTABLE).toString();
425  File exeFile = InstalledFileLocator.getDefault().locate(executableToFindName, TikaTextExtractor.class.getPackage().getName(), false);
426  if (null == exeFile) {
427  return null;
428  }
429 
430  if (!exeFile.canExecute()) {
431  return null;
432  }
433 
434  return exeFile;
435  }
436 
442  @Override
443  public Map<String, String> getMetadata() {
444  if (metadataMap != null) {
445  return ImmutableMap.copyOf(metadataMap);
446  }
447 
448  try {
449  metadataMap = new HashMap<>();
450  InputStream stream = new ReadContentInputStream(content);
451  ContentHandler doNothingContentHandler = new DefaultHandler();
452  Metadata mtdt = new Metadata();
453  parser.parse(stream, doNothingContentHandler, mtdt);
454  for (String mtdtKey : mtdt.names()) {
455  metadataMap.put(mtdtKey, mtdt.get(mtdtKey));
456  }
457  } catch (IOException | SAXException | TikaException ex) {
458  AUTOPSY_LOGGER.log(Level.WARNING, String.format("Error getting metadata for file [id=%d] %s, see Tika log for details...", //NON-NLS
459  content.getId(), content.getName()));
460  TIKA_LOGGER.log(Level.WARNING, "Exception: Unable to get metadata for " //NON-NLS
461  + "content" + content.getId() + ": " + content.getName(), ex); //NON-NLS
462  }
463 
464  return metadataMap;
465  }
466 
472  @Override
473  public boolean isSupported() {
474  if (!(content instanceof AbstractFile)) {
475  return false;
476  }
477 
478  String detectedType = ((AbstractFile) content).getMIMEType();
479  if (detectedType == null
480  || BINARY_MIME_TYPES.contains(detectedType) //any binary unstructured blobs (string extraction will be used)
481  || ARCHIVE_MIME_TYPES.contains(detectedType)
482  || (detectedType.startsWith("video/") && !detectedType.equals("video/x-flv")) //skip video other than flv (tika supports flv only) //NON-NLS
483  || detectedType.equals(SQLITE_MIMETYPE) //Skip sqlite files, Tika cannot handle virtual tables and will fail with an exception. //NON-NLS
484  ) {
485  return false;
486  }
487 
488  return TIKA_SUPPORTED_TYPES.contains(detectedType);
489  }
490 
496  private static String formatLanguagePacks(List<String> languagePacks) {
497  return String.join("+", languagePacks);
498  }
499 
507  private static int getTimeout(long size) {
508  if (size < 1024 * 1024L) //1MB
509  {
510  return 60;
511  } else if (size < 10 * 1024 * 1024L) //10MB
512  {
513  return 1200;
514  } else if (size < 100 * 1024 * 1024L) //100MB
515  {
516  return 3600;
517  } else {
518  return 3 * 3600;
519  }
520 
521  }
522 
532  @Override
533  public void setExtractionSettings(Lookup context) {
534  if (context != null) {
535  ImageConfig configInstance = context.lookup(ImageConfig.class);
536  if (configInstance != null) {
537  if (Objects.nonNull(configInstance.getOCREnabled())) {
538  this.tesseractOCREnabled = configInstance.getOCREnabled();
539  }
540 
541  if (Objects.nonNull(configInstance.getOCRLanguages())) {
542  this.languagePacks = formatLanguagePacks(configInstance.getOCRLanguages());
543  }
544  }
545 
546  ProcessTerminator terminatorInstance = context.lookup(ProcessTerminator.class);
547  if (terminatorInstance != null) {
548  this.processTerminator = terminatorInstance;
549  }
550  }
551  }
552 
557  private static class ReaderCharSource extends CharSource {
558 
559  private final Reader reader;
560 
561  ReaderCharSource(Reader reader) {
562  this.reader = reader;
563  }
564 
565  @Override
566  public Reader openStream() throws IOException {
567  return reader;
568  }
569  }
570 }
GetTikaReader(AutoDetectParser parser, InputStream stream, Metadata metadata, ParseContext parseContext)

Copyright © 2012-2018 Basis Technology. Generated on: Fri Jun 21 2019
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.