EmbeddedDocumentMemoryExtractor { public TikaDocumentSource extract(final TikaDocument rootDocument, final String embeddedDocumentDigest) throws SAXException, TikaException, IOException { ParseContext context = new ParseContext(); ContentHandler handler = new BodyContentHandler(-1); context.set(Parser.class, parser); DigestEmbeddedDocumentExtractor extractor = new DigestEmbeddedDocumentExtractor(rootDocument, embeddedDocumentDigest, context, digester, algorithm); context.set(org.apache.tika.extractor.EmbeddedDocumentExtractor.class, extractor); parser.parse(new FileInputStream(rootDocument.getPath().toFile()), handler, rootDocument.getMetadata(), context); return extractor.getDocument(); } EmbeddedDocumentMemoryExtractor(final UpdatableDigester digester);  EmbeddedDocumentMemoryExtractor(final DigestingParser.Digester digester, String algorithm, boolean ocr); TikaDocumentSource extract(final TikaDocument rootDocument, final String embeddedDocumentDigest);  }
@Test public void test_embedded_file_not_found() throws Exception { try { new EmbeddedDocumentMemoryExtractor(new UpdatableDigester("prj", "SHA-256")). extract(tikaDocument, "unknownDigest"); fail("NullPointerException should have been thrown"); } catch (ContentNotFoundException npe) { assertThat(npe.getMessage()).contains("<unknownDigest> embedded document not found in root document"); assertThat(npe.getMessage()).contains("documents/recursive_embedded.docx"); } }
@Test public void test_embedded_file_extraction_level_1() throws Exception { TikaDocumentSource emfImage = new EmbeddedDocumentMemoryExtractor(new UpdatableDigester("prj", "SHA-256")). extract(tikaDocument, "1eeb334ca60c61baca50b9df851b60c52b856c727932d0d1cae4e56a34190e7e"); assertThat(emfImage).isNotNull(); assertThat(emfImage.content).hasSize(4992); }
@Test public void test_embedded_file_extraction_level_2() throws Exception { TikaDocumentSource textContent = new EmbeddedDocumentMemoryExtractor(new UpdatableDigester("prj", "SHA-256")). extract(tikaDocument, "13d7b88767d478c03a3f9b01649297f254b3f0845ca1728658d7f7b922d28a32"); assertThat(textContent).isNotNull(); assertThat(new String(textContent.content)).isEqualTo("embed_1a"); }
@Test public void test_embedded_file_extraction_level_4() throws Exception { TikaDocumentSource textContent = new EmbeddedDocumentMemoryExtractor(new UpdatableDigester("prj", "SHA-256")). extract(tikaDocument, "c6eb226439d6a1a38ad23d69a98b2f321024eeb9ba32cd0238cc39a3d44b5130"); assertThat(textContent).isNotNull(); assertThat(new String(textContent.content)).isEqualTo("embed_4"); }
@Test public void test_embedded_file_extraction_level_2_sha384() throws Exception { TikaDocument tikaDocument384 = new DocumentFactory().withIdentifier(new DigestIdentifier("SHA-384", Charset.defaultCharset())). create(getClass().getResource("/documents/recursive_embedded.docx")); TikaDocumentSource textContent = new EmbeddedDocumentMemoryExtractor(new UpdatableDigester("prj", "SHA-384")). extract(tikaDocument384, "ad1892526e4c1c1c967390da3a8354b6926b03680156d7d76274e6971248be965bc15998a0260f19b801012227f03fae"); assertThat(textContent).isNotNull(); assertThat(new String(textContent.content)).isEqualTo("embed_2b"); }
@Test public void test_embedded_file_extraction_bug() throws Exception { final Extractor extractor = new Extractor(); extractor.setDigester(new CommonsDigester(1024 * 1024 * 20, "SHA256")); TikaDocument tikaDocument256 = new DocumentFactory().withIdentifier(new DigestIdentifier("SHA-256", Charset.defaultCharset())). create(getClass().getResource("/documents/embedded_file_bug.eml")); TikaDocumentSource pngFile = new EmbeddedDocumentMemoryExtractor(new CommonsDigester(1024 * 1024 * 20, "SHA256"), "SHA-256", false). extract(tikaDocument256, "adfa536a06e9517b61e75fce4751fa4c0b73616f0f8e64fc95518d20811a589f"); assertThat(pngFile).isNotNull(); assertThat(new String(pngFile.content)).hasSize(634); }
@Test public void test_embedded_file_content_extraction_should_have_same_hashes_than_extracted_docs() throws Exception { Extractor extractor = new Extractor(documentFactory); extractor.setDigester(new UpdatableDigester("prj", "SHA-256")); TikaDocument extracted = extractor.extract(Paths.get(getClass().getResource("/documents/recursive_embedded.docx").getPath())); try (Reader reader = extracted.getReader()) { Spewer.toString(reader); } EmbeddedDocumentMemoryExtractor contentExtractor = new EmbeddedDocumentMemoryExtractor( new UpdatableDigester("prj", "SHA-256")); assertThat(extracted.getEmbeds()).hasSize(2); assertThat(contentExtractor.extract(extracted, extracted.getEmbeds().get(0).getId())).isNotNull(); assertThat(contentExtractor.extract(extracted, extracted.getEmbeds().get(1).getId())).isNotNull(); assertThat(contentExtractor.extract(extracted, extracted.getEmbeds().get(1).getEmbeds().get(0).getId())).isNotNull(); }
@Test public void test_extract_embedded_without_ocr() throws Exception { EmbeddedDocumentMemoryExtractor contentExtractor = new EmbeddedDocumentMemoryExtractor( new CommonsDigester(20 * 1024 * 1024, CommonsDigester.DigestAlgorithm.SHA256.toString()), "SHA-256", false); TikaDocumentSource actual = contentExtractor.extract(documentFactory.create(Paths.get(getClass().getResource("/documents/embedded_with_duplicate.tgz").getPath())), "f69680093a1c466d628c7b352a449e8e8a0ce291f61837f9bed9d55ff3658544"); assertThat(new String(actual.content).replace("\n", "")).isEqualTo("level2"); }
@Test public void test_hash_with_ocr_and_without_ocr_is_the_same() throws Exception { EmbeddedDocumentMemoryExtractor ocrExtractor = new EmbeddedDocumentMemoryExtractor( new CommonsDigester(20 * 1024 * 1024, CommonsDigester.DigestAlgorithm.SHA256.toString()), "SHA-256", true); EmbeddedDocumentMemoryExtractor noOcrExtractor = new EmbeddedDocumentMemoryExtractor( new CommonsDigester(20 * 1024 * 1024, CommonsDigester.DigestAlgorithm.SHA256.toString()), "SHA-256", false); assertThat(ocrExtractor.extract(documentFactory.create(Paths.get(getClass().getResource("/documents/embedded_with_duplicate.tgz").getPath())), "f35a3e02f71564f653db8d0115fa5caaff27341f5767096242fd90ad6392b81d")).isNotNull(); assertThat(noOcrExtractor.extract(documentFactory.create(Paths.get(getClass().getResource("/documents/embedded_with_duplicate.tgz").getPath())), "f35a3e02f71564f653db8d0115fa5caaff27341f5767096242fd90ad6392b81d")).isNotNull(); }