Skip to content

Commit ca17edd

Browse files
committed
Revised checkResource implementation
Issue: SPR-14729
1 parent c26bf87 commit ca17edd

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateView.java

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.web.servlet.view.script;
1818

19-
import java.io.FileNotFoundException;
2019
import java.io.IOException;
2120
import java.io.InputStreamReader;
2221
import java.nio.charset.Charset;
@@ -193,19 +192,6 @@ public void setResourceLoaderPath(String resourceLoaderPath) {
193192
}
194193
}
195194

196-
@Override
197-
public boolean checkResource(Locale locale) throws Exception {
198-
try {
199-
getTemplate(getUrl());
200-
return true;
201-
}
202-
catch (IllegalStateException exc) {
203-
if (logger.isDebugEnabled()) {
204-
logger.debug("No ScriptTemplate view found for URL: " + getUrl());
205-
}
206-
return false;
207-
}
208-
}
209195

210196
@Override
211197
protected void initApplicationContext(ApplicationContext context) {
@@ -264,7 +250,6 @@ else if (this.engine != null) {
264250
Assert.isTrue(this.renderFunction != null, "The 'renderFunction' property must be defined.");
265251
}
266252

267-
268253
protected ScriptEngine getEngine() {
269254
if (Boolean.FALSE.equals(this.sharedEngine)) {
270255
Map<Object, ScriptEngine> engines = enginesHolder.get();
@@ -299,14 +284,17 @@ protected ScriptEngine createEngineFromName() {
299284

300285
protected void loadScripts(ScriptEngine engine) {
301286
if (!ObjectUtils.isEmpty(this.scripts)) {
302-
try {
303-
for (String script : this.scripts) {
304-
Resource resource = getResource(script);
287+
for (String script : this.scripts) {
288+
Resource resource = getResource(script);
289+
if (resource == null) {
290+
throw new IllegalStateException("Script resource [" + script + "] not found");
291+
}
292+
try {
305293
engine.eval(new InputStreamReader(resource.getInputStream()));
306294
}
307-
}
308-
catch (Exception ex) {
309-
throw new IllegalStateException("Failed to load script", ex);
295+
catch (Throwable ex) {
296+
throw new IllegalStateException("Failed to evaluate script [" + script + "]", ex);
297+
}
310298
}
311299
}
312300
}
@@ -318,7 +306,7 @@ protected Resource getResource(String location) {
318306
return resource;
319307
}
320308
}
321-
throw new IllegalStateException("Resource [" + location + "] not found");
309+
return null;
322310
}
323311

324312
protected ScriptTemplateConfig autodetectViewConfig() throws BeansException {
@@ -333,6 +321,12 @@ protected ScriptTemplateConfig autodetectViewConfig() throws BeansException {
333321
}
334322
}
335323

324+
325+
@Override
326+
public boolean checkResource(Locale locale) throws Exception {
327+
return (getResource(getUrl()) != null);
328+
}
329+
336330
@Override
337331
protected void prepareResponse(HttpServletRequest request, HttpServletResponse response) {
338332
super.prepareResponse(request, response);
@@ -369,6 +363,9 @@ protected void renderMergedOutputModel(Map<String, Object> model, HttpServletReq
369363

370364
protected String getTemplate(String path) throws IOException {
371365
Resource resource = getResource(path);
366+
if (resource == null) {
367+
throw new IllegalStateException("Template resource [" + path + "] not found");
368+
}
372369
InputStreamReader reader = new InputStreamReader(resource.getInputStream(), this.charset);
373370
return FileCopyUtils.copyToString(reader);
374371
}

spring-webmvc/src/test/java/org/springframework/web/servlet/view/script/ScriptTemplateViewTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import org.springframework.web.context.support.StaticWebApplicationContext;
4545
import org.springframework.web.servlet.DispatcherServlet;
4646

47-
import static org.hamcrest.Matchers.*;
4847
import static org.junit.Assert.*;
4948
import static org.mockito.BDDMockito.*;
5049

@@ -64,6 +63,7 @@ public class ScriptTemplateViewTests {
6463
@Rule
6564
public ExpectedException expectedException = ExpectedException.none();
6665

66+
6767
@Before
6868
public void setup() {
6969
this.configurer = new ScriptTemplateConfigurer();

0 commit comments

Comments
 (0)