001/*
002 * Copyright 2012-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.autoconfigure.mobile;
018
019import java.util.List;
020
021import org.springframework.boot.autoconfigure.AutoConfigureAfter;
022import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
023import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
024import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
025import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
026import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
027import org.springframework.context.annotation.Bean;
028import org.springframework.context.annotation.Configuration;
029import org.springframework.core.annotation.Order;
030import org.springframework.mobile.device.DeviceHandlerMethodArgumentResolver;
031import org.springframework.mobile.device.DeviceResolver;
032import org.springframework.mobile.device.DeviceResolverHandlerInterceptor;
033import org.springframework.web.method.support.HandlerMethodArgumentResolver;
034import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
035import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
036
037/**
038 * {@link EnableAutoConfiguration Auto-configuration} for Spring Mobile's
039 * {@link DeviceResolver}.
040 *
041 * @author Roy Clarkson
042 */
043@Configuration
044@ConditionalOnClass({ DeviceResolverHandlerInterceptor.class,
045                DeviceHandlerMethodArgumentResolver.class })
046@AutoConfigureAfter(WebMvcAutoConfiguration.class)
047@ConditionalOnWebApplication
048public class DeviceResolverAutoConfiguration {
049
050        @Bean
051        @ConditionalOnMissingBean(DeviceResolverHandlerInterceptor.class)
052        public DeviceResolverHandlerInterceptor deviceResolverHandlerInterceptor() {
053                return new DeviceResolverHandlerInterceptor();
054        }
055
056        @Bean
057        public DeviceHandlerMethodArgumentResolver deviceHandlerMethodArgumentResolver() {
058                return new DeviceHandlerMethodArgumentResolver();
059        }
060
061        @Configuration
062        @Order(0)
063        protected static class DeviceResolverMvcConfiguration
064                        extends WebMvcConfigurerAdapter {
065
066                private DeviceResolverHandlerInterceptor deviceResolverHandlerInterceptor;
067
068                private DeviceHandlerMethodArgumentResolver deviceHandlerMethodArgumentResolver;
069
070                protected DeviceResolverMvcConfiguration(
071                                DeviceResolverHandlerInterceptor deviceResolverHandlerInterceptor,
072                                DeviceHandlerMethodArgumentResolver deviceHandlerMethodArgumentResolver) {
073                        this.deviceResolverHandlerInterceptor = deviceResolverHandlerInterceptor;
074                        this.deviceHandlerMethodArgumentResolver = deviceHandlerMethodArgumentResolver;
075                }
076
077                @Override
078                public void addInterceptors(InterceptorRegistry registry) {
079                        registry.addInterceptor(this.deviceResolverHandlerInterceptor);
080                }
081
082                @Override
083                public void addArgumentResolvers(
084                                List<HandlerMethodArgumentResolver> argumentResolvers) {
085                        argumentResolvers.add(this.deviceHandlerMethodArgumentResolver);
086                }
087
088        }
089
090}