Skip to content

Commit e179523

Browse files
authored
TIKA-4474 -- force spooling on ooxml (#2386)
* TIKA-4474: force spool ooxml files * TIKA-4474: handle write limit reached in test
1 parent f76d365 commit e179523

4 files changed

Lines changed: 17 additions & 44 deletions

File tree

tika-core/src/test/java/org/apache/tika/TikaTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@
4242

4343
import org.apache.commons.io.IOUtils;
4444
import org.xml.sax.ContentHandler;
45+
import org.xml.sax.SAXException;
4546

4647
import org.apache.tika.config.TikaConfig;
48+
import org.apache.tika.exception.WriteLimitReachedException;
4749
import org.apache.tika.extractor.EmbeddedResourceHandler;
4850
import org.apache.tika.io.FilenameUtils;
4951
import org.apache.tika.io.TikaInputStream;
@@ -569,10 +571,12 @@ protected String getText(String filePath, Parser parser, Metadata metadata,
569571
public String getText(InputStream is, Parser parser, ParseContext context, Metadata metadata)
570572
throws Exception {
571573
ContentHandler handler = new BodyContentHandler(1000000);
572-
try {
574+
try(is){
573575
parser.parse(is, handler, metadata, context);
574-
} finally {
575-
is.close();
576+
} catch (SAXException e) {
577+
if (!WriteLimitReachedException.isWriteLimitReached(e)) {
578+
throw e;
579+
}
576580
}
577581
return handler.toString();
578582
}

tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-microsoft-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/OOXMLExtractorFactory.java

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ public static void parse(InputStream stream, ContentHandler baseHandler, Metadat
105105
OOXMLExtractor extractor = null;
106106

107107
// Locate or Open the OPCPackage for the file
108-
TikaInputStream tis = TikaInputStream.cast(stream);
109-
if (tis != null && tis.getOpenContainer() instanceof OPCPackageWrapper) {
108+
TikaInputStream tis = TikaInputStream.get(stream);
109+
if (tis.getOpenContainer() instanceof OPCPackageWrapper) {
110110
pkg = ((OPCPackageWrapper) tis.getOpenContainer()).getOPCPackage();
111-
} else if (tis != null && tis.hasFile()) {
111+
} else {
112112
try {
113113
pkg = OPCPackage.open(tis.getFile().getPath(), PackageAccess.READ);
114114
} catch (InvalidOperationException e) {
@@ -117,44 +117,6 @@ public static void parse(InputStream stream, ContentHandler baseHandler, Metadat
117117
pkg = OPCPackage.open(tmpRepairedCopy, PackageAccess.READ);
118118
}
119119
tis.setOpenContainer(new OPCPackageWrapper(pkg));
120-
} else {
121-
//OPCPackage slurps rris into memory so we can close rris
122-
//without apparent problems
123-
mustRevertPackage = true;
124-
try (RereadableInputStream rereadableInputStream = new RereadableInputStream(stream,
125-
MAX_BUFFER_LENGTH, false)) {
126-
try {
127-
pkg = OPCPackage.open(CloseShieldInputStream.wrap(rereadableInputStream));
128-
} catch (UnsupportedZipFeatureException e) {
129-
if (e.getFeature() !=
130-
UnsupportedZipFeatureException.Feature.DATA_DESCRIPTOR) {
131-
throw e;
132-
}
133-
rereadableInputStream.rewind();
134-
tmpRepairedCopy = Files.createTempFile("tika-ooxml-repair-", "").toFile();
135-
ZipSalvager.salvageCopy(rereadableInputStream, tmpRepairedCopy, false);
136-
//if there isn't enough left to be opened as a package
137-
//throw an exception -- we may want to fall back to streaming
138-
//parsing
139-
pkg = OPCPackage.open(tmpRepairedCopy, PackageAccess.READ);
140-
} catch (IOException e) {
141-
if (e instanceof EOFException) {
142-
//keep going
143-
} else if (e instanceof IOException && e.getMessage() != null &&
144-
e.getMessage().contains("Truncated")) {
145-
//keep going
146-
} else {
147-
throw e;
148-
}
149-
rereadableInputStream.rewind();
150-
tmpRepairedCopy = Files.createTempFile("tika-ooxml-repair-", "").toFile();
151-
ZipSalvager.salvageCopy(rereadableInputStream, tmpRepairedCopy, false);
152-
//if there isn't enough left to be opened as a package
153-
//throw an exception -- we may want to fall back to streaming
154-
//parsing
155-
pkg = OPCPackage.open(tmpRepairedCopy, PackageAccess.READ);
156-
}
157-
}
158120
}
159121

160122
if (pkg != null) {

tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-microsoft-module/src/test/java/org/apache/tika/parser/microsoft/ooxml/OOXMLParserTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,4 +1814,11 @@ public void testFeatureExtraction() throws Exception {
18141814
assertEquals("true", m.get(Office.HAS_TRACK_CHANGES));
18151815
assertEquals("true", m.get(Office.HAS_COMMENTS));
18161816
}
1817+
1818+
@Test
1819+
public void testNoRecordSizeOverflow() throws Exception{
1820+
//TIKA-4474 -- test: files (passed as stream) no longer have limit on record size as they are spooled
1821+
String content = getText("testRecordSizeExceeded.xlsx");
1822+
assertContains("Repetitive content pattern 3 for compression test row 1", content);
1823+
}
18171824
}

0 commit comments

Comments
 (0)