You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ti...@apache.org on 2026/04/01 04:41:15 UTC

(tika) branch main updated: [TIKA-4704] delete intermediate file and directory; improve javadoc; use jdk11 methods; use try-with-resources (#2728)

This is an automated email from the ASF dual-hosted git repository.

tilman pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tika.git


The following commit(s) were added to refs/heads/main by this push:
     new bc57575ffb [TIKA-4704] delete intermediate file and directory; improve javadoc; use jdk11 methods; use try-with-resources (#2728)
bc57575ffb is described below

commit bc57575ffbaff5f6b360a8b2c5e35e2ad34f7a6f
Author: Tilman Hausherr <ti...@snafu.de>
AuthorDate: Wed Apr 1 06:41:09 2026 +0200

    [TIKA-4704] delete intermediate file and directory; improve javadoc; use jdk11 methods; use try-with-resources (#2728)
    
    * [TIKA-4704] delete intermediate file; improve javadoc; use jdk11 methods
    
    * Update tika-server/tika-server-core/src/test/java/org/apache/tika/server/core/CXFTestBase.java
    
    Co-authored-by: Copilot <17...@users.noreply.github.com>
    
    * Refactor InputStream handling with try-with-resources; add forgotten deletion
    
    * Refactor CXFTestBase.java for logging and formatting
    
    Removed logging of config file deletion and improved formatting in CXFTestBase.java.
    
    * Also delete inputTempDirectory when all done
    
    ---------
    
    Co-authored-by: Copilot <17...@users.noreply.github.com>
---
 .../org/apache/tika/server/core/CXFTestBase.java   | 65 +++++++++++++---------
 1 file changed, 38 insertions(+), 27 deletions(-)

diff --git a/tika-server/tika-server-core/src/test/java/org/apache/tika/server/core/CXFTestBase.java b/tika-server/tika-server-core/src/test/java/org/apache/tika/server/core/CXFTestBase.java
index e0f454e5c3..69c14521af 100644
--- a/tika-server/tika-server-core/src/test/java/org/apache/tika/server/core/CXFTestBase.java
+++ b/tika-server/tika-server-core/src/test/java/org/apache/tika/server/core/CXFTestBase.java
@@ -101,6 +101,7 @@ public abstract class CXFTestBase {
     protected TikaLoader tika;
     private PipesParser pipesParser;
     private Path pipesConfigPath;
+    private Path inputTempDirectory = null;
 
     public static void createPluginsConfig(Path configPath, Path inputDir, Path jsonOutputDir, Path bytesOutputDir, Long timeoutMillis) throws IOException {
 
@@ -151,10 +152,10 @@ public abstract class CXFTestBase {
 
     public static InputStream gzip(InputStream is) throws IOException {
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        OutputStream gz = new GzipCompressorOutputStream(bos);
-        IOUtils.copy(is, gz);
-        gz.flush();
-        gz.close();
+        try (OutputStream gz = new GzipCompressorOutputStream(bos)) {
+            is.transferTo(gz);
+            gz.flush();
+        }
         return new ByteArrayInputStream(bos.toByteArray());
     }
 
@@ -185,23 +186,26 @@ public abstract class CXFTestBase {
             // Copy tika config to temp file first
             Files.copy(getTikaConfigInputStream(), tmp, StandardCopyOption.REPLACE_EXISTING);
 
-            InputStream pipesConfigInputStream = getPipesConfigInputStream();
-            if (pipesConfigInputStream != null) {
-                // Test provided its own pipes config - merge in PASSBACK_ALL emit strategy
-                this.pipesConfigPath = mergePassbackAllStrategy(pipesConfigInputStream);
-            } else {
-                // Create a default pipes config, merging metadata-filters from tika config
-                this.pipesConfigPath = createDefaultTestConfig(tmp);
+            Path tmpPipesConfigPath;
+            try (InputStream pipesConfigInputStream = getPipesConfigInputStream()) {
+                if (pipesConfigInputStream != null) {
+                    // Test provided its own pipes config - merge in PASSBACK_ALL emit strategy
+                    tmpPipesConfigPath = mergePassbackAllStrategy(pipesConfigInputStream);
+                } else {
+                    // Create a default pipes config, merging metadata-filters from tika config
+                    tmpPipesConfigPath = createDefaultTestConfig(tmp);
+                }
             }
 
             this.tika = TikaLoader.load(tmp);
 
             // Create input temp directory for pipes-based parsing
-            Path inputTempDirectory = Files.createTempDirectory("tika-server-test-input-");
+            inputTempDirectory = Files.createTempDirectory("tika-server-test-input-");
 
             // Initialize PipesParsingHelper for pipes-based parsing
             // Merge the fetcher config with basePath pointing to the temp directory
-            this.pipesConfigPath = mergeFetcherConfig(this.pipesConfigPath, inputTempDirectory);
+            this.pipesConfigPath = mergeFetcherConfig(tmpPipesConfigPath, inputTempDirectory);
+            Files.delete(tmpPipesConfigPath);
             TikaJsonConfig tikaJsonConfig = TikaJsonConfig.load(this.pipesConfigPath);
             PipesConfig pipesConfig = tikaJsonConfig.deserialize("pipes", PipesConfig.class);
             if (pipesConfig == null) {
@@ -243,6 +247,8 @@ public abstract class CXFTestBase {
     /**
      * Merges PASSBACK_ALL emit strategy into a pipes config.
      * This ensures the child process uses PASSBACK_ALL regardless of what's in the config file.
+     * 
+     * @return JSON config file in the temp directory.
      */
     private Path mergePassbackAllStrategy(InputStream pipesConfigInputStream) throws IOException {
         ObjectMapper mapper = new ObjectMapper();
@@ -268,6 +274,8 @@ public abstract class CXFTestBase {
     /**
      * Merges the tika-server-fetcher configuration into the pipes config.
      * The fetcher is configured with basePath pointing to the input temp directory.
+     * 
+     * @return new (different) JSON configPath in the temp directory.
      */
     private Path mergeFetcherConfig(Path configPath, Path inputTempDirectory) throws IOException {
         ObjectMapper mapper = new ObjectMapper();
@@ -301,6 +309,8 @@ public abstract class CXFTestBase {
      * If the tika config contains metadata-filters, they are merged into the pipes config.
      *
      * @param tikaConfigPath path to the tika config (may contain metadata-filters)
+     * 
+     * @return JSON config file in the temp directory.
      */
     private Path createDefaultTestConfig(Path tikaConfigPath) throws IOException {
         Path pluginsDir = Paths.get("target/plugins").toAbsolutePath();
@@ -417,6 +427,9 @@ public abstract class CXFTestBase {
                 LOG.warn("Error deleting config file", e);
             }
         }
+        if (inputTempDirectory != null) {
+            Files.deleteIfExists(inputTempDirectory);
+        }
     }
 
     protected Map<String, String> readZipArchive(InputStream inputStream) throws IOException {
@@ -424,14 +437,15 @@ public abstract class CXFTestBase {
         Path tempFile = null;
         try {
             tempFile = writeTemporaryArchiveFile(inputStream, "zip");
-            try (ZipFile zip = ZipFile.builder().setPath(tempFile).get())
-            {
+            try (ZipFile zip = ZipFile.builder().setPath(tempFile).get()) {
                 Enumeration<ZipArchiveEntry> entries = zip.getEntries();
                 while (entries.hasMoreElements()) {
                     ZipArchiveEntry entry = entries.nextElement();
-                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
-                    IOUtils.copy(zip.getInputStream(entry), bos);
-                    data.put(entry.getName(), DigestUtils.md5Hex(bos.toByteArray()));
+                    try (InputStream zis = zip.getInputStream(entry)) {
+                        byte[] bytes = zis.readAllBytes();
+                        data.put(entry.getName(), DigestUtils.md5Hex(bytes));
+                    }
+                    
                 }
             }
         } finally {
@@ -447,14 +461,14 @@ public abstract class CXFTestBase {
         Path tempFile = null;
         try {
             tempFile = writeTemporaryArchiveFile(inputStream, "zip");
-            try (ZipFile zip = ZipFile.builder().setPath(tempFile).get())
-            {
+            try (ZipFile zip = ZipFile.builder().setPath(tempFile).get())  {
                 Enumeration<ZipArchiveEntry> entries = zip.getEntries();
                 while (entries.hasMoreElements()) {
                     ZipArchiveEntry entry = entries.nextElement();
-                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
-                    IOUtils.copy(zip.getInputStream(entry), bos);
-                    data.put(entry.getName(), bos.toByteArray());
+                    try (InputStream zis = zip.getInputStream(entry)) {
+                        byte[] bytes = zis.readAllBytes();
+                        data.put(entry.getName(), bytes);
+                    }
                 }
             }
         } finally {
@@ -472,10 +486,7 @@ public abstract class CXFTestBase {
             if (entry == null) {
                 break;
             }
-
-            ByteArrayOutputStream bos = new ByteArrayOutputStream();
-            IOUtils.copy(zip, bos);
-            data.put(entry.getName(), DigestUtils.md5Hex(bos.toByteArray()));
+            data.put(entry.getName(), DigestUtils.md5Hex(zip.readAllBytes()));
         }
 
         return data;