From 8b203acc212704a9f12560aa1f10f54668db1509 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 - Ends wsfs use of ensuretrailingslash method Signed-off-by: Rodrigo Pastrana --- .../ws/client/HPCCFileSprayClient.java | 6 +- .../hpccsystems/ws/client/utils/Utils.java | 101 ++++++++++++++++++ .../ws/client/utils/UtilsTest.java | 29 +++++ 3 files changed, 133 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..f59fe29fc 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.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.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.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..9db3f3145 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,107 @@ 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 - the prefix path + * @param postfixPath - the postfix path + * @return - new path comprised of path prefix a linux style path separator and path postfix + */ + 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 - the prefix path + * @param postfixPath - the postfix path + * @return - new path comprised of path prefix a windows style path separator and path postfix + */ + 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 - the prefix path + * @param postfixPath - the postfix path + * @return - new path comprised of path prefix a path separator and path postfix + * @throws Exception - Invalid paths, indiscernible path style + */ + 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 and contain path seperator"); + } + } + + /** + * 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 - the prefix path + * @param slash - separator to use when appending path sections + * @param postfixPath - the postfix path + * @return - new path comprised of path prefix a path separator and path postfix + */ + 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 - the path + * @return - new path comprised of path prefix a path separator and path postfix + * @throws Exception - Invalid paths, indiscernible path style + */ + 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..99876b48a 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,35 @@ 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() {