Added Log4J initilization to WAYF servlet.
authorwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Thu, 30 May 2002 22:42:33 +0000 (22:42 +0000)
committerwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Thu, 30 May 2002 22:42:33 +0000 (22:42 +0000)
Converted conditional logic on WAYF cache selection to polymorphism.

git-svn-id: https://subversion.switch.ch/svn/shibboleth/java-idp/trunk@13 ab3bd59b-922f-494d-bb5f-6f0a3c29deca

src/edu/internet2/middleware/shibboleth/wayf/CookieWayfCache.java [new file with mode: 0755]
src/edu/internet2/middleware/shibboleth/wayf/NullWayfCache.java [new file with mode: 0755]
src/edu/internet2/middleware/shibboleth/wayf/OriginSet.java
src/edu/internet2/middleware/shibboleth/wayf/SessionWayfCache.java [new file with mode: 0755]
src/edu/internet2/middleware/shibboleth/wayf/WayfCache.java [new file with mode: 0755]
src/edu/internet2/middleware/shibboleth/wayf/WayfCacheBase.java [new file with mode: 0755]
src/edu/internet2/middleware/shibboleth/wayf/WayfCacheFactory.java [new file with mode: 0755]
src/edu/internet2/middleware/shibboleth/wayf/WayfService.java

diff --git a/src/edu/internet2/middleware/shibboleth/wayf/CookieWayfCache.java b/src/edu/internet2/middleware/shibboleth/wayf/CookieWayfCache.java
new file mode 100755 (executable)
index 0000000..88b35a6
--- /dev/null
@@ -0,0 +1,66 @@
+package edu.internet2.middleware.shibboleth.wayf;
+
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * Implementation of <code>WayfCache</code> that uses Http Cookies to cache
+ * user selections.
+ * 
+ * @author Walter Hoehn wassa&#064;columbia.edu
+ */
+public class CookieWayfCache extends WayfCacheBase implements WayfCache {
+
+       /**
+        * @see WayfCache#addHsToCache(HttpServletRequest)
+        */
+       public void addHsToCache(
+               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();
+                **/
+               res.addCookie(cacheCookie);
+       }
+
+       /**
+        * @see WayfCache#deleteHsFromCache(HttpServletRequest)
+        */
+       public void deleteHsFromCache(
+               HttpServletRequest req,
+               HttpServletResponse res) {
+
+               Cookie[] cookies = req.getCookies();
+               for (int i = 0; i < cookies.length; i++) {
+                       if (cookies[i].getName().equals("selectedHandleService")) {
+                               cookies[i].setMaxAge(0);
+                               res.addCookie(cookies[i]);
+                       }
+               }
+       }
+
+       /**
+        * @see WayfCache#getCachedHS(HttpServletRequest)
+        */
+       public String getCachedHS(HttpServletRequest req) {
+
+               Cookie[] cookies = req.getCookies();
+               if (cookies != null) {
+                       for (int i = 0; i < cookies.length; i++) {
+                               if (cookies[i].getName().equals("selectedHandleService")) {
+                                       return cookies[i].getValue();
+                               }
+                       }
+               }
+               return null;
+       }
+
+}
\ No newline at end of file
diff --git a/src/edu/internet2/middleware/shibboleth/wayf/NullWayfCache.java b/src/edu/internet2/middleware/shibboleth/wayf/NullWayfCache.java
new file mode 100755 (executable)
index 0000000..9366c2e
--- /dev/null
@@ -0,0 +1,43 @@
+package edu.internet2.middleware.shibboleth.wayf;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * Implementaton of the <code>WayfCache</code> interface that does no cacheing of 
+ * user selections.
+ * 
+ * @author Walter Hoehn wassa&#064;columbia.edu
+ */
+
+public class NullWayfCache implements WayfCache {
+
+       /**
+        * @see WayfCache#addHsToCache(HttpServletRequest)
+        */
+       public void addHsToCache(String handleService, HttpServletRequest req, HttpServletResponse res) {
+               //don't do anything
+       }
+
+       /**
+        * @see WayfCache#deleteHsFromCache(HttpServletRequest)
+        */
+       public void deleteHsFromCache(HttpServletRequest req, HttpServletResponse res) {
+               //don't do anything
+       }
+
+       /**
+        * @see WayfCache#getCachedHS(HttpServletRequest)
+        */
+       public String getCachedHS(HttpServletRequest req) {
+               return null;
+       }
+
+       /**
+        * @see WayfCache#hasCachedHS(HttpServletRequest)
+        */
+       public boolean hasCachedHS(HttpServletRequest req) {
+               return false;
+       }
+
+}
\ No newline at end of file
index d82666d..9044b52 100755 (executable)
@@ -6,6 +6,7 @@ import java.util.ArrayList;
  * This class is used to create logical groupings of shibboleth Origin Sites.
  * Each grouping is associated with a textual name that might be displayed 
  * in a UI to accommodate user selection.
+ * 
  * @author Walter Hoehn wassa&#064;columbia.edu
  */
 
diff --git a/src/edu/internet2/middleware/shibboleth/wayf/SessionWayfCache.java b/src/edu/internet2/middleware/shibboleth/wayf/SessionWayfCache.java
new file mode 100755 (executable)
index 0000000..f06b57c
--- /dev/null
@@ -0,0 +1,53 @@
+package edu.internet2.middleware.shibboleth.wayf;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
+/**
+ * Implementation of <code>WayfCache</code> that uses Java Servlet Sessions to cache
+ * user selections.
+ * 
+ * @author Walter Hoehn wassa&#064;columbia.edu
+ */
+
+public class SessionWayfCache extends WayfCacheBase implements WayfCache {
+
+       /**
+        * @see WayfCache#addHsToCache(HttpServletRequest)
+        */
+       public void addHsToCache(
+               String handleService,
+               HttpServletRequest req,
+               HttpServletResponse res) {
+                       
+               HttpSession session = req.getSession(true);
+               session.setMaxInactiveInterval(7200);
+               session.setAttribute("selectedHandleService", handleService);
+       }
+
+       /**
+        * @see WayfCache#deleteHsFromCache(HttpServletRequest)
+        */
+       public void deleteHsFromCache(
+               HttpServletRequest req,
+               HttpServletResponse res) {
+                       
+               HttpSession session = req.getSession(false);
+               if (session != null) {
+                       session.removeAttribute("selectedHandleService");
+               }
+       }
+
+       /**
+        * @see WayfCache#getCachedHS(HttpServletRequest)
+        */
+       public String getCachedHS(HttpServletRequest req) {
+               
+               HttpSession session = req.getSession(false);
+               if (session == null) {
+                       return null;
+               }
+               return (String) session.getAttribute("selectedHandleService");
+       }
+}
\ No newline at end of file
diff --git a/src/edu/internet2/middleware/shibboleth/wayf/WayfCache.java b/src/edu/internet2/middleware/shibboleth/wayf/WayfCache.java
new file mode 100755 (executable)
index 0000000..d26ae86
--- /dev/null
@@ -0,0 +1,42 @@
+package edu.internet2.middleware.shibboleth.wayf;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * Defines a method for cacheing user selections regarding which 
+ * shibboleth Handle Service should be used.
+ * 
+ * @author Walter Hoehn wassa&#064;columbia.edu
+ */
+
+public interface WayfCache {
+
+       /**
+        * Add the specified Shibboleth Handle Service to the cache.
+        */
+       public void addHsToCache(
+               String handleService,
+               HttpServletRequest req,
+               HttpServletResponse res);
+
+       /**
+        * Delete the Shibboleth Handle Service assoctiated with the current requester from the cache.
+        */
+       public void deleteHsFromCache(
+               HttpServletRequest req,
+               HttpServletResponse res);
+
+       /**
+        * Returns boolean indicator as to whether the current requester has a Handle Service entry 
+        * in the cache. 
+        */
+       public boolean hasCachedHS(HttpServletRequest req);
+
+       /**
+        * Retrieves the Handle Service associated with the current requester.  Returns null
+        * if there is none currently associated.
+        */
+       public String getCachedHS(HttpServletRequest req);
+
+}
\ No newline at end of file
diff --git a/src/edu/internet2/middleware/shibboleth/wayf/WayfCacheBase.java b/src/edu/internet2/middleware/shibboleth/wayf/WayfCacheBase.java
new file mode 100755 (executable)
index 0000000..fc342af
--- /dev/null
@@ -0,0 +1,26 @@
+package edu.internet2.middleware.shibboleth.wayf;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * @author Administrator
+ *
+ * To change this generated comment edit the template variable "typecomment":
+ * Window>Preferences>Java>Templates.
+ * To enable and disable the creation of type comments go to
+ * Window>Preferences>Java>Code Generation.
+ */
+public abstract class WayfCacheBase implements WayfCache {
+
+       /**
+        * @see WayfCache#getCachedHS(HttpServletRequest)
+        */
+       public boolean hasCachedHS(HttpServletRequest req) {
+               if (getCachedHS(req) == null) {
+                       return false;
+               } else {
+                       return true;
+               }
+       }
+
+}
diff --git a/src/edu/internet2/middleware/shibboleth/wayf/WayfCacheFactory.java b/src/edu/internet2/middleware/shibboleth/wayf/WayfCacheFactory.java
new file mode 100755 (executable)
index 0000000..267d98a
--- /dev/null
@@ -0,0 +1,31 @@
+package edu.internet2.middleware.shibboleth.wayf;
+
+import org.apache.log4j.Logger;
+
+/**
+ * Factory for creating instances of <code>WayfCache</code> based on 
+ * the state of the <code>WayfConfig</code>.
+ * 
+ * @author Walter Hoehn wassa&#064;columbia.edu
+ */
+public class WayfCacheFactory {
+
+       private static Logger log =
+               Logger.getLogger(WayfCacheFactory.class.getName());
+
+       public static WayfCache getInstance() {
+
+               if (WayfConfig.getCache().equals("NONE")) {
+                       return new NullWayfCache();
+               } else if (WayfConfig.getCache().equals("SESSION")) {
+                       return new SessionWayfCache();
+               } else if (WayfConfig.getCache().equals("COOKIES")) {
+                       return new CookieWayfCache();
+               } else {
+                       log.warn(
+                               "Invalid Cache type specified: running with cache type NONE.");
+                       return new NullWayfCache();
+               }
+       }
+
+}
\ No newline at end of file
index cefe824..4c1f850 100755 (executable)
@@ -6,12 +6,11 @@ import java.net.URLEncoder;
 
 import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletException;
-import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpSession;
 import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
 import org.xml.sax.SAXException;
 
 /**
@@ -26,6 +25,7 @@ public class WayfService extends HttpServlet {
 
        private String wayfConfigFileLocation;
        private static Logger log = Logger.getLogger(WayfService.class.getName());
+       private String log4jConfigFileLocation;
 
        /**
         * @see GenericServlet#init()
@@ -33,8 +33,8 @@ public class WayfService extends HttpServlet {
        public void init() throws ServletException {
 
                super.init();
-
                loadInitParams();
+               initLogger();
                //initialize configuration from file
                InputStream is =
                        getServletContext().getResourceAsStream(wayfConfigFileLocation);
@@ -74,6 +74,32 @@ public class WayfService extends HttpServlet {
                        WayfConfig.getLogoLocation());
        }
 
+       private void loadInitParams() {
+
+               wayfConfigFileLocation =
+                       getServletConfig().getInitParameter("WAYFConfigFileLocation");
+               if (wayfConfigFileLocation == null) {
+                       wayfConfigFileLocation = "/WEB-INF/conf/shibboleth.xml";
+               }
+               log4jConfigFileLocation =
+                       getServletConfig().getInitParameter("log4jConfigFileLocation");
+               if (log4jConfigFileLocation == null) {
+                       log4jConfigFileLocation = "/WEB-INF/conf/log4j.properties";
+               }
+
+       }
+
+       /**
+        * Starts up Log4J.
+        */
+
+       private void initLogger() {
+
+               PropertyConfigurator.configure(
+                       getServletContext().getRealPath("/") + log4jConfigFileLocation);
+
+       }
+
        /**
         * @see HttpServlet#doGet(HttpServletRequest, HttpServletResponse)
         */
@@ -82,7 +108,7 @@ public class WayfService extends HttpServlet {
                //Tell the browser not to cache the WAYF page
                res.setHeader("Cache-Control", "no-cache");
                res.setHeader("Pragma", "no-cache");
-               res.setDateHeader("Expires", 0 );
+               res.setDateHeader("Expires", 0);
 
                //Decide how to route the request based on query string
                String requestType = req.getParameter("action");
@@ -91,9 +117,13 @@ public class WayfService extends HttpServlet {
                }
                try {
                        if (requestType.equals("deleteFromCache")) {
-                               handleDeleteFromCache(req, res);
-                       } else if (hasCachedHS(req)) {
-                               handleRedirect(req, res, getCachedHS(req));
+                               WayfCacheFactory.getInstance().deleteHsFromCache(req, res);
+                               handleLookup(req, res);
+                       } else if (WayfCacheFactory.getInstance().hasCachedHS(req)) {
+                               handleRedirect(
+                                       req,
+                                       res,
+                                       WayfCacheFactory.getInstance().getCachedHS(req));
                        } else if (requestType.equals("search")) {
                                handleSearch(req, res);
                        } else if (requestType.equals("selection")) {
@@ -106,16 +136,6 @@ public class WayfService extends HttpServlet {
                }
        }
 
-       private void loadInitParams() {
-
-               wayfConfigFileLocation =
-                       getServletConfig().getInitParameter("WAYFConfigFileLocation");
-               if (wayfConfigFileLocation == null) {
-                       wayfConfigFileLocation = "/WEB-INF/conf/shibboleth.xml";
-               }
-
-       }
-
        private void handleLookup(HttpServletRequest req, HttpServletResponse res)
                throws WayfException {
 
@@ -156,34 +176,6 @@ public class WayfService extends HttpServlet {
 
        }
 
-       private void handleDeleteFromCache(
-               HttpServletRequest req,
-               HttpServletResponse res)
-               throws WayfException {
-
-               if (WayfConfig.getCache().equals("SESSION")) {
-
-                       HttpSession session = req.getSession(false);
-                       if (session != null) {
-                               session.removeAttribute("selectedHandleService");
-                       }
-
-               } else if (WayfConfig.getCache().equals("COOKIES")) {
-
-                       Cookie[] cookies = req.getCookies();
-                       for (int i = 0; i < cookies.length; i++) {
-                               if (cookies[i].getName().equals("selectedHandleService")) {
-                                       cookies[i].setMaxAge(0);
-                                       res.addCookie(cookies[i]);
-                               }
-                       }
-
-               }
-
-               handleLookup(req, res);
-
-       }
-
        private void handleSelection(
                HttpServletRequest req,
                HttpServletResponse res)
@@ -194,44 +186,15 @@ public class WayfService extends HttpServlet {
                if (handleService == null) {
                        handleLookup(req, res);
                } else {
-                       addHsToCache(req, res, handleService);
+                       WayfCacheFactory.getInstance().addHsToCache(
+                               handleService,
+                               req,
+                               res);
                        handleRedirect(req, res, handleService);
                }
 
        }
 
-       private void addHsToCache(
-               HttpServletRequest req,
-               HttpServletResponse res,
-               String handleService) {
-
-               if (WayfConfig.getCache().equals("NONE")) {
-                       return;
-               } else if (WayfConfig.getCache().equals("SESSION")) {
-
-                       HttpSession session = req.getSession(true);
-                       session.setMaxInactiveInterval(7200);
-                       session.setAttribute("selectedHandleService", handleService);
-               } else if (WayfConfig.getCache().equals("COOKIES")) {
-
-                       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();
-                        **/
-                       res.addCookie(cacheCookie);
-
-               } else {
-                       log.warn(
-                               "Invalid Cache type specified: running with cache type NONE.");
-               }
-       }
-
        private void handleRedirect(
                HttpServletRequest req,
                HttpServletResponse res,
@@ -299,38 +262,4 @@ public class WayfService extends HttpServlet {
                return target;
        }
 
-       private boolean hasCachedHS(HttpServletRequest req) {
-
-               if (getCachedHS(req) == null) {
-                       return false;
-               } else {
-                       return true;
-               }
-       }
-       private String getCachedHS(HttpServletRequest req) {
-
-               if (WayfConfig.getCache().equals("NONE")) {
-                       return null;
-               } else if (WayfConfig.getCache().equals("SESSION")) {
-                       HttpSession session = req.getSession(false);
-                       if (session == null) {
-                               return null;
-                       }
-                       return (String) session.getAttribute("selectedHandleService");
-
-               } else if (WayfConfig.getCache().equals("COOKIES")) {
-
-                       Cookie[] cookies = req.getCookies();
-                       for (int i = 0; i < cookies.length; i++) {
-                               if (cookies[i].getName().equals("selectedHandleService")) {
-                                       return cookies[i].getValue();
-                               }
-                       }
-
-               }
-               log.warn("Invalid Cache type specified: running with cache type NONE.");
-               return null;
-
-       }
-
 }
\ No newline at end of file