From 3e97cdbc3bf1f462c8ed84dcde3a735aa99a2d2e Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 3 Nov 2014 09:55:52 +0000 Subject: [PATCH] Trim some fat from groovy templates The old resolver class is deprecated already, and there's no reason to continue to support the View and View resolver implementations from Boot 1.1, so I removed those. Also there is no need to support older versions of Groovy, so I removed a config class that was conditional on a groovy version (by looking for the markup support). Fixes gh-1578 --- .../GroovyTemplateAutoConfiguration.java | 69 +------------- .../template/web/GroovyTemplateView.java | 64 ------------- .../web/GroovyTemplateViewResolver.java | 90 ------------------- 3 files changed, 2 insertions(+), 221 deletions(-) delete mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/web/GroovyTemplateView.java delete mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/web/GroovyTemplateViewResolver.java diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java index 39f69a0e42c..0e581b53580 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java @@ -16,30 +16,19 @@ package org.springframework.boot.autoconfigure.groovy.template; -import groovy.text.SimpleTemplateEngine; -import groovy.text.TemplateEngine; import groovy.text.markup.MarkupTemplateEngine; -import java.net.URL; -import java.net.URLClassLoader; -import java.util.ArrayList; -import java.util.List; - import javax.annotation.PostConstruct; import javax.servlet.Servlet; -import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; -import org.springframework.boot.autoconfigure.groovy.template.web.GroovyTemplateViewResolver; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.i18n.LocaleContextHolder; @@ -65,13 +54,13 @@ import org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver; * @since 1.1.0 */ @Configuration -@ConditionalOnClass(TemplateEngine.class) +@ConditionalOnClass(MarkupTemplateEngine.class) @AutoConfigureAfter(WebMvcAutoConfiguration.class) @EnableConfigurationProperties(GroovyTemplateProperties.class) public class GroovyTemplateAutoConfiguration { @Configuration - @ConditionalOnClass({ MarkupTemplateEngine.class, GroovyMarkupConfigurer.class }) + @ConditionalOnClass(GroovyMarkupConfigurer.class) public static class GroovyMarkupConfiguration { @Autowired @@ -110,48 +99,6 @@ public class GroovyTemplateAutoConfiguration { } - @Configuration - @ConditionalOnMissingClass(name = "groovy.text.markup.MarkupTemplateEngine") - public static class GroovySimpleTemplateEngineConfiguration implements - BeanClassLoaderAware { - - @Autowired - private GroovyTemplateProperties properties; - - @Autowired - private ApplicationContext resourceLoader; - - private ClassLoader classLoader = GroovyWebConfiguration.class.getClassLoader(); - - @Override - public void setBeanClassLoader(ClassLoader classLoader) { - this.classLoader = classLoader; - } - - protected ClassLoader createParentLoaderForTemplates() throws Exception { - Resource[] resources = this.resourceLoader.getResources(this.properties - .getPrefix()); - if (resources.length > 0) { - List urls = new ArrayList(); - for (Resource resource : resources) { - if (resource.exists()) { - urls.add(resource.getURL()); - } - } - return new URLClassLoader(urls.toArray(new URL[urls.size()]), - this.classLoader); - } - return this.classLoader; - } - - @Bean - @ConditionalOnMissingBean(TemplateEngine.class) - public SimpleTemplateEngine groovyTemplateEngine() throws Exception { - return new SimpleTemplateEngine(createParentLoaderForTemplates()); - } - - } - @Configuration @ConditionalOnClass({ Servlet.class, LocaleContextHolder.class, UrlBasedViewResolver.class }) @@ -163,24 +110,12 @@ public class GroovyTemplateAutoConfiguration { @Bean @ConditionalOnMissingBean(name = "groovyMarkupViewResolver") - @ConditionalOnClass(MarkupTemplateEngine.class) public GroovyMarkupViewResolver groovyMarkupViewResolver() { GroovyMarkupViewResolver resolver = new GroovyMarkupViewResolver(); configureViewResolver(resolver); return resolver; } - @Bean - @ConditionalOnMissingBean(name = "groovyTemplateViewResolver") - @ConditionalOnMissingClass(MarkupTemplateEngine.class) - public GroovyTemplateViewResolver groovyTemplateViewResolver(TemplateEngine engine) { - GroovyTemplateViewResolver resolver = new GroovyTemplateViewResolver(); - configureViewResolver(resolver); - resolver.setPrefix(this.properties.getPrefix()); - resolver.setTemplateEngine(engine); - return resolver; - } - private void configureViewResolver(UrlBasedViewResolver resolver) { resolver.setSuffix(this.properties.getSuffix()); resolver.setCache(this.properties.isCache()); diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/web/GroovyTemplateView.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/web/GroovyTemplateView.java deleted file mode 100644 index d9e2c8fe74a..00000000000 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/web/GroovyTemplateView.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2012-2014 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.boot.autoconfigure.groovy.template.web; - -import groovy.text.Template; - -import java.io.BufferedWriter; -import java.util.Map; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.springframework.web.servlet.View; -import org.springframework.web.servlet.support.RequestContext; -import org.springframework.web.servlet.view.AbstractUrlBasedView; - -/** - * Spring MVC {@link View} that can render Groovy templates. - * - * @author Dave Syer - * @since 1.1.0 - */ -public class GroovyTemplateView extends AbstractUrlBasedView { - - private final Template template; - - public GroovyTemplateView(Template template) { - this.template = template; - } - - @Override - protected void renderMergedOutputModel(Map model, - HttpServletRequest request, HttpServletResponse response) throws Exception { - applyContentType(response); - this.template.make(model).writeTo(new BufferedWriter(response.getWriter())); - } - - /** - * Apply this view's content type as specified in the "contentType" bean property to - * the given response. - * @param response current HTTP response - * @see #setContentType - */ - protected void applyContentType(HttpServletResponse response) { - if (response.getContentType() == null) { - response.setContentType(getContentType()); - } - } - -} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/web/GroovyTemplateViewResolver.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/web/GroovyTemplateViewResolver.java deleted file mode 100644 index 42f36163eb7..00000000000 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/web/GroovyTemplateViewResolver.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright 2012-2014 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.boot.autoconfigure.groovy.template.web; - -import groovy.text.SimpleTemplateEngine; -import groovy.text.Template; -import groovy.text.TemplateEngine; - -import java.util.Locale; - -import org.springframework.beans.propertyeditors.LocaleEditor; -import org.springframework.core.io.Resource; -import org.springframework.web.servlet.View; -import org.springframework.web.servlet.ViewResolver; -import org.springframework.web.servlet.view.UrlBasedViewResolver; - -/** - * Spring MVC {@link ViewResolver} for {@link GroovyTemplateView}s. - * - * @author Dave Syer - * @since 1.1.0 - * @see GroovyTemplateView - */ -public class GroovyTemplateViewResolver extends UrlBasedViewResolver { - - private TemplateEngine engine = new SimpleTemplateEngine(); - - public GroovyTemplateViewResolver() { - setViewClass(GroovyTemplateView.class); - } - - /** - * @param engine the engine to set - */ - public void setTemplateEngine(TemplateEngine engine) { - this.engine = engine; - } - - @Override - protected View loadView(String viewName, Locale locale) throws Exception { - Resource resource = resolveResource(viewName, locale); - if (resource == null) { - return null; - } - Template template = this.engine.createTemplate(resource.getURL()); - GroovyTemplateView view = new GroovyTemplateView(template); - view.setApplicationContext(getApplicationContext()); - view.setServletContext(getServletContext()); - view.setContentType(getContentType()); - return view; - } - - private Resource resolveResource(String viewName, Locale locale) { - String l10n = ""; - if (locale != null) { - LocaleEditor localeEditor = new LocaleEditor(); - localeEditor.setValue(locale); - l10n = "_" + localeEditor.getAsText(); - } - return resolveFromLocale(viewName, l10n); - } - - private Resource resolveFromLocale(String viewName, String locale) { - Resource resource = getApplicationContext().getResource( - getPrefix() + viewName + locale + getSuffix()); - if (resource == null || !resource.exists()) { - if (locale.isEmpty()) { - return null; - } - int index = locale.lastIndexOf("_"); - return resolveFromLocale(viewName, locale.substring(0, index)); - } - return resource; - } - -}