You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ta...@apache.org on 2018/03/09 16:50:56 UTC

[tika] branch master updated: TIKA-2604 -- properly escape (or not) class path in windows and linux environments.

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 1df10c3  TIKA-2604 -- properly escape (or not) class path in windows and linux environments.
1df10c3 is described below

commit 1df10c3ea2fd14e63beebd818540648d162bf150
Author: tballison <ta...@mitre.org>
AuthorDate: Fri Mar 9 11:50:46 2018 -0500

    TIKA-2604 -- properly escape (or not) class path in windows and linux environments.
---
 .../apache/tika/cli/BatchCommandLineBuilder.java   | 27 +++++++++++++++-------
 .../tika/cli/TikaCLIBatchCommandLineTest.java      | 14 ++++++-----
 2 files changed, 27 insertions(+), 14 deletions(-)

diff --git a/tika-app/src/main/java/org/apache/tika/cli/BatchCommandLineBuilder.java b/tika-app/src/main/java/org/apache/tika/cli/BatchCommandLineBuilder.java
index a08311e..2c99414 100644
--- a/tika-app/src/main/java/org/apache/tika/cli/BatchCommandLineBuilder.java
+++ b/tika-app/src/main/java/org/apache/tika/cli/BatchCommandLineBuilder.java
@@ -17,6 +17,9 @@
 
 package org.apache.tika.cli;
 
+
+import org.apache.commons.lang.SystemUtils;
+
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -41,6 +44,7 @@ class BatchCommandLineBuilder {
     static Pattern JVM_OPTS_PATTERN = Pattern.compile("^(--?)J(.+)");
 
     protected static String[] build(String[] args) throws IOException {
+
         Map<String, String> processArgs = new LinkedHashMap<String, String>();
         Map<String, String> jvmOpts = new LinkedHashMap<String,String>();
         //take the args, and divide them into process args and options for
@@ -53,11 +57,6 @@ class BatchCommandLineBuilder {
         //maybe the user specified a different classpath?!
         if (! jvmOpts.containsKey("-cp") && ! jvmOpts.containsKey("--classpath")) {
             String cp = System.getProperty("java.class.path");
-            //need to test for " " on *nix, can't just add double quotes
-            //across platforms.
-            if (cp.contains(" ")){
-                cp = "\""+cp+"\"";
-            }
             jvmOpts.put("-cp", cp);
         }
 
@@ -70,7 +69,7 @@ class BatchCommandLineBuilder {
         }
         //use the log4j config file inside the app /resources/log4j_batch_process.properties
         if (! hasLog4j) {
-            jvmOpts.put("-Dlog4j.configuration=\"log4j_batch_process.properties\"", "");
+            jvmOpts.put("-Dlog4j.configuration=log4j_batch_process.properties", "");
         }
         //now build the full command line
         List<String> fullCommand = new ArrayList<String>();
@@ -79,7 +78,7 @@ class BatchCommandLineBuilder {
         for (Map.Entry<String, String> e : jvmOpts.entrySet()) {
             fullCommand.add(e.getKey());
             if (e.getValue().length() > 0) {
-                fullCommand.add(e.getValue());
+                fullCommand.add(commandLineSafe(e.getValue()));
             }
             if (e.getKey().contains("java.awt.headless")) {
                 foundHeadlessOption = true;
@@ -94,12 +93,24 @@ class BatchCommandLineBuilder {
         for (Map.Entry<String, String> e : processArgs.entrySet()) {
             fullCommand.add(e.getKey());
             if (e.getValue().length() > 0) {
-                fullCommand.add(e.getValue());
+                fullCommand.add(commandLineSafe(e.getValue()));
             }
         }
         return fullCommand.toArray(new String[fullCommand.size()]);
     }
 
+    protected static String commandLineSafe(String arg) {
+        if (arg == null) {
+            return arg;
+        }
+        //need to test for " " on windows, can't just add double quotes
+        //across platforms.
+        if (arg.contains(" ") && SystemUtils.IS_OS_WINDOWS) {
+            arg = "\"" + arg + "\"";
+        }
+        return arg;
+    }
+
 
     /**
      * Take the input args and separate them into args that belong on the commandline
diff --git a/tika-app/src/test/java/org/apache/tika/cli/TikaCLIBatchCommandLineTest.java b/tika-app/src/test/java/org/apache/tika/cli/TikaCLIBatchCommandLineTest.java
index e543ccc..2eb2677 100644
--- a/tika-app/src/test/java/org/apache/tika/cli/TikaCLIBatchCommandLineTest.java
+++ b/tika-app/src/test/java/org/apache/tika/cli/TikaCLIBatchCommandLineTest.java
@@ -41,6 +41,7 @@ public class TikaCLIBatchCommandLineTest {
     Path testFile = null;
 
     String testInputPathForCommandLine;
+    String escapedInputPathForCommandLine;
 
     @Before
     public void init() {
@@ -57,6 +58,7 @@ public class TikaCLIBatchCommandLineTest {
             throw new RuntimeException("Couldn't open testFile");
         }
         testInputPathForCommandLine = testInput.toAbsolutePath().toString();
+        escapedInputPathForCommandLine = BatchCommandLineBuilder.commandLineSafe(testInputPathForCommandLine);
     }
 
     @After
@@ -114,7 +116,7 @@ public class TikaCLIBatchCommandLineTest {
         assertEquals("true", attrs.get("-recursiveParserWrapper"));
         assertEquals("html", attrs.get("-basicHandlerType"));
         assertEquals("batch-config.xml", attrs.get("-bc"));
-        assertEquals(testInputPathForCommandLine, attrs.get("-inputDir"));
+        assertEquals(escapedInputPathForCommandLine, attrs.get("-inputDir"));
     }
 
     @Test
@@ -125,7 +127,7 @@ public class TikaCLIBatchCommandLineTest {
 
         String[] commandLine = BatchCommandLineBuilder.build(params);
         Map<String, String> attrs = mapify(commandLine);
-        assertEquals(testInputPathForCommandLine, attrs.get("-inputDir"));
+        assertEquals(escapedInputPathForCommandLine, attrs.get("-inputDir"));
         assertEquals(outputRoot, attrs.get("-outputDir"));
     }
 
@@ -136,21 +138,21 @@ public class TikaCLIBatchCommandLineTest {
 
         String[] commandLine = BatchCommandLineBuilder.build(params);
         Map<String, String> attrs = mapify(commandLine);
-        assertEquals(testInputPathForCommandLine, attrs.get("-inputDir"));
+        assertEquals(escapedInputPathForCommandLine, attrs.get("-inputDir"));
         assertEquals(outputRoot, attrs.get("-outputDir"));
 
         params = new String[]{"--inputDir", testInputPathForCommandLine, "--outputDir", outputRoot};
 
         commandLine = BatchCommandLineBuilder.build(params);
         attrs = mapify(commandLine);
-        assertEquals(testInputPathForCommandLine, attrs.get("-inputDir"));
+        assertEquals(escapedInputPathForCommandLine, attrs.get("-inputDir"));
         assertEquals(outputRoot, attrs.get("-outputDir"));
 
         params = new String[]{"-inputDir", testInputPathForCommandLine, "-outputDir", outputRoot};
 
         commandLine = BatchCommandLineBuilder.build(params);
         attrs = mapify(commandLine);
-        assertEquals(testInputPathForCommandLine, attrs.get("-inputDir"));
+        assertEquals(escapedInputPathForCommandLine, attrs.get("-inputDir"));
         assertEquals(outputRoot, attrs.get("-outputDir"));
     }
 
@@ -163,7 +165,7 @@ public class TikaCLIBatchCommandLineTest {
                 "--config="+configPath};
         String[] commandLine = BatchCommandLineBuilder.build(params);
         Map<String, String> attrs = mapify(commandLine);
-        assertEquals(testInputPathForCommandLine, attrs.get("-inputDir"));
+        assertEquals(escapedInputPathForCommandLine, attrs.get("-inputDir"));
         assertEquals(outputRoot, attrs.get("-outputDir"));
         assertEquals(configPath, attrs.get("-c"));
 

-- 
To stop receiving notification emails like this one, please contact
tallison@apache.org.