Skip to content

Commit f532de9

Browse files
committed
Revised checkResource implementation
Issue: SPR-14729 (cherry picked from commit ca17edd)
1 parent 9e5435e commit f532de9

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;
@@ -194,19 +193,6 @@ public void setResourceLoaderPath(String resourceLoaderPath) {
194193
}
195194
}
196195

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

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

268-
269254
protected ScriptEngine getEngine() {
270255
if (Boolean.FALSE.equals(this.sharedEngine)) {
271256
Map<Object, ScriptEngine> engines = enginesHolder.get();
@@ -300,14 +285,17 @@ protected ScriptEngine createEngineFromName() {
300285

301286
protected void loadScripts(ScriptEngine engine) {
302287
if (!ObjectUtils.isEmpty(this.scripts)) {
303-
try {
304-
for (String script : this.scripts) {
305-
Resource resource = getResource(script);
288+
for (String script : this.scripts) {
289+
Resource resource = getResource(script);
290+
if (resource == null) {
291+
throw new IllegalStateException("Script resource [" + script + "] not found");
292+
}
293+
try {
306294
engine.eval(new InputStreamReader(resource.getInputStream()));
307295
}
308-
}
309-
catch (Exception ex) {
310-
throw new IllegalStateException("Failed to load script", ex);
296+
catch (Throwable ex) {
297+
throw new IllegalStateException("Failed to evaluate script [" + script + "]", ex);
298+
}
311299
}
312300
}
313301
}
@@ -319,7 +307,7 @@ protected Resource getResource(String location) {
319307
return resource;
320308
}
321309
}
322-
throw new IllegalStateException("Resource [" + location + "] not found");
310+
return null;
323311
}
324312

325313
protected ScriptTemplateConfig autodetectViewConfig() throws BeansException {
@@ -334,6 +322,12 @@ protected ScriptTemplateConfig autodetectViewConfig() throws BeansException {
334322
}
335323
}
336324

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

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

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)