Skip to content

Commit a87ab8c

Browse files
committed
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 <[email protected]>
1 parent 5551ce8 commit a87ab8c

File tree

3 files changed

+148
-3
lines changed

3 files changed

+148
-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.ensureTrailingPathSlash(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.ensureTrailingPathSlash(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.ensureTrailingPathSlash(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

+115
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,121 @@ 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 contain path seperator");
1174+
}
1175+
}
1176+
1177+
/**
1178+
* Constructs new path based on provided pre and post path sections and string representation of useLinuxSep directive
1179+
* Linux style separator is used if useLinuxSep resolves to the literal "true" otherwise Windows separator is used.
1180+
*
1181+
* @param prefixPath - the prefix path
1182+
* @param postfixPath - the postfix path
1183+
* @param useLinuxSep - directive to use linux path separator represented as a literal string
1184+
* @return - new path comprised of path prefix a path separator and path postfix
1185+
*/
1186+
public static String appendPathSections(String prefixPath, String postfixPath, String useLinuxSep)
1187+
{
1188+
return appendPathSections(prefixPath, useLinuxSep.equalsIgnoreCase("true") ? LINUX_SEP : WIN_SEP, postfixPath);
1189+
}
1190+
1191+
/**
1192+
* Constructs new path based on provided pre and post path sections
1193+
* Ensures resulting path is properly delimited at the point of concatenation
1194+
* Uses provided char as delimiter between pre and post path sections
1195+
*
1196+
* @param prefixPath - the prefix path
1197+
* @param slash - separator to use when appending path sections
1198+
* @param postfixPath - the postfix path
1199+
* @return - new path comprised of path prefix a path separator and path postfix
1200+
*/
1201+
public static String appendPathSections(String prefixPath, char slash, String postfixPath)
1202+
{
1203+
prefixPath = trimTrailing(prefixPath);
1204+
1205+
if (prefixPath.length() == 0 || prefixPath.charAt(prefixPath.length()-1) != slash)
1206+
prefixPath = prefixPath + slash;
1207+
1208+
postfixPath = postfixPath.trim();
1209+
1210+
if (postfixPath.length() > 0 && postfixPath.charAt(0) == slash)
1211+
prefixPath = prefixPath + postfixPath.substring(1);
1212+
else
1213+
prefixPath = prefixPath + postfixPath;
1214+
1215+
return prefixPath;
1216+
}
1217+
1218+
/**
1219+
* Infers path style (linux/windows) based on presence of Linux separator
1220+
* @param path
1221+
* @return - new path comprised of path prefix a path separator and path postfix
1222+
* @throws Exception - Invalid paths, indiscernible path style
1223+
*/
1224+
public static char inferPathSeperatorType(String path) throws Exception
1225+
{
1226+
if (path.length() == 0)
1227+
throw new Exception("Zero len path detected!");
1228+
1229+
return path.contains(Character.toString(LINUX_SEP)) ? LINUX_SEP : WIN_SEP;
1230+
}
1231+
11171232
/**
11181233
* Removes trailing whitespace characters from a string.
11191234
*

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

+30
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,36 @@
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+
37+
assertEquals("C:\\some\\path\\", Utils.appendPathSections("C:\\some\\ ", " \\path\\"));
38+
assertEquals("C:\\some\\path\\", Utils.appendPathSections("C:\\some", " path\\"));
39+
}
40+
1141
@Test
1242
public void testEnsureTrailingSlashTrailingWhiteSpace()
1343
{

0 commit comments

Comments
 (0)