Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HPCC4J-630 Ensure Filespray path constructed correctly #739

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading