Skip to content

Commit 2a6cbc9

Browse files
committed
HPCC4J-581 WsFS Client should add path delim only if needed
- Introduces functionality to strip trailing white space - Adds trimright utility functionality - Adds new test cases - Adjusts pre-existing test cases to expected | actual format - Do not trim original string parameter - Do not duplicate Path URL param Signed-off-by: Rodrigo Pastrana <[email protected]>
1 parent 7914a0f commit 2a6cbc9

File tree

3 files changed

+115
-50
lines changed

3 files changed

+115
-50
lines changed

Diff for: wsclient/src/main/java/org/hpccsystems/ws/client/HPCCFileSprayClient.java

+15-9
Original file line numberDiff line numberDiff line change
@@ -1004,9 +1004,8 @@ public ProgressResponseWrapper sprayVariable(DelimitedDataOptions options, DropZ
10041004
if (targetDropZone == null) throw new Exception("TargetDropZone object not available!");
10051005

10061006
SprayVariable request = new SprayVariable();
1007-
10081007
request.setSourceIP(targetDropZone.getNetAddress());
1009-
request.setSourcePath(targetDropZone.getPath() + "/" + sourceFileName);
1008+
request.setSourcePath(Utils.ensureTrailingPathSlash(targetDropZone.getPath()) + sourceFileName);
10101009
request.setDestGroup(destGroup);
10111010
request.setDestLogicalName(targetFileName);
10121011
request.setOverwrite(overwrite);
@@ -1162,7 +1161,7 @@ public ProgressResponseWrapper sprayXML(DropZoneWrapper targetDropZone, String s
11621161

11631162
request.setDestGroup(destGroup);
11641163
request.setSourceIP(targetDropZone.getNetAddress());
1165-
request.setSourcePath(targetDropZone.getPath() + "/" + sourceFileName);
1164+
request.setSourcePath(Utils.ensureTrailingPathSlash(targetDropZone.getPath()) + sourceFileName);
11661165
request.setDestLogicalName(targetFileName);
11671166
request.setOverwrite(overwrite);
11681167
request.setSourceFormat(format.getValue());
@@ -1318,7 +1317,7 @@ public ProgressResponseWrapper sprayFixed(DropZoneWrapper targetDropZone, String
13181317
request.setDestGroup(destGroup);
13191318
request.setSourceRecordSize(recordSize);
13201319
request.setSourceIP(targetDropZone.getNetAddress());
1321-
request.setSourcePath(targetDropZone.getPath() + "/" + sourceFileName);
1320+
request.setSourcePath(Utils.ensureTrailingPathSlash(targetDropZone.getPath()) + sourceFileName);
13221321
request.setDestLogicalName(targetFileLabel);
13231322
request.setOverwrite(overwrite);
13241323
request.setPrefix(prefix);
@@ -1481,11 +1480,18 @@ public boolean uploadLargeFile(File uploadFile, DropZoneWrapper dropZone)
14811480
URLConnection fileUploadConnection = null;
14821481
URL fileUploadURL = null;
14831482
String uploadurlbuilder = UPLOADURI;
1484-
uploadurlbuilder += "&NetAddress=" + dropZone.getNetAddress();
1485-
String path = dropZone.getPath().trim();
1486-
if (!path.endsWith("/"))
1487-
path += "/";
1488-
uploadurlbuilder += "&Path=" + path;
1483+
1484+
if (dropZone.getPath().isEmpty())
1485+
{
1486+
log.error("HPCCFileSprayClient::uploadLargeFile: empty dropZone path detected!");
1487+
return false;
1488+
}
1489+
1490+
uploadurlbuilder += "&NetAddress=" + dropZone.getNetAddress() + "&Path=" + Utils.ensureTrailingPathSlash(dropZone.getPath());
1491+
1492+
if (!dropZone.getName().isEmpty())
1493+
uploadurlbuilder += "&DropZoneName=" + dropZone.getName();
1494+
14891495
uploadurlbuilder += "&OS=" + (dropZone.getLinux().equalsIgnoreCase("true") ? "2" : "1");
14901496
uploadurlbuilder += "&rawxml_=1";
14911497
WritableByteChannel outchannel = null;

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

+43
Original file line numberDiff line numberDiff line change
@@ -1061,21 +1061,64 @@ public static DocumentBuilder newSafeXMLDocBuilder() throws ParserConfigurationE
10611061
return safeXMLDocBuilder;
10621062
}
10631063

1064+
/**
1065+
* Ensures the given path contains a trailing path delimiter.
1066+
* Does not introduce duplicate trailing path delimiter if one already exists.
1067+
* Defaults to Linux style separator if the given path either contains a Linux style separator, or the path is empty.
1068+
* Strips all trailing white space character
1069+
* @param path The path to be postfixed
1070+
* @return original path with proper trailing path delimiter
1071+
*/
10641072
public static String ensureTrailingPathSlash(String path)
10651073
{
10661074
return ensureTrailingPathSlash(path, (path.contains(Character.toString(LINUX_SEP)) || path.length() == 0) ? LINUX_SEP : WIN_SEP);
10671075
}
10681076

1077+
/**
1078+
* Ensures the given path contains a trailing path delimiter.
1079+
* Does not introduce duplicate trailing path delimiter if one already exists.
1080+
* Uses Linux style path separator 'useLinuxSep' == "true", otherwise uses windows style path separator
1081+
* Strips all trailing white space character
1082+
* @param path path The path to be postfixed
1083+
* @param useLinuxSep String, if "true" linux styled path delimiter will be used
1084+
* @return original path with proper trailing path delimiter
1085+
*/
10691086
public static String ensureTrailingPathSlash(String path, String useLinuxSep)
10701087
{
10711088
return ensureTrailingPathSlash(path, useLinuxSep.equalsIgnoreCase("true") ? LINUX_SEP : WIN_SEP);
10721089
}
10731090

1091+
/**
1092+
* Ensures the given path contains a trailing path delimiter.
1093+
* Does not introduce duplicate trailing path delimiter if one already exists.
1094+
* Uses provided 'slash' as trailing path delimiter
1095+
* Strips all trailing white space character
1096+
* @param path The path to be postfixed
1097+
* @param slash The character to append
1098+
* @return original path with proper trailing path delimiter
1099+
*/
10741100
public static String ensureTrailingPathSlash(String path, char slash)
10751101
{
1102+
path = trimTrailing(path);
1103+
10761104
if (path.length() == 0 || path.charAt(path.length()-1) != slash)
10771105
path = path + slash;
10781106

10791107
return path;
10801108
}
1109+
1110+
/**
1111+
* Removes trailing whitespace characters from a string.
1112+
*
1113+
* @param originalStr the original string from which trailing whitespace should be removed
1114+
* @return a new string with the same characters as the original string, minus any trailing whitespace
1115+
*/
1116+
public static String trimTrailing(String originalStr)
1117+
{
1118+
int strIndex = originalStr.length()-1;
1119+
while(strIndex >= 0 && Character.isWhitespace(originalStr.charAt(strIndex)))
1120+
strIndex--;
1121+
1122+
return originalStr.substring(0,strIndex+1);
1123+
}
10811124
}

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

+57-41
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,75 @@
88

99
public class UtilsTest
1010
{
11+
@Test
12+
public void testEnsureTrailingSlashTrailingWhiteSpace()
13+
{
14+
assertEquals(Character.toString(Utils.LINUX_SEP), Utils.ensureTrailingPathSlash(""));
15+
assertEquals(Character.toString(Utils.LINUX_SEP), Utils.ensureTrailingPathSlash(Character.toString(Utils.LINUX_SEP)+ " "));
16+
assertEquals(Character.toString(Utils.WIN_SEP), Utils.ensureTrailingPathSlash(Character.toString(Utils.WIN_SEP) + " "));
17+
assertEquals(Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP), Utils.ensureTrailingPathSlash(Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP)+"\t"));
18+
assertEquals("C:\\some\\Path\\", Utils.ensureTrailingPathSlash("C:\\some\\Path "));
19+
assertEquals("C:\\some\\Path\\", Utils.ensureTrailingPathSlash("C:\\some\\Path\\ "));
20+
assertEquals("/another/path/", Utils.ensureTrailingPathSlash("/another/path "));
21+
assertEquals("/another/path/", Utils.ensureTrailingPathSlash("/another/path/\t\t"));
22+
assertEquals("/another/path/", Utils.ensureTrailingPathSlash("/another/path/\n"));
23+
assertEquals("/another/path/", Utils.ensureTrailingPathSlash("/another/path/" + '\u005Cn'));
24+
assertEquals("/another/path/", Utils.ensureTrailingPathSlash("/another/path/ " + '\u005Ct'));
25+
}
26+
1127
@Test
1228
public void testEnsureTrailingSlashNoSlashSpecified()
1329
{
14-
assertEquals(Utils.ensureTrailingPathSlash(""), Character.toString(Utils.LINUX_SEP)); //no sep in path, default to lin sep
15-
assertEquals(Utils.ensureTrailingPathSlash(Character.toString(Utils.LINUX_SEP)), Character.toString(Utils.LINUX_SEP));//no change expected
16-
assertEquals(Utils.ensureTrailingPathSlash(Character.toString(Utils.WIN_SEP)), Character.toString(Utils.WIN_SEP)); //no change expected
17-
assertEquals(Utils.ensureTrailingPathSlash(Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP)), Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP));//no change expected
18-
assertEquals(Utils.ensureTrailingPathSlash("C:\\some\\Path"), "C:\\some\\Path\\");
19-
assertEquals(Utils.ensureTrailingPathSlash("C:\\some\\Path\\"), "C:\\some\\Path\\");
20-
assertEquals(Utils.ensureTrailingPathSlash("/another/path"), "/another/path/");
21-
assertEquals(Utils.ensureTrailingPathSlash("/another/path/"), "/another/path/");
30+
assertEquals(Character.toString(Utils.LINUX_SEP), Utils.ensureTrailingPathSlash("")); //no sep in path, default to lin sep
31+
assertEquals(Character.toString(Utils.LINUX_SEP), Utils.ensureTrailingPathSlash(Character.toString(Utils.LINUX_SEP)));//no change expected
32+
assertEquals(Character.toString(Utils.WIN_SEP), Utils.ensureTrailingPathSlash(Character.toString(Utils.WIN_SEP))); //no change expected
33+
assertEquals(Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP), Utils.ensureTrailingPathSlash(Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP)));//no change expected
34+
assertEquals("C:\\some\\Path\\", Utils.ensureTrailingPathSlash("C:\\some\\Path"));
35+
assertEquals("C:\\some\\Path\\", Utils.ensureTrailingPathSlash("C:\\some\\Path\\"));
36+
assertEquals("/another/path/", Utils.ensureTrailingPathSlash("/another/path"));
37+
assertEquals("/another/path/", Utils.ensureTrailingPathSlash("/another/path/"));
2238
}
2339

2440
@Test
2541
public void testEnsureTrailingSlashSlashSpecified()
2642
{
27-
assertEquals(Utils.ensureTrailingPathSlash("", Utils.LINUX_SEP), Character.toString(Utils.LINUX_SEP));
28-
assertEquals(Utils.ensureTrailingPathSlash("", Utils.WIN_SEP), Character.toString(Utils.WIN_SEP));
29-
assertEquals(Utils.ensureTrailingPathSlash(Character.toString(Utils.LINUX_SEP), Utils.WIN_SEP), Character.toString(Utils.LINUX_SEP)+Utils.WIN_SEP);
30-
assertEquals(Utils.ensureTrailingPathSlash(Character.toString(Utils.LINUX_SEP), Utils.LINUX_SEP), Character.toString(Utils.LINUX_SEP));
31-
assertEquals(Utils.ensureTrailingPathSlash(Character.toString(Utils.WIN_SEP), Utils.WIN_SEP), Character.toString(Utils.WIN_SEP));
32-
assertEquals(Utils.ensureTrailingPathSlash(Character.toString(Utils.WIN_SEP), Utils.LINUX_SEP), Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP));
33-
assertEquals(Utils.ensureTrailingPathSlash(Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP), Utils.LINUX_SEP), Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP));
34-
assertEquals(Utils.ensureTrailingPathSlash(Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP), Utils.WIN_SEP), Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP)+Character.toString(Utils.WIN_SEP));
35-
assertEquals(Utils.ensureTrailingPathSlash("C:\\some\\Path", Utils.LINUX_SEP), "C:\\some\\Path\\"+Utils.LINUX_SEP);
36-
assertEquals(Utils.ensureTrailingPathSlash("C:\\some\\Path", Utils.WIN_SEP), "C:\\some\\Path\\"+Utils.WIN_SEP);
37-
assertEquals(Utils.ensureTrailingPathSlash("C:\\some\\Path\\", Utils.LINUX_SEP), "C:\\some\\Path\\" + Utils.LINUX_SEP);
38-
assertEquals(Utils.ensureTrailingPathSlash("C:\\some\\Path\\", Utils.WIN_SEP), "C:\\some\\Path\\");
39-
assertEquals(Utils.ensureTrailingPathSlash("/another/path", Utils.LINUX_SEP), "/another/path" + Utils.LINUX_SEP);
40-
assertEquals(Utils.ensureTrailingPathSlash("/another/path", Utils.WIN_SEP), "/another/path/"+ Utils.WIN_SEP);
41-
assertEquals(Utils.ensureTrailingPathSlash("/another/path/", Utils.LINUX_SEP), "/another/path/");
42-
assertEquals(Utils.ensureTrailingPathSlash("/another/path/", Utils.WIN_SEP), "/another/path/"+Utils.WIN_SEP);
43+
assertEquals(Character.toString(Utils.LINUX_SEP), Utils.ensureTrailingPathSlash("", Utils.LINUX_SEP));
44+
assertEquals(Character.toString(Utils.WIN_SEP), Utils.ensureTrailingPathSlash("", Utils.WIN_SEP));
45+
assertEquals(Character.toString(Utils.LINUX_SEP)+Utils.WIN_SEP, Utils.ensureTrailingPathSlash(Character.toString(Utils.LINUX_SEP), Utils.WIN_SEP));
46+
assertEquals(Character.toString(Utils.LINUX_SEP), Utils.ensureTrailingPathSlash(Character.toString(Utils.LINUX_SEP), Utils.LINUX_SEP));
47+
assertEquals(Character.toString(Utils.WIN_SEP), Utils.ensureTrailingPathSlash(Character.toString(Utils.WIN_SEP), Utils.WIN_SEP));
48+
assertEquals(Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP), Utils.ensureTrailingPathSlash(Character.toString(Utils.WIN_SEP), Utils.LINUX_SEP));
49+
assertEquals(Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP), Utils.ensureTrailingPathSlash(Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP), Utils.LINUX_SEP));
50+
assertEquals(Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP)+Character.toString(Utils.WIN_SEP), Utils.ensureTrailingPathSlash(Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP), Utils.WIN_SEP));
51+
assertEquals("C:\\some\\Path\\"+Utils.LINUX_SEP, Utils.ensureTrailingPathSlash("C:\\some\\Path", Utils.LINUX_SEP));
52+
assertEquals("C:\\some\\Path\\"+Utils.WIN_SEP, Utils.ensureTrailingPathSlash("C:\\some\\Path", Utils.WIN_SEP));
53+
assertEquals("C:\\some\\Path\\" + Utils.LINUX_SEP, Utils.ensureTrailingPathSlash("C:\\some\\Path\\", Utils.LINUX_SEP));
54+
assertEquals("C:\\some\\Path\\", Utils.ensureTrailingPathSlash("C:\\some\\Path\\", Utils.WIN_SEP));
55+
assertEquals("/another/path" + Utils.LINUX_SEP, Utils.ensureTrailingPathSlash("/another/path", Utils.LINUX_SEP));
56+
assertEquals("/another/path/"+ Utils.WIN_SEP, Utils.ensureTrailingPathSlash("/another/path", Utils.WIN_SEP));
57+
assertEquals("/another/path/", Utils.ensureTrailingPathSlash("/another/path/", Utils.LINUX_SEP));
58+
assertEquals("/another/path/"+Utils.WIN_SEP, Utils.ensureTrailingPathSlash("/another/path/", Utils.WIN_SEP));
4359
}
4460

4561
@Test
4662
public void testEnsureTrailingSlashUseLinuxBoolTest()
4763
{
48-
assertEquals(Utils.ensureTrailingPathSlash("", "true"), Character.toString(Utils.LINUX_SEP));
49-
assertEquals(Utils.ensureTrailingPathSlash("", "false"), Character.toString(Utils.WIN_SEP));
50-
assertEquals(Utils.ensureTrailingPathSlash("", "xyz"), Character.toString(Utils.WIN_SEP));
51-
assertEquals(Utils.ensureTrailingPathSlash(Character.toString(Utils.LINUX_SEP), "false"), Character.toString(Utils.LINUX_SEP)+Utils.WIN_SEP);
52-
assertEquals(Utils.ensureTrailingPathSlash(Character.toString(Utils.LINUX_SEP), "true"), Character.toString(Utils.LINUX_SEP));
53-
assertEquals(Utils.ensureTrailingPathSlash(Character.toString(Utils.WIN_SEP), "false"), Character.toString(Utils.WIN_SEP));
54-
assertEquals(Utils.ensureTrailingPathSlash(Character.toString(Utils.WIN_SEP), "true"), Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP));
55-
assertEquals(Utils.ensureTrailingPathSlash(Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP), "true"), Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP));
56-
assertEquals(Utils.ensureTrailingPathSlash(Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP), "false"), Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP)+Character.toString(Utils.WIN_SEP));
57-
assertEquals(Utils.ensureTrailingPathSlash("C:\\some\\Path", "true"), "C:\\some\\Path\\"+Utils.LINUX_SEP);
58-
assertEquals(Utils.ensureTrailingPathSlash("C:\\some\\Path", "false"), "C:\\some\\Path\\"+Utils.WIN_SEP);
59-
assertEquals(Utils.ensureTrailingPathSlash("C:\\some\\Path\\", "true"), "C:\\some\\Path\\" + Utils.LINUX_SEP);
60-
assertEquals(Utils.ensureTrailingPathSlash("C:\\some\\Path\\", "false"), "C:\\some\\Path\\");
61-
assertEquals(Utils.ensureTrailingPathSlash("/another/path", "TRUE"), "/another/path" + Utils.LINUX_SEP);
62-
assertEquals(Utils.ensureTrailingPathSlash("/another/path", "FALSE"), "/another/path/"+ Utils.WIN_SEP);
63-
assertEquals(Utils.ensureTrailingPathSlash("/another/path/", "TrUe"), "/another/path/");
64-
assertEquals(Utils.ensureTrailingPathSlash("/another/path/", "FalSe"), "/another/path/"+Utils.WIN_SEP);
64+
assertEquals(Character.toString(Utils.LINUX_SEP), Utils.ensureTrailingPathSlash("", "true"));
65+
assertEquals(Character.toString(Utils.WIN_SEP), Utils.ensureTrailingPathSlash("", "false"));
66+
assertEquals(Character.toString(Utils.WIN_SEP), Utils.ensureTrailingPathSlash("", "xyz"));
67+
assertEquals(Character.toString(Utils.LINUX_SEP)+Utils.WIN_SEP, Utils.ensureTrailingPathSlash(Character.toString(Utils.LINUX_SEP), "false"));
68+
assertEquals(Character.toString(Utils.LINUX_SEP), Utils.ensureTrailingPathSlash(Character.toString(Utils.LINUX_SEP), "true"));
69+
assertEquals(Character.toString(Utils.WIN_SEP), Utils.ensureTrailingPathSlash(Character.toString(Utils.WIN_SEP), "false"));
70+
assertEquals(Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP), Utils.ensureTrailingPathSlash(Character.toString(Utils.WIN_SEP), "true"));
71+
assertEquals(Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP), Utils.ensureTrailingPathSlash(Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP), "true"));
72+
assertEquals(Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP)+Character.toString(Utils.WIN_SEP), Utils.ensureTrailingPathSlash(Character.toString(Utils.WIN_SEP)+Character.toString(Utils.LINUX_SEP), "false"));
73+
assertEquals("C:\\some\\Path\\"+Utils.LINUX_SEP, Utils.ensureTrailingPathSlash("C:\\some\\Path", "true"));
74+
assertEquals("C:\\some\\Path\\"+Utils.WIN_SEP, Utils.ensureTrailingPathSlash("C:\\some\\Path", "false"));
75+
assertEquals("C:\\some\\Path\\" + Utils.LINUX_SEP, Utils.ensureTrailingPathSlash("C:\\some\\Path\\", "true"));
76+
assertEquals("C:\\some\\Path\\", Utils.ensureTrailingPathSlash("C:\\some\\Path\\", "false"));
77+
assertEquals("/another/path" + Utils.LINUX_SEP, Utils.ensureTrailingPathSlash("/another/path", "TRUE"));
78+
assertEquals("/another/path/"+ Utils.WIN_SEP, Utils.ensureTrailingPathSlash("/another/path", "FALSE"));
79+
assertEquals("/another/path/", Utils.ensureTrailingPathSlash("/another/path/", "TrUe"));
80+
assertEquals("/another/path/"+Utils.WIN_SEP, Utils.ensureTrailingPathSlash("/another/path/", "FalSe"));
6581
}
6682
}

0 commit comments

Comments
 (0)