Skip to content

Commit 8f32b87

Browse files
royclarksonDave Syer
authored andcommitted
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
1 parent e891aa3 commit 8f32b87

File tree

7 files changed

+538
-7
lines changed

7 files changed

+538
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.mobile;
18+
19+
import org.apache.commons.logging.Log;
20+
import org.apache.commons.logging.LogFactory;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
23+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
27+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
28+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
29+
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration;
30+
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
31+
import org.springframework.boot.bind.RelaxedPropertyResolver;
32+
import org.springframework.context.EnvironmentAware;
33+
import org.springframework.context.annotation.Bean;
34+
import org.springframework.context.annotation.Configuration;
35+
import org.springframework.core.Ordered;
36+
import org.springframework.core.env.Environment;
37+
import org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver;
38+
import org.springframework.web.servlet.ViewResolver;
39+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
40+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
41+
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
42+
43+
/**
44+
* {@link EnableAutoConfiguration Auto-configuration} for Spring Mobile's
45+
* {@link LiteDeviceDelegatingViewResolver}. If {@link ThymeleafViewResolver} is available
46+
* it is configured as the delegate view resolver. Otherwise,
47+
* {@link InternalResourceViewResolver} is used as a fallback.
48+
*
49+
* @author Roy Clarkson
50+
* @since 1.1.0
51+
*/
52+
@Configuration
53+
@ConditionalOnWebApplication
54+
@ConditionalOnClass(LiteDeviceDelegatingViewResolver.class)
55+
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
56+
public class DevceDelegatingViewResolverAutoConfiguration {
57+
58+
private static Log logger = LogFactory.getLog(WebMvcConfigurerAdapter.class);
59+
60+
public static final String DEFAULT_NORMAL_PREFIX = "";
61+
62+
public static final String DEFAULT_MOBILE_PREFIX = "mobile/";
63+
64+
public static final String DEFAULT_TABLET_PREFIX = "tablet/";
65+
66+
public static final String DEFAULT_NORMAL_SUFFIX = "";
67+
68+
public static final String DEFAULT_MOBILE_SUFFIX = "";
69+
70+
public static final String DEFAULT_TABLET_SUFFIX = "";
71+
72+
@Configuration
73+
@ConditionalOnMissingBean(name = "deviceDelegatingViewResolver")
74+
@ConditionalOnExpression("${spring.mobile.deviceDelegatingViewResolver.enabled:false}")
75+
protected static class DevceDelegatingViewResolverConfiguration {
76+
77+
@Configuration
78+
@ConditionalOnBean(ThymeleafViewResolver.class)
79+
@AutoConfigureAfter(ThymeleafAutoConfiguration.class)
80+
protected static class ThymeleafViewResolverViewResolverDelegateConfiguration
81+
extends AbstractDelegateConfiguration {
82+
83+
@Autowired
84+
private ThymeleafViewResolver thymeleafViewResolver;
85+
86+
@Bean
87+
public LiteDeviceDelegatingViewResolver deviceDelegatingViewResolver() {
88+
if (logger.isDebugEnabled()) {
89+
logger.debug("LiteDeviceDelegatingViewResolver delegates to ThymeleafViewResolver");
90+
}
91+
return getConfiguredViewResolver(thymeleafViewResolver,
92+
thymeleafViewResolver.getOrder());
93+
}
94+
95+
}
96+
97+
@Configuration
98+
@ConditionalOnMissingBean(ThymeleafViewResolver.class)
99+
@ConditionalOnBean(InternalResourceViewResolver.class)
100+
protected static class InternalResourceViewResolverDelegateConfiguration extends
101+
AbstractDelegateConfiguration {
102+
103+
@Autowired
104+
private InternalResourceViewResolver internalResourceViewResolver;
105+
106+
@Bean
107+
public LiteDeviceDelegatingViewResolver deviceDelegatingViewResolver() {
108+
if (logger.isDebugEnabled()) {
109+
logger.debug("LiteDeviceDelegatingViewResolver delegates to InternalResourceViewResolver");
110+
}
111+
return getConfiguredViewResolver(internalResourceViewResolver,
112+
internalResourceViewResolver.getOrder());
113+
}
114+
115+
}
116+
117+
private static abstract class AbstractDelegateConfiguration implements
118+
EnvironmentAware {
119+
120+
private RelaxedPropertyResolver environment;
121+
122+
@Override
123+
public void setEnvironment(Environment environment) {
124+
this.environment = new RelaxedPropertyResolver(environment,
125+
"spring.mobile.deviceDelegatingViewResolver.");
126+
}
127+
128+
protected LiteDeviceDelegatingViewResolver getConfiguredViewResolver(
129+
ViewResolver delegate, int delegateOrder) {
130+
LiteDeviceDelegatingViewResolver resolver = new LiteDeviceDelegatingViewResolver(
131+
delegate);
132+
resolver.setNormalPrefix(this.environment.getProperty("normalPrefix",
133+
DEFAULT_NORMAL_PREFIX));
134+
resolver.setMobilePrefix(this.environment.getProperty("mobilePrefix",
135+
DEFAULT_MOBILE_PREFIX));
136+
resolver.setTabletPrefix(this.environment.getProperty("tabletPrefix",
137+
DEFAULT_TABLET_PREFIX));
138+
resolver.setNormalSuffix(this.environment.getProperty("normalSuffix",
139+
DEFAULT_NORMAL_SUFFIX));
140+
resolver.setMobileSuffix(this.environment.getProperty("mobileSuffix",
141+
DEFAULT_MOBILE_SUFFIX));
142+
resolver.setTabletSuffix(this.environment.getProperty("tabletSuffix",
143+
DEFAULT_TABLET_SUFFIX));
144+
resolver.setOrder(getAdjustedOrder(delegateOrder));
145+
return resolver;
146+
}
147+
148+
private int getAdjustedOrder(int delegateViewResolverOrder) {
149+
if (delegateViewResolverOrder == Ordered.HIGHEST_PRECEDENCE) {
150+
return Ordered.HIGHEST_PRECEDENCE;
151+
} else {
152+
// The view resolver must be ordered higher than the delegate view
153+
// resolver, otherwise the view names will not be adjusted
154+
return delegateViewResolverOrder - 1;
155+
}
156+
}
157+
158+
}
159+
160+
}
161+
162+
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mobile/SitePreferenceAutoConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@
4545
*/
4646
@Configuration
4747
@ConditionalOnClass({ SitePreferenceHandlerInterceptor.class,
48-
SitePreferenceHandlerMethodArgumentResolver.class })
48+
SitePreferenceHandlerMethodArgumentResolver.class })
4949
@AutoConfigureAfter(DeviceResolverAutoConfiguration.class)
50-
@ConditionalOnExpression("${spring.mobile.enableSitePreference:true}")
50+
@ConditionalOnExpression("${spring.mobile.sitePreference.enabled:true}")
5151
public class SitePreferenceAutoConfiguration {
5252

5353
@Configuration

spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
2929
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\
3030
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
3131
org.springframework.boot.autoconfigure.mobile.DeviceResolverAutoConfiguration,\
32+
org.springframework.boot.autoconfigure.mobile.DevceDelegatingViewResolverAutoConfiguration,\
3233
org.springframework.boot.autoconfigure.mobile.SitePreferenceAutoConfiguration,\
3334
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
3435
org.springframework.boot.autoconfigure.mongo.MongoDataAutoConfiguration,\

0 commit comments

Comments
 (0)