Remove support for Velocity following its deprecation in 1.4
Closes gh-6971pull/7075/head
parent
464915d2cc
commit
2a035d0748
@ -1,165 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.velocity;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.servlet.Servlet;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.velocity.app.VelocityEngine;
|
||||
import org.apache.velocity.exception.VelocityException;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.autoconfigure.template.TemplateLocation;
|
||||
import org.springframework.boot.autoconfigure.web.ConditionalOnEnabledResourceChain;
|
||||
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.web.servlet.view.velocity.EmbeddedVelocityViewResolver;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.ui.velocity.VelocityEngineFactory;
|
||||
import org.springframework.ui.velocity.VelocityEngineFactoryBean;
|
||||
import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityConfig;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for Velocity.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Brian Clozel
|
||||
* @since 1.1.0
|
||||
* @deprecated as of 1.4 following the deprecation of Velocity support in Spring Framework
|
||||
* 4.3
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ VelocityEngine.class, VelocityEngineFactory.class })
|
||||
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
|
||||
@EnableConfigurationProperties(VelocityProperties.class)
|
||||
@Deprecated
|
||||
public class VelocityAutoConfiguration {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(VelocityAutoConfiguration.class);
|
||||
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
private final VelocityProperties properties;
|
||||
|
||||
public VelocityAutoConfiguration(ApplicationContext applicationContext,
|
||||
VelocityProperties properties) {
|
||||
this.applicationContext = applicationContext;
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void checkTemplateLocationExists() {
|
||||
if (this.properties.isCheckTemplateLocation()) {
|
||||
TemplateLocation location = new TemplateLocation(
|
||||
this.properties.getResourceLoaderPath());
|
||||
if (!location.exists(this.applicationContext)) {
|
||||
logger.warn("Cannot find template location: " + location
|
||||
+ " (please add some templates, check your Velocity "
|
||||
+ "configuration, or set spring.velocity."
|
||||
+ "checkTemplateLocation=false)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
protected static class VelocityConfiguration {
|
||||
|
||||
@Autowired
|
||||
protected VelocityProperties properties;
|
||||
|
||||
protected void applyProperties(VelocityEngineFactory factory) {
|
||||
factory.setResourceLoaderPath(this.properties.getResourceLoaderPath());
|
||||
factory.setPreferFileSystemAccess(this.properties.isPreferFileSystemAccess());
|
||||
Properties velocityProperties = new Properties();
|
||||
velocityProperties.setProperty("input.encoding",
|
||||
this.properties.getCharsetName());
|
||||
velocityProperties.putAll(this.properties.getProperties());
|
||||
factory.setVelocityProperties(velocityProperties);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnNotWebApplication
|
||||
@Deprecated
|
||||
public static class VelocityNonWebConfiguration extends VelocityConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public VelocityEngineFactoryBean velocityConfiguration() {
|
||||
VelocityEngineFactoryBean velocityEngineFactoryBean = new VelocityEngineFactoryBean();
|
||||
applyProperties(velocityEngineFactoryBean);
|
||||
return velocityEngineFactoryBean;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnClass(Servlet.class)
|
||||
@ConditionalOnWebApplication
|
||||
@Deprecated
|
||||
public static class VelocityWebConfiguration extends VelocityConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(VelocityConfig.class)
|
||||
public VelocityConfigurer velocityConfigurer() {
|
||||
VelocityConfigurer configurer = new VelocityConfigurer();
|
||||
applyProperties(configurer);
|
||||
return configurer;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public VelocityEngine velocityEngine(VelocityConfigurer configurer)
|
||||
throws VelocityException, IOException {
|
||||
return configurer.getVelocityEngine();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(name = "velocityViewResolver")
|
||||
@ConditionalOnProperty(name = "spring.velocity.enabled", matchIfMissing = true)
|
||||
public EmbeddedVelocityViewResolver velocityViewResolver() {
|
||||
EmbeddedVelocityViewResolver resolver = new EmbeddedVelocityViewResolver();
|
||||
this.properties.applyToViewResolver(resolver);
|
||||
return resolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnEnabledResourceChain
|
||||
public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
|
||||
return new ResourceUrlEncodingFilter();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,138 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.velocity;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.boot.autoconfigure.template.AbstractTemplateViewResolverProperties;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityViewResolver;
|
||||
|
||||
/**
|
||||
* {@link ConfigurationProperties} for configuring Velocity.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @since 1.1.0
|
||||
* @deprecated as of 1.4 following the deprecation of Velocity support in Spring Framework
|
||||
* 4.3
|
||||
*/
|
||||
@Deprecated
|
||||
@ConfigurationProperties(prefix = "spring.velocity")
|
||||
public class VelocityProperties extends AbstractTemplateViewResolverProperties {
|
||||
|
||||
public static final String DEFAULT_RESOURCE_LOADER_PATH = "classpath:/templates/";
|
||||
|
||||
public static final String DEFAULT_PREFIX = "";
|
||||
|
||||
public static final String DEFAULT_SUFFIX = ".vm";
|
||||
|
||||
/**
|
||||
* Name of the DateTool helper object to expose in the Velocity context of the view.
|
||||
*/
|
||||
private String dateToolAttribute;
|
||||
|
||||
/**
|
||||
* Name of the NumberTool helper object to expose in the Velocity context of the view.
|
||||
*/
|
||||
private String numberToolAttribute;
|
||||
|
||||
/**
|
||||
* Additional velocity properties.
|
||||
*/
|
||||
private Map<String, String> properties = new HashMap<String, String>();
|
||||
|
||||
/**
|
||||
* Template path.
|
||||
*/
|
||||
private String resourceLoaderPath = DEFAULT_RESOURCE_LOADER_PATH;
|
||||
|
||||
/**
|
||||
* Velocity Toolbox config location, for example "/WEB-INF/toolbox.xml". Automatically
|
||||
* loads a Velocity Tools toolbox definition file and expose all defined tools in the
|
||||
* specified scopes.
|
||||
*/
|
||||
private String toolboxConfigLocation;
|
||||
|
||||
/**
|
||||
* Prefer file system access for template loading. File system access enables hot
|
||||
* detection of template changes.
|
||||
*/
|
||||
private boolean preferFileSystemAccess = true;
|
||||
|
||||
public VelocityProperties() {
|
||||
super(DEFAULT_PREFIX, DEFAULT_SUFFIX);
|
||||
}
|
||||
|
||||
public String getDateToolAttribute() {
|
||||
return this.dateToolAttribute;
|
||||
}
|
||||
|
||||
public void setDateToolAttribute(String dateToolAttribute) {
|
||||
this.dateToolAttribute = dateToolAttribute;
|
||||
}
|
||||
|
||||
public String getNumberToolAttribute() {
|
||||
return this.numberToolAttribute;
|
||||
}
|
||||
|
||||
public void setNumberToolAttribute(String numberToolAttribute) {
|
||||
this.numberToolAttribute = numberToolAttribute;
|
||||
}
|
||||
|
||||
public Map<String, String> getProperties() {
|
||||
return this.properties;
|
||||
}
|
||||
|
||||
public void setProperties(Map<String, String> properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
public String getResourceLoaderPath() {
|
||||
return this.resourceLoaderPath;
|
||||
}
|
||||
|
||||
public void setResourceLoaderPath(String resourceLoaderPath) {
|
||||
this.resourceLoaderPath = resourceLoaderPath;
|
||||
}
|
||||
|
||||
public String getToolboxConfigLocation() {
|
||||
return this.toolboxConfigLocation;
|
||||
}
|
||||
|
||||
public void setToolboxConfigLocation(String toolboxConfigLocation) {
|
||||
this.toolboxConfigLocation = toolboxConfigLocation;
|
||||
}
|
||||
|
||||
public boolean isPreferFileSystemAccess() {
|
||||
return this.preferFileSystemAccess;
|
||||
}
|
||||
|
||||
public void setPreferFileSystemAccess(boolean preferFileSystemAccess) {
|
||||
this.preferFileSystemAccess = preferFileSystemAccess;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyToViewResolver(Object viewResolver) {
|
||||
super.applyToViewResolver(viewResolver);
|
||||
VelocityViewResolver resolver = (VelocityViewResolver) viewResolver;
|
||||
resolver.setToolboxConfigLocation(getToolboxConfigLocation());
|
||||
resolver.setDateToolAttribute(getDateToolAttribute());
|
||||
resolver.setNumberToolAttribute(getNumberToolAttribute());
|
||||
}
|
||||
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.velocity;
|
||||
|
||||
import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider;
|
||||
import org.springframework.boot.bind.RelaxedPropertyResolver;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.PropertyResolver;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* {@link TemplateAvailabilityProvider} that provides availability information for
|
||||
* Velocity view templates.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @since 1.1.0
|
||||
* @deprecated as of 1.4 following the deprecation of Velocity support in Spring Framework
|
||||
* 4.3
|
||||
*/
|
||||
@Deprecated
|
||||
public class VelocityTemplateAvailabilityProvider
|
||||
implements TemplateAvailabilityProvider {
|
||||
|
||||
@Override
|
||||
public boolean isTemplateAvailable(String view, Environment environment,
|
||||
ClassLoader classLoader, ResourceLoader resourceLoader) {
|
||||
if (ClassUtils.isPresent("org.apache.velocity.app.VelocityEngine", classLoader)) {
|
||||
PropertyResolver resolver = new RelaxedPropertyResolver(environment,
|
||||
"spring.velocity.");
|
||||
String loaderPath = resolver.getProperty("resource-loader-path",
|
||||
VelocityProperties.DEFAULT_RESOURCE_LOADER_PATH);
|
||||
String prefix = resolver.getProperty("prefix",
|
||||
VelocityProperties.DEFAULT_PREFIX);
|
||||
String suffix = resolver.getProperty("suffix",
|
||||
VelocityProperties.DEFAULT_SUFFIX);
|
||||
return resourceLoader.getResource(loaderPath + prefix + view + suffix)
|
||||
.exists();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Auto-configuration for Velocity.
|
||||
* @deprecated as of 1.4 following the deprecation of Velocity support in Spring Framework
|
||||
* 4.3
|
||||
*/
|
||||
package org.springframework.boot.autoconfigure.velocity;
|
@ -1,246 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.velocity;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.velocity.Template;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
import org.apache.velocity.app.VelocityEngine;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.test.rule.OutputCapture;
|
||||
import org.springframework.boot.test.util.EnvironmentTestUtils;
|
||||
import org.springframework.boot.web.servlet.view.velocity.EmbeddedVelocityViewResolver;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.servlet.View;
|
||||
import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter;
|
||||
import org.springframework.web.servlet.support.RequestContext;
|
||||
import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityViewResolver;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
|
||||
/**
|
||||
* Tests for {@link VelocityAutoConfiguration}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class VelocityAutoConfigurationTests {
|
||||
|
||||
@Rule
|
||||
public OutputCapture output = new OutputCapture();
|
||||
|
||||
private AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
|
||||
|
||||
@Before
|
||||
public void setupContext() {
|
||||
this.context.setServletContext(new MockServletContext());
|
||||
}
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultConfiguration() {
|
||||
registerAndRefreshContext();
|
||||
assertThat(this.context.getBean(VelocityViewResolver.class)).isNotNull();
|
||||
assertThat(this.context.getBean(VelocityConfigurer.class)).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nonExistentTemplateLocation() {
|
||||
registerAndRefreshContext(
|
||||
"spring.velocity.resourceLoaderPath:" + "classpath:/does-not-exist/");
|
||||
this.output.expect(containsString("Cannot find template location"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyTemplateLocation() {
|
||||
new File("target/test-classes/templates/empty-directory").mkdir();
|
||||
registerAndRefreshContext("spring.velocity.resourceLoaderPath:"
|
||||
+ "classpath:/templates/empty-directory/");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultViewResolution() throws Exception {
|
||||
registerAndRefreshContext();
|
||||
MockHttpServletResponse response = render("home");
|
||||
String result = response.getContentAsString();
|
||||
assertThat(result).contains("home");
|
||||
assertThat(response.getContentType()).isEqualTo("text/html;charset=UTF-8");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customContentType() throws Exception {
|
||||
registerAndRefreshContext("spring.velocity.contentType:application/json");
|
||||
MockHttpServletResponse response = render("home");
|
||||
String result = response.getContentAsString();
|
||||
assertThat(result).contains("home");
|
||||
assertThat(response.getContentType()).isEqualTo("application/json;charset=UTF-8");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customCharset() throws Exception {
|
||||
registerAndRefreshContext("spring.velocity.charset:ISO-8859-1");
|
||||
assertThat(this.context.getBean(VelocityConfigurer.class).getVelocityEngine()
|
||||
.getProperty("input.encoding")).isEqualTo("ISO-8859-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customPrefix() throws Exception {
|
||||
registerAndRefreshContext("spring.velocity.prefix:prefix/");
|
||||
MockHttpServletResponse response = render("prefixed");
|
||||
String result = response.getContentAsString();
|
||||
assertThat(result).contains("prefixed");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customSuffix() throws Exception {
|
||||
registerAndRefreshContext("spring.velocity.suffix:.freemarker");
|
||||
MockHttpServletResponse response = render("suffixed");
|
||||
String result = response.getContentAsString();
|
||||
assertThat(result).contains("suffixed");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTemplateLoaderPath() throws Exception {
|
||||
registerAndRefreshContext(
|
||||
"spring.velocity.resourceLoaderPath:classpath:/custom-templates/");
|
||||
MockHttpServletResponse response = render("custom");
|
||||
String result = response.getContentAsString();
|
||||
assertThat(result).contains("custom");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void disableCache() {
|
||||
registerAndRefreshContext("spring.velocity.cache:false");
|
||||
assertThat(this.context.getBean(VelocityViewResolver.class).getCacheLimit())
|
||||
.isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customVelocitySettings() {
|
||||
registerAndRefreshContext(
|
||||
"spring.velocity.properties.directive.parse.max.depth:10");
|
||||
assertThat(this.context.getBean(VelocityConfigurer.class).getVelocityEngine()
|
||||
.getProperty("directive.parse.max.depth")).isEqualTo("10");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderTemplate() throws Exception {
|
||||
registerAndRefreshContext();
|
||||
VelocityConfigurer velocity = this.context.getBean(VelocityConfigurer.class);
|
||||
StringWriter writer = new StringWriter();
|
||||
Template template = velocity.getVelocityEngine().getTemplate("message.vm");
|
||||
template.process();
|
||||
VelocityContext velocityContext = new VelocityContext();
|
||||
velocityContext.put("greeting", "Hello World");
|
||||
template.merge(velocityContext, writer);
|
||||
assertThat(writer.toString()).contains("Hello World");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderNonWebAppTemplate() throws Exception {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
VelocityAutoConfiguration.class);
|
||||
try {
|
||||
VelocityEngine velocity = context.getBean(VelocityEngine.class);
|
||||
StringWriter writer = new StringWriter();
|
||||
Template template = velocity.getTemplate("message.vm");
|
||||
template.process();
|
||||
VelocityContext velocityContext = new VelocityContext();
|
||||
velocityContext.put("greeting", "Hello World");
|
||||
template.merge(velocityContext, writer);
|
||||
assertThat(writer.toString()).contains("Hello World");
|
||||
}
|
||||
finally {
|
||||
context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void usesEmbeddedVelocityViewResolver() {
|
||||
registerAndRefreshContext("spring.velocity.toolbox:/toolbox.xml");
|
||||
VelocityViewResolver resolver = this.context.getBean(VelocityViewResolver.class);
|
||||
assertThat(resolver).isInstanceOf(EmbeddedVelocityViewResolver.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void registerResourceHandlingFilterDisabledByDefault() throws Exception {
|
||||
registerAndRefreshContext();
|
||||
assertThat(this.context.getBeansOfType(ResourceUrlEncodingFilter.class))
|
||||
.isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void registerResourceHandlingFilterOnlyIfResourceChainIsEnabled()
|
||||
throws Exception {
|
||||
registerAndRefreshContext("spring.resources.chain.enabled:true");
|
||||
assertThat(this.context.getBean(ResourceUrlEncodingFilter.class)).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allowSessionOverride() {
|
||||
registerAndRefreshContext("spring.velocity.allow-session-override:true");
|
||||
AbstractTemplateViewResolver viewResolver = this.context
|
||||
.getBean(VelocityViewResolver.class);
|
||||
assertThat(viewResolver).extracting("allowSessionOverride").containsExactly(true);
|
||||
}
|
||||
|
||||
private void registerAndRefreshContext(String... env) {
|
||||
EnvironmentTestUtils.addEnvironment(this.context, env);
|
||||
this.context.register(VelocityAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
}
|
||||
|
||||
public String getGreeting() {
|
||||
return "Hello World";
|
||||
}
|
||||
|
||||
private MockHttpServletResponse render(String viewName) throws Exception {
|
||||
VelocityViewResolver resolver = this.context.getBean(VelocityViewResolver.class);
|
||||
View view = resolver.resolveViewName(viewName, Locale.UK);
|
||||
assertThat(view).isNotNull();
|
||||
HttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setAttribute(RequestContext.WEB_APPLICATION_CONTEXT_ATTRIBUTE,
|
||||
this.context);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
view.render(null, request, response);
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.velocity;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link VelocityTemplateAvailabilityProvider}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class VelocityTemplateAvailabilityProviderTests {
|
||||
|
||||
private final TemplateAvailabilityProvider provider = new VelocityTemplateAvailabilityProvider();
|
||||
|
||||
private final ResourceLoader resourceLoader = new DefaultResourceLoader();
|
||||
|
||||
private final MockEnvironment environment = new MockEnvironment();
|
||||
|
||||
@Test
|
||||
public void availabilityOfTemplateInDefaultLocation() {
|
||||
assertThat(this.provider.isTemplateAvailable("home", this.environment,
|
||||
getClass().getClassLoader(), this.resourceLoader)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void availabilityOfTemplateThatDoesNotExist() {
|
||||
assertThat(this.provider.isTemplateAvailable("whatever", this.environment,
|
||||
getClass().getClassLoader(), this.resourceLoader)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void availabilityOfTemplateWithCustomLoaderPath() {
|
||||
this.environment.setProperty("spring.velocity.resourceLoaderPath",
|
||||
"classpath:/custom-templates/");
|
||||
assertThat(this.provider.isTemplateAvailable("custom", this.environment,
|
||||
getClass().getClassLoader(), this.resourceLoader)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void availabilityOfTemplateWithCustomPrefix() {
|
||||
this.environment.setProperty("spring.velocity.prefix", "prefix/");
|
||||
assertThat(this.provider.isTemplateAvailable("prefixed", this.environment,
|
||||
getClass().getClassLoader(), this.resourceLoader)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void availabilityOfTemplateWithCustomSuffix() {
|
||||
this.environment.setProperty("spring.velocity.suffix", ".freemarker");
|
||||
assertThat(this.provider.isTemplateAvailable("suffixed", this.environment,
|
||||
getClass().getClassLoader(), this.resourceLoader)).isTrue();
|
||||
}
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<!-- Your own application should inherit from spring-boot-starter-parent -->
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-samples</artifactId>
|
||||
<version>1.5.0.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>spring-boot-sample-velocity</artifactId>
|
||||
<name>Spring Boot Web Velocity Sample</name>
|
||||
<description>Spring Boot Web Velocity Sample</description>
|
||||
<url>http://projects.spring.io/spring-boot/</url>
|
||||
<organization>
|
||||
<name>Pivotal Software, Inc.</name>
|
||||
<url>http://www.spring.io</url>
|
||||
</organization>
|
||||
<properties>
|
||||
<main.basedir>${basedir}/../..</main.basedir>
|
||||
<m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context-support</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.velocity</groupId>
|
||||
<artifactId>velocity</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<useSystemClassLoader>false</useSystemClassLoader>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -1,55 +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 sample.velocity;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.velocity.app.VelocityEngine;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.ui.velocity.VelocityEngineUtils;
|
||||
|
||||
@SpringBootApplication
|
||||
@Deprecated
|
||||
public class SampleVelocityApplication implements CommandLineRunner {
|
||||
|
||||
@Value("${application.message}")
|
||||
private String message;
|
||||
|
||||
@Autowired
|
||||
private VelocityEngine engine;
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
model.put("time", new Date());
|
||||
model.put("message", this.message);
|
||||
System.out.println(VelocityEngineUtils.mergeTemplateIntoString(this.engine,
|
||||
"welcome.vm", "UTF-8", model));
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication.run(SampleVelocityApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -1 +0,0 @@
|
||||
application.message: Hello, Andy
|
@ -1,2 +0,0 @@
|
||||
From application: $time
|
||||
Message: $message
|
@ -1,47 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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 sample.velocity;
|
||||
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.rule.OutputCapture;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Basic integration tests for Velocity application with no web layer.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class SampleVelocityApplicationTests {
|
||||
|
||||
@ClassRule
|
||||
public static OutputCapture output = new OutputCapture();
|
||||
|
||||
@Test
|
||||
public void testVelocityTemplate() throws Exception {
|
||||
String result = SampleVelocityApplicationTests.output.toString();
|
||||
assertThat(result).contains("Hello, Andy");
|
||||
}
|
||||
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<!-- Your own application should inherit from spring-boot-starter-parent -->
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-samples</artifactId>
|
||||
<version>1.5.0.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>spring-boot-sample-web-velocity</artifactId>
|
||||
<name>Spring Boot Web Velocity Sample</name>
|
||||
<description>Spring Boot Web Velocity Sample</description>
|
||||
<url>http://projects.spring.io/spring-boot/</url>
|
||||
<organization>
|
||||
<name>Pivotal Software, Inc.</name>
|
||||
<url>http://www.spring.io</url>
|
||||
</organization>
|
||||
<properties>
|
||||
<main.basedir>${basedir}/../..</main.basedir>
|
||||
<m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-velocity</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<useSystemClassLoader>false</useSystemClassLoader>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -1,29 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2015 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 sample.web.velocity;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SampleWebVelocityApplication {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication.run(SampleWebVelocityApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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 sample.web.velocity;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
@Controller
|
||||
public class WelcomeController {
|
||||
|
||||
@Value("${application.message:Hello World}")
|
||||
private String message = "Hello World";
|
||||
|
||||
@GetMapping("/")
|
||||
public String welcome(Map<String, Object> model) {
|
||||
model.put("time", new Date());
|
||||
model.put("message", this.message);
|
||||
return "welcome";
|
||||
}
|
||||
|
||||
}
|
@ -1,2 +0,0 @@
|
||||
application.message: Hello, Andy
|
||||
spring.velocity.dateToolAttribute: dateTool
|
@ -1,9 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html lang="en">
|
||||
|
||||
<body>
|
||||
Something went wrong: ${status} ${error}
|
||||
</body>
|
||||
|
||||
</html>
|
@ -1,14 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html lang="en">
|
||||
|
||||
<body>
|
||||
<span>Time:</span>
|
||||
<ul>
|
||||
<li>From controller: $time</li>
|
||||
<li>From velocity: $dateTool</li>
|
||||
</ul>
|
||||
<span>Message: $message</span>
|
||||
</body>
|
||||
|
||||
</html>
|
@ -1,74 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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 sample.web.velocity;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Basic integration tests for Velocity application.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
@DirtiesContext
|
||||
public class SampleWebVelocityApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@Test
|
||||
public void testVelocityTemplate() throws Exception {
|
||||
ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(entity.getBody()).contains("Hello, Andy");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVelocityErrorTemplate() throws Exception {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
|
||||
HttpEntity<String> requestEntity = new HttpEntity<String>(headers);
|
||||
|
||||
ResponseEntity<String> responseEntity = this.restTemplate
|
||||
.exchange("/does-not-exist", HttpMethod.GET, requestEntity, String.class);
|
||||
|
||||
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
||||
assertThat(responseEntity.getBody())
|
||||
.contains("Something went wrong: 404 Not Found");
|
||||
}
|
||||
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starters</artifactId>
|
||||
<version>1.5.0.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>spring-boot-starter-velocity</artifactId>
|
||||
<name>Spring Boot Velocity Starter</name>
|
||||
<description>Starter for building MVC web applications using Velocity views.
|
||||
Deprecated since 1.4</description>
|
||||
<url>http://projects.spring.io/spring-boot/</url>
|
||||
<organization>
|
||||
<name>Pivotal Software, Inc.</name>
|
||||
<url>http://www.spring.io</url>
|
||||
</organization>
|
||||
<properties>
|
||||
<main.basedir>${basedir}/../..</main.basedir>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-beanutils</groupId>
|
||||
<artifactId>commons-beanutils</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-collections</groupId>
|
||||
<artifactId>commons-collections</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-digester</groupId>
|
||||
<artifactId>commons-digester</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.velocity</groupId>
|
||||
<artifactId>velocity</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.velocity</groupId>
|
||||
<artifactId>velocity-tools</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context-support</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -1,113 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.web.servlet.view.velocity;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
import org.apache.velocity.context.Context;
|
||||
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityToolboxView;
|
||||
|
||||
/**
|
||||
* Extended version of {@link VelocityToolboxView} that can load toolbox locations from
|
||||
* the classpath as well as the servlet context. This is useful when running in an
|
||||
* embedded web server.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
* @since 1.2.5
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class EmbeddedVelocityToolboxView extends VelocityToolboxView {
|
||||
|
||||
@Override
|
||||
protected Context createVelocityContext(Map<String, Object> model,
|
||||
HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
org.apache.velocity.tools.view.context.ChainedContext context = new org.apache.velocity.tools.view.context.ChainedContext(
|
||||
new VelocityContext(model), getVelocityEngine(), request, response,
|
||||
getServletContext());
|
||||
if (getToolboxConfigLocation() != null) {
|
||||
setContextToolbox(context);
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void setContextToolbox(
|
||||
org.apache.velocity.tools.view.context.ChainedContext context) {
|
||||
org.apache.velocity.tools.view.ToolboxManager toolboxManager = org.apache.velocity.tools.view.servlet.ServletToolboxManager
|
||||
.getInstance(getToolboxConfigFileAwareServletContext(),
|
||||
getToolboxConfigLocation());
|
||||
Map<String, Object> toolboxContext = toolboxManager.getToolbox(context);
|
||||
context.setToolbox(toolboxContext);
|
||||
}
|
||||
|
||||
private ServletContext getToolboxConfigFileAwareServletContext() {
|
||||
ProxyFactory factory = new ProxyFactory();
|
||||
factory.setTarget(getServletContext());
|
||||
factory.addAdvice(new GetResourceMethodInterceptor(getToolboxConfigLocation()));
|
||||
return (ServletContext) factory.getProxy(getClass().getClassLoader());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link MethodInterceptor} to allow the calls to getResourceAsStream() to resolve
|
||||
* the toolboxFile from the classpath.
|
||||
*/
|
||||
private static class GetResourceMethodInterceptor implements MethodInterceptor {
|
||||
|
||||
private final String toolboxFile;
|
||||
|
||||
GetResourceMethodInterceptor(String toolboxFile) {
|
||||
if (toolboxFile != null && !toolboxFile.startsWith("/")) {
|
||||
toolboxFile = "/" + toolboxFile;
|
||||
}
|
||||
this.toolboxFile = toolboxFile;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(MethodInvocation invocation) throws Throwable {
|
||||
if (invocation.getMethod().getName().equals("getResourceAsStream")
|
||||
&& invocation.getArguments()[0].equals(this.toolboxFile)) {
|
||||
InputStream inputStream = (InputStream) invocation.proceed();
|
||||
if (inputStream == null) {
|
||||
try {
|
||||
inputStream = new ClassPathResource(this.toolboxFile,
|
||||
Thread.currentThread().getContextClassLoader())
|
||||
.getInputStream();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
return inputStream;
|
||||
}
|
||||
return invocation.proceed();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.web.servlet.view.velocity;
|
||||
|
||||
import org.springframework.web.servlet.view.velocity.VelocityView;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityViewResolver;
|
||||
|
||||
/**
|
||||
* Extended version of {@link VelocityViewResolver} that uses
|
||||
* {@link EmbeddedVelocityToolboxView} when the {@link #setToolboxConfigLocation(String)
|
||||
* toolboxConfigLocation} is set.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 1.2.5
|
||||
* @deprecated as of 1.4 following the deprecation of Velocity support in Spring Framework
|
||||
* 4.3
|
||||
*/
|
||||
@Deprecated
|
||||
public class EmbeddedVelocityViewResolver extends VelocityViewResolver {
|
||||
|
||||
private String toolboxConfigLocation;
|
||||
|
||||
@Override
|
||||
protected void initApplicationContext() {
|
||||
if (this.toolboxConfigLocation != null) {
|
||||
if (VelocityView.class.equals(getViewClass())) {
|
||||
this.logger.info("Using EmbeddedVelocityToolboxView instead of "
|
||||
+ "default VelocityView due to specified toolboxConfigLocation");
|
||||
setViewClass(EmbeddedVelocityToolboxView.class);
|
||||
}
|
||||
}
|
||||
super.initApplicationContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setToolboxConfigLocation(String toolboxConfigLocation) {
|
||||
super.setToolboxConfigLocation(toolboxConfigLocation);
|
||||
this.toolboxConfigLocation = toolboxConfigLocation;
|
||||
}
|
||||
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2015 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Velocity support classes.
|
||||
*/
|
||||
package org.springframework.boot.web.servlet.view.velocity;
|
@ -1,95 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.web.servlet.view.velocity;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.velocity.tools.ToolContext;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link EmbeddedVelocityToolboxView}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class EmbeddedVelocityToolboxViewTests {
|
||||
|
||||
private static final String PATH = EmbeddedVelocityToolboxViewTests.class.getPackage()
|
||||
.getName().replace(".", "/");
|
||||
|
||||
@Test
|
||||
public void loadsContextFromClassPath() throws Exception {
|
||||
ToolContext context = getToolContext(PATH + "/toolbox.xml");
|
||||
assertThat(context.getToolbox().keySet()).contains("math");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadsWithoutConfig() throws Exception {
|
||||
ToolContext context = getToolContext(null);
|
||||
assertThat(context).isNotNull();
|
||||
}
|
||||
|
||||
private ToolContext getToolContext(String toolboxConfigLocation) throws Exception {
|
||||
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
|
||||
context.setServletContext(new MockServletContext());
|
||||
context.register(Config.class);
|
||||
context.refresh();
|
||||
EmbeddedVelocityToolboxView view = context
|
||||
.getBean(EmbeddedVelocityToolboxView.class);
|
||||
view.setToolboxConfigLocation(toolboxConfigLocation);
|
||||
Map<String, Object> model = new LinkedHashMap<String, Object>();
|
||||
HttpServletRequest request = new MockHttpServletRequest();
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
ToolContext toolContext = (ToolContext) view.createVelocityContext(model, request,
|
||||
response);
|
||||
context.close();
|
||||
return toolContext;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class Config {
|
||||
|
||||
@Bean
|
||||
public EmbeddedVelocityToolboxView view() {
|
||||
EmbeddedVelocityToolboxView view = new EmbeddedVelocityToolboxView();
|
||||
view.setUrl("http://example.com");
|
||||
return view;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public VelocityConfigurer velocityConfigurer() {
|
||||
return new VelocityConfigurer();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,98 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.web.servlet.view.velocity;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityView;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link EmbeddedVelocityViewResolver}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class EmbeddedVelocityViewResolverTests {
|
||||
|
||||
@Test
|
||||
public void standardViewWithoutToolboxConfig() throws Exception {
|
||||
ApplicationContext context = loadContext(WithoutToolboxConfig.class);
|
||||
EmbeddedVelocityViewResolver resolver = context
|
||||
.getBean(EmbeddedVelocityViewResolver.class);
|
||||
Object viewClass = ReflectionTestUtils.getField(resolver, "viewClass");
|
||||
assertThat(viewClass).isEqualTo(VelocityView.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void embeddedViewWithToolboxConfig() throws Exception {
|
||||
ApplicationContext context = loadContext(WithToolboxConfig.class);
|
||||
EmbeddedVelocityViewResolver resolver = context
|
||||
.getBean(EmbeddedVelocityViewResolver.class);
|
||||
Object viewClass = ReflectionTestUtils.getField(resolver, "viewClass");
|
||||
assertThat(viewClass).isEqualTo(EmbeddedVelocityToolboxView.class);
|
||||
}
|
||||
|
||||
private ApplicationContext loadContext(Class<?> config) {
|
||||
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
|
||||
context.setServletContext(new MockServletContext());
|
||||
context.register(config);
|
||||
context.refresh();
|
||||
return context;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class WithoutToolboxConfig {
|
||||
|
||||
@Bean
|
||||
public EmbeddedVelocityViewResolver resolver() {
|
||||
return new EmbeddedVelocityViewResolver();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public VelocityConfigurer velocityConfigurer() {
|
||||
return new VelocityConfigurer();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class WithToolboxConfig {
|
||||
|
||||
@Bean
|
||||
public EmbeddedVelocityViewResolver resolver() {
|
||||
EmbeddedVelocityViewResolver resolver = new EmbeddedVelocityViewResolver();
|
||||
resolver.setToolboxConfigLocation("/toolbox.xml");
|
||||
return resolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public VelocityConfigurer velocityConfigurer() {
|
||||
return new VelocityConfigurer();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<toolbox>
|
||||
<tool>
|
||||
<key>math</key>
|
||||
<scope>application</scope>
|
||||
<class>org.apache.velocity.tools.generic.MathTool</class>
|
||||
</tool>
|
||||
</toolbox>
|
Loading…
Reference in New Issue