This is an automated email from the ASF dual-hosted git repository. tilman pushed a commit to branch TIKA-4704-5 in repository https://gitbox.apache.org/repos/asf/tika.git
commit 3a0bffc62485870afcd7466213324d4e8119ec23 Author: Tilman Hausherr <[email protected]> AuthorDate: Tue Mar 31 10:07:58 2026 +0200 [TIKA-4704] use TemporaryResources to avoid leak --- .../apache/tika/parser/odf/OpenDocumentParser.java | 74 ++++++++++++---------- 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-miscoffice-module/src/main/java/org/apache/tika/parser/odf/OpenDocumentParser.java b/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-miscoffice-module/src/main/java/org/apache/tika/parser/odf/OpenDocumentParser.java index 78ecd8916f..f8932383c3 100644 --- a/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-miscoffice-module/src/main/java/org/apache/tika/parser/odf/OpenDocumentParser.java +++ b/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-miscoffice-module/src/main/java/org/apache/tika/parser/odf/OpenDocumentParser.java @@ -38,6 +38,7 @@ import org.apache.tika.config.Field; import org.apache.tika.exception.EncryptedDocumentException; import org.apache.tika.exception.TikaException; import org.apache.tika.extractor.EmbeddedDocumentUtil; +import org.apache.tika.io.TemporaryResources; import org.apache.tika.io.TikaInputStream; import org.apache.tika.metadata.Metadata; import org.apache.tika.metadata.TikaCoreProperties; @@ -131,47 +132,52 @@ public class OpenDocumentParser implements Parser { // Use a File if we can, and an already open zip is even better ZipFile zipFile = null; TikaInputStream tmpTis = null; - if (stream instanceof TikaInputStream) { - TikaInputStream tis = (TikaInputStream) stream; - Object container = ((TikaInputStream) stream).getOpenContainer(); - if (container instanceof ZipFile) { - zipFile = (ZipFile) container; + TemporaryResources tmp = new TemporaryResources(); + try { + if (stream instanceof TikaInputStream) { + TikaInputStream tis = (TikaInputStream) stream; + Object container = ((TikaInputStream) stream).getOpenContainer(); + if (container instanceof ZipFile) { + zipFile = (ZipFile) container; + } else { + zipFile = new ZipFile(tis.getFile()); + tis.setOpenContainer(zipFile); + } } else { - zipFile = new ZipFile(tis.getFile()); - tis.setOpenContainer(zipFile); + tmpTis = TikaInputStream.get(stream, tmp, metadata); + tmpTis.setOpenContainer(new ZipFile(tmpTis.getFile())); + zipFile = (ZipFile) tmpTis.getOpenContainer(); } - } else { - tmpTis = TikaInputStream.get(stream); - tmpTis.setOpenContainer(new ZipFile(tmpTis.getFile())); - zipFile = (ZipFile) tmpTis.getOpenContainer(); - } - // Prepare to handle the content - XHTMLContentHandler xhtml = new XHTMLContentHandler(baseHandler, metadata); - xhtml.startDocument(); - // As we don't know which of the metadata or the content - // we'll hit first, catch the endDocument call initially - EndDocumentShieldingContentHandler handler = new EndDocumentShieldingContentHandler(xhtml); + // Prepare to handle the content + XHTMLContentHandler xhtml = new XHTMLContentHandler(baseHandler, metadata); + xhtml.startDocument(); + // As we don't know which of the metadata or the content + // we'll hit first, catch the endDocument call initially + EndDocumentShieldingContentHandler handler = new EndDocumentShieldingContentHandler(xhtml); - try { try { - handleZipFile(zipFile, metadata, context, handler, embeddedDocumentUtil); - } finally { - //Do we want to close silently == catch an exception here? - if (tmpTis != null) { - //tmpTis handles closing of the open zip container - tmpTis.close(); + try { + handleZipFile(zipFile, metadata, context, handler, embeddedDocumentUtil); + } finally { + //Do we want to close silently == catch an exception here? + if (tmpTis != null) { + //tmpTis handles closing of the open zip container + tmpTis.close(); + } } + } catch (SAXException e) { + if (e.getCause() instanceof EncryptedDocumentException) { + throw (EncryptedDocumentException)e.getCause(); + } + throw e; } - } catch (SAXException e) { - if (e.getCause() instanceof EncryptedDocumentException) { - throw (EncryptedDocumentException)e.getCause(); - } - throw e; - } - // Only now call the end document - if (handler.isEndDocumentWasCalled()) { - handler.reallyEndDocument(); + // Only now call the end document + if (handler.isEndDocumentWasCalled()) { + handler.reallyEndDocument(); + } + } finally { + tmp.dispose(); } }
