parent
e43d0adc4f
commit
2ae4678b1e
@ -1,143 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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.thymeleaf.spring5.view.ThymeleafViewResolver;
|
||||
|
||||
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.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
|
||||
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.web.servlet.view.MustacheViewResolver;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
|
||||
import org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver;
|
||||
|
||||
/**
|
||||
* {@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
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.1.0
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnWebApplication(type = Type.SERVLET)
|
||||
@ConditionalOnClass(LiteDeviceDelegatingViewResolver.class)
|
||||
@ConditionalOnProperty(prefix = "spring.mobile.devicedelegatingviewresolver", name = "enabled", havingValue = "true")
|
||||
@EnableConfigurationProperties(DeviceDelegatingViewResolverProperties.class)
|
||||
@AutoConfigureAfter({ WebMvcAutoConfiguration.class, FreeMarkerAutoConfiguration.class,
|
||||
GroovyTemplateAutoConfiguration.class, MustacheAutoConfiguration.class,
|
||||
ThymeleafAutoConfiguration.class })
|
||||
public class DeviceDelegatingViewResolverAutoConfiguration {
|
||||
|
||||
@Configuration
|
||||
protected static class LiteDeviceDelegatingViewResolverFactoryConfiguration {
|
||||
|
||||
@Bean
|
||||
public DeviceDelegatingViewResolverFactory deviceDelegatingViewResolverFactory(
|
||||
DeviceDelegatingViewResolverProperties properties) {
|
||||
return new DeviceDelegatingViewResolverFactory(properties);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnClass(FreeMarkerViewResolver.class)
|
||||
protected static class DeviceDelegatingFreeMarkerViewResolverConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(FreeMarkerViewResolver.class)
|
||||
public LiteDeviceDelegatingViewResolver deviceDelegatingFreeMarkerViewResolver(
|
||||
DeviceDelegatingViewResolverFactory factory,
|
||||
FreeMarkerViewResolver viewResolver) {
|
||||
return factory.createViewResolver(viewResolver);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnClass(GroovyMarkupViewResolver.class)
|
||||
protected static class DeviceDelegatingGroovyMarkupViewResolverConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(GroovyMarkupViewResolver.class)
|
||||
public LiteDeviceDelegatingViewResolver deviceDelegatingGroovyMarkupViewResolver(
|
||||
DeviceDelegatingViewResolverFactory factory,
|
||||
GroovyMarkupViewResolver viewResolver) {
|
||||
return factory.createViewResolver(viewResolver);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnClass(InternalResourceViewResolver.class)
|
||||
protected static class DeviceDelegatingJspViewResolverConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(InternalResourceViewResolver.class)
|
||||
public LiteDeviceDelegatingViewResolver deviceDelegatingJspViewResolver(
|
||||
DeviceDelegatingViewResolverFactory factory,
|
||||
InternalResourceViewResolver viewResolver) {
|
||||
return factory.createViewResolver(viewResolver);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnClass(MustacheViewResolver.class)
|
||||
protected static class DeviceDelegatingMustacheViewResolverConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(MustacheViewResolver.class)
|
||||
public LiteDeviceDelegatingViewResolver deviceDelegatingMustacheViewResolver(
|
||||
DeviceDelegatingViewResolverFactory factory,
|
||||
MustacheViewResolver viewResolver) {
|
||||
return factory.createViewResolver(viewResolver);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnClass(ThymeleafViewResolver.class)
|
||||
protected static class DeviceDelegatingThymeleafViewResolverConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(ThymeleafViewResolver.class)
|
||||
public LiteDeviceDelegatingViewResolver deviceDelegatingThymeleafViewResolver(
|
||||
DeviceDelegatingViewResolverFactory factory,
|
||||
ThymeleafViewResolver viewResolver) {
|
||||
return factory.createViewResolver(viewResolver);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,87 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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.springframework.core.Ordered;
|
||||
import org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
|
||||
/**
|
||||
* A factory for {@link LiteDeviceDelegatingViewResolver} that applies customizations of
|
||||
* {@link DeviceDelegatingViewResolverProperties}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.4.0
|
||||
*/
|
||||
public class DeviceDelegatingViewResolverFactory {
|
||||
|
||||
private final DeviceDelegatingViewResolverProperties properties;
|
||||
|
||||
public DeviceDelegatingViewResolverFactory(
|
||||
DeviceDelegatingViewResolverProperties properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link LiteDeviceDelegatingViewResolver} delegating to the specified
|
||||
* {@link ViewResolver}.
|
||||
* @param delegate the view resolver to delegate to
|
||||
* @param delegatingOrder the order of the {@link LiteDeviceDelegatingViewResolver}
|
||||
* @return a {@link LiteDeviceDelegatingViewResolver} handling the specified resolver
|
||||
*/
|
||||
public LiteDeviceDelegatingViewResolver createViewResolver(ViewResolver delegate,
|
||||
int delegatingOrder) {
|
||||
LiteDeviceDelegatingViewResolver resolver = new LiteDeviceDelegatingViewResolver(
|
||||
delegate);
|
||||
resolver.setEnableFallback(this.properties.isEnableFallback());
|
||||
resolver.setNormalPrefix(this.properties.getNormalPrefix());
|
||||
resolver.setNormalSuffix(this.properties.getNormalSuffix());
|
||||
resolver.setMobilePrefix(this.properties.getMobilePrefix());
|
||||
resolver.setMobileSuffix(this.properties.getMobileSuffix());
|
||||
resolver.setTabletPrefix(this.properties.getTabletPrefix());
|
||||
resolver.setTabletSuffix(this.properties.getTabletSuffix());
|
||||
resolver.setOrder(delegatingOrder);
|
||||
return resolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link LiteDeviceDelegatingViewResolver} delegating to the specified
|
||||
* {@link ViewResolver} and computing a sensible order for it. The specified
|
||||
* {@link ViewResolver} should implement {@link Ordered}, consider using
|
||||
* {@link #createViewResolver(ViewResolver, int)} if that's not the case.
|
||||
* @param delegate the view resolver to delegate to
|
||||
* @return a {@link LiteDeviceDelegatingViewResolver} handling the specified resolver
|
||||
*/
|
||||
public LiteDeviceDelegatingViewResolver createViewResolver(ViewResolver delegate) {
|
||||
if (!(delegate instanceof Ordered)) {
|
||||
throw new IllegalStateException("ViewResolver " + delegate
|
||||
+ " should implement " + Ordered.class.getName());
|
||||
}
|
||||
int delegateOrder = ((Ordered) delegate).getOrder();
|
||||
return createViewResolver(delegate, adjustOrder(delegateOrder));
|
||||
}
|
||||
|
||||
private int adjustOrder(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;
|
||||
}
|
||||
|
||||
}
|
@ -1,121 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* Properties for device view resolver.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "spring.mobile.devicedelegatingviewresolver")
|
||||
public class DeviceDelegatingViewResolverProperties {
|
||||
|
||||
/**
|
||||
* Enable support for fallback resolution.
|
||||
*/
|
||||
private boolean enableFallback;
|
||||
|
||||
/**
|
||||
* Prefix that gets prepended to view names for normal devices.
|
||||
*/
|
||||
private String normalPrefix = "";
|
||||
|
||||
/**
|
||||
* Suffix that gets appended to view names for normal devices.
|
||||
*/
|
||||
private String normalSuffix = "";
|
||||
|
||||
/**
|
||||
* Prefix that gets prepended to view names for mobile devices.
|
||||
*/
|
||||
private String mobilePrefix = "mobile/";
|
||||
|
||||
/**
|
||||
* Suffix that gets appended to view names for mobile devices.
|
||||
*/
|
||||
private String mobileSuffix = "";
|
||||
|
||||
/**
|
||||
* Prefix that gets prepended to view names for tablet devices.
|
||||
*/
|
||||
private String tabletPrefix = "tablet/";
|
||||
|
||||
/**
|
||||
* Suffix that gets appended to view names for tablet devices.
|
||||
*/
|
||||
private String tabletSuffix = "";
|
||||
|
||||
public void setEnableFallback(boolean enableFallback) {
|
||||
this.enableFallback = enableFallback;
|
||||
}
|
||||
|
||||
public boolean isEnableFallback() {
|
||||
return this.enableFallback;
|
||||
}
|
||||
|
||||
public String getNormalPrefix() {
|
||||
return this.normalPrefix;
|
||||
}
|
||||
|
||||
public void setNormalPrefix(String normalPrefix) {
|
||||
this.normalPrefix = normalPrefix;
|
||||
}
|
||||
|
||||
public String getNormalSuffix() {
|
||||
return this.normalSuffix;
|
||||
}
|
||||
|
||||
public void setNormalSuffix(String normalSuffix) {
|
||||
this.normalSuffix = normalSuffix;
|
||||
}
|
||||
|
||||
public String getMobilePrefix() {
|
||||
return this.mobilePrefix;
|
||||
}
|
||||
|
||||
public void setMobilePrefix(String mobilePrefix) {
|
||||
this.mobilePrefix = mobilePrefix;
|
||||
}
|
||||
|
||||
public String getMobileSuffix() {
|
||||
return this.mobileSuffix;
|
||||
}
|
||||
|
||||
public void setMobileSuffix(String mobileSuffix) {
|
||||
this.mobileSuffix = mobileSuffix;
|
||||
}
|
||||
|
||||
public String getTabletPrefix() {
|
||||
return this.tabletPrefix;
|
||||
}
|
||||
|
||||
public void setTabletPrefix(String tabletPrefix) {
|
||||
this.tabletPrefix = tabletPrefix;
|
||||
}
|
||||
|
||||
public String getTabletSuffix() {
|
||||
return this.tabletSuffix;
|
||||
}
|
||||
|
||||
public void setTabletSuffix(String tabletSuffix) {
|
||||
this.tabletSuffix = tabletSuffix;
|
||||
}
|
||||
|
||||
}
|
@ -1,90 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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 java.util.List;
|
||||
|
||||
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.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
|
||||
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.mobile.device.DeviceHandlerMethodArgumentResolver;
|
||||
import org.springframework.mobile.device.DeviceResolver;
|
||||
import org.springframework.mobile.device.DeviceResolverHandlerInterceptor;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for Spring Mobile's
|
||||
* {@link DeviceResolver}.
|
||||
*
|
||||
* @author Roy Clarkson
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ DeviceResolverHandlerInterceptor.class,
|
||||
DeviceHandlerMethodArgumentResolver.class })
|
||||
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
|
||||
@ConditionalOnWebApplication(type = Type.SERVLET)
|
||||
public class DeviceResolverAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(DeviceResolverHandlerInterceptor.class)
|
||||
public DeviceResolverHandlerInterceptor deviceResolverHandlerInterceptor() {
|
||||
return new DeviceResolverHandlerInterceptor();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DeviceHandlerMethodArgumentResolver deviceHandlerMethodArgumentResolver() {
|
||||
return new DeviceHandlerMethodArgumentResolver();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Order(0)
|
||||
protected static class DeviceResolverMvcConfiguration implements WebMvcConfigurer {
|
||||
|
||||
private DeviceResolverHandlerInterceptor deviceResolverHandlerInterceptor;
|
||||
|
||||
private DeviceHandlerMethodArgumentResolver deviceHandlerMethodArgumentResolver;
|
||||
|
||||
protected DeviceResolverMvcConfiguration(
|
||||
DeviceResolverHandlerInterceptor deviceResolverHandlerInterceptor,
|
||||
DeviceHandlerMethodArgumentResolver deviceHandlerMethodArgumentResolver) {
|
||||
this.deviceResolverHandlerInterceptor = deviceResolverHandlerInterceptor;
|
||||
this.deviceHandlerMethodArgumentResolver = deviceHandlerMethodArgumentResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(this.deviceResolverHandlerInterceptor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addArgumentResolvers(
|
||||
List<HandlerMethodArgumentResolver> argumentResolvers) {
|
||||
argumentResolvers.add(this.deviceHandlerMethodArgumentResolver);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,92 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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 java.util.List;
|
||||
|
||||
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.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.mobile.device.DeviceResolver;
|
||||
import org.springframework.mobile.device.site.SitePreferenceHandler;
|
||||
import org.springframework.mobile.device.site.SitePreferenceHandlerInterceptor;
|
||||
import org.springframework.mobile.device.site.SitePreferenceHandlerMethodArgumentResolver;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for Spring Mobile's
|
||||
* {@link SitePreferenceHandler}. The site preference feature depends on a
|
||||
* {@link DeviceResolver} first being registered.
|
||||
*
|
||||
* @author Roy Clarkson
|
||||
* @since 1.1.0
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ SitePreferenceHandlerInterceptor.class,
|
||||
SitePreferenceHandlerMethodArgumentResolver.class })
|
||||
@AutoConfigureAfter(DeviceResolverAutoConfiguration.class)
|
||||
@ConditionalOnProperty(prefix = "spring.mobile.sitepreference", name = "enabled", havingValue = "true", matchIfMissing = true)
|
||||
@ConditionalOnWebApplication(type = Type.SERVLET)
|
||||
public class SitePreferenceAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(SitePreferenceHandlerInterceptor.class)
|
||||
public SitePreferenceHandlerInterceptor sitePreferenceHandlerInterceptor() {
|
||||
return new SitePreferenceHandlerInterceptor();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SitePreferenceHandlerMethodArgumentResolver sitePreferenceHandlerMethodArgumentResolver() {
|
||||
return new SitePreferenceHandlerMethodArgumentResolver();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class SitePreferenceMvcConfiguration implements WebMvcConfigurer {
|
||||
|
||||
private final SitePreferenceHandlerInterceptor sitePreferenceHandlerInterceptor;
|
||||
|
||||
private final SitePreferenceHandlerMethodArgumentResolver sitePreferenceHandlerMethodArgumentResolver;
|
||||
|
||||
protected SitePreferenceMvcConfiguration(
|
||||
SitePreferenceHandlerInterceptor sitePreferenceHandlerInterceptor,
|
||||
org.springframework.mobile.device.site.SitePreferenceHandlerMethodArgumentResolver sitePreferenceHandlerMethodArgumentResolver) {
|
||||
this.sitePreferenceHandlerInterceptor = sitePreferenceHandlerInterceptor;
|
||||
this.sitePreferenceHandlerMethodArgumentResolver = sitePreferenceHandlerMethodArgumentResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(this.sitePreferenceHandlerInterceptor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addArgumentResolvers(
|
||||
List<HandlerMethodArgumentResolver> argumentResolvers) {
|
||||
argumentResolvers.add(this.sitePreferenceHandlerMethodArgumentResolver);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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 Spring Mobile.
|
||||
*/
|
||||
package org.springframework.boot.autoconfigure.mobile;
|
@ -1,253 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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 java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.beans.PropertyAccessor;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.boot.web.servlet.view.MustacheViewResolver;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
|
||||
import org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link DeviceDelegatingViewResolverAutoConfiguration}.
|
||||
*
|
||||
* @author Roy Clarkson
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class DeviceDelegatingViewResolverAutoConfigurationTests {
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private AnnotationConfigWebApplicationContext context;
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deviceDelegatingViewResolverDefaultDisabled() throws Exception {
|
||||
load();
|
||||
this.thrown.expect(NoSuchBeanDefinitionException.class);
|
||||
this.context.getBean(LiteDeviceDelegatingViewResolver.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deviceDelegatingJspResourceViewResolver() throws Exception {
|
||||
load("spring.mobile.devicedelegatingviewresolver.enabled:true");
|
||||
assertThat(this.context.getBeansOfType(LiteDeviceDelegatingViewResolver.class))
|
||||
.hasSize(1);
|
||||
InternalResourceViewResolver internalResourceViewResolver = this.context
|
||||
.getBean(InternalResourceViewResolver.class);
|
||||
assertLiteDeviceDelegatingViewResolver(internalResourceViewResolver,
|
||||
"deviceDelegatingJspViewResolver");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deviceDelegatingFreeMarkerViewResolver() throws Exception {
|
||||
load(Collections.singletonList(FreeMarkerAutoConfiguration.class),
|
||||
"spring.mobile.devicedelegatingviewresolver.enabled:true");
|
||||
assertThat(this.context.getBeansOfType(LiteDeviceDelegatingViewResolver.class))
|
||||
.hasSize(2);
|
||||
assertLiteDeviceDelegatingViewResolver(
|
||||
this.context.getBean(FreeMarkerViewResolver.class),
|
||||
"deviceDelegatingFreeMarkerViewResolver");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deviceDelegatingGroovyMarkupViewResolver() throws Exception {
|
||||
load(Collections.singletonList(GroovyTemplateAutoConfiguration.class),
|
||||
"spring.mobile.devicedelegatingviewresolver.enabled:true");
|
||||
assertThat(this.context.getBeansOfType(LiteDeviceDelegatingViewResolver.class))
|
||||
.hasSize(2);
|
||||
assertLiteDeviceDelegatingViewResolver(
|
||||
this.context.getBean(GroovyMarkupViewResolver.class),
|
||||
"deviceDelegatingGroovyMarkupViewResolver");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deviceDelegatingMustacheViewResolver() throws Exception {
|
||||
load(Collections.singletonList(MustacheAutoConfiguration.class),
|
||||
"spring.mobile.devicedelegatingviewresolver.enabled:true");
|
||||
assertThat(this.context.getBeansOfType(LiteDeviceDelegatingViewResolver.class))
|
||||
.hasSize(2);
|
||||
assertLiteDeviceDelegatingViewResolver(
|
||||
this.context.getBean(MustacheViewResolver.class),
|
||||
"deviceDelegatingMustacheViewResolver");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deviceDelegatingThymeleafViewResolver() throws Exception {
|
||||
load(Collections.singletonList(ThymeleafAutoConfiguration.class),
|
||||
"spring.mobile.devicedelegatingviewresolver.enabled:true");
|
||||
assertThat(this.context.getBeansOfType(LiteDeviceDelegatingViewResolver.class))
|
||||
.hasSize(2);
|
||||
assertLiteDeviceDelegatingViewResolver(
|
||||
this.context.getBean(ThymeleafViewResolver.class),
|
||||
"deviceDelegatingThymeleafViewResolver");
|
||||
}
|
||||
|
||||
public void assertLiteDeviceDelegatingViewResolver(ViewResolver delegate,
|
||||
String delegatingBeanName) {
|
||||
LiteDeviceDelegatingViewResolver deviceDelegatingViewResolver = this.context
|
||||
.getBean(delegatingBeanName, LiteDeviceDelegatingViewResolver.class);
|
||||
assertThat(deviceDelegatingViewResolver.getViewResolver()).isSameAs(delegate);
|
||||
assertThat(deviceDelegatingViewResolver.getOrder())
|
||||
.isEqualTo(((Ordered) delegate).getOrder() - 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deviceDelegatingViewResolverDisabled() throws Exception {
|
||||
load(Arrays.asList(FreeMarkerAutoConfiguration.class,
|
||||
GroovyTemplateAutoConfiguration.class, MustacheAutoConfiguration.class,
|
||||
ThymeleafAutoConfiguration.class),
|
||||
"spring.mobile.devicedelegatingviewresolver.enabled:false");
|
||||
assertThat(this.context.getBeansOfType(LiteDeviceDelegatingViewResolver.class))
|
||||
.hasSize(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultPropertyValues() throws Exception {
|
||||
load("spring.mobile.devicedelegatingviewresolver.enabled:true");
|
||||
LiteDeviceDelegatingViewResolver liteDeviceDelegatingViewResolver = this.context
|
||||
.getBean("deviceDelegatingJspViewResolver",
|
||||
LiteDeviceDelegatingViewResolver.class);
|
||||
DirectFieldAccessor accessor = new DirectFieldAccessor(
|
||||
liteDeviceDelegatingViewResolver);
|
||||
assertThat(accessor.getPropertyValue("enableFallback")).isEqualTo(Boolean.FALSE);
|
||||
assertThat(accessor.getPropertyValue("normalPrefix")).isEqualTo("");
|
||||
assertThat(accessor.getPropertyValue("mobilePrefix")).isEqualTo("mobile/");
|
||||
assertThat(accessor.getPropertyValue("tabletPrefix")).isEqualTo("tablet/");
|
||||
assertThat(accessor.getPropertyValue("normalSuffix")).isEqualTo("");
|
||||
assertThat(accessor.getPropertyValue("mobileSuffix")).isEqualTo("");
|
||||
assertThat(accessor.getPropertyValue("tabletSuffix")).isEqualTo("");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void overrideEnableFallback() throws Exception {
|
||||
PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor(
|
||||
"spring.mobile.devicedelegatingviewresolver.enabled:true",
|
||||
"spring.mobile.devicedelegatingviewresolver.enableFallback:true");
|
||||
assertThat(accessor.getPropertyValue("enableFallback")).isEqualTo(Boolean.TRUE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void overrideNormalPrefix() throws Exception {
|
||||
PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor(
|
||||
"spring.mobile.devicedelegatingviewresolver.enabled:true",
|
||||
"spring.mobile.devicedelegatingviewresolver.normalPrefix:normal/");
|
||||
assertThat(accessor.getPropertyValue("normalPrefix")).isEqualTo("normal/");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void overrideMobilePrefix() throws Exception {
|
||||
PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor(
|
||||
"spring.mobile.devicedelegatingviewresolver.enabled:true",
|
||||
"spring.mobile.devicedelegatingviewresolver.mobilePrefix:mob/");
|
||||
assertThat(accessor.getPropertyValue("mobilePrefix")).isEqualTo("mob/");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void overrideTabletPrefix() throws Exception {
|
||||
PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor(
|
||||
"spring.mobile.devicedelegatingviewresolver.enabled:true",
|
||||
"spring.mobile.devicedelegatingviewresolver.tabletPrefix:tab/");
|
||||
assertThat(accessor.getPropertyValue("tabletPrefix")).isEqualTo("tab/");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void overrideNormalSuffix() throws Exception {
|
||||
PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor(
|
||||
"spring.mobile.devicedelegatingviewresolver.enabled:true",
|
||||
"spring.mobile.devicedelegatingviewresolver.normalSuffix:.nor");
|
||||
assertThat(accessor.getPropertyValue("normalSuffix")).isEqualTo(".nor");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void overrideMobileSuffix() throws Exception {
|
||||
PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor(
|
||||
"spring.mobile.devicedelegatingviewresolver.enabled:true",
|
||||
"spring.mobile.devicedelegatingviewresolver.mobileSuffix:.mob");
|
||||
assertThat(accessor.getPropertyValue("mobileSuffix")).isEqualTo(".mob");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void overrideTabletSuffix() throws Exception {
|
||||
PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor(
|
||||
"spring.mobile.devicedelegatingviewresolver.enabled:true",
|
||||
"spring.mobile.devicedelegatingviewresolver.tabletSuffix:.tab");
|
||||
assertThat(accessor.getPropertyValue("tabletSuffix")).isEqualTo(".tab");
|
||||
}
|
||||
|
||||
private PropertyAccessor getLiteDeviceDelegatingViewResolverAccessor(
|
||||
String... configuration) {
|
||||
load(configuration);
|
||||
LiteDeviceDelegatingViewResolver liteDeviceDelegatingViewResolver = this.context
|
||||
.getBean("deviceDelegatingJspViewResolver",
|
||||
LiteDeviceDelegatingViewResolver.class);
|
||||
return new DirectFieldAccessor(liteDeviceDelegatingViewResolver);
|
||||
}
|
||||
|
||||
public void load(String... environment) {
|
||||
load(null, environment);
|
||||
}
|
||||
|
||||
public void load(List<Class<?>> config, String... environment) {
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
this.context.setServletContext(new MockServletContext());
|
||||
if (config != null) {
|
||||
this.context.register(config.toArray(new Class[config.size()]));
|
||||
}
|
||||
this.context.register(WebMvcAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class,
|
||||
DeviceDelegatingViewResolverAutoConfiguration.class);
|
||||
TestPropertyValues.of(environment).applyTo(this.context);
|
||||
this.context.refresh();
|
||||
}
|
||||
|
||||
}
|
@ -1,137 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.mobile.device.Device;
|
||||
import org.springframework.mobile.device.DeviceHandlerMethodArgumentResolver;
|
||||
import org.springframework.mobile.device.DeviceResolverHandlerInterceptor;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
/**
|
||||
* Tests for {@link DeviceResolverAutoConfiguration}.
|
||||
*
|
||||
* @author Roy Clarkson
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class DeviceResolverAutoConfigurationTests {
|
||||
|
||||
private AnnotationConfigWebApplicationContext context;
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deviceResolverHandlerInterceptorCreated() throws Exception {
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
this.context.register(DeviceResolverAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.getBean(DeviceResolverHandlerInterceptor.class))
|
||||
.isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deviceHandlerMethodArgumentResolverCreated() throws Exception {
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
this.context.register(DeviceResolverAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.getBean(DeviceHandlerMethodArgumentResolver.class))
|
||||
.isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deviceResolverHandlerInterceptorRegistered() throws Exception {
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
this.context.setServletContext(new MockServletContext());
|
||||
this.context.register(Config.class);
|
||||
this.context.refresh();
|
||||
RequestMappingHandlerMapping mapping = this.context
|
||||
.getBean(RequestMappingHandlerMapping.class);
|
||||
HandlerInterceptor[] interceptors = mapping
|
||||
.getHandler(new MockHttpServletRequest()).getInterceptors();
|
||||
assertThat(interceptors)
|
||||
.hasAtLeastOneElementOfType(DeviceResolverHandlerInterceptor.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deviceHandlerMethodArgumentWorksWithSpringData() throws Exception {
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
this.context.register(Config.class);
|
||||
this.context.setServletContext(new MockServletContext());
|
||||
this.context.refresh();
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
|
||||
mockMvc.perform(get("/")).andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ImportAutoConfiguration({ WebMvcAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
DeviceResolverAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class,
|
||||
SpringDataWebAutoConfiguration.class,
|
||||
RepositoryRestMvcAutoConfiguration.class })
|
||||
protected static class Config {
|
||||
|
||||
@Bean
|
||||
public MyController controller() {
|
||||
return new MyController();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Controller
|
||||
protected static class MyController {
|
||||
|
||||
@RequestMapping("/")
|
||||
public ResponseEntity<Void> test(Device device) {
|
||||
if (device.getDevicePlatform() != null) {
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,157 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.mobile.device.site.SitePreferenceHandlerInterceptor;
|
||||
import org.springframework.mobile.device.site.SitePreferenceHandlerMethodArgumentResolver;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link SitePreferenceAutoConfiguration}.
|
||||
*
|
||||
* @author Roy Clarkson
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class SitePreferenceAutoConfigurationTests {
|
||||
|
||||
private AnnotationConfigWebApplicationContext context;
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sitePreferenceHandlerInterceptorCreated() {
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
this.context.register(SitePreferenceAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.getBean(SitePreferenceHandlerInterceptor.class))
|
||||
.isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sitePreferenceHandlerInterceptorEnabled() throws Exception {
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
TestPropertyValues.of("spring.mobile.sitepreference.enabled:true")
|
||||
.applyTo(this.context);
|
||||
this.context.register(SitePreferenceAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.getBean(SitePreferenceHandlerInterceptor.class))
|
||||
.isNotNull();
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchBeanDefinitionException.class)
|
||||
public void sitePreferenceHandlerInterceptorDisabled() {
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
TestPropertyValues.of("spring.mobile.sitepreference.enabled:false")
|
||||
.applyTo(this.context);
|
||||
this.context.register(SitePreferenceAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
this.context.getBean(SitePreferenceHandlerInterceptor.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sitePreferenceMethodArgumentResolverCreated() throws Exception {
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
this.context.register(SitePreferenceAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(
|
||||
this.context.getBean(SitePreferenceHandlerMethodArgumentResolver.class))
|
||||
.isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sitePreferenceMethodArgumentResolverEnabled() throws Exception {
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
TestPropertyValues.of("spring.mobile.sitepreference.enabled:true")
|
||||
.applyTo(this.context);
|
||||
this.context.register(SitePreferenceAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(
|
||||
this.context.getBean(SitePreferenceHandlerMethodArgumentResolver.class))
|
||||
.isNotNull();
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchBeanDefinitionException.class)
|
||||
public void sitePreferenceMethodArgumentResolverDisabled() {
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
TestPropertyValues.of("spring.mobile.sitepreference.enabled:false")
|
||||
.applyTo(this.context);
|
||||
this.context.register(SitePreferenceAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
this.context.getBean(SitePreferenceHandlerMethodArgumentResolver.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sitePreferenceHandlerInterceptorRegistered() throws Exception {
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
this.context.setServletContext(new MockServletContext());
|
||||
this.context.register(Config.class, WebMvcAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
SitePreferenceAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
RequestMappingHandlerMapping mapping = this.context
|
||||
.getBean(RequestMappingHandlerMapping.class);
|
||||
HandlerInterceptor[] interceptors = mapping
|
||||
.getHandler(new MockHttpServletRequest()).getInterceptors();
|
||||
assertThat(interceptors)
|
||||
.hasAtLeastOneElementOfType(SitePreferenceHandlerInterceptor.class);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class Config {
|
||||
|
||||
@Bean
|
||||
public MyController controller() {
|
||||
return new MyController();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Controller
|
||||
protected static class MyController {
|
||||
|
||||
@RequestMapping("/")
|
||||
public void test() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package org.test
|
||||
|
||||
@Controller
|
||||
@EnableDeviceResolver
|
||||
class Example {
|
||||
|
||||
@RequestMapping("/")
|
||||
@ResponseBody
|
||||
String helloWorld(Device device) {
|
||||
if (device.isNormal()) {
|
||||
"Hello Normal Device!"
|
||||
} else if (device.isMobile()) {
|
||||
"Hello Mobile Device!"
|
||||
} else if (device.isTablet()) {
|
||||
"Hello Tablet Device!"
|
||||
} else {
|
||||
"Hello Unknown Device!"
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,53 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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.cli.compiler.autoconfigure;
|
||||
|
||||
import org.codehaus.groovy.ast.ClassNode;
|
||||
import org.codehaus.groovy.control.CompilationFailedException;
|
||||
import org.codehaus.groovy.control.customizers.ImportCustomizer;
|
||||
|
||||
import org.springframework.boot.cli.compiler.AstUtils;
|
||||
import org.springframework.boot.cli.compiler.CompilerAutoConfiguration;
|
||||
import org.springframework.boot.cli.compiler.DependencyCustomizer;
|
||||
import org.springframework.boot.groovy.EnableDeviceResolver;
|
||||
|
||||
/**
|
||||
* {@link CompilerAutoConfiguration} for Spring Mobile.
|
||||
*
|
||||
* @author Roy Clarkson
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class SpringMobileCompilerAutoConfiguration extends CompilerAutoConfiguration {
|
||||
|
||||
@Override
|
||||
public boolean matches(ClassNode classNode) {
|
||||
return AstUtils.hasAtLeastOneAnnotation(classNode, "EnableDeviceResolver");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyDependencies(DependencyCustomizer dependencies)
|
||||
throws CompilationFailedException {
|
||||
dependencies.add("spring-boot-starter-mobile");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyImports(ImportCustomizer imports) throws CompilationFailedException {
|
||||
imports.addStarImports("org.springframework.mobile.device");
|
||||
imports.addImports(EnableDeviceResolver.class.getCanonicalName());
|
||||
}
|
||||
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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.groovy;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.boot.cli.compiler.autoconfigure.SpringMobileCompilerAutoConfiguration;
|
||||
|
||||
/**
|
||||
* Pseudo annotation used to trigger {@link SpringMobileCompilerAutoConfiguration}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface EnableDeviceResolver {
|
||||
|
||||
}
|
@ -1,52 +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>${revision}</version>
|
||||
</parent>
|
||||
<artifactId>spring-boot-starter-mobile</artifactId>
|
||||
<name>Spring Boot Mobile Starter</name>
|
||||
<description>Starter for building web applications using Spring Mobile</description>
|
||||
<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>org.springframework.mobile</groupId>
|
||||
<artifactId>spring-mobile-device</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.basepom.maven</groupId>
|
||||
<artifactId>duplicate-finder-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>duplicate-dependencies</id>
|
||||
<phase>validate</phase>
|
||||
<goals>
|
||||
<goal>check</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<ignoredClassPatterns>
|
||||
<ignoredClassPattern>javax.annotation.*</ignoredClassPattern>
|
||||
</ignoredClassPatterns>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -1 +0,0 @@
|
||||
provides: spring-mobile-device
|
Loading…
Reference in New Issue