Revised checkResource implementation

Issue: SPR-14729
This commit is contained in:
Juergen Hoeller 2016-09-18 21:04:37 +02:00
parent c26bf871b7
commit ca17edd5ac
2 changed files with 20 additions and 23 deletions

View File

@ -16,7 +16,6 @@
package org.springframework.web.servlet.view.script; package org.springframework.web.servlet.view.script;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.charset.Charset; import java.nio.charset.Charset;
@ -193,19 +192,6 @@ public class ScriptTemplateView extends AbstractUrlBasedView {
} }
} }
@Override
public boolean checkResource(Locale locale) throws Exception {
try {
getTemplate(getUrl());
return true;
}
catch (IllegalStateException exc) {
if (logger.isDebugEnabled()) {
logger.debug("No ScriptTemplate view found for URL: " + getUrl());
}
return false;
}
}
@Override @Override
protected void initApplicationContext(ApplicationContext context) { protected void initApplicationContext(ApplicationContext context) {
@ -264,7 +250,6 @@ public class ScriptTemplateView extends AbstractUrlBasedView {
Assert.isTrue(this.renderFunction != null, "The 'renderFunction' property must be defined."); Assert.isTrue(this.renderFunction != null, "The 'renderFunction' property must be defined.");
} }
protected ScriptEngine getEngine() { protected ScriptEngine getEngine() {
if (Boolean.FALSE.equals(this.sharedEngine)) { if (Boolean.FALSE.equals(this.sharedEngine)) {
Map<Object, ScriptEngine> engines = enginesHolder.get(); Map<Object, ScriptEngine> engines = enginesHolder.get();
@ -299,14 +284,17 @@ public class ScriptTemplateView extends AbstractUrlBasedView {
protected void loadScripts(ScriptEngine engine) { protected void loadScripts(ScriptEngine engine) {
if (!ObjectUtils.isEmpty(this.scripts)) { if (!ObjectUtils.isEmpty(this.scripts)) {
try { for (String script : this.scripts) {
for (String script : this.scripts) { Resource resource = getResource(script);
Resource resource = getResource(script); if (resource == null) {
throw new IllegalStateException("Script resource [" + script + "] not found");
}
try {
engine.eval(new InputStreamReader(resource.getInputStream())); engine.eval(new InputStreamReader(resource.getInputStream()));
} }
} catch (Throwable ex) {
catch (Exception ex) { throw new IllegalStateException("Failed to evaluate script [" + script + "]", ex);
throw new IllegalStateException("Failed to load script", ex); }
} }
} }
} }
@ -318,7 +306,7 @@ public class ScriptTemplateView extends AbstractUrlBasedView {
return resource; return resource;
} }
} }
throw new IllegalStateException("Resource [" + location + "] not found"); return null;
} }
protected ScriptTemplateConfig autodetectViewConfig() throws BeansException { protected ScriptTemplateConfig autodetectViewConfig() throws BeansException {
@ -333,6 +321,12 @@ public class ScriptTemplateView extends AbstractUrlBasedView {
} }
} }
@Override
public boolean checkResource(Locale locale) throws Exception {
return (getResource(getUrl()) != null);
}
@Override @Override
protected void prepareResponse(HttpServletRequest request, HttpServletResponse response) { protected void prepareResponse(HttpServletRequest request, HttpServletResponse response) {
super.prepareResponse(request, response); super.prepareResponse(request, response);
@ -369,6 +363,9 @@ public class ScriptTemplateView extends AbstractUrlBasedView {
protected String getTemplate(String path) throws IOException { protected String getTemplate(String path) throws IOException {
Resource resource = getResource(path); Resource resource = getResource(path);
if (resource == null) {
throw new IllegalStateException("Template resource [" + path + "] not found");
}
InputStreamReader reader = new InputStreamReader(resource.getInputStream(), this.charset); InputStreamReader reader = new InputStreamReader(resource.getInputStream(), this.charset);
return FileCopyUtils.copyToString(reader); return FileCopyUtils.copyToString(reader);
} }

View File

@ -44,7 +44,6 @@ import org.springframework.mock.web.test.MockServletContext;
import org.springframework.web.context.support.StaticWebApplicationContext; import org.springframework.web.context.support.StaticWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.DispatcherServlet;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*; import static org.mockito.BDDMockito.*;
@ -64,6 +63,7 @@ public class ScriptTemplateViewTests {
@Rule @Rule
public ExpectedException expectedException = ExpectedException.none(); public ExpectedException expectedException = ExpectedException.none();
@Before @Before
public void setup() { public void setup() {
this.configurer = new ScriptTemplateConfigurer(); this.configurer = new ScriptTemplateConfigurer();