Skip to content

Commit 9162574

Browse files
authored
HPCC4J-630 Ensure Filespray path constructed correctly (#739)
Signed-off-by: Rodrigo Pastrana <[email protected]>
1 parent 4814837 commit 9162574

File tree

3 files changed

+133
-3
lines changed

3 files changed

+133
-3
lines changed

wsclient/src/main/java/org/hpccsystems/ws/client/HPCCFileSprayClient.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ public ProgressResponseWrapper sprayVariable(DelimitedDataOptions options, DropZ
10051005

10061006
SprayVariable request = new SprayVariable();
10071007
request.setSourceIP(targetDropZone.getNetAddress());
1008-
request.setSourcePath(Utils.ensureTrailingPathSlash(targetDropZone.getPath()) + sourceFileName);
1008+
request.setSourcePath(Utils.appendPathSections(targetDropZone.getPath(), sourceFileName));
10091009
request.setDestGroup(destGroup);
10101010
request.setDestLogicalName(targetFileName);
10111011
request.setOverwrite(overwrite);
@@ -1161,7 +1161,7 @@ public ProgressResponseWrapper sprayXML(DropZoneWrapper targetDropZone, String s
11611161

11621162
request.setDestGroup(destGroup);
11631163
request.setSourceIP(targetDropZone.getNetAddress());
1164-
request.setSourcePath(Utils.ensureTrailingPathSlash(targetDropZone.getPath()) + sourceFileName);
1164+
request.setSourcePath(Utils.appendPathSections(targetDropZone.getPath(), sourceFileName));
11651165
request.setDestLogicalName(targetFileName);
11661166
request.setOverwrite(overwrite);
11671167
request.setSourceFormat(format.getValue());
@@ -1317,7 +1317,7 @@ public ProgressResponseWrapper sprayFixed(DropZoneWrapper targetDropZone, String
13171317
request.setDestGroup(destGroup);
13181318
request.setSourceRecordSize(recordSize);
13191319
request.setSourceIP(targetDropZone.getNetAddress());
1320-
request.setSourcePath(Utils.ensureTrailingPathSlash(targetDropZone.getPath()) + sourceFileName);
1320+
request.setSourcePath(Utils.appendPathSections(targetDropZone.getPath(), sourceFileName));
13211321
request.setDestLogicalName(targetFileLabel);
13221322
request.setOverwrite(overwrite);
13231323
request.setPrefix(prefix);

wsclient/src/main/java/org/hpccsystems/ws/client/utils/Utils.java

+101
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,107 @@ public static String ensureTrailingPathSlash(String path, char slash)
11141114
return path;
11151115
}
11161116

1117+
/**
1118+
* Constructs new path based on provided pre and post path sections
1119+
* Ensures resulting path is properly delimited at the point of concatenation
1120+
* Uses Linux path style path separator
1121+
*
1122+
* @param prefixPath - the prefix path
1123+
* @param postfixPath - the postfix path
1124+
* @return - new path comprised of path prefix a linux style path separator and path postfix
1125+
*/
1126+
public static String appendLinuxPathSections(String prefixPath, String postfixPath)
1127+
{
1128+
return appendPathSections(prefixPath, LINUX_SEP, postfixPath);
1129+
}
1130+
1131+
/**
1132+
* Constructs new path based on provided pre and post path sections
1133+
* Ensures resulting path is properly delimited at the point of concatenation
1134+
* Uses Windows path style path separator
1135+
*
1136+
* @param prefixPath - the prefix path
1137+
* @param postfixPath - the postfix path
1138+
* @return - new path comprised of path prefix a windows style path separator and path postfix
1139+
*/
1140+
public static String appendWindowsPathSections(String prefixPath, String postfixPath)
1141+
{
1142+
return appendPathSections(prefixPath, WIN_SEP, postfixPath);
1143+
}
1144+
1145+
/**
1146+
* Constructs new path based on provided pre and post path sections
1147+
* Ensures resulting path is properly delimited at the point of concatenation
1148+
* Infers proper path separator on presence of Linux or Windows style path separator in prefix path
1149+
*
1150+
* @param prefixPath - the prefix path
1151+
* @param postfixPath - the postfix path
1152+
* @return - new path comprised of path prefix a path separator and path postfix
1153+
* @throws Exception - Invalid paths, indiscernible path style
1154+
*/
1155+
public static String appendPathSections(String prefixPath, String postfixPath) throws Exception
1156+
{
1157+
if (prefixPath == null)
1158+
prefixPath = "";
1159+
1160+
if (postfixPath == null)
1161+
postfixPath = "";
1162+
1163+
if (prefixPath.length() == 0 && postfixPath.length() == 0)
1164+
return "";
1165+
1166+
try
1167+
{
1168+
char pathSep = inferPathSeperatorType(prefixPath.length() != 0 ? prefixPath : postfixPath);
1169+
return appendPathSections(prefixPath, pathSep, postfixPath);
1170+
}
1171+
catch (Exception e)
1172+
{
1173+
throw new Exception("Could not append path sections, ensure original path sections are valid and contain path seperator");
1174+
}
1175+
}
1176+
1177+
/**
1178+
* Constructs new path based on provided pre and post path sections
1179+
* Ensures resulting path is properly delimited at the point of concatenation
1180+
* Uses provided char as delimiter between pre and post path sections
1181+
*
1182+
* @param prefixPath - the prefix path
1183+
* @param slash - separator to use when appending path sections
1184+
* @param postfixPath - the postfix path
1185+
* @return - new path comprised of path prefix a path separator and path postfix
1186+
*/
1187+
public static String appendPathSections(String prefixPath, char slash, String postfixPath)
1188+
{
1189+
prefixPath = trimTrailing(prefixPath);
1190+
1191+
if (prefixPath.length() == 0 || prefixPath.charAt(prefixPath.length()-1) != slash)
1192+
prefixPath = prefixPath + slash;
1193+
1194+
postfixPath = postfixPath.trim();
1195+
1196+
if (postfixPath.length() > 0 && postfixPath.charAt(0) == slash)
1197+
prefixPath = prefixPath + postfixPath.substring(1);
1198+
else
1199+
prefixPath = prefixPath + postfixPath;
1200+
1201+
return prefixPath;
1202+
}
1203+
1204+
/**
1205+
* Infers path style (linux/windows) based on presence of Linux separator
1206+
* @param path - the path
1207+
* @return - new path comprised of path prefix a path separator and path postfix
1208+
* @throws Exception - Invalid paths, indiscernible path style
1209+
*/
1210+
public static char inferPathSeperatorType(String path) throws Exception
1211+
{
1212+
if (path.length() == 0)
1213+
throw new Exception("Zero len path detected!");
1214+
1215+
return path.contains(Character.toString(LINUX_SEP)) ? LINUX_SEP : WIN_SEP;
1216+
}
1217+
11171218
/**
11181219
* Removes trailing whitespace characters from a string.
11191220
*

wsclient/src/test/java/org/hpccsystems/ws/client/utils/UtilsTest.java

+29
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,35 @@
88

99
public class UtilsTest
1010
{
11+
12+
@Test
13+
public void testappendPathSections() throws Exception
14+
{
15+
//appendWindowsPathSections
16+
assertEquals(Character.toString(Utils.WIN_SEP), Utils.appendWindowsPathSections("", ""));
17+
assertEquals("C:\\some\\path\\", Utils.appendWindowsPathSections("C:\\some\\ ", " \\path\\"));
18+
assertEquals("C:\\some\\path\\", Utils.appendWindowsPathSections("C:\\some", " path\\"));
19+
20+
//appendLinuxPathSections
21+
assertEquals(Character.toString(Utils.LINUX_SEP), Utils.appendLinuxPathSections("", ""));
22+
assertEquals("/root/path/relative/path", Utils.appendLinuxPathSections("/root/path ", " relative/path"));
23+
assertEquals("/root/path/relative/path", Utils.appendLinuxPathSections("/root/path/ ", " /relative/path"));
24+
assertEquals("/relative/path", Utils.appendLinuxPathSections("/ ", "/relative/path"));
25+
assertEquals("/relative/path", Utils.appendLinuxPathSections("/ ", "/relative/path"));
26+
assertEquals("/relative/path", Utils.appendLinuxPathSections("/", " /relative/path"));
27+
28+
//appendPathSections
29+
assertEquals("/relative/path", Utils.appendPathSections("/", " /relative/path"));
30+
assertEquals("/root/path/relative/path", Utils.appendPathSections("/root/path ", " relative/path"));
31+
assertEquals("/root/path/relative/path", Utils.appendPathSections("/root/path/ ", " /relative/path"));
32+
assertEquals("/relative/path", Utils.appendPathSections("/ ", "/relative/path"));
33+
assertEquals("/relative/path", Utils.appendPathSections("/ ", "/relative/path"));
34+
assertEquals("/relative/path", Utils.appendPathSections("/", " /relative/path"));
35+
36+
assertEquals("C:\\some\\path\\", Utils.appendPathSections("C:\\some\\ ", " \\path\\"));
37+
assertEquals("C:\\some\\path", Utils.appendPathSections("C:\\some", " path"));
38+
}
39+
1140
@Test
1241
public void testEnsureTrailingSlashTrailingWhiteSpace()
1342
{

0 commit comments

Comments
 (0)