--- /dev/null
+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@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
--- /dev/null
+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@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
* 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@columbia.edu
*/
--- /dev/null
+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@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
--- /dev/null
+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@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
--- /dev/null
+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;
+ }
+ }
+
+}
--- /dev/null
+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@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
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;
/**
private String wayfConfigFileLocation;
private static Logger log = Logger.getLogger(WayfService.class.getName());
+ private String log4jConfigFileLocation;
/**
* @see GenericServlet#init()
public void init() throws ServletException {
super.init();
-
loadInitParams();
+ initLogger();
//initialize configuration from file
InputStream is =
getServletContext().getResourceAsStream(wayfConfigFileLocation);
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)
*/
//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");
}
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")) {
}
}
- 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 {
}
- 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)
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,
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