Skip to content

Commit ed93689

Browse files
authored
[GLUTEN-XXXX][CORE] Fix URL-encoded paths in ResourceUtil causing JAR loading failures (#11672)
Fix URL-encoded path handling in ResourceUtil.getResources() by using URL.toURI() and URI for proper path decoding instead of URL.getPath().
1 parent 86f2d2c commit ed93689

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

gluten-core/src/main/java/org/apache/gluten/utils/ResourceUtil.java

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
import java.io.File;
2626
import java.io.IOException;
27+
import java.net.URI;
28+
import java.net.URISyntaxException;
2729
import java.net.URL;
2830
import java.util.*;
2931
import java.util.regex.Matcher;
@@ -73,26 +75,33 @@ private static void getResources(
7375
final String protocol = containerUrl.getProtocol();
7476
switch (protocol) {
7577
case "file":
76-
final File fileContainer = new File(containerUrl.getPath());
77-
Preconditions.checkState(
78-
fileContainer.exists() && fileContainer.isDirectory(),
79-
"Specified file container " + containerUrl + " is not a directory or not a file");
80-
getResourcesFromDirectory(fileContainer, fileContainer, pattern, buffer);
78+
try {
79+
final File fileContainer = new File(containerUrl.toURI());
80+
Preconditions.checkState(
81+
fileContainer.exists() && fileContainer.isDirectory(),
82+
"Specified file container " + containerUrl + " is not a directory or not a file");
83+
getResourcesFromDirectory(fileContainer, fileContainer, pattern, buffer);
84+
} catch (URISyntaxException e) {
85+
throw new GlutenException(e);
86+
}
8187
break;
8288
case "jar":
83-
final String jarContainerPath = containerUrl.getPath();
84-
final Pattern jarContainerPattern = Pattern.compile("file:([^!]+)!/(.+)");
85-
final Matcher m = jarContainerPattern.matcher(jarContainerPath);
86-
if (!m.matches()) {
87-
throw new GlutenException("Illegal Jar container URL: " + containerUrl);
89+
try {
90+
final String jarContainerPath = containerUrl.getPath();
91+
final Pattern jarContainerPattern = Pattern.compile("file:([^!]+)!/(.+)");
92+
final Matcher m = jarContainerPattern.matcher(jarContainerPath);
93+
if (!m.matches()) {
94+
throw new GlutenException("Illegal Jar container URL: " + containerUrl);
95+
}
96+
final File jarFile = new File(new URI("file://" + m.group(1)));
97+
Preconditions.checkState(
98+
jarFile.exists() && jarFile.isFile(),
99+
"Specified Jar container " + containerUrl + " is not a Jar file");
100+
final String dir = m.group(2);
101+
getResourcesFromJarFile(jarFile, dir, pattern, buffer);
102+
} catch (URISyntaxException e) {
103+
throw new GlutenException(e);
88104
}
89-
final String jarPath = m.group(1);
90-
final File jarFile = new File(jarPath);
91-
Preconditions.checkState(
92-
jarFile.exists() && jarFile.isFile(),
93-
"Specified Jar container " + containerUrl + " is not a Jar file");
94-
final String dir = m.group(2);
95-
getResourcesFromJarFile(jarFile, dir, pattern, buffer);
96105
break;
97106
default:
98107
throw new GlutenException("Unrecognizable resource protocol: " + protocol);

gluten-core/src/test/java/org/apache/gluten/util/ResourceUtilTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.junit.Assert;
2222
import org.junit.Test;
2323

24+
import java.net.URI;
2425
import java.util.List;
2526
import java.util.regex.Pattern;
2627

@@ -43,4 +44,16 @@ public void testJar() {
4344
Assert.assertEquals(1, classes.size());
4445
Assert.assertEquals("apache/spark/SparkContext.class", classes.get(0));
4546
}
47+
48+
/** Verifies URI correctly decodes percent-encoded paths and preserves '+' characters. */
49+
@Test
50+
public void testUriDecodesPercentEncodedPaths() throws Exception {
51+
// %40 should decode to @
52+
URI uri1 = new URI("file:///path/user%40domain/file.jar");
53+
Assert.assertEquals("/path/user@domain/file.jar", uri1.getPath());
54+
55+
// + should be preserved (not converted to space like URLDecoder does)
56+
URI uri2 = new URI("file:///path/file+name.jar");
57+
Assert.assertEquals("/path/file+name.jar", uri2.getPath());
58+
}
4659
}

0 commit comments

Comments
 (0)