Commit 8f32b87c authored by Roy Clarkson's avatar Roy Clarkson Committed by Dave Syer

Improve Spring Mobile Auto-configuration

- Upgrade Spring Mobile dependency to 1.1.2
- Rename SitePreferenceAutoConfiguration "enabled" property
- Add Auto-configuration for LiteDeviceDelegatingViewResolver
- Update docs

Fixes gh-1049
parent e891aa35
/*
* 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.mobile;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.env.Environment;
import org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
/**
* {@link EnableAutoConfiguration Auto-configuration} for Spring Mobile's
* {@link LiteDeviceDelegatingViewResolver}. If {@link ThymeleafViewResolver} is available
* it is configured as the delegate view resolver. Otherwise,
* {@link InternalResourceViewResolver} is used as a fallback.
*
* @author Roy Clarkson
* @since 1.1.0
*/
@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass(LiteDeviceDelegatingViewResolver.class)
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
public class DevceDelegatingViewResolverAutoConfiguration {
private static Log logger = LogFactory.getLog(WebMvcConfigurerAdapter.class);
public static final String DEFAULT_NORMAL_PREFIX = "";
public static final String DEFAULT_MOBILE_PREFIX = "mobile/";
public static final String DEFAULT_TABLET_PREFIX = "tablet/";
public static final String DEFAULT_NORMAL_SUFFIX = "";
public static final String DEFAULT_MOBILE_SUFFIX = "";
public static final String DEFAULT_TABLET_SUFFIX = "";
@Configuration
@ConditionalOnMissingBean(name = "deviceDelegatingViewResolver")
@ConditionalOnExpression("${spring.mobile.deviceDelegatingViewResolver.enabled:false}")
protected static class DevceDelegatingViewResolverConfiguration {
@Configuration
@ConditionalOnBean(ThymeleafViewResolver.class)
@AutoConfigureAfter(ThymeleafAutoConfiguration.class)
protected static class ThymeleafViewResolverViewResolverDelegateConfiguration
extends AbstractDelegateConfiguration {
@Autowired
private ThymeleafViewResolver thymeleafViewResolver;
@Bean
public LiteDeviceDelegatingViewResolver deviceDelegatingViewResolver() {
if (logger.isDebugEnabled()) {
logger.debug("LiteDeviceDelegatingViewResolver delegates to ThymeleafViewResolver");
}
return getConfiguredViewResolver(thymeleafViewResolver,
thymeleafViewResolver.getOrder());
}
}
@Configuration
@ConditionalOnMissingBean(ThymeleafViewResolver.class)
@ConditionalOnBean(InternalResourceViewResolver.class)
protected static class InternalResourceViewResolverDelegateConfiguration extends
AbstractDelegateConfiguration {
@Autowired
private InternalResourceViewResolver internalResourceViewResolver;
@Bean
public LiteDeviceDelegatingViewResolver deviceDelegatingViewResolver() {
if (logger.isDebugEnabled()) {
logger.debug("LiteDeviceDelegatingViewResolver delegates to InternalResourceViewResolver");
}
return getConfiguredViewResolver(internalResourceViewResolver,
internalResourceViewResolver.getOrder());
}
}
private static abstract class AbstractDelegateConfiguration implements
EnvironmentAware {
private RelaxedPropertyResolver environment;
@Override
public void setEnvironment(Environment environment) {
this.environment = new RelaxedPropertyResolver(environment,
"spring.mobile.deviceDelegatingViewResolver.");
}
protected LiteDeviceDelegatingViewResolver getConfiguredViewResolver(
ViewResolver delegate, int delegateOrder) {
LiteDeviceDelegatingViewResolver resolver = new LiteDeviceDelegatingViewResolver(
delegate);
resolver.setNormalPrefix(this.environment.getProperty("normalPrefix",
DEFAULT_NORMAL_PREFIX));
resolver.setMobilePrefix(this.environment.getProperty("mobilePrefix",
DEFAULT_MOBILE_PREFIX));
resolver.setTabletPrefix(this.environment.getProperty("tabletPrefix",
DEFAULT_TABLET_PREFIX));
resolver.setNormalSuffix(this.environment.getProperty("normalSuffix",
DEFAULT_NORMAL_SUFFIX));
resolver.setMobileSuffix(this.environment.getProperty("mobileSuffix",
DEFAULT_MOBILE_SUFFIX));
resolver.setTabletSuffix(this.environment.getProperty("tabletSuffix",
DEFAULT_TABLET_SUFFIX));
resolver.setOrder(getAdjustedOrder(delegateOrder));
return resolver;
}
private int getAdjustedOrder(int delegateViewResolverOrder) {
if (delegateViewResolverOrder == Ordered.HIGHEST_PRECEDENCE) {
return Ordered.HIGHEST_PRECEDENCE;
} else {
// The view resolver must be ordered higher than the delegate view
// resolver, otherwise the view names will not be adjusted
return delegateViewResolverOrder - 1;
}
}
}
}
}
...@@ -45,9 +45,9 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter ...@@ -45,9 +45,9 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
*/ */
@Configuration @Configuration
@ConditionalOnClass({ SitePreferenceHandlerInterceptor.class, @ConditionalOnClass({ SitePreferenceHandlerInterceptor.class,
SitePreferenceHandlerMethodArgumentResolver.class }) SitePreferenceHandlerMethodArgumentResolver.class })
@AutoConfigureAfter(DeviceResolverAutoConfiguration.class) @AutoConfigureAfter(DeviceResolverAutoConfiguration.class)
@ConditionalOnExpression("${spring.mobile.enableSitePreference:true}") @ConditionalOnExpression("${spring.mobile.sitePreference.enabled:true}")
public class SitePreferenceAutoConfiguration { public class SitePreferenceAutoConfiguration {
@Configuration @Configuration
......
...@@ -29,6 +29,7 @@ org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\ ...@@ -29,6 +29,7 @@ org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\ org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\ org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
org.springframework.boot.autoconfigure.mobile.DeviceResolverAutoConfiguration,\ org.springframework.boot.autoconfigure.mobile.DeviceResolverAutoConfiguration,\
org.springframework.boot.autoconfigure.mobile.DevceDelegatingViewResolverAutoConfiguration,\
org.springframework.boot.autoconfigure.mobile.SitePreferenceAutoConfiguration,\ org.springframework.boot.autoconfigure.mobile.SitePreferenceAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\ org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoDataAutoConfiguration,\ org.springframework.boot.autoconfigure.mongo.MongoDataAutoConfiguration,\
......
...@@ -70,7 +70,7 @@ public class SitePreferenceAutoConfigurationTests { ...@@ -70,7 +70,7 @@ public class SitePreferenceAutoConfigurationTests {
@Test @Test
public void sitePreferenceHandlerInterceptorEnabled() throws Exception { public void sitePreferenceHandlerInterceptorEnabled() throws Exception {
this.context = new AnnotationConfigWebApplicationContext(); this.context = new AnnotationConfigWebApplicationContext();
EnvironmentTestUtils.addEnvironment(context, "spring.mobile.enableSitePreference:true"); EnvironmentTestUtils.addEnvironment(context, "spring.mobile.sitePreference.enabled:true");
this.context.register(SitePreferenceAutoConfiguration.class); this.context.register(SitePreferenceAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
assertNotNull(this.context.getBean(SitePreferenceHandlerInterceptor.class)); assertNotNull(this.context.getBean(SitePreferenceHandlerInterceptor.class));
...@@ -79,7 +79,7 @@ public class SitePreferenceAutoConfigurationTests { ...@@ -79,7 +79,7 @@ public class SitePreferenceAutoConfigurationTests {
@Test(expected = NoSuchBeanDefinitionException.class) @Test(expected = NoSuchBeanDefinitionException.class)
public void sitePreferenceHandlerInterceptorDisabled() { public void sitePreferenceHandlerInterceptorDisabled() {
this.context = new AnnotationConfigWebApplicationContext(); this.context = new AnnotationConfigWebApplicationContext();
EnvironmentTestUtils.addEnvironment(context, "spring.mobile.enableSitePreference:false"); EnvironmentTestUtils.addEnvironment(context, "spring.mobile.sitePreference.enabled:false");
this.context.register(SitePreferenceAutoConfiguration.class); this.context.register(SitePreferenceAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
this.context.getBean(SitePreferenceHandlerInterceptor.class); this.context.getBean(SitePreferenceHandlerInterceptor.class);
...@@ -96,7 +96,7 @@ public class SitePreferenceAutoConfigurationTests { ...@@ -96,7 +96,7 @@ public class SitePreferenceAutoConfigurationTests {
@Test @Test
public void sitePreferenceMethodArgumentResolverEnabled() throws Exception { public void sitePreferenceMethodArgumentResolverEnabled() throws Exception {
this.context = new AnnotationConfigWebApplicationContext(); this.context = new AnnotationConfigWebApplicationContext();
EnvironmentTestUtils.addEnvironment(context, "spring.mobile.enableSitePreference:true"); EnvironmentTestUtils.addEnvironment(context, "spring.mobile.sitePreference.enabled:true");
this.context.register(SitePreferenceAutoConfiguration.class); this.context.register(SitePreferenceAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
assertNotNull(this.context.getBean(SitePreferenceHandlerMethodArgumentResolver.class)); assertNotNull(this.context.getBean(SitePreferenceHandlerMethodArgumentResolver.class));
...@@ -105,7 +105,7 @@ public class SitePreferenceAutoConfigurationTests { ...@@ -105,7 +105,7 @@ public class SitePreferenceAutoConfigurationTests {
@Test(expected = NoSuchBeanDefinitionException.class) @Test(expected = NoSuchBeanDefinitionException.class)
public void sitePreferenceMethodArgumentResolverDisabled() { public void sitePreferenceMethodArgumentResolverDisabled() {
this.context = new AnnotationConfigWebApplicationContext(); this.context = new AnnotationConfigWebApplicationContext();
EnvironmentTestUtils.addEnvironment(context, "spring.mobile.enableSitePreference:false"); EnvironmentTestUtils.addEnvironment(context, "spring.mobile.sitePreference.enabled:false");
this.context.register(SitePreferenceAutoConfiguration.class); this.context.register(SitePreferenceAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
this.context.getBean(SitePreferenceHandlerMethodArgumentResolver.class); this.context.getBean(SitePreferenceHandlerMethodArgumentResolver.class);
......
...@@ -102,7 +102,7 @@ ...@@ -102,7 +102,7 @@
<spring-hateoas.version>0.12.0.RELEASE</spring-hateoas.version> <spring-hateoas.version>0.12.0.RELEASE</spring-hateoas.version>
<spring-integration.version>4.0.2.RELEASE</spring-integration.version> <spring-integration.version>4.0.2.RELEASE</spring-integration.version>
<spring-loaded.version>1.2.0.RELEASE</spring-loaded.version> <spring-loaded.version>1.2.0.RELEASE</spring-loaded.version>
<spring-mobile.version>1.1.1.RELEASE</spring-mobile.version> <spring-mobile.version>1.1.2.RELEASE</spring-mobile.version>
<spring-social.version>1.1.0.RELEASE</spring-social.version> <spring-social.version>1.1.0.RELEASE</spring-social.version>
<spring-social-facebook.version>1.1.1.RELEASE</spring-social-facebook.version> <spring-social-facebook.version>1.1.1.RELEASE</spring-social-facebook.version>
<spring-social-twitter.version>1.1.0.RELEASE</spring-social-twitter.version> <spring-social-twitter.version>1.1.0.RELEASE</spring-social-twitter.version>
......
...@@ -289,6 +289,18 @@ content into your application; rather pick only the properties that you need. ...@@ -289,6 +289,18 @@ content into your application; rather pick only the properties that you need.
spring.social.twitter.appId= # your application's Twitter App ID spring.social.twitter.appId= # your application's Twitter App ID
spring.social.twitter.appSecret= # your application's Twitter App Secret spring.social.twitter.appSecret= # your application's Twitter App Secret
# SPRING MOBILE SITE PREFERENCE ({sc-spring-boot-autoconfigure}/mobile/SitePreferenceAutoConfiguration.{sc-ext}[SitePreferenceAutoConfiguration])
spring.mobile.sitePreference.enabled=true # enabled by default
# SPRING MOBILE DEVICE VIEWS ({sc-spring-boot-autoconfigure}/mobile/DevceDelegatingViewResolverAutoConfiguration.{sc-ext}[DevceDelegatingViewResolverAutoConfiguration])
spring.mobile.deviceDelegatingViewResolver.enabled=true # disabled by default
spring.mobile.deviceDelegatingViewResolver.normalPrefix=nor/
spring.mobile.deviceDelegatingViewResolver.mobilePrefix=mob/ # default is "mobile/"
spring.mobile.deviceDelegatingViewResolver.tabletPrefix=tab/ # default is "tablet/"
spring.mobile.deviceDelegatingViewResolver.normalSuffix=.nor
spring.mobile.deviceDelegatingViewResolver.mobileSuffix=.mob
spring.mobile.deviceDelegatingViewResolver.tabletSuffix=.tab
# ---------------------------------------- # ----------------------------------------
# ACTUATOR PROPERTIES # ACTUATOR PROPERTIES
# ---------------------------------------- # ----------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment