This is an automated email from the ASF dual-hosted git repository.
tilman pushed a commit to branch branch_3x
in repository https://gitbox.apache.org/repos/asf/tika.git
The following commit(s) were added to refs/heads/branch_3x by this push:
new c2e0ff711c [TIKA-4704] use TemporaryResources to avoid leak (#2726)
c2e0ff711c is described below
commit c2e0ff711c410e50c0bf11eb90fe40c3e4508c57
Author: Tilman Hausherr <[email protected]>
AuthorDate: Tue Mar 31 12:17:51 2026 +0200
[TIKA-4704] use TemporaryResources to avoid leak (#2726)
* [TIKA-4704] use TemporaryResources to avoid leak
* Remove TODO comment about temp file leak
Removed TODO comment regarding temporary file leak in ODFParserTest.
* Use try-with-resources for TemporaryResources
* Refactor TemporaryResources usage in OOXMLExtractorFactory
Use try-with-resources for TemporaryResources to ensure proper disposal.
---
.../microsoft/ooxml/OOXMLExtractorFactory.java | 4 +-
.../apache/tika/parser/odf/OpenDocumentParser.java | 71 +++++++++++-----------
.../org/apache/tika/parser/odf/ODFParserTest.java | 1 -
3 files changed, 38 insertions(+), 38 deletions(-)
diff --git
a/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-microsoft-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/OOXMLExtractorFactory.java
b/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-microsoft-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/OOXMLExtractorFactory.java
index 09ac11dc72..7e1e3c0fc6 100644
---
a/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-microsoft-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/OOXMLExtractorFactory.java
+++
b/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-microsoft-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/OOXMLExtractorFactory.java
@@ -98,8 +98,7 @@ public class OOXMLExtractorFactory {
//if the pkg is in the opencontainer of a TikaInputStream, it will get
closed.
//However, if a regular inputstream has been sent in, we need to
revert the pkg.
boolean mustRevertPackage = false;
- TemporaryResources tmp = new TemporaryResources();
- try {
+ try (TemporaryResources tmp = new TemporaryResources()) {
OOXMLExtractor extractor = null;
// Locate or Open the OPCPackage for the file
@@ -228,7 +227,6 @@ public class OOXMLExtractorFactory {
tmpRepairedCopy.getAbsolutePath());
}
}
- tmp.dispose();
}
}
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..f85f71ff35 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,49 @@ 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;
+ try (TemporaryResources tmp = new TemporaryResources()) {
+ 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();
+ }
}
}
diff --git
a/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-miscoffice-module/src/test/java/org/apache/tika/parser/odf/ODFParserTest.java
b/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-miscoffice-module/src/test/java/org/apache/tika/parser/odf/ODFParserTest.java
index 891bbdbdce..72d7d683ab 100644
---
a/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-miscoffice-module/src/test/java/org/apache/tika/parser/odf/ODFParserTest.java
+++
b/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-miscoffice-module/src/test/java/org/apache/tika/parser/odf/ODFParserTest.java
@@ -389,7 +389,6 @@ public class ODFParserTest extends TikaTest {
assertThrows(IOException.class, () -> {
parser.parse(is, handler, metadata, new ParseContext());
});
- //TODO temp file leak
}
}