Skip to content

Commit

Permalink
HPCC4J-630 Ensure Filespray path constructed correctly
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
rpastrana committed Aug 21, 2024
1 parent 5551ce8 commit 8b203ac
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand Down
101 changes: 101 additions & 0 deletions wsclient/src/main/java/org/hpccsystems/ws/client/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit 8b203ac

Please sign in to comment.