Added configurability for domain and expiration to cookie cache
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / wayf / SessionWayfCache.java
1 package edu.internet2.middleware.shibboleth.wayf;
2
3 import javax.servlet.http.HttpServletRequest;
4 import javax.servlet.http.HttpServletResponse;
5 import javax.servlet.http.HttpSession;
6
7 /**
8  * Implementation of <code>WayfCache</code> that uses Java Servlet Sessions to cache
9  * user selections.
10  * 
11  * @author Walter Hoehn wassa&#064;columbia.edu
12  */
13
14 public class SessionWayfCache extends WayfCacheBase implements WayfCache {
15         
16         private int expiration;
17         
18         /**
19          * @param expiration The time in seconds between requests at which point this cache entry 
20          * will be invalidated.
21          */
22         public SessionWayfCache(int expiration) {
23                 if (expiration == 0) {
24                         this.expiration = 7200;
25                 } else {
26                         this.expiration = expiration;
27                 }
28         }
29
30
31         /**
32          * @see WayfCache#addHsToCache(HttpServletRequest)
33          */
34         public void addHsToCache(
35                 String handleService,
36                 HttpServletRequest req,
37                 HttpServletResponse res) {
38                         
39                 HttpSession session = req.getSession(true);
40                 session.setMaxInactiveInterval(expiration);
41                 session.setAttribute("selectedHandleService", handleService);
42         }
43
44         /**
45          * @see WayfCache#deleteHsFromCache(HttpServletRequest)
46          */
47         public void deleteHsFromCache(
48                 HttpServletRequest req,
49                 HttpServletResponse res) {
50                         
51                 HttpSession session = req.getSession(false);
52                 if (session != null) {
53                         session.removeAttribute("selectedHandleService");
54                 }
55         }
56
57         /**
58          * @see WayfCache#getCachedHS(HttpServletRequest)
59          */
60         public String getCachedHS(HttpServletRequest req) {
61                 
62                 HttpSession session = req.getSession(false);
63                 if (session == null) {
64                         return null;
65                 }
66                 return (String) session.getAttribute("selectedHandleService");
67         }
68 }