Refactored instantiation of helper classes: @Service instead of manual
[examples/facebook-app] / src / main / java / de / juplo / yourshouter / SocialConfig.java
1 package de.juplo.yourshouter;
2
3
4
5 import javax.inject.Inject;
6 import org.apache.http.HttpRequestFactory;
7 import org.springframework.context.annotation.Bean;
8 import org.springframework.context.annotation.Configuration;
9 import org.springframework.context.annotation.Scope;
10 import org.springframework.context.annotation.ScopedProxyMode;
11 import org.springframework.social.UserIdSource;
12 import org.springframework.core.env.Environment;
13 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
14 import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
15 import org.springframework.social.config.annotation.EnableSocial;
16 import org.springframework.social.config.annotation.SocialConfigurerAdapter;
17 import org.springframework.social.connect.Connection;
18 import org.springframework.social.connect.ConnectionFactoryLocator;
19 import org.springframework.social.connect.ConnectionRepository;
20 import org.springframework.social.connect.ConnectionSignUp;
21 import org.springframework.social.connect.UsersConnectionRepository;
22 import org.springframework.social.connect.mem.InMemoryUsersConnectionRepository;
23 import org.springframework.social.connect.web.ConnectController;
24 import org.springframework.social.connect.web.ProviderSignInController;
25 import org.springframework.social.connect.web.SignInAdapter;
26 import org.springframework.social.facebook.api.Facebook;
27 import org.springframework.social.facebook.connect.FacebookConnectionFactory;
28 import org.springframework.social.facebook.web.CanvasSignInController;
29
30
31 /**
32  * Spring Social Configuration.
33  *
34  * @author Kai Moritz
35  */
36 @Configuration
37 @EnableSocial
38 public class SocialConfig extends SocialConfigurerAdapter
39 {
40   @Inject
41   ConnectionSignUp connectionSignUp;
42   @Inject
43   SignInAdapter signInAdapter;
44
45
46   /**
47    * Add a {@link FacebookConnectionFactory} to the configuration.
48    * The factory is configured through the keys <code>facebook.app.id</code>
49    * and <code>facebook.app.secret</code>.
50    *
51    * @param config
52    * @param env 
53    */
54   @Override
55   public void addConnectionFactories(
56       ConnectionFactoryConfigurer config,
57       Environment env
58       )
59   {
60     config.addConnectionFactory(
61         new FacebookConnectionFactory(
62             env.getProperty("facebook.app.id"),
63             env.getProperty("facebook.app.secret")
64             )
65         );
66   }
67
68   /**
69    * Configure an instance of {@link InMemoryUsersConnection} as persistent
70    * store of user/connection-mappings.
71    *
72    * At the moment, no special configuration is needed.
73    *
74    * @param connectionFactoryLocator
75    *     The {@link ConnectionFactoryLocator} will be injected by Spring.
76    * @return
77    *     The configured {@link UsersConnectionRepository}.
78    */
79   @Override
80   public UsersConnectionRepository getUsersConnectionRepository(
81       ConnectionFactoryLocator connectionFactoryLocator
82       )
83   {
84     InMemoryUsersConnectionRepository repository =
85         new InMemoryUsersConnectionRepository(connectionFactoryLocator);
86     repository.setConnectionSignUp(connectionSignUp);
87     return repository;
88   }
89
90   /**
91    * Configure our new implementation of {@link UserIdSource}, that retrieves
92    * the current user from the {@link SecurityContext}.
93    *
94    * @return
95    *     An instance of {@link AnonymousUserIdSource}.
96    *
97    * @see {@link SecurityContextUserIdSource}
98    * @see {@link SecurityContext}
99    * @see {@link UserCookieInterceptor}
100    */
101   @Override
102   public UserIdSource getUserIdSource()
103   {
104     return new SecurityContextUserIdSource();
105   }
106
107
108   /**
109    * Configuration of the controller, that handles the authorization against
110    * the Facebook-API, to connect a user to Facebook.
111    *
112    * At the moment, no special configuration is needed.
113    *
114    * @param factoryLocator
115    *     The {@link ConnectionFactoryLocator} will be injected by Spring.
116    * @param repository
117    *     The {@link ConnectionRepository} will be injected by Spring.
118    * @return
119    *     The configured controller.
120    */
121   @Bean
122   public ConnectController connectController(
123       ConnectionFactoryLocator factoryLocator,
124       ConnectionRepository repository
125       )
126   {
127     ConnectController controller =
128         new ConnectController(factoryLocator, repository);
129     return controller;
130   }
131
132   /**
133    * Configure the {@link ProviderSignInController} to use our implementation
134    * of {@link SignInAdapter} to sign in the user by storing the ID in the
135    * {@link SecurityContext} and the user-cookie.
136    *
137    * @param factoryLocator The {@link ConnectionFactoryLocator} will be injected by Spring.
138    * @param repository The {@link UserConnectionRepository} will be injected by Spring.
139    * @return The configured {@link ProviderSignInController}
140    */
141   @Bean
142   public ProviderSignInController signInController(
143       ConnectionFactoryLocator factoryLocator,
144       UsersConnectionRepository repository
145       )
146   {
147     ProviderSignInController controller =
148         new ProviderSignInController(factoryLocator, repository, signInAdapter);
149     return controller;
150   }
151
152   /**
153    * Configure the {@link CanvasSignInController} to enable sign-in through
154    * the <code>signed_request</code>, that Facebook sends to the canvas-page.
155    *
156    * @param factoryLocator The {@link ConnectionFactoryLocator} will be injected by Spring.
157    * @param repository The {@link UserConnectionRepository} will be injected by Spring.
158    * @param env The {@link Environment}, to read additional parameters from.
159    * @return The configured {@link CanvasSignInController}
160    */
161   @Bean
162   public CanvasSignInController canvasSignInController(
163       ConnectionFactoryLocator factoryLocator,
164       UsersConnectionRepository repository,
165       Environment env
166       )
167   {
168     return
169         new CanvasSignInController(
170             factoryLocator,
171             repository,
172             signInAdapter,
173             env.getProperty("facebook.app.id"),
174             env.getProperty("facebook.app.secret"),
175             env.getProperty("facebook.app.canvas")
176             );
177   }
178
179   /**
180    * Configure a scoped bean named <code>facebook</code>, that enables
181    * access to the Graph-API in the name of the current user.
182    *
183    * @param repository
184    *     The {@link ConnectionRepository} will be injected by Spring.
185    * @return
186    *     A {@Connection<Facebook>}, that represents the authorization of the
187    *     current user against the Graph-API, or <code>null</code>, if the
188    *     current user is not connected to the API.
189    */
190   @Bean
191   @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
192   public Facebook facebook(ConnectionRepository repository)
193   {
194     Connection<Facebook> connection =
195         repository.findPrimaryConnection(Facebook.class);
196     return connection != null ? connection.getApi() : null;
197   }
198
199   /**
200    * Use the <code>HttpClient</code> from Apaches <code>HttpComponents</code>
201    * for HTTP-requests.
202    *
203    * We also configure shorter intervals for the connection timeout and the
204    * read timeout.
205    *
206    * @param env The {@link Environment}, to read additional parameters from.
207    * @return The alternative implementation of {@link HttpRequestFactory}.
208    */
209   @Bean
210   public HttpComponentsClientHttpRequestFactory requestFactory(Environment env)
211   {
212     HttpComponentsClientHttpRequestFactory factory =
213         new HttpComponentsClientHttpRequestFactory();
214     factory.setConnectTimeout(
215         Integer.parseInt(env.getProperty("httpclient.timeout.connection"))
216         );
217     factory.setReadTimeout(
218         Integer.parseInt(env.getProperty("httpclient.timeout.read"))
219         );
220     return factory;
221   }
222 }