53087095376ef0cfa2384cdc5efeeb2b9efed71d
[examples/facebook-app] / src / main / java / de / juplo / yourshouter / SocialConfig.java
1 package de.juplo.yourshouter;
2
3
4
5 import org.springframework.context.annotation.Bean;
6 import org.springframework.context.annotation.Configuration;
7 import org.springframework.context.annotation.Scope;
8 import org.springframework.context.annotation.ScopedProxyMode;
9 import org.springframework.social.UserIdSource;
10 import org.springframework.core.env.Environment;
11 import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
12 import org.springframework.social.config.annotation.EnableSocial;
13 import org.springframework.social.config.annotation.SocialConfigurerAdapter;
14 import org.springframework.social.connect.Connection;
15 import org.springframework.social.connect.ConnectionFactoryLocator;
16 import org.springframework.social.connect.ConnectionRepository;
17 import org.springframework.social.connect.UsersConnectionRepository;
18 import org.springframework.social.connect.mem.InMemoryUsersConnectionRepository;
19 import org.springframework.social.connect.web.ConnectController;
20 import org.springframework.social.facebook.api.Facebook;
21 import org.springframework.social.facebook.connect.FacebookConnectionFactory;
22
23
24 /**
25  * Spring Social Configuration.
26  *
27  * @author Kai Moritz
28  */
29 @Configuration
30 @EnableSocial
31 public class SocialConfig extends SocialConfigurerAdapter
32 {
33   /**
34    * Add a {@link FacebookConnectionFactory} to the configuration.
35    * The factory is configured through the keys <code>facebook.app.id</code>
36    * and <code>facebook.app.secret</code>.
37    *
38    * @param config
39    * @param env 
40    */
41   @Override
42   public void addConnectionFactories(
43       ConnectionFactoryConfigurer config,
44       Environment env
45       )
46   {
47     config.addConnectionFactory(
48         new FacebookConnectionFactory(
49             env.getProperty("facebook.app.id"),
50             env.getProperty("facebook.app.secret")
51             )
52         );
53   }
54
55   /**
56    * Configure an instance of {@link InMemoryUsersConnection} as persistent
57    * store of user/connection-mappings.
58    *
59    * At the moment, no special configuration is needed.
60    *
61    * @param connectionFactoryLocator
62    *     The {@link ConnectionFactoryLocator} will be injected by Spring.
63    * @return
64    *     The configured {@link UsersConnectionRepository}.
65    */
66   @Override
67   public UsersConnectionRepository getUsersConnectionRepository(
68       ConnectionFactoryLocator connectionFactoryLocator
69       )
70   {
71     InMemoryUsersConnectionRepository repository =
72         new InMemoryUsersConnectionRepository(connectionFactoryLocator);
73     return repository;
74   }
75
76   /**
77    * Configure a {@link UserIdSource}, that is equivalent to the one, that is
78    * created by Spring-Boot.
79    *
80    * @return
81    *     An instance of {@link AnonymousUserIdSource}.
82    *
83    * @see {@link AnonymousUserIdSource}
84    */
85   @Override
86   public UserIdSource getUserIdSource()
87   {
88     return new AnonymousUserIdSource();
89   }
90
91
92   /**
93    * Configuration of the controller, that handles the authorization against
94    * the Facebook-API, to connect a user to Facebook.
95    *
96    * At the moment, no special configuration is needed.
97    *
98    * @param factoryLocator
99    *     The {@link ConnectionFactoryLocator} will be injected by Spring.
100    * @param repository
101    *     The {@link ConnectionRepository} will be injected by Spring.
102    * @return
103    *     The configured controller.
104    */
105   @Bean
106   public ConnectController connectController(
107       ConnectionFactoryLocator factoryLocator,
108       ConnectionRepository repository
109       )
110   {
111     ConnectController controller =
112         new ConnectController(factoryLocator, repository);
113     return controller;
114   }
115
116   /**
117    * Configure a scoped bean named <code>facebook</code>, that enables
118    * access to the Graph-API in the name of the current user.
119    *
120    * @param repository
121    *     The {@link ConnectionRepository} will be injected by Spring.
122    * @return
123    *     A {@Connection<Facebook>}, that represents the authorization of the
124    *     current user against the Graph-API, or <code>null</code>, if the
125    *     current user is not connected to the API.
126    */
127   @Bean
128   @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
129   public Facebook facebook(ConnectionRepository repository)
130   {
131     Connection<Facebook> connection =
132         repository.findPrimaryConnection(Facebook.class);
133     return connection != null ? connection.getApi() : null;
134   }
135 }