6a6cba62fecb33b8cd85d9de55d7d447a5ba1321
[examples/facebook-app] / src / main / java / de / juplo / yourshouter / UserCookieInterceptor.java
1 package de.juplo.yourshouter;
2
3
4 import java.util.Collections;
5 import javax.servlet.http.HttpServletRequest;
6 import javax.servlet.http.HttpServletResponse;
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9
10 import org.springframework.social.connect.UsersConnectionRepository;
11 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
12
13
14 /**
15  * Intercepts all requests to handle the user-cookie.
16  *
17  * @author Kai Moritz
18  */
19 public final class UserCookieInterceptor extends HandlerInterceptorAdapter
20 {
21   private final static Logger LOG =
22       LoggerFactory.getLogger(UserCookieInterceptor.class);
23
24
25   private final UsersConnectionRepository repository;
26
27
28   /**
29    * Creates an instance of this class, that uses the given instance of
30    * {@link UsersConnectionRepository}.
31    *
32    * @param repository
33    *     The instance of {@link UsersConnectionRepository} to use.
34    */
35   public UserCookieInterceptor(UsersConnectionRepository repository)
36   {
37     this.repository = repository;
38   }
39
40
41   /**
42    * Before a request is handled, the current user is loaded from the cookie,
43    * if the cookie is present and the user is known. If the user is not known,
44    * the cookie is removed.
45    *
46    * @param request
47    *     The {@link HttpServletRequest} that is intercepted.
48    * @param response
49    *     The {@link HttpServletResponse} that is intercepted.
50    * @param handler
51    *     The handler, that handles the intercepted request.
52    * @return
53    *     Always <code>true</code>, to indicate, that the intercepted request
54    *     should be handled normally.
55    */
56   @Override
57   public boolean preHandle(
58       HttpServletRequest request,
59       HttpServletResponse response,
60       Object handler
61       )
62   {
63     String user = UserCookieGenerator.INSTANCE.readCookieValue(request);
64     if (user != null)
65     {
66       if (!repository
67           .findUserIdsConnectedTo("facebook", Collections.singleton(user))
68           .isEmpty()
69           )
70       {
71         LOG.info("loading user {} from cookie", user);
72         SecurityContext.setCurrentUser(user);
73         return true;
74       }
75       else
76       {
77         LOG.warn("user {} is not known!", user);
78         UserCookieGenerator.INSTANCE.removeCookie(response);
79       }
80     }
81     return true;
82   }
83
84   /**
85    * After a request, the user is removed from the security-context.
86    *
87    * @param request
88    *     The {@link HttpServletRequest} that is intercepted.
89    * @param response
90    *     The {@link HttpServletResponse} that is intercepted.
91    * @param handler
92    *     The handler, that handles the intercepted request.
93    * @param exception
94    *     If an exception was thrown during the handling of this request, it is
95    *     handed in through this parameter.
96    */
97   @Override
98   public void afterCompletion(
99       HttpServletRequest request,
100       HttpServletResponse response,
101       Object handler,
102       Exception exception
103       )
104   {
105     SecurityContext.remove();
106   }
107 }