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.social;
018
019import org.springframework.boot.autoconfigure.AutoConfigureAfter;
020import org.springframework.boot.autoconfigure.AutoConfigureBefore;
021import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
022import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
023import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
024import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
025import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
026import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
027import org.springframework.boot.context.properties.EnableConfigurationProperties;
028import org.springframework.context.annotation.Bean;
029import org.springframework.context.annotation.Configuration;
030import org.springframework.context.annotation.Scope;
031import org.springframework.context.annotation.ScopedProxyMode;
032import org.springframework.social.config.annotation.EnableSocial;
033import org.springframework.social.config.annotation.SocialConfigurerAdapter;
034import org.springframework.social.connect.Connection;
035import org.springframework.social.connect.ConnectionFactory;
036import org.springframework.social.connect.ConnectionRepository;
037import org.springframework.social.connect.web.GenericConnectionStatusView;
038import org.springframework.social.facebook.api.Facebook;
039import org.springframework.social.facebook.connect.FacebookConnectionFactory;
040
041/**
042 * {@link EnableAutoConfiguration Auto-configuration} for Spring Social connectivity with
043 * Facebook.
044 *
045 * @author Craig Walls
046 * @since 1.1.0
047 */
048@Configuration
049@ConditionalOnClass({ SocialConfigurerAdapter.class, FacebookConnectionFactory.class })
050@ConditionalOnProperty(prefix = "spring.social.facebook", name = "app-id")
051@AutoConfigureBefore(SocialWebAutoConfiguration.class)
052@AutoConfigureAfter(WebMvcAutoConfiguration.class)
053public class FacebookAutoConfiguration {
054
055        @Configuration
056        @EnableSocial
057        @EnableConfigurationProperties(FacebookProperties.class)
058        @ConditionalOnWebApplication
059        protected static class FacebookConfigurerAdapter extends SocialAutoConfigurerAdapter {
060
061                private final FacebookProperties properties;
062
063                protected FacebookConfigurerAdapter(FacebookProperties properties) {
064                        this.properties = properties;
065                }
066
067                @Bean
068                @ConditionalOnMissingBean(Facebook.class)
069                @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
070                public Facebook facebook(ConnectionRepository repository) {
071                        Connection<Facebook> connection = repository
072                                        .findPrimaryConnection(Facebook.class);
073                        return connection != null ? connection.getApi() : null;
074                }
075
076                @Bean(name = { "connect/facebookConnect", "connect/facebookConnected" })
077                @ConditionalOnProperty(prefix = "spring.social", name = "auto-connection-views")
078                public GenericConnectionStatusView facebookConnectView() {
079                        return new GenericConnectionStatusView("facebook", "Facebook");
080                }
081
082                @Override
083                protected ConnectionFactory<?> createConnectionFactory() {
084                        return new FacebookConnectionFactory(this.properties.getAppId(),
085                                        this.properties.getAppSecret());
086                }
087
088        }
089
090}