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.ConditionalOnProperty; 026import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; 027import org.springframework.context.annotation.Bean; 028import org.springframework.context.annotation.Configuration; 029import org.springframework.mobile.device.DeviceResolver; 030import org.springframework.mobile.device.site.SitePreferenceHandler; 031import org.springframework.mobile.device.site.SitePreferenceHandlerInterceptor; 032import org.springframework.mobile.device.site.SitePreferenceHandlerMethodArgumentResolver; 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 SitePreferenceHandler}. The site preference feature depends on a 040 * {@link DeviceResolver} first being registered. 041 * 042 * @author Roy Clarkson 043 * @since 1.1.0 044 */ 045@Configuration 046@ConditionalOnClass({ SitePreferenceHandlerInterceptor.class, 047 SitePreferenceHandlerMethodArgumentResolver.class }) 048@AutoConfigureAfter(DeviceResolverAutoConfiguration.class) 049@ConditionalOnProperty(prefix = "spring.mobile.sitepreference", name = "enabled", havingValue = "true", matchIfMissing = true) 050@ConditionalOnWebApplication 051public class SitePreferenceAutoConfiguration { 052 053 @Bean 054 @ConditionalOnMissingBean(SitePreferenceHandlerInterceptor.class) 055 public SitePreferenceHandlerInterceptor sitePreferenceHandlerInterceptor() { 056 return new SitePreferenceHandlerInterceptor(); 057 } 058 059 @Bean 060 public SitePreferenceHandlerMethodArgumentResolver sitePreferenceHandlerMethodArgumentResolver() { 061 return new SitePreferenceHandlerMethodArgumentResolver(); 062 } 063 064 @Configuration 065 protected static class SitePreferenceMvcConfiguration 066 extends WebMvcConfigurerAdapter { 067 068 private final SitePreferenceHandlerInterceptor sitePreferenceHandlerInterceptor; 069 070 private final SitePreferenceHandlerMethodArgumentResolver sitePreferenceHandlerMethodArgumentResolver; 071 072 protected SitePreferenceMvcConfiguration( 073 SitePreferenceHandlerInterceptor sitePreferenceHandlerInterceptor, 074 org.springframework.mobile.device.site.SitePreferenceHandlerMethodArgumentResolver sitePreferenceHandlerMethodArgumentResolver) { 075 this.sitePreferenceHandlerInterceptor = sitePreferenceHandlerInterceptor; 076 this.sitePreferenceHandlerMethodArgumentResolver = sitePreferenceHandlerMethodArgumentResolver; 077 } 078 079 @Override 080 public void addInterceptors(InterceptorRegistry registry) { 081 registry.addInterceptor(this.sitePreferenceHandlerInterceptor); 082 } 083 084 @Override 085 public void addArgumentResolvers( 086 List<HandlerMethodArgumentResolver> argumentResolvers) { 087 argumentResolvers.add(this.sitePreferenceHandlerMethodArgumentResolver); 088 } 089 090 } 091 092}