Skip to content

Commit 24ddd49

Browse files
authored
Merge pull request #2715 from lprimak/fix-relative-resource-loading
bugfix(3.x): restore resource loading file:// and relative path funct…
2 parents 4088694 + 332e93a commit 24ddd49

3 files changed

Lines changed: 25 additions & 5 deletions

File tree

lang/src/main/java/org/apache/shiro/lang/io/ResourceUtils.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.IOException;
2828
import java.io.InputStream;
2929
import java.net.URL;
30+
import java.nio.file.Paths;
3031

3132
/**
3233
* Static helper methods for loading {@code Stream}-backed resources.
@@ -120,7 +121,6 @@ public static boolean resourceExists(String resourcePath) {
120121
* @throws IOException if there is a problem acquiring the resource at the specified path.
121122
*/
122123
public static InputStream getInputStreamForPath(String resourcePath) throws IOException {
123-
124124
URL url = getURLForPath(resourcePath);
125125
if (url == null) {
126126
throw new IOException("Resource [" + resourcePath + "] could not be found.");
@@ -135,7 +135,7 @@ public static InputStream getInputStreamForPath(String resourcePath) throws IOEx
135135
* ({@link #CLASSPATH_PREFIX CLASSPATH_PREFIX},
136136
* {@link #URL_PREFIX URL_PREFIX}, or {@link #FILE_PREFIX FILE_PREFIX}). If the path is not prefixed by one
137137
* of these schemes, the path is assumed to be a file-based path that can be loaded with a
138-
* call to {@link URI#create(String)}.
138+
* call to {@link Paths#get(String, String...)}.
139139
*
140140
* @param resourcePath the String path representing the resource to obtain.
141141
* @return the URL for the specified resource.
@@ -148,8 +148,10 @@ public static URL getURLForPath(String resourcePath) throws IOException {
148148
url = ClassUtils.getResource(stripPrefix(resourcePath));
149149
} else if (resourcePath.startsWith(URL_PREFIX)) {
150150
url = URI.create(stripPrefix(resourcePath)).toURL();
151+
} else if (resourcePath.startsWith(FILE_PREFIX)) {
152+
url = Paths.get(stripPrefix(resourcePath)).toUri().toURL();
151153
} else {
152-
url = URI.create(resourcePath).toURL();
154+
url = Paths.get(resourcePath).toUri().toURL();
153155
}
154156

155157
if (url == null) {

lang/src/test/java/org/apache/shiro/lang/util/ClassUtilsTest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020

2121
import org.junit.jupiter.api.Test;
2222

23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import static org.apache.shiro.lang.io.ResourceUtils.getInputStreamForPath;
2326
import static org.assertj.core.api.Assertions.assertThat;
2427

2528
class ClassUtilsTest {
26-
2729
@Test
2830
void testGetPrimitiveClasses() throws UnknownClassException {
29-
3031
assertThat(ClassUtils.forName("boolean")).isEqualTo(boolean.class);
3132
assertThat(ClassUtils.forName("byte")).isEqualTo(byte.class);
3233
assertThat(ClassUtils.forName("char")).isEqualTo(char.class);
@@ -84,4 +85,17 @@ void testGetClass() {
8485
assertThat(ClassUtils.forName(ClassUtilsTest.class.getName())).isEqualTo(ClassUtilsTest.class);
8586
assertThat(ClassUtils.forName(ClassUtilsTest[].class.getName())).isEqualTo(ClassUtilsTest[].class);
8687
}
88+
89+
@Test
90+
void inputStreamFileLoading() throws IOException {
91+
try (InputStream is = getInputStreamForPath("classpath:org/apache/shiro/lang/util/ClassUtilsTest.class")) {
92+
assertThat(is.readAllBytes()).isNotEmpty();
93+
}
94+
try (InputStream is = getInputStreamForPath("target/test-classes/test-data/file.json")) {
95+
assertThat(is.readAllBytes()).isNotEmpty();
96+
}
97+
try (InputStream is = getInputStreamForPath("file:target/test-classes/test-data/file.json")) {
98+
assertThat(is.readAllBytes()).isNotEmpty();
99+
}
100+
}
87101
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"id": 1,
3+
"name": "example"
4+
}

0 commit comments

Comments
 (0)