|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.tika.parser.pdf; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | + |
| 22 | +import java.io.ByteArrayInputStream; |
| 23 | +import java.io.IOException; |
| 24 | +import java.nio.file.Path; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Random; |
| 29 | + |
| 30 | +import org.apache.pdfbox.cos.COSName; |
| 31 | +import org.apache.pdfbox.pdmodel.PDDocument; |
| 32 | +import org.apache.pdfbox.pdmodel.PDDocumentNameDictionary; |
| 33 | +import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode; |
| 34 | +import org.apache.pdfbox.pdmodel.PDPage; |
| 35 | +import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification; |
| 36 | +import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | +import org.junit.jupiter.api.io.TempDir; |
| 39 | +import org.xml.sax.ContentHandler; |
| 40 | +import org.xml.sax.helpers.DefaultHandler; |
| 41 | + |
| 42 | +import org.apache.tika.TikaTest; |
| 43 | +import org.apache.tika.extractor.EmbeddedDocumentExtractor; |
| 44 | +import org.apache.tika.io.TikaInputStream; |
| 45 | +import org.apache.tika.metadata.Metadata; |
| 46 | +import org.apache.tika.parser.ParseContext; |
| 47 | + |
| 48 | +/** |
| 49 | + * A PDF attachment's bytes are in the document already. Rewinding the stream |
| 50 | + * handed to the embedded-document extractor -- which digesting does for every |
| 51 | + * embedded document -- must re-open (re-decode) the attachment from the |
| 52 | + * document, not cache a copy of it and spill that copy to a temp file. |
| 53 | + * <p> |
| 54 | + * The payload is over the 1 MB a cache keeps in memory, so a cached stream has |
| 55 | + * to spill to rewind and the difference is observable. The assertion is on the |
| 56 | + * stream the extractor is handed: the parser owns the child's |
| 57 | + * {@code TemporaryResources}, so a watched directory would pass either way. |
| 58 | + */ |
| 59 | +public class PDFEmbeddedFileNoTempFileTest extends TikaTest { |
| 60 | + |
| 61 | + private static final int PAYLOAD_LENGTH = 2 * 1024 * 1024; |
| 62 | + |
| 63 | + @TempDir |
| 64 | + Path tempDir; |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testAttachmentIsNotSpooled() throws Exception { |
| 68 | + Path pdf = tempDir.resolve("attachment.pdf"); |
| 69 | + byte[] payload = payload(); |
| 70 | + try (PDDocument doc = new PDDocument()) { |
| 71 | + doc.addPage(new PDPage()); |
| 72 | + //Flate-encoded, as real attachments are: a rewind has to re-decode |
| 73 | + PDEmbeddedFile file = new PDEmbeddedFile(doc, new ByteArrayInputStream(payload), |
| 74 | + COSName.FLATE_DECODE); |
| 75 | + file.setSize(payload.length); |
| 76 | + PDComplexFileSpecification spec = new PDComplexFileSpecification(); |
| 77 | + spec.setFile("attachment.bin"); |
| 78 | + spec.setEmbeddedFile(file); |
| 79 | + PDEmbeddedFilesNameTreeNode tree = new PDEmbeddedFilesNameTreeNode(); |
| 80 | + tree.setNames(Map.of("attachment.bin", spec)); |
| 81 | + PDDocumentNameDictionary names = new PDDocumentNameDictionary(doc.getDocumentCatalog()); |
| 82 | + names.setEmbeddedFiles(tree); |
| 83 | + doc.getDocumentCatalog().setNames(names); |
| 84 | + doc.save(pdf.toFile()); |
| 85 | + } |
| 86 | + |
| 87 | + RecordingExtractor extractor = new RecordingExtractor(); |
| 88 | + ParseContext context = new ParseContext(); |
| 89 | + context.set(EmbeddedDocumentExtractor.class, extractor); |
| 90 | + Metadata metadata = new Metadata(); |
| 91 | + try (TikaInputStream tis = TikaInputStream.get(pdf, metadata)) { |
| 92 | + new PDFParser().parse(tis, new DefaultHandler(), metadata, context); |
| 93 | + } |
| 94 | + |
| 95 | + assertTrue(extractor.lengths.contains(PAYLOAD_LENGTH), |
| 96 | + "the attachment reached the extractor in full; saw " + extractor.lengths); |
| 97 | + for (int i = 0; i < extractor.spooled.size(); i++) { |
| 98 | + assertEquals(false, extractor.spooled.get(i), "embedded stream " + i + " (" |
| 99 | + + extractor.lengths.get(i) |
| 100 | + + " bytes) was spooled to disk to rewind instead of re-opened"); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** Incompressible filler, so Flate keeps it at full size. */ |
| 105 | + private static byte[] payload() { |
| 106 | + byte[] bytes = new byte[PAYLOAD_LENGTH]; |
| 107 | + new Random(4878).nextBytes(bytes); |
| 108 | + return bytes; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Rewinds each embedded stream the way a digester does, then records whether |
| 113 | + * that left it backed by a temp file and how many bytes it still yields. |
| 114 | + */ |
| 115 | + private static class RecordingExtractor implements EmbeddedDocumentExtractor { |
| 116 | + private final List<Boolean> spooled = new ArrayList<>(); |
| 117 | + private final List<Integer> lengths = new ArrayList<>(); |
| 118 | + |
| 119 | + @Override |
| 120 | + public boolean shouldParseEmbedded(Metadata metadata, ParseContext context) { |
| 121 | + return true; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public void parseEmbedded(TikaInputStream stream, ContentHandler handler, |
| 126 | + Metadata metadata, ParseContext context, boolean outputHtml) |
| 127 | + throws IOException { |
| 128 | + stream.enableRewind(); |
| 129 | + stream.readAllBytes(); |
| 130 | + stream.rewind(); |
| 131 | + spooled.add(stream.hasFile()); |
| 132 | + lengths.add(stream.readAllBytes().length); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments