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.linkedin.api.LinkedIn;
039import org.springframework.social.linkedin.connect.LinkedInConnectionFactory;
040
041/**
042 * {@link EnableAutoConfiguration Auto-configuration} for Spring Social connectivity with
043 * LinkedIn.
044 *
045 * @author Craig Walls
046 * @since 1.1.0
047 */
048@Configuration
049@ConditionalOnClass({ SocialConfigurerAdapter.class, LinkedInConnectionFactory.class })
050@ConditionalOnProperty(prefix = "spring.social.linkedin", name = "app-id")
051@AutoConfigureBefore(SocialWebAutoConfiguration.class)
052@AutoConfigureAfter(WebMvcAutoConfiguration.class)
053public class LinkedInAutoConfiguration {
054
055        @Configuration
056        @EnableSocial
057        @EnableConfigurationProperties(LinkedInProperties.class)
058        @ConditionalOnWebApplication
059        protected static class LinkedInConfigurerAdapter extends SocialAutoConfigurerAdapter {
060
061                private final LinkedInProperties properties;
062
063                protected LinkedInConfigurerAdapter(LinkedInProperties properties) {
064                        this.properties = properties;
065                }
066
067                @Bean
068                @ConditionalOnMissingBean(LinkedIn.class)
069                @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
070                public LinkedIn linkedin(ConnectionRepository repository) {
071                        Connection<LinkedIn> connection = repository
072                                        .findPrimaryConnection(LinkedIn.class);
073                        return connection != null ? connection.getApi() : null;
074                }
075
076                @Bean(name = { "connect/linkedinConnect", "connect/linkedinConnected" })
077                @ConditionalOnProperty(prefix = "spring.social", name = "auto-connection-views")
078                public GenericConnectionStatusView linkedInConnectView() {
079                        return new GenericConnectionStatusView("linkedin", "LinkedIn");
080                }
081
082                @Override
083                protected ConnectionFactory<?> createConnectionFactory() {
084                        return new LinkedInConnectionFactory(this.properties.getAppId(),
085                                        this.properties.getAppSecret());
086                }
087
088        }
089
090}