Commit 2852f742 authored by Phillip Webb's avatar Phillip Webb

Polish Spring Mobile Auto-configuration

Update Spring Mobile support with the following changes:
- Apply source formatting
- User lowercase property prefixes
- Use dashed notation when accessing properties
- Inline some constants

See gh-1049
parent 6902f2ac
...@@ -45,7 +45,7 @@ import org.thymeleaf.spring4.view.ThymeleafViewResolver; ...@@ -45,7 +45,7 @@ import org.thymeleaf.spring4.view.ThymeleafViewResolver;
* {@link LiteDeviceDelegatingViewResolver}. If {@link ThymeleafViewResolver} is available * {@link LiteDeviceDelegatingViewResolver}. If {@link ThymeleafViewResolver} is available
* it is configured as the delegate view resolver. Otherwise, * it is configured as the delegate view resolver. Otherwise,
* {@link InternalResourceViewResolver} is used as a fallback. * {@link InternalResourceViewResolver} is used as a fallback.
* *
* @author Roy Clarkson * @author Roy Clarkson
* @since 1.1.0 * @since 1.1.0
*/ */
...@@ -57,21 +57,49 @@ public class DeviceDelegatingViewResolverAutoConfiguration { ...@@ -57,21 +57,49 @@ public class DeviceDelegatingViewResolverAutoConfiguration {
private static Log logger = LogFactory.getLog(WebMvcConfigurerAdapter.class); private static Log logger = LogFactory.getLog(WebMvcConfigurerAdapter.class);
public static final String DEFAULT_NORMAL_PREFIX = ""; private static abstract class AbstractDelegateConfiguration implements
EnvironmentAware {
public static final String DEFAULT_MOBILE_PREFIX = "mobile/"; private RelaxedPropertyResolver environment;
public static final String DEFAULT_TABLET_PREFIX = "tablet/"; @Override
public void setEnvironment(Environment environment) {
this.environment = new RelaxedPropertyResolver(environment,
"spring.mobile.devicedelegatingviewresolver.");
}
public static final String DEFAULT_NORMAL_SUFFIX = ""; protected LiteDeviceDelegatingViewResolver getConfiguredViewResolver(
ViewResolver delegate, int delegateOrder) {
LiteDeviceDelegatingViewResolver resolver = new LiteDeviceDelegatingViewResolver(
delegate);
resolver.setNormalPrefix(getProperty("normal-prefix", ""));
resolver.setNormalSuffix(getProperty("normal-suffix", ""));
resolver.setMobilePrefix(getProperty("mobile-prefix", "mobile/"));
resolver.setMobileSuffix(getProperty("mobile-suffix", ""));
resolver.setTabletPrefix(getProperty("tablet-prefix", "tablet/"));
resolver.setTabletSuffix(getProperty("tablet-suffix", ""));
resolver.setOrder(getAdjustedOrder(delegateOrder));
return resolver;
}
private String getProperty(String key, String defaultValue) {
return this.environment.getProperty(key, defaultValue);
}
public static final String DEFAULT_MOBILE_SUFFIX = ""; private int getAdjustedOrder(int order) {
if (order == Ordered.HIGHEST_PRECEDENCE) {
return Ordered.HIGHEST_PRECEDENCE;
}
// The view resolver must be ordered higher than the delegate view
// resolver, otherwise the view names will not be adjusted
return order - 1;
}
public static final String DEFAULT_TABLET_SUFFIX = ""; }
@Configuration @Configuration
@ConditionalOnMissingBean(name = "deviceDelegatingViewResolver") @ConditionalOnMissingBean(name = "deviceDelegatingViewResolver")
@ConditionalOnExpression("${spring.mobile.deviceDelegatingViewResolver.enabled:false}") @ConditionalOnExpression("${spring.mobile.devicedelegatingviewresolver.enabled:false}")
protected static class DeviceDelegatingViewResolverConfiguration { protected static class DeviceDelegatingViewResolverConfiguration {
@Configuration @Configuration
...@@ -81,15 +109,16 @@ public class DeviceDelegatingViewResolverAutoConfiguration { ...@@ -81,15 +109,16 @@ public class DeviceDelegatingViewResolverAutoConfiguration {
extends AbstractDelegateConfiguration { extends AbstractDelegateConfiguration {
@Autowired @Autowired
private ThymeleafViewResolver thymeleafViewResolver; private ThymeleafViewResolver viewResolver;
@Bean @Bean
public LiteDeviceDelegatingViewResolver deviceDelegatingViewResolver() { public LiteDeviceDelegatingViewResolver deviceDelegatingViewResolver() {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("LiteDeviceDelegatingViewResolver delegates to ThymeleafViewResolver"); logger.debug("LiteDeviceDelegatingViewResolver delegates to "
+ "ThymeleafViewResolver");
} }
return getConfiguredViewResolver(thymeleafViewResolver, return getConfiguredViewResolver(this.viewResolver,
thymeleafViewResolver.getOrder()); this.viewResolver.getOrder());
} }
} }
...@@ -101,58 +130,16 @@ public class DeviceDelegatingViewResolverAutoConfiguration { ...@@ -101,58 +130,16 @@ public class DeviceDelegatingViewResolverAutoConfiguration {
AbstractDelegateConfiguration { AbstractDelegateConfiguration {
@Autowired @Autowired
private InternalResourceViewResolver internalResourceViewResolver; private InternalResourceViewResolver viewResolver;
@Bean @Bean
public LiteDeviceDelegatingViewResolver deviceDelegatingViewResolver() { public LiteDeviceDelegatingViewResolver deviceDelegatingViewResolver() {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("LiteDeviceDelegatingViewResolver delegates to InternalResourceViewResolver"); 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;
} }
return getConfiguredViewResolver(this.viewResolver,
this.viewResolver.getOrder());
} }
} }
......
...@@ -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.sitePreference.enabled:true}") @ConditionalOnExpression("${spring.mobile.sitepreference.enabled:true}")
public class SitePreferenceAutoConfiguration { public class SitePreferenceAutoConfiguration {
@Configuration @Configuration
......
...@@ -16,9 +16,6 @@ ...@@ -16,9 +16,6 @@
package org.springframework.boot.autoconfigure.mobile; package org.springframework.boot.autoconfigure.mobile;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.List; import java.util.List;
...@@ -41,9 +38,12 @@ import org.springframework.util.ReflectionUtils; ...@@ -41,9 +38,12 @@ import org.springframework.util.ReflectionUtils;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
/** /**
* Tests for {@link SitePreferenceAutoConfiguration}. * Tests for {@link SitePreferenceAutoConfiguration}.
* *
* @author Roy Clarkson * @author Roy Clarkson
*/ */
public class SitePreferenceAutoConfigurationTests { public class SitePreferenceAutoConfigurationTests {
...@@ -70,7 +70,8 @@ public class SitePreferenceAutoConfigurationTests { ...@@ -70,7 +70,8 @@ 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.sitePreference.enabled:true"); EnvironmentTestUtils.addEnvironment(this.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 +80,8 @@ public class SitePreferenceAutoConfigurationTests { ...@@ -79,7 +80,8 @@ 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.sitePreference.enabled:false"); EnvironmentTestUtils.addEnvironment(this.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);
...@@ -90,22 +92,26 @@ public class SitePreferenceAutoConfigurationTests { ...@@ -90,22 +92,26 @@ public class SitePreferenceAutoConfigurationTests {
this.context = new AnnotationConfigWebApplicationContext(); this.context = new AnnotationConfigWebApplicationContext();
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));
} }
@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.sitePreference.enabled:true"); EnvironmentTestUtils.addEnvironment(this.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));
} }
@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.sitePreference.enabled:false"); EnvironmentTestUtils.addEnvironment(this.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);
...@@ -120,7 +126,8 @@ public class SitePreferenceAutoConfigurationTests { ...@@ -120,7 +126,8 @@ public class SitePreferenceAutoConfigurationTests {
SitePreferenceAutoConfiguration.class, SitePreferenceAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
context.refresh(); context.refresh();
RequestMappingHandlerMapping mapping = (RequestMappingHandlerMapping) context.getBean("requestMappingHandlerMapping"); RequestMappingHandlerMapping mapping = (RequestMappingHandlerMapping) context
.getBean("requestMappingHandlerMapping");
Field interceptorsField = ReflectionUtils.findField( Field interceptorsField = ReflectionUtils.findField(
RequestMappingHandlerMapping.class, "interceptors"); RequestMappingHandlerMapping.class, "interceptors");
interceptorsField.setAccessible(true); interceptorsField.setAccessible(true);
......
...@@ -290,16 +290,16 @@ content into your application; rather pick only the properties that you need. ...@@ -290,16 +290,16 @@ content into your application; rather pick only the properties that you need.
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 SITE PREFERENCE ({sc-spring-boot-autoconfigure}/mobile/SitePreferenceAutoConfiguration.{sc-ext}[SitePreferenceAutoConfiguration])
spring.mobile.sitePreference.enabled=true # enabled by default spring.mobile.sitepreference.enabled=true # enabled by default
# SPRING MOBILE DEVICE VIEWS ({sc-spring-boot-autoconfigure}/mobile/DevceDelegatingViewResolverAutoConfiguration.{sc-ext}[DevceDelegatingViewResolverAutoConfiguration]) # SPRING MOBILE DEVICE VIEWS ({sc-spring-boot-autoconfigure}/mobile/DevceDelegatingViewResolverAutoConfiguration.{sc-ext}[DevceDelegatingViewResolverAutoConfiguration])
spring.mobile.deviceDelegatingViewResolver.enabled=true # disabled by default spring.mobile.devicedelegatingviewresolver.enabled=true # disabled by default
spring.mobile.deviceDelegatingViewResolver.normalPrefix=nor/ spring.mobile.devicedelegatingviewresolver.normalPrefix=
spring.mobile.deviceDelegatingViewResolver.mobilePrefix=mob/ # default is "mobile/" spring.mobile.devicedelegatingviewresolver.normalSuffix=
spring.mobile.deviceDelegatingViewResolver.tabletPrefix=tab/ # default is "tablet/" spring.mobile.devicedelegatingviewresolver.mobilePrefix=mobile/
spring.mobile.deviceDelegatingViewResolver.normalSuffix=.nor spring.mobile.devicedelegatingviewresolver.mobileSuffix=
spring.mobile.deviceDelegatingViewResolver.mobileSuffix=.mob spring.mobile.devicedelegatingviewresolver.tabletPrefix=tablet/
spring.mobile.deviceDelegatingViewResolver.tabletSuffix=.tab spring.mobile.devicedelegatingviewresolver.tabletSuffix=
# ---------------------------------------- # ----------------------------------------
# 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