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