88b35a623853e94281006db9acffe3b8ca75c5d1
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / wayf / CookieWayfCache.java
1 package edu.internet2.middleware.shibboleth.wayf;
2
3 import javax.servlet.http.Cookie;
4 import javax.servlet.http.HttpServletRequest;
5 import javax.servlet.http.HttpServletResponse;
6
7 /**
8  * Implementation of <code>WayfCache</code> that uses Http Cookies to cache
9  * user selections.
10  * 
11  * @author Walter Hoehn wassa&#064;columbia.edu
12  */
13 public class CookieWayfCache extends WayfCacheBase implements WayfCache {
14
15         /**
16          * @see WayfCache#addHsToCache(HttpServletRequest)
17          */
18         public void addHsToCache(
19                 String handleService,
20                 HttpServletRequest req,
21                 HttpServletResponse res) {
22                 Cookie cacheCookie = new Cookie("selectedHandleService", handleService);
23                 cacheCookie.setComment(
24                         "Used to cache selection of a user's Handle Service");
25
26                 //Should probably get this stuff from config
27                 /**     
28                  cacheCookie.setMaxAge();
29                  cacheCookie.setDomain();
30                  **/
31                 res.addCookie(cacheCookie);
32         }
33
34         /**
35          * @see WayfCache#deleteHsFromCache(HttpServletRequest)
36          */
37         public void deleteHsFromCache(
38                 HttpServletRequest req,
39                 HttpServletResponse res) {
40
41                 Cookie[] cookies = req.getCookies();
42                 for (int i = 0; i < cookies.length; i++) {
43                         if (cookies[i].getName().equals("selectedHandleService")) {
44                                 cookies[i].setMaxAge(0);
45                                 res.addCookie(cookies[i]);
46                         }
47                 }
48         }
49
50         /**
51          * @see WayfCache#getCachedHS(HttpServletRequest)
52          */
53         public String getCachedHS(HttpServletRequest req) {
54
55                 Cookie[] cookies = req.getCookies();
56                 if (cookies != null) {
57                         for (int i = 0; i < cookies.length; i++) {
58                                 if (cookies[i].getName().equals("selectedHandleService")) {
59                                         return cookies[i].getValue();
60                                 }
61                         }
62                 }
63                 return null;
64         }
65
66 }