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 org.thymeleaf.spring4.view.ThymeleafViewResolver; 020 021import org.springframework.boot.autoconfigure.AutoConfigureAfter; 022import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 023import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; 024import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 025import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 026import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; 027import org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration; 028import org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration; 029import org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration; 030import org.springframework.boot.autoconfigure.mustache.web.MustacheViewResolver; 031import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration; 032import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; 033import org.springframework.boot.context.properties.EnableConfigurationProperties; 034import org.springframework.context.annotation.Bean; 035import org.springframework.context.annotation.Configuration; 036import org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver; 037import org.springframework.web.servlet.view.InternalResourceViewResolver; 038import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver; 039import org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver; 040 041/** 042 * {@link EnableAutoConfiguration Auto-configuration} for Spring Mobile's 043 * {@link LiteDeviceDelegatingViewResolver}. If {@link ThymeleafViewResolver} is available 044 * it is configured as the delegate view resolver. Otherwise, 045 * {@link InternalResourceViewResolver} is used as a fallback. 046 * 047 * @author Roy Clarkson 048 * @author Stephane Nicoll 049 * @since 1.1.0 050 */ 051@Configuration 052@ConditionalOnWebApplication 053@ConditionalOnClass(LiteDeviceDelegatingViewResolver.class) 054@ConditionalOnProperty(prefix = "spring.mobile.devicedelegatingviewresolver", name = "enabled", havingValue = "true") 055@EnableConfigurationProperties(DeviceDelegatingViewResolverProperties.class) 056@AutoConfigureAfter({ WebMvcAutoConfiguration.class, FreeMarkerAutoConfiguration.class, 057 GroovyTemplateAutoConfiguration.class, MustacheAutoConfiguration.class, 058 ThymeleafAutoConfiguration.class }) 059public class DeviceDelegatingViewResolverAutoConfiguration { 060 061 @Configuration 062 protected static class LiteDeviceDelegatingViewResolverFactoryConfiguration { 063 064 @Bean 065 public DeviceDelegatingViewResolverFactory deviceDelegatingViewResolverFactory( 066 DeviceDelegatingViewResolverProperties properties) { 067 return new DeviceDelegatingViewResolverFactory(properties); 068 } 069 070 } 071 072 @Configuration 073 @ConditionalOnClass(FreeMarkerViewResolver.class) 074 protected static class DeviceDelegatingFreeMarkerViewResolverConfiguration { 075 076 @Bean 077 @ConditionalOnBean(FreeMarkerViewResolver.class) 078 public LiteDeviceDelegatingViewResolver deviceDelegatingFreeMarkerViewResolver( 079 DeviceDelegatingViewResolverFactory factory, 080 FreeMarkerViewResolver viewResolver) { 081 return factory.createViewResolver(viewResolver); 082 } 083 084 } 085 086 @Configuration 087 @ConditionalOnClass(GroovyMarkupViewResolver.class) 088 protected static class DeviceDelegatingGroovyMarkupViewResolverConfiguration { 089 090 @Bean 091 @ConditionalOnBean(GroovyMarkupViewResolver.class) 092 public LiteDeviceDelegatingViewResolver deviceDelegatingGroovyMarkupViewResolver( 093 DeviceDelegatingViewResolverFactory factory, 094 GroovyMarkupViewResolver viewResolver) { 095 return factory.createViewResolver(viewResolver); 096 } 097 098 } 099 100 @Configuration 101 @ConditionalOnClass(InternalResourceViewResolver.class) 102 protected static class DeviceDelegatingJspViewResolverConfiguration { 103 104 @Bean 105 @ConditionalOnBean(InternalResourceViewResolver.class) 106 public LiteDeviceDelegatingViewResolver deviceDelegatingJspViewResolver( 107 DeviceDelegatingViewResolverFactory factory, 108 InternalResourceViewResolver viewResolver) { 109 return factory.createViewResolver(viewResolver); 110 } 111 112 } 113 114 @Configuration 115 @ConditionalOnClass(MustacheViewResolver.class) 116 protected static class DeviceDelegatingMustacheViewResolverConfiguration { 117 118 @Bean 119 @ConditionalOnBean(MustacheViewResolver.class) 120 public LiteDeviceDelegatingViewResolver deviceDelegatingMustacheViewResolver( 121 DeviceDelegatingViewResolverFactory factory, 122 MustacheViewResolver viewResolver) { 123 return factory.createViewResolver(viewResolver); 124 } 125 126 } 127 128 @Configuration 129 @ConditionalOnClass(ThymeleafViewResolver.class) 130 protected static class DeviceDelegatingThymeleafViewResolverConfiguration { 131 132 @Bean 133 @ConditionalOnBean(ThymeleafViewResolver.class) 134 public LiteDeviceDelegatingViewResolver deviceDelegatingThymeleafViewResolver( 135 DeviceDelegatingViewResolverFactory factory, 136 ThymeleafViewResolver viewResolver) { 137 return factory.createViewResolver(viewResolver); 138 } 139 140 } 141 142}