Added configurability for domain and expiration to cookie cache
authorwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Thu, 20 Jun 2002 20:29:48 +0000 (20:29 +0000)
committerwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Thu, 20 Jun 2002 20:29:48 +0000 (20:29 +0000)
git-svn-id: https://subversion.switch.ch/svn/shibboleth/java-idp/trunk@161 ab3bd59b-922f-494d-bb5f-6f0a3c29deca

src/edu/internet2/middleware/shibboleth/wayf/CookieWayfCache.java
src/edu/internet2/middleware/shibboleth/wayf/SessionWayfCache.java
src/edu/internet2/middleware/shibboleth/wayf/WayfCacheFactory.java
src/edu/internet2/middleware/shibboleth/wayf/WayfCacheOptions.java [new file with mode: 0755]
src/edu/internet2/middleware/shibboleth/wayf/WayfConfig.java
src/edu/internet2/middleware/shibboleth/wayf/WayfService.java
src/schemas/wayfconfig.xsd

index 88b35a6..f622bb9 100755 (executable)
@@ -11,6 +11,24 @@ import javax.servlet.http.HttpServletResponse;
  * @author Walter Hoehn wassa&#064;columbia.edu
  */
 public class CookieWayfCache extends WayfCacheBase implements WayfCache {
  * @author Walter Hoehn wassa&#064;columbia.edu
  */
 public class CookieWayfCache extends WayfCacheBase implements WayfCache {
+       
+       private int expiration;
+       private String domain;
+
+       /**
+        * Constructs a <code>CookieWayfCache</code>
+        * @param expiration Cache validity period in seconds
+        * @param domain Domain to which the cookie will be released
+        */
+       public CookieWayfCache(int expiration, String domain) {
+               
+                       this.expiration = expiration;
+                       if (domain !=null && domain != "") {
+                               this.domain = domain;
+                       }
+       }
+
+
 
        /**
         * @see WayfCache#addHsToCache(HttpServletRequest)
 
        /**
         * @see WayfCache#addHsToCache(HttpServletRequest)
@@ -19,15 +37,8 @@ public class CookieWayfCache extends WayfCacheBase implements WayfCache {
                String handleService,
                HttpServletRequest req,
                HttpServletResponse res) {
                String handleService,
                HttpServletRequest req,
                HttpServletResponse res) {
-               Cookie cacheCookie = new Cookie("selectedHandleService", handleService);
-               cacheCookie.setComment(
-                       "Used to cache selection of a user's Handle Service");
-
-               //Should probably get this stuff from config
-               /**     
-                cacheCookie.setMaxAge();
-                cacheCookie.setDomain();
-                **/
+               Cookie cacheCookie = new Cookie("edu.internet2.middleware.shibboleth.wayf.selectedHandleService", handleService);
+               configureCookie(cacheCookie);
                res.addCookie(cacheCookie);
        }
 
                res.addCookie(cacheCookie);
        }
 
@@ -40,7 +51,8 @@ public class CookieWayfCache extends WayfCacheBase implements WayfCache {
 
                Cookie[] cookies = req.getCookies();
                for (int i = 0; i < cookies.length; i++) {
 
                Cookie[] cookies = req.getCookies();
                for (int i = 0; i < cookies.length; i++) {
-                       if (cookies[i].getName().equals("selectedHandleService")) {
+                       if (cookies[i].getName().equals("edu.internet2.middleware.shibboleth.wayf.selectedHandleService")) {
+                               configureCookie(cookies[i]);
                                cookies[i].setMaxAge(0);
                                res.addCookie(cookies[i]);
                        }
                                cookies[i].setMaxAge(0);
                                res.addCookie(cookies[i]);
                        }
@@ -55,12 +67,26 @@ public class CookieWayfCache extends WayfCacheBase implements WayfCache {
                Cookie[] cookies = req.getCookies();
                if (cookies != null) {
                        for (int i = 0; i < cookies.length; i++) {
                Cookie[] cookies = req.getCookies();
                if (cookies != null) {
                        for (int i = 0; i < cookies.length; i++) {
-                               if (cookies[i].getName().equals("selectedHandleService")) {
+                               if (cookies[i].getName().equals("edu.internet2.middleware.shibboleth.wayf.selectedHandleService")) {
                                        return cookies[i].getValue();
                                }
                        }
                }
                return null;
        }
                                        return cookies[i].getValue();
                                }
                        }
                }
                return null;
        }
+       
+       private void configureCookie(Cookie cookie) {
+               
+               cookie.setComment(
+                       "Used to cache selection of a user's Shibboleth Handle Service");
+               cookie.setPath("/");
+
+               if (expiration > 0) {    
+                       cookie.setMaxAge(expiration);
+               }
+               if (domain != null && domain != "") {
+                       cookie.setDomain(domain);
+               }
+       }
 
 }
\ No newline at end of file
 
 }
\ No newline at end of file
index f06b57c..8c1d9ad 100755 (executable)
@@ -12,6 +12,21 @@ import javax.servlet.http.HttpSession;
  */
 
 public class SessionWayfCache extends WayfCacheBase implements WayfCache {
  */
 
 public class SessionWayfCache extends WayfCacheBase implements WayfCache {
+       
+       private int expiration;
+       
+       /**
+        * @param expiration The time in seconds between requests at which point this cache entry 
+        * will be invalidated.
+        */
+       public SessionWayfCache(int expiration) {
+               if (expiration == 0) {
+                       this.expiration = 7200;
+               } else {
+                       this.expiration = expiration;
+               }
+       }
+
 
        /**
         * @see WayfCache#addHsToCache(HttpServletRequest)
 
        /**
         * @see WayfCache#addHsToCache(HttpServletRequest)
@@ -22,7 +37,7 @@ public class SessionWayfCache extends WayfCacheBase implements WayfCache {
                HttpServletResponse res) {
                        
                HttpSession session = req.getSession(true);
                HttpServletResponse res) {
                        
                HttpSession session = req.getSession(true);
-               session.setMaxInactiveInterval(7200);
+               session.setMaxInactiveInterval(expiration);
                session.setAttribute("selectedHandleService", handleService);
        }
 
                session.setAttribute("selectedHandleService", handleService);
        }
 
index 217096c..7108251 100755 (executable)
@@ -13,19 +13,24 @@ public class WayfCacheFactory {
        private static Logger log =
                Logger.getLogger(WayfCacheFactory.class.getName());
 
        private static Logger log =
                Logger.getLogger(WayfCacheFactory.class.getName());
 
-       public static WayfCache getInstance(String cacheType) {
+       public static WayfCache getInstance(String cacheType, WayfCacheOptions options) {
 
                if (cacheType.equals("NONE")) {
                        return new NullWayfCache();
                } else if (cacheType.equals("SESSION")) {
 
                if (cacheType.equals("NONE")) {
                        return new NullWayfCache();
                } else if (cacheType.equals("SESSION")) {
-                       return new SessionWayfCache();
+                       return new SessionWayfCache(options.getExpiration());
                } else if (cacheType.equals("COOKIES")) {
                } else if (cacheType.equals("COOKIES")) {
-                       return new CookieWayfCache();
+                       return new CookieWayfCache(options.getExpiration(), options.getDomain());
                } else {
                        log.warn(
                                "Invalid Cache type specified: running with cache type NONE.");
                        return new NullWayfCache();
                }
        }
                } else {
                        log.warn(
                                "Invalid Cache type specified: running with cache type NONE.");
                        return new NullWayfCache();
                }
        }
+       
+       public static WayfCache getInstance(String cacheType) {
+               
+               return getInstance(cacheType, new WayfCacheOptions());
+               }
 
 }
\ No newline at end of file
 
 }
\ No newline at end of file
diff --git a/src/edu/internet2/middleware/shibboleth/wayf/WayfCacheOptions.java b/src/edu/internet2/middleware/shibboleth/wayf/WayfCacheOptions.java
new file mode 100755 (executable)
index 0000000..0404b0f
--- /dev/null
@@ -0,0 +1,50 @@
+package edu.internet2.middleware.shibboleth.wayf;
+
+/**
+ * Runtime configuration bundle that is passed to a <code>WayfCacheFactory</code>.
+ * 
+ * @author Walter Hoehn wassa&#064;columbia.edu
+ */
+public class WayfCacheOptions {
+       
+       private int expiration;
+       private String domain;
+
+       /**
+        * Returns the domain.
+        * @return String
+        */
+       public String getDomain() {
+               return domain;
+       }
+
+
+       /**
+        * Returns the expiration.
+        * @return int
+        */
+       public int getExpiration() {
+               return expiration;
+       }
+
+
+       /**
+        * Sets the domain.
+        * @param domain The domain to set
+        */
+       public void setDomain(String domain) {
+               this.domain = domain;
+       }
+
+
+       /**
+        * Sets the expiration.
+        * @param expiration The expiration to set
+        */
+       public void setExpiration(int expiration) {
+               this.expiration = expiration;
+       }
+
+
+}
+
index 1ad991e..bb619fb 100755 (executable)
@@ -6,7 +6,7 @@ import org.apache.log4j.Logger;
 
 /**
  * Class used by the  WAYF service to determine runtime options
 
 /**
  * Class used by the  WAYF service to determine runtime options
- * Most of the fields of this class should have reasonable defaults set
+ * Most of the fields of this class should have reasonable defaults.
  * @author Walter Hoehn wassa&#064;columbia.edu
  */
 
  * @author Walter Hoehn wassa&#064;columbia.edu
  */
 
@@ -24,7 +24,8 @@ public class WayfConfig {
        private String searchResultEmptyText =
                "No institution found that matches your search " + "criteria, please try again.";
        private HashSet ignoredForMatch = new HashSet();
        private String searchResultEmptyText =
                "No institution found that matches your search " + "criteria, please try again.";
        private HashSet ignoredForMatch = new HashSet();
-
+       private int cacheExpiration;
+       private String cacheDomain;
        private String cacheType = "COOKIES";
 
        public WayfConfig() {
        private String cacheType = "COOKIES";
 
        public WayfConfig() {
@@ -99,4 +100,40 @@ public class WayfConfig {
                }
        }
 
                }
        }
 
+       /**
+        * Returns the cacheDomain.
+        * @return String
+        */
+       public String getCacheDomain() {
+               return cacheDomain;
+       }
+
+
+       /**
+        * Returns the cacheExpiration.
+        * @return int
+        */
+       public int getCacheExpiration() {
+               return cacheExpiration;
+       }
+
+
+       /**
+        * Sets the cacheDomain.
+        * @param cacheDomain The cacheDomain to set
+        */
+       public void setCacheDomain(String cacheDomain) {
+               this.cacheDomain = cacheDomain;
+       }
+
+
+       /**
+        * Sets the cacheExpiration.
+        * @param cacheExpiration The cacheExpiration to set
+        */
+       public void setCacheExpiration(int cacheExpiration) {
+               this.cacheExpiration = cacheExpiration;
+       }
+
+
 }
\ No newline at end of file
 }
\ No newline at end of file
index 4655ed7..7dad435 100755 (executable)
@@ -27,6 +27,7 @@ public class WayfService extends HttpServlet {
        private String siteConfigFileLocation;
        private WayfConfig config;
        private WayfOrigins originConfig;
        private String siteConfigFileLocation;
        private WayfConfig config;
        private WayfOrigins originConfig;
+       private WayfCacheOptions wOptions = new WayfCacheOptions();
        private static Logger log = Logger.getLogger(WayfService.class.getName());
 
        /**
        private static Logger log = Logger.getLogger(WayfService.class.getName());
 
        /**
@@ -39,6 +40,11 @@ public class WayfService extends HttpServlet {
                loadInitParams();
                log.info("Loading configuration from file.");
                configure();
                loadInitParams();
                log.info("Loading configuration from file.");
                configure();
+               
+               //Setup Cacheing options
+               wOptions.setDomain(config.getCacheDomain());
+               wOptions.setExpiration(config.getCacheExpiration());
+               
                initViewConfig();
                log.info("WAYF initialization completed.");
        }
                initViewConfig();
                log.info("WAYF initialization completed.");
        }
@@ -126,7 +132,7 @@ public class WayfService extends HttpServlet {
                try {
                        if (requestType.equals("deleteFromCache")) {
                                log.debug("Deleting saved HS from cache");
                try {
                        if (requestType.equals("deleteFromCache")) {
                                log.debug("Deleting saved HS from cache");
-                               WayfCacheFactory.getInstance(config.getCacheType()).deleteHsFromCache(req, res);
+                               WayfCacheFactory.getInstance(config.getCacheType(), wOptions).deleteHsFromCache(req, res);
                                handleLookup(req, res);
                        } else if (WayfCacheFactory.getInstance(config.getCacheType()).hasCachedHS(req)) {
                                forwardToHS(
                                handleLookup(req, res);
                        } else if (WayfCacheFactory.getInstance(config.getCacheType()).hasCachedHS(req)) {
                                forwardToHS(
@@ -194,7 +200,7 @@ public class WayfService extends HttpServlet {
                if (handleService == null) {
                        handleLookup(req, res);
                } else {
                if (handleService == null) {
                        handleLookup(req, res);
                } else {
-                       WayfCacheFactory.getInstance(config.getCacheType()).addHsToCache(handleService, req, res);
+                       WayfCacheFactory.getInstance(config.getCacheType(), wOptions).addHsToCache(handleService, req, res);
                        forwardToHS(req, res, handleService);
                }
 
                        forwardToHS(req, res, handleService);
                }
 
index 52df726..fdff366 100755 (executable)
@@ -17,6 +17,8 @@
                     <xs:attribute name="supportContact" type="xs:string" use="optional"/>
                     <xs:attribute name="logoLocation" type="xs:string" use="optional"/>
                     <xs:attribute name="cacheType" type="xs:string" use="optional"/>
                     <xs:attribute name="supportContact" type="xs:string" use="optional"/>
                     <xs:attribute name="logoLocation" type="xs:string" use="optional"/>
                     <xs:attribute name="cacheType" type="xs:string" use="optional"/>
+                    <xs:attribute name="cacheExpiration" type="xs:string" use="optional"/>
+                    <xs:attribute name="cacheDomain" type="xs:string" use="optional"/>
                 </xs:complexType>
         </xs:element>
 </xs:schema>
                 </xs:complexType>
         </xs:element>
 </xs:schema>