Skip to content

Commit 285cae1

Browse files
committed
fix classpath resource loading
1 parent 8cad74c commit 285cae1

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

click-velocity/src/main/java/org/apache/velocity/tools/view/ClickVelocityWebappResourceLoader.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public InputStream getResourceStream (String name) throws ResourceNotFoundExcept
7878
throw new ResourceNotFoundException("WebappResourceLoader: No template name provided");
7979
}
8080
if (name.startsWith("/")){
81-
name = name.substring(1).trim();
81+
name = name.substring(1);
8282
}
8383
try {
8484
val is = servletContext.getResourceAsStream('/'+ name);// [META-INF/resources] /name
@@ -124,40 +124,44 @@ public boolean isSourceModified (Resource resource) {
124124
rootPath is null if the servlet container cannot translate the virtual path to a real path for any reason
125125
(such as when the content is being made available from a .war archive)
126126
127-
@param resource Resource the resource to check
127+
@param templateResource Resource the resource to check
128128
@return long The time when the resource was last modified or 0 if the file can't be read
129129
*/
130130
@Override
131-
public long getLastModified (Resource resource) {
131+
public long getLastModified (Resource templateResource) {
132+
String name = templateResource.getName();
133+
if (name.startsWith("/")){
134+
name = name.substring(1);
135+
}
132136
try {
133137
String rootPath = servletContext.getRealPath("/");
134-
if (rootPath != null) return new File(rootPath, resource.getName()).lastModified();// ? cachedFile.canRead()
138+
if (rootPath != null) return new File(rootPath, name).lastModified();// ? cachedFile.canRead()
135139
} catch (Exception e){
136-
log.warn("getLastModified: Could not get last modified time for {}", resource.getName(), e);
140+
log.warn("getLastModified: Could not get last modified time for {}", name, e);
137141
}
138142
try {
139-
URL url = servletContext.getResource('/'+resource.getName());// web /
143+
URL url = servletContext.getResource('/'+name);// web /
140144
if (url != null) return url.openConnection().getLastModified();
141145
} catch (Exception e){
142-
log.warn("getLastModified: Could not get last modified time for {}", resource.getName(), e);
146+
log.warn("getLastModified: Could not get last modified time for {}", name, e);
143147
}
144148
try {
145-
URL url = ClickUtils.getResource(resource.getName(), getClass());// /
149+
URL url = ClickUtils.getResource(name, getClass());// /
146150
if (url != null) return url.openConnection().getLastModified();
147151
} catch (Exception e){
148-
log.warn("getLastModified: Could not get last modified time for {}", resource.getName(), e);
152+
log.warn("getLastModified: Could not get last modified time for {}", name, e);
149153
}
150154
try {
151-
URL url = ClickUtils.getResource("templates/"+resource.getName(), getClass());
155+
URL url = ClickUtils.getResource("templates/"+name, getClass());
152156
if (url != null) return url.openConnection().getLastModified();
153157
} catch (Exception e){
154-
log.warn("getLastModified: Could not get last modified time for {}", resource.getName(), e);
158+
log.warn("getLastModified: Could not get last modified time for {}", name, e);
155159
}
156160
try {
157-
URL url = ClickUtils.getResource("static/"+ resource.getName(), getClass());
161+
URL url = ClickUtils.getResource("static/"+ name, getClass());
158162
if (url != null) return url.openConnection().getLastModified();
159163
} catch (Exception e){
160-
log.warn("getLastModified: Could not get last modified time for {}", resource.getName(), e);
164+
log.warn("getLastModified: Could not get last modified time for {}", name, e);
161165
}
162166
return 0;
163167
}

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2454,11 +2454,12 @@ public static InputStream getResourceAsStream (@NonNull String name, @NonNull Cl
24542454
is = ClickUtils.class.getResourceAsStream(name);
24552455
if (is != null){ return is; }
24562456

2457-
if (name.startsWith("/WEB-INF/")){// hack old Click to work with Boot
2458-
is = getResourceAsStream(name.substring(9), aClass);
2457+
if (name.startsWith("WEB-INF/")){// hack old Click to work with Boot
2458+
name = name.substring(8);// ^ cut
2459+
is = getResourceAsStream(name, aClass);
24592460
if (is != null){ return is; }
24602461

2461-
return getResourceAsStream("META-INF/resources"+name.substring(9), aClass);
2462+
return getResourceAsStream("META-INF/resources/"+name, aClass);
24622463
}
24632464
return null;// not found anywhere
24642465
}
@@ -2508,7 +2509,17 @@ public static URL getResource (@NonNull String name, Class<?> aClass) {
25082509
URL url = aClass.getResource(name);//3 caller class
25092510
if (url != null){ return url; }
25102511
}
2511-
return ClickUtils.class.getResource(name);//4 click-lib class
2512+
URL url = ClickUtils.class.getResource(name);//4 click-lib class
2513+
if (url != null){ return url; }
2514+
2515+
if (name.startsWith("WEB-INF/")){// hack old Click to work with Boot
2516+
name = name.substring(8);// ^ cut
2517+
url = getResource(name, aClass);
2518+
if (url != null){ return url; }
2519+
2520+
return getResource("META-INF/resources/"+name, aClass);
2521+
}
2522+
return null;// not found anywhere
25122523
}
25132524

25142525
/**

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,16 +712,21 @@ public void testGetResource () {
712712
assertNull(Thread.currentThread().getContextClassLoader().getResource("/click-page.properties"));// Class loaders supports only absolute without /
713713
assertNull(getClass().getResource("click-page.properties"));// Class supports relative and /absolute
714714
assertEquals("click-page.properties", fileName(getClass().getResource("/click-page.properties")));
715+
assertEquals("click-page.properties", fileName(ClickUtils.getResource("/WEB-INF/click-page.properties", getClass())));
715716

716717
assertNull("click-page.properties", ClickUtils.getResource("static/testResAbs.xxx", getClass()));// not found (not exists)
717718

718719
assertEquals("testResAbs.txt", fileName(ClickUtils.getResource("static/testResAbs.txt", getClass())));
719720
assertEquals("TestPage.properties", fileName(ClickUtils.getResource("TestPage.properties", getClass())));
721+
722+
assertEquals("table.css", fileName(ClickUtils.getResource("/WEB-INF/click/table.css", getClass())));
720723
}
721724

722725
public void testGetResourceAsStream () throws IOException {
723726
// file:/C:/TEMP/Apache-Click/click/out/test/resources/click-page.properties
724727
assertEquals("version=Version 0.21", IOUtils.toString(ClickUtils.getResourceAsStream("click-page.properties", getClass())));
728+
assertEquals("version=Version 0.21", IOUtils.toString(ClickUtils.getResourceAsStream("/WEB-INF/click-page.properties", getClass())));
729+
assertEquals("version=Version 0.21", IOUtils.toString(ClickUtils.getResourceAsStream("WEB-INF/click-page.properties", getClass())));
725730
assertEquals("version=Version 0.21", IOUtils.toString(Thread.currentThread().getContextClassLoader().getResourceAsStream("click-page.properties")));
726731
assertNull(Thread.currentThread().getContextClassLoader().getResourceAsStream("/click-page.properties"));// Class loaders supports only absolute without /
727732
assertNull(getClass().getResourceAsStream("click-page.properties"));// Class supports relative and /absolute
@@ -731,5 +736,8 @@ public void testGetResourceAsStream () throws IOException {
731736

732737
assertEquals("ABS", IOUtils.toString(ClickUtils.getResourceAsStream("static/testResAbs.txt", getClass())));
733738
assertEquals("title=Title", IOUtils.toString(ClickUtils.getResourceAsStream("TestPage.properties", getClass())));
739+
assertEquals("title=Title", IOUtils.toString(ClickUtils.getResourceAsStream("/WEB-INF/TestPage.properties", getClass())));
740+
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"));
734742
}
735743
}

0 commit comments

Comments
 (0)