Added configurability for domain and expiration to cookie cache
[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         private int expiration;
16         private String domain;
17
18         /**
19          * Constructs a <code>CookieWayfCache</code>
20          * @param expiration Cache validity period in seconds
21          * @param domain Domain to which the cookie will be released
22          */
23         public CookieWayfCache(int expiration, String domain) {
24                 
25                         this.expiration = expiration;
26                         if (domain !=null && domain != "") {
27                                 this.domain = domain;
28                         }
29         }
30
31
32
33         /**
34          * @see WayfCache#addHsToCache(HttpServletRequest)
35          */
36         public void addHsToCache(
37                 String handleService,
38                 HttpServletRequest req,
39                 HttpServletResponse res) {
40                 Cookie cacheCookie = new Cookie("edu.internet2.middleware.shibboleth.wayf.selectedHandleService", handleService);
41                 configureCookie(cacheCookie);
42                 res.addCookie(cacheCookie);
43         }
44
45         /**
46          * @see WayfCache#deleteHsFromCache(HttpServletRequest)
47          */
48         public void deleteHsFromCache(
49                 HttpServletRequest req,
50                 HttpServletResponse res) {
51
52                 Cookie[] cookies = req.getCookies();
53                 for (int i = 0; i < cookies.length; i++) {
54                         if (cookies[i].getName().equals("edu.internet2.middleware.shibboleth.wayf.selectedHandleService")) {
55                                 configureCookie(cookies[i]);
56                                 cookies[i].setMaxAge(0);
57                                 res.addCookie(cookies[i]);
58                         }
59                 }
60         }
61
62         /**
63          * @see WayfCache#getCachedHS(HttpServletRequest)
64          */
65         public String getCachedHS(HttpServletRequest req) {
66
67                 Cookie[] cookies = req.getCookies();
68                 if (cookies != null) {
69                         for (int i = 0; i < cookies.length; i++) {
70                                 if (cookies[i].getName().equals("edu.internet2.middleware.shibboleth.wayf.selectedHandleService")) {
71                                         return cookies[i].getValue();
72                                 }
73                         }
74                 }
75                 return null;
76         }
77         
78         private void configureCookie(Cookie cookie) {
79                 
80                 cookie.setComment(
81                         "Used to cache selection of a user's Shibboleth Handle Service");
82                 cookie.setPath("/");
83
84                 if (expiration > 0) {    
85                         cookie.setMaxAge(expiration);
86                 }
87                 if (domain != null && domain != "") {
88                         cookie.setDomain(domain);
89                 }
90         }
91
92 }