From c04a83cddca24fd5349d627150f5503504c0acbc Mon Sep 17 00:00:00 2001 From: Rodrigo Pastrana Date: Mon, 19 Aug 2024 16:52:10 -0400 Subject: [PATCH] HPCC4J-630 Ensure Filespray path constructed correctly - Adds util methods to append paths - Adds util methods to detect path seperator from sample path - Adds junit test for new methods - Ensures wsfs methods use new util path append method Signed-off-by: Rodrigo Pastrana --- .../ws/client/HPCCFileSprayClient.java | 6 +- .../hpccsystems/ws/client/utils/Utils.java | 115 ++++++++++++++++++ .../ws/client/utils/UtilsTest.java | 30 +++++ 3 files changed, 148 insertions(+), 3 deletions(-) diff --git a/wsclient/src/main/java/org/hpccsystems/ws/client/HPCCFileSprayClient.java b/wsclient/src/main/java/org/hpccsystems/ws/client/HPCCFileSprayClient.java index a7283a6cd..5cb79770c 100644 --- a/wsclient/src/main/java/org/hpccsystems/ws/client/HPCCFileSprayClient.java +++ b/wsclient/src/main/java/org/hpccsystems/ws/client/HPCCFileSprayClient.java @@ -1005,7 +1005,7 @@ public ProgressResponseWrapper sprayVariable(DelimitedDataOptions options, DropZ SprayVariable request = new SprayVariable(); request.setSourceIP(targetDropZone.getNetAddress()); - request.setSourcePath(Utils.ensureTrailingPathSlash(targetDropZone.getPath()) + sourceFileName); + request.setSourcePath(Utils.ensureTrailingPathSlash(Utils.appendPathSections(targetDropZone.getPath(),sourceFileName))); request.setDestGroup(destGroup); request.setDestLogicalName(targetFileName); request.setOverwrite(overwrite); @@ -1161,7 +1161,7 @@ public ProgressResponseWrapper sprayXML(DropZoneWrapper targetDropZone, String s request.setDestGroup(destGroup); request.setSourceIP(targetDropZone.getNetAddress()); - request.setSourcePath(Utils.ensureTrailingPathSlash(targetDropZone.getPath()) + sourceFileName); + request.setSourcePath(Utils.ensureTrailingPathSlash(Utils.appendPathSections(targetDropZone.getPath(), sourceFileName))); request.setDestLogicalName(targetFileName); request.setOverwrite(overwrite); request.setSourceFormat(format.getValue()); @@ -1317,7 +1317,7 @@ public ProgressResponseWrapper sprayFixed(DropZoneWrapper targetDropZone, String request.setDestGroup(destGroup); request.setSourceRecordSize(recordSize); request.setSourceIP(targetDropZone.getNetAddress()); - request.setSourcePath(Utils.ensureTrailingPathSlash(targetDropZone.getPath()) + sourceFileName); + request.setSourcePath(Utils.ensureTrailingPathSlash(Utils.appendPathSections(targetDropZone.getPath(), sourceFileName))); request.setDestLogicalName(targetFileLabel); request.setOverwrite(overwrite); request.setPrefix(prefix); diff --git a/wsclient/src/main/java/org/hpccsystems/ws/client/utils/Utils.java b/wsclient/src/main/java/org/hpccsystems/ws/client/utils/Utils.java index 16a4676d5..d9d77677c 100644 --- a/wsclient/src/main/java/org/hpccsystems/ws/client/utils/Utils.java +++ b/wsclient/src/main/java/org/hpccsystems/ws/client/utils/Utils.java @@ -1114,6 +1114,121 @@ public static String ensureTrailingPathSlash(String path, char slash) return path; } + /** + * Constructs new path based on provided pre and post path sections + * Ensures resulting path is properly delimited at the point of concatenation + * Uses Linux path style path separator + * + * @param prefixPath + * @param postfixPath + * @return + */ + public static String appendLinuxPathSections(String prefixPath, String postfixPath) + { + return appendPathSections(prefixPath, LINUX_SEP, postfixPath); + } + + /** + * Constructs new path based on provided pre and post path sections + * Ensures resulting path is properly delimited at the point of concatenation + * Uses Windows path style path separator + * + * @param prefixPath + * @param postfixPath + * @return + */ + public static String appendWindowsPathSections(String prefixPath, String postfixPath) + { + return appendPathSections(prefixPath, WIN_SEP, postfixPath); + } + + /** + * Constructs new path based on provided pre and post path sections + * Ensures resulting path is properly delimited at the point of concatenation + * Infers proper path separator on presence of Linux or Windows style path separator in prefix path + * + * @param prefixPath + * @param postfixPath + * @return + * @throws Exception + */ + public static String appendPathSections(String prefixPath, String postfixPath) throws Exception + { + if (prefixPath == null) + prefixPath = ""; + + if (postfixPath == null) + postfixPath = ""; + + if (prefixPath.length() == 0 && postfixPath.length() == 0) + return ""; + + try + { + char pathSep = inferPathSeperatorType(prefixPath.length() != 0 ? prefixPath : postfixPath); + return appendPathSections(prefixPath, pathSep, postfixPath); + } + catch (Exception e) + { + throw new Exception("Could not append path sections, ensure original path sections are valid contain path seperator"); + } + } + + /** + * Constructs new path based on provided pre and post path sections and string representation of useLinuxSep directive + * Linux style separator is used if useLinuxSep resolves to the literal "true" otherwise Windows separator is used. + * + * @param prefixPath + * @param postfixPath + * @param useLinuxSep + * @return + */ + public static String appendPathSections(String prefixPath, String postfixPath, String useLinuxSep) + { + return appendPathSections(prefixPath, useLinuxSep.equalsIgnoreCase("true") ? LINUX_SEP : WIN_SEP, postfixPath); + } + + /** + * Constructs new path based on provided pre and post path sections + * Ensures resulting path is properly delimited at the point of concatenation + * Uses provided char as delimiter between pre and post path sections + * + * @param prefixPath + * @param slash + * @param postfixPath + * @return + */ + public static String appendPathSections(String prefixPath, char slash, String postfixPath) + { + prefixPath = trimTrailing(prefixPath); + + if (prefixPath.length() == 0 || prefixPath.charAt(prefixPath.length()-1) != slash) + prefixPath = prefixPath + slash; + + postfixPath = postfixPath.trim(); + + if (postfixPath.length() > 0 && postfixPath.charAt(0) == slash) + prefixPath = prefixPath + postfixPath.substring(1); + else + prefixPath = prefixPath + postfixPath; + + return prefixPath; + } + + /** + * Infers path style (linux/windows) based on presence of Linux separator + * @param path + * @return + * @throws Exception + */ + public static char inferPathSeperatorType(String path) throws Exception + { + if (path.length() == 0) + throw new Exception("Zero len path detected!"); + + return path.contains(Character.toString(LINUX_SEP)) ? LINUX_SEP : WIN_SEP; + } + /** * Removes trailing whitespace characters from a string. * diff --git a/wsclient/src/test/java/org/hpccsystems/ws/client/utils/UtilsTest.java b/wsclient/src/test/java/org/hpccsystems/ws/client/utils/UtilsTest.java index eef268e47..70aa4fe1b 100644 --- a/wsclient/src/test/java/org/hpccsystems/ws/client/utils/UtilsTest.java +++ b/wsclient/src/test/java/org/hpccsystems/ws/client/utils/UtilsTest.java @@ -8,6 +8,36 @@ public class UtilsTest { + + @Test + public void testappendPathSections() throws Exception + { + //appendWindowsPathSections + assertEquals(Character.toString(Utils.WIN_SEP), Utils.appendWindowsPathSections("", "")); + assertEquals("C:\\some\\path\\", Utils.appendWindowsPathSections("C:\\some\\ ", " \\path\\")); + assertEquals("C:\\some\\path\\", Utils.appendWindowsPathSections("C:\\some", " path\\")); + + //appendLinuxPathSections + assertEquals(Character.toString(Utils.LINUX_SEP), Utils.appendLinuxPathSections("", "")); + assertEquals("/root/path/relative/path", Utils.appendLinuxPathSections("/root/path ", " relative/path")); + assertEquals("/root/path/relative/path", Utils.appendLinuxPathSections("/root/path/ ", " /relative/path")); + assertEquals("/relative/path", Utils.appendLinuxPathSections("/ ", "/relative/path")); + assertEquals("/relative/path", Utils.appendLinuxPathSections("/ ", "/relative/path")); + assertEquals("/relative/path", Utils.appendLinuxPathSections("/", " /relative/path")); + + //appendPathSections + assertEquals("/relative/path", Utils.appendPathSections("/", " /relative/path")); + assertEquals("/root/path/relative/path", Utils.appendPathSections("/root/path ", " relative/path")); + assertEquals("/root/path/relative/path", Utils.appendPathSections("/root/path/ ", " /relative/path")); + assertEquals("/relative/path", Utils.appendPathSections("/ ", "/relative/path")); + assertEquals("/relative/path", Utils.appendPathSections("/ ", "/relative/path")); + assertEquals("/relative/path", Utils.appendPathSections("/", " /relative/path")); + + + assertEquals("C:\\some\\path\\", Utils.appendPathSections("C:\\some\\ ", " \\path\\")); + assertEquals("C:\\some\\path\\", Utils.appendPathSections("C:\\some", " path\\")); + } + @Test public void testEnsureTrailingSlashTrailingWhiteSpace() {