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