Skip to content

Commit 4122c39

Browse files
committed
ClickUtils.getResourceAsStream better legacy support
1 parent 427fc01 commit 4122c39

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

click/src/main/java/org/apache/click/util/ClickUtils.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2417,23 +2417,20 @@ public static String getRequestURI(HttpServletRequest request) {
24172417
}
24182418

24192419
/**
2420-
* Finds a resource with a given name. This method returns null if no
2421-
* resource with this name is found.
2420+
* Finds a resource with a given name. This method returns {@code null} if no resource with this name is found.
24222421
* <p>
2423-
* This method uses the current <tt>Thread</tt> context <tt>ClassLoader</tt> to find
2424-
* the resource. If the resource is not found the class loader of the given
2425-
* class is then used to find the resource.
2426-
*
2422+
* This method uses the current <tt>Thread</tt> context <tt>ClassLoader</tt> to find the resource.
2423+
* If the resource is not found the class loader of the given class is then used to find the resource.
2424+
24272425
* @param name the name of the resource
2428-
* @param aClass the class lookup the resource against, if the resource is
2429-
* not found using the current <tt>Thread</tt> context <tt>ClassLoader</tt>.
2430-
* @return the input stream of the resource if found or null otherwise
2426+
* @param aClass the class lookup the resource against, if the resource is not found using the current <tt>Thread</tt> context <tt>ClassLoader</tt>.
2427+
* @return the input stream of the resource if found or {@code null} otherwise
24312428
*/
24322429
@Nullable
24332430
public static InputStream getResourceAsStream (@NonNull String name, @NonNull Class<?> aClass) {
24342431
if (name.startsWith("/")){
24352432
log.warn("getResourceAsStream: name must be relative, but: {} @ {}", name, aClass);
2436-
name = name.substring(1);
2433+
name = name.substring(1);// ~ /WEB-INF/foo ~ WEB-INF/foo
24372434
}
24382435
val classLoader = Thread.currentThread().getContextClassLoader();
24392436
if (classLoader != null){
@@ -2451,15 +2448,13 @@ public static InputStream getResourceAsStream (@NonNull String name, @NonNull Cl
24512448
InputStream is = aClass.getResourceAsStream(name);
24522449
if (is != null){ return is; }
24532450

2454-
is = ClickUtils.class.getResourceAsStream(name);
2451+
is = ClickUtils.class.getResourceAsStream(name);// fallback to lib class loader
24552452
if (is != null){ return is; }
24562453

2457-
if (name.startsWith("WEB-INF/")){// hack old Click to work with Boot
2458-
name = name.substring(8);// ^ cut
2459-
is = getResourceAsStream(name, aClass);
2460-
if (is != null){ return is; }
2461-
2462-
return getResourceAsStream("META-INF/resources/"+name, aClass);
2454+
if (name.startsWith("WEB-INF/")){// hack old Click to work with Boot ?WEB-INF/classes
2455+
return getResourceAsStream(name.substring(8), aClass);// no more WEB-INF ⇒ resource in /resources?
2456+
} else if (!name.startsWith("META-INF/resources/")){// try in pseudo "/WEB-INF/" ~ replace "/WEB-INF/" with real servlet-container location of resources
2457+
return getResourceAsStream("META-INF/resources/"+ name, aClass);
24632458
}
24642459
return null;// not found anywhere
24652460
}
@@ -2468,9 +2463,9 @@ public static InputStream getResourceAsStream (@NonNull String name, @NonNull Cl
24682463
Get best ClassLoader.
24692464
Get {@link Thread#getContextClassLoader Thread.currentThread().getContextClassLoader()}.
24702465
If null then fallback to {@link Class#getClassLoader() Throw.class::getClassLoader}*/
2471-
public static ClassLoader classLoader (){
2466+
public static ClassLoader classLoader () {
24722467
var cl = Thread.currentThread().getContextClassLoader();
2473-
return cl != null ? cl // <^ ~ Objects.requireNonNullElseGet
2468+
return cl != null ? cl // <^ ~ Objects.requireNonNullElseGet
24742469
: ClickUtils.class.getClassLoader();
24752470
}
24762471

click/src/test/java/org/apache/click/util/ClickUtilsTest.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,14 @@ public class ClickUtilsTest extends TestCase {
4141
DATE_OF_BIRTH = calendar.getTime();
4242
}
4343

44-
/**
45-
* Setup a MockContext for each test.
46-
*/
44+
/** Setup a MockContext for each test */
4745
@Override
4846
protected void setUp() {
4947
MockContext.initContext(Locale.ENGLISH);
5048
}
5149

52-
/**
53-
* Sanity checks for ClickUtils.copyFormToObject.
54-
*/
50+
/** Sanity checks for ClickUtils.copyFormToObject */
5551
public void testCopyFormToObject() {
56-
5752
// set up the form
5853
Form form = new Form("sample");
5954

@@ -578,7 +573,7 @@ public void validate() {
578573
assertEquals("steve", field.getValue());
579574

580575
// assert that the form error was reset, leaving form in a valid state
581-
assertEquals(null, form.getError());
576+
assertNull(form.getError());
582577
assertTrue(form.isValid());
583578
}
584579

@@ -738,6 +733,9 @@ public void testGetResourceAsStream () throws IOException {
738733
assertEquals("title=Title", IOUtils.toString(ClickUtils.getResourceAsStream("TestPage.properties", getClass())));
739734
assertEquals("title=Title", IOUtils.toString(ClickUtils.getResourceAsStream("/WEB-INF/TestPage.properties", getClass())));
740735
assertEquals("title=Title", IOUtils.toString(ClickUtils.getResourceAsStream("WEB-INF/TestPage.properties", getClass())));
741-
assertTrue(IOUtils.toString(ClickUtils.getResource("/WEB-INF/click/table.css", getClass())).contains("table.blue1"));
736+
assertTrue(IOUtils.toString(ClickUtils.getResourceAsStream("/WEB-INF/click/table.css", getClass())).contains("table.blue1"));
737+
assertTrue(IOUtils.toString(ClickUtils.getResourceAsStream("WEB-INF/click/table.css", getClass())).contains("table.blue1"));
738+
assertTrue(IOUtils.toString(ClickUtils.getResourceAsStream("/click/table.css", getClass())).contains("table.blue1"));
739+
assertTrue(IOUtils.toString(ClickUtils.getResourceAsStream("click/table.css", getClass())).contains("table.blue1"));
742740
}
743741
}

0 commit comments

Comments
 (0)