Changed the name of the WAYF servlet (for clarity).
authorwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Thu, 30 May 2002 16:29:33 +0000 (16:29 +0000)
committerwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Thu, 30 May 2002 16:29:33 +0000 (16:29 +0000)
Converted logging in the WAYF to Log4J.

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

src/edu/internet2/middleware/shibboleth/wayf/WayfConfig.java
src/edu/internet2/middleware/shibboleth/wayf/WayfHandler.java [deleted file]
src/edu/internet2/middleware/shibboleth/wayf/WayfService.java [new file with mode: 0755]
webApplication/WEB-INF/web.xml

index 6d46f1a..0489c61 100755 (executable)
@@ -1,10 +1,8 @@
 package edu.internet2.middleware.shibboleth.wayf;
 
-import java.util.Enumeration;
 import java.util.HashSet;
-import java.util.StringTokenizer;
 
-import javax.servlet.ServletContext;
+import org.apache.log4j.Logger;
 
 /**
  * Class used by the  WAYF service to determine runtime options
@@ -14,184 +12,170 @@ import javax.servlet.ServletContext;
 
 public class WayfConfig {
 
-    private static WayfOrigins wd;
-    private static String location;
-    private static String logoLocation = "images/internet2.gif";
-    private static String supportContact = "mailto:shib-support@internet2.org";
-    private static String helpText =
-        "In order to fulfill the request for the  web resource you "
-            + "have just chosen, information must first be obtained from "
-            + "your home institution. Please select the institution with "
-            + "which you are affiliated.";
-    private static String searchResultEmptyText =
-        "No institution found that matches your search " + "criteria, please try again.";
-    private static HashSet ignoredForMatch = new HashSet();
-
-    private static String cache = "SESSION";
-
-    private static ServletContext servletContext;
-
-    /**
-     * Constructor for WayfConfig.
-     */
-    public WayfConfig() {
-        super();
-    }
-
-    /**
-     * Gets the wd.
-     * @return Returns a WayfOrigins
-     */
-    public static WayfOrigins getWAYFData() {
-        return wd;
-    }
-
-    /**
-     * Sets the wd.
-     * @param wd The wd to set
-     */
-    public void setWAYFData(WayfOrigins wd) {
-        WayfConfig.wd = wd;
-    }
-
-    /**
-     * Gets the searchResultEmptyText.
-     * @return Returns a String
-     */
-    public static String getSearchResultEmptyText() {
-        return searchResultEmptyText;
-    }
-
-    /**
-     * Sets the searchResultEmptyText.
-     * @param searchResultEmptyText The searchResultEmptyText to set
-     */
-    public void setSearchResultEmptyText(String searchResultEmptyText) {
-        WayfConfig.searchResultEmptyText = searchResultEmptyText;
-    }
-
-    /**
-     * Gets the helpText.
-     * @return Returns a String
-     */
-    public static String getHelpText() {
-        return helpText;
-    }
-
-    /**
-     * Sets the helpText.
-     * @param helpText The helpText to set
-     */
-    public void setHelpText(String helpText) {
-        WayfConfig.helpText = helpText;
-    }
-
-    /**
-     * Gets the supportContact.
-     * @return Returns a String
-     */
-    public static String getSupportContact() {
-        return supportContact;
-    }
-
-    /**
-     * Sets the supportContact.
-     * @param supportContact The supportContact to set
-     */
-    public void setSupportContact(String supportContact) {
-        WayfConfig.supportContact = supportContact;
-    }
-
-    /**
-     * Gets the logoLocation.
-     * @return Returns a String
-     */
-    public static String getLogoLocation() {
-        return logoLocation;
-    }
-
-    /**
-     * Sets the logoLocation.
-     * @param logoLocation The logoLocation to set
-     */
-    public void setLogoLocation(String logoLocation) {
-        WayfConfig.logoLocation = logoLocation;
-    }
-
-    /**
-     * Gets the location.
-     * @return Returns a String
-     */
-    public static String getLocation() {
-        return location;
-    }
-
-    /**
-     * Sets the location.
-     * @param location The location to set
-     */
-    public void setLocation(String location) {
-        WayfConfig.location = location;
-    }
-
-    /**
-     * Determines if a particular string token should be used for matching when a user searches for origins.
-     * @param str The string to lookup
-     */
-    public static boolean isIgnoredForMatch(String str) {
-
-        if (ignoredForMatch.contains(str.toLowerCase())) {
-            return true;
-        } else {
-            return false;
-        }
-    }
-
-    /**
-     * Sets the tokens that should be ignored when a user searches for an origin site.
-     * @param s The ignored tokens are passed as a single string, each separated by whitespace
-     */
-    public void addIgnoredForMatch(String s) {
-
-        ignoredForMatch.add(s.toLowerCase());
-    }
-
-    /**
-     * Gets the servletContext.
-     * @return Returns a ServletContext
-     */
-    public static ServletContext getServletContext() {
-        return servletContext;
-    }
-
-    /**
-     * Sets the servletContext.
-     * @param servletContext The servletContext to set
-     */
-    public static void setServletContext(ServletContext servletContext) {
-        WayfConfig.servletContext = servletContext;
-    }
-
-    /**
-     * Gets the cache.
-     * @return Returns a String
-     */
-    public static String getCache() {
-        return cache;
-    }
-
-    /**
-     * Sets the cache.
-     * @param cache The cache to set
-     */
-    public void setCache(String cache) {
-        if (cache.toUpperCase().equals("NONE")
-            || cache.toUpperCase().equals("SESSION")
-            || cache.toUpperCase().equals("COOKIES")) {
-            WayfConfig.cache = cache.toUpperCase();
-        } else {
-            getServletContext().log("Cache type :" + cache + ": not recognized, using default.");
-        }
-    }
+       private static Logger log = Logger.getLogger(WayfConfig.class.getName());
+
+       private static WayfOrigins wd;
+       private static String location;
+       private static String logoLocation = "images/internet2.gif";
+       private static String supportContact = "mailto:shib-support@internet2.org";
+       private static String helpText =
+               "In order to fulfill the request for the  web resource you "
+                       + "have just chosen, information must first be obtained from "
+                       + "your home institution. Please select the institution with "
+                       + "which you are affiliated.";
+       private static String searchResultEmptyText =
+               "No institution found that matches your search "
+                       + "criteria, please try again.";
+       private static HashSet ignoredForMatch = new HashSet();
+
+       private static String cache = "SESSION";
+
+       /**
+        * Constructor for WayfConfig.
+        */
+       public WayfConfig() {
+               super();
+       }
+
+       /**
+        * Gets the wd.
+        * @return Returns a WayfOrigins
+        */
+       public static WayfOrigins getWAYFData() {
+               return wd;
+       }
+
+       /**
+        * Sets the wd.
+        * @param wd The wd to set
+        */
+       public void setWAYFData(WayfOrigins wd) {
+               WayfConfig.wd = wd;
+       }
+
+       /**
+        * Gets the searchResultEmptyText.
+        * @return Returns a String
+        */
+       public static String getSearchResultEmptyText() {
+               return searchResultEmptyText;
+       }
+
+       /**
+        * Sets the searchResultEmptyText.
+        * @param searchResultEmptyText The searchResultEmptyText to set
+        */
+       public void setSearchResultEmptyText(String searchResultEmptyText) {
+               WayfConfig.searchResultEmptyText = searchResultEmptyText;
+       }
+
+       /**
+        * Gets the helpText.
+        * @return Returns a String
+        */
+       public static String getHelpText() {
+               return helpText;
+       }
+
+       /**
+        * Sets the helpText.
+        * @param helpText The helpText to set
+        */
+       public void setHelpText(String helpText) {
+               WayfConfig.helpText = helpText;
+       }
+
+       /**
+        * Gets the supportContact.
+        * @return Returns a String
+        */
+       public static String getSupportContact() {
+               return supportContact;
+       }
+
+       /**
+        * Sets the supportContact.
+        * @param supportContact The supportContact to set
+        */
+       public void setSupportContact(String supportContact) {
+               WayfConfig.supportContact = supportContact;
+       }
+
+       /**
+        * Gets the logoLocation.
+        * @return Returns a String
+        */
+       public static String getLogoLocation() {
+               return logoLocation;
+       }
+
+       /**
+        * Sets the logoLocation.
+        * @param logoLocation The logoLocation to set
+        */
+       public void setLogoLocation(String logoLocation) {
+               WayfConfig.logoLocation = logoLocation;
+       }
+
+       /**
+        * Gets the location.
+        * @return Returns a String
+        */
+       public static String getLocation() {
+               return location;
+       }
+
+       /**
+        * Sets the location.
+        * @param location The location to set
+        */
+       public void setLocation(String location) {
+               WayfConfig.location = location;
+       }
+
+       /**
+        * Determines if a particular string token should be used for matching when a user searches for origins.
+        * @param str The string to lookup
+        */
+       public static boolean isIgnoredForMatch(String str) {
+
+               if (ignoredForMatch.contains(str.toLowerCase())) {
+                       return true;
+               } else {
+                       return false;
+               }
+       }
+
+       /**
+        * Sets the tokens that should be ignored when a user searches for an origin site.
+        * @param s The ignored tokens are passed as a single string, each separated by whitespace
+        */
+       public void addIgnoredForMatch(String s) {
+
+               ignoredForMatch.add(s.toLowerCase());
+       }
+
+       /**
+       * Gets the cache.
+       * @return Returns a String
+       */
+       public static String getCache() {
+               return cache;
+       }
+
+       /**
+        * Sets the cache.
+        * @param cache The cache to set
+        */
+       public void setCache(String cache) {
+               if (cache.toUpperCase().equals("NONE")
+                       || cache.toUpperCase().equals("SESSION")
+                       || cache.toUpperCase().equals("COOKIES")) {
+                       WayfConfig.cache = cache.toUpperCase();
+               } else {
+                       log.warn(
+                               "Cache type :" + cache + ": not recognized, using default.");
+               }
+       }
 
 }
\ No newline at end of file
diff --git a/src/edu/internet2/middleware/shibboleth/wayf/WayfHandler.java b/src/edu/internet2/middleware/shibboleth/wayf/WayfHandler.java
deleted file mode 100755 (executable)
index 10557d4..0000000
+++ /dev/null
@@ -1,288 +0,0 @@
-package edu.internet2.middleware.shibboleth.wayf;
-
-import java.io.IOException;
-import java.io.InputStream;
-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.xml.sax.SAXException;
-
-/**
- * This is the main handler servlet for the WAYF service.  It configures itself, chooses among 
- * several handler methods for each request, populates some beans (model), and then passes to an
- * appropriate jsp page.
- *
- * @author             Walter Hoehn
- */
-
-public class WayfHandler extends HttpServlet {
-
-    private String wayfConfigFileLocation;
-
-    public void init() throws ServletException {
-
-        super.init();
-
-        WayfConfig.setServletContext(getServletContext());
-        loadInitParams();
-        //initialize configuration from file
-        InputStream is = getServletContext().getResourceAsStream(wayfConfigFileLocation);
-        WayfConfigDigester digester = new WayfConfigDigester();
-        try {
-            digester.parse(is);
-            //If the config file cannot be read then we will not be able to start the WAYF... bail out!
-        } catch (SAXException se) {
-            getServletContext().log("Error parsing WAYF configuration file.", se);
-            System.err.println("Error parsing WAYF configuration file: " + se);
-            throw new ServletException("Error parsing WAYF configuration file.", se);
-        } catch (IOException ioe) {
-            getServletContext().log("Error reading WAYF configuration file.", ioe);
-            System.err.println("Error reading WAYF configuration file: " + ioe);
-            throw new ServletException("Error reading WAYF configuration file.", ioe);
-        }
-
-        //Setup appliation-wide beans from config
-        getServletContext().setAttribute("originsets", WayfConfig.getWAYFData().getOriginSets());
-        String wayfLocation = WayfConfig.getLocation();
-        if (wayfLocation == null) {
-            wayfLocation = "WAYF";
-        }
-        getServletContext().setAttribute("wayfLocation", wayfLocation);
-        getServletContext().setAttribute("supportContact", WayfConfig.getSupportContact());
-        getServletContext().setAttribute("helpText", WayfConfig.getHelpText());
-        getServletContext().setAttribute("searchResultEmptyText", WayfConfig.getSearchResultEmptyText());
-        getServletContext().setAttribute("logoLocation", WayfConfig.getLogoLocation());
-    }
-
-    public void doGet(HttpServletRequest req, HttpServletResponse res) {
-
-        //Tell the browser not to cache the WAYF page
-        res.setHeader("Cache-Control", "no-cache");
-        res.setHeader("Pragma", "no-cache");
-
-        //Decide how to route the request based on query string
-        String requestType = req.getParameter("action");
-        if (requestType == null) {
-            requestType = "lookup";
-        }
-        try {
-            if (requestType.equals("deleteFromCache")) {
-                handleDeleteFromCache(req, res);
-            } else if (hasCachedHS(req)) {
-                handleRedirect(req, res, getCachedHS(req));
-            } else if (requestType.equals("search")) {
-                handleSearch(req, res);
-            } else if (requestType.equals("selection")) {
-                handleSelection(req, res);
-            } else {
-                handleLookup(req, res);
-            }
-        } catch (WayfException we) {
-            handleError(req, res, we);
-        }
-    }
-
-    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 {
-
-        if ((getSHIRE(req) == null) || (getTarget(req) == null)) {
-            throw new WayfException("Invalid or missing data from SHIRE");
-        }
-        req.setAttribute("shire", getSHIRE(req));
-        req.setAttribute("target", getTarget(req));
-        req.setAttribute("encodedShire", URLEncoder.encode(getSHIRE(req)));
-        req.setAttribute("encodedTarget", URLEncoder.encode(getTarget(req)));
-
-        RequestDispatcher rd = req.getRequestDispatcher("/wayf.jsp");
-        try {
-            rd.forward(req, res);
-        } catch (IOException ioe) {
-            throw new WayfException("Problem displaying WAYF UI." + ioe.toString());
-        } catch (ServletException se) {
-            throw new WayfException("Problem displaying WAYF UI." + se.toString());
-        }
-    }
-
-    private void handleSearch(HttpServletRequest req, HttpServletResponse res) throws WayfException {
-
-        if (req.getParameter("string") != null) {
-            Origin[] origins = WayfConfig.getWAYFData().seachForMatchingOrigins(req.getParameter("string"));
-            if (origins.length != 0) {
-                req.setAttribute("searchresults", origins);
-            } else {
-                req.setAttribute("searchResultsEmpty", "true");
-            }
-        }
-        handleLookup(req, res);
-
-    }
-
-    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")) {
-            System.out.println("writing with cookes");
-
-            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) throws WayfException {
-
-        String handleService = WayfConfig.getWAYFData().lookupHSbyName(req.getParameter("origin"));
-        if (handleService == null) {
-            handleLookup(req, res);
-        } else {
-            addHsToCache(req, res, handleService);
-            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 {
-            WayfConfig.getServletContext().log("Invalid Cache type specified: running with cache type NONE.");
-        }
-    }
-
-    private void handleRedirect(HttpServletRequest req, HttpServletResponse res, String handleService)
-        throws WayfException {
-
-        String shire = getSHIRE(req);
-        String target = getTarget(req);
-
-        try {
-            res.sendRedirect(
-                handleService + "?target=" + URLEncoder.encode(target) + "&shire=" + URLEncoder.encode(shire));
-        } catch (IOException ioe) {
-            throw new WayfException("Error forwarding to HS: " + ioe.toString());
-        }
-
-    }
-
-    private void handleError(HttpServletRequest req, HttpServletResponse res, WayfException we) {
-
-        getServletContext().log("WAYF Failure: " + we.toString());
-        req.setAttribute("errorText", we.toString());
-        RequestDispatcher rd = req.getRequestDispatcher("/wayferror.jsp");
-
-        try {
-            rd.forward(req, res);
-        } catch (IOException ioe) {
-            getServletContext().log("Problem trying to display WAYF error page: " + ioe.toString());
-            System.err.println("Problem trying to display WAYF error page: " + ioe.toString());
-        } catch (ServletException se) {
-            getServletContext().log("Problem trying to display WAYF error page: " + se.toString());
-            System.err.println("Problem trying to display WAYF error page: " + se.toString());
-        }
-    }
-
-    private String getSHIRE(HttpServletRequest req) throws WayfException {
-
-        String shire = (String) req.getAttribute("shire");
-        if (req.getParameter("shire") != null) {
-            shire = req.getParameter("shire");
-        }
-        if (shire == null) {
-            throw new WayfException("Invalid data from SHIRE: No acceptance URL received.");
-        }
-        return shire;
-    }
-
-    private String getTarget(HttpServletRequest req) throws WayfException {
-
-        String target = (String) req.getAttribute("target");
-        if (req.getParameter("target") != null) {
-            target = req.getParameter("target");
-        }
-        if (target == null) {
-            throw new WayfException("Invalid data from SHIRE: No target URL received.");
-        }
-        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();
-                }
-            }
-
-        }
-        WayfConfig.getServletContext().log("Invalid Cache type specified: running with cache type NONE.");
-        return null;
-
-    }
-
-}
\ No newline at end of file
diff --git a/src/edu/internet2/middleware/shibboleth/wayf/WayfService.java b/src/edu/internet2/middleware/shibboleth/wayf/WayfService.java
new file mode 100755 (executable)
index 0000000..a720412
--- /dev/null
@@ -0,0 +1,329 @@
+package edu.internet2.middleware.shibboleth.wayf;
+
+import java.io.IOException;
+import java.io.InputStream;
+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.xml.sax.SAXException;
+
+/**
+ * This is the main handler servlet for the WAYF service.  It configures itself, chooses among 
+ * several handler methods for each request, populates some beans (model), and then passes to an
+ * appropriate jsp page.
+ *
+ * @author             Walter Hoehn
+ */
+
+public class WayfService extends HttpServlet {
+
+       private String wayfConfigFileLocation;
+       private static Logger log = Logger.getLogger(WayfService.class.getName());
+
+       public void init() throws ServletException {
+
+               super.init();
+
+               loadInitParams();
+               //initialize configuration from file
+               InputStream is =
+                       getServletContext().getResourceAsStream(wayfConfigFileLocation);
+               WayfConfigDigester digester = new WayfConfigDigester();
+               try {
+                       digester.parse(is);
+               } catch (SAXException se) {
+                       log.fatal("Error parsing WAYF configuration file.", se);
+                       throw new ServletException(
+                               "Error parsing WAYF configuration file.",
+                               se);
+               } catch (IOException ioe) {
+                       log.fatal("Error reading WAYF configuration file.", ioe);
+                       throw new ServletException(
+                               "Error reading WAYF configuration file.",
+                               ioe);
+               }
+
+               //Setup appliation-wide beans from config
+               getServletContext().setAttribute(
+                       "originsets",
+                       WayfConfig.getWAYFData().getOriginSets());
+               String wayfLocation = WayfConfig.getLocation();
+               if (wayfLocation == null) {
+                       wayfLocation = "WAYF";
+               }
+               getServletContext().setAttribute("wayfLocation", wayfLocation);
+               getServletContext().setAttribute(
+                       "supportContact",
+                       WayfConfig.getSupportContact());
+               getServletContext().setAttribute("helpText", WayfConfig.getHelpText());
+               getServletContext().setAttribute(
+                       "searchResultEmptyText",
+                       WayfConfig.getSearchResultEmptyText());
+               getServletContext().setAttribute(
+                       "logoLocation",
+                       WayfConfig.getLogoLocation());
+       }
+
+       public void doGet(HttpServletRequest req, HttpServletResponse res) {
+
+               //Tell the browser not to cache the WAYF page
+               res.setHeader("Cache-Control", "no-cache");
+               res.setHeader("Pragma", "no-cache");
+
+               //Decide how to route the request based on query string
+               String requestType = req.getParameter("action");
+               if (requestType == null) {
+                       requestType = "lookup";
+               }
+               try {
+                       if (requestType.equals("deleteFromCache")) {
+                               handleDeleteFromCache(req, res);
+                       } else if (hasCachedHS(req)) {
+                               handleRedirect(req, res, getCachedHS(req));
+                       } else if (requestType.equals("search")) {
+                               handleSearch(req, res);
+                       } else if (requestType.equals("selection")) {
+                               handleSelection(req, res);
+                       } else {
+                               handleLookup(req, res);
+                       }
+               } catch (WayfException we) {
+                       handleError(req, res, we);
+               }
+       }
+
+       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 {
+
+               if ((getSHIRE(req) == null) || (getTarget(req) == null)) {
+                       throw new WayfException("Invalid or missing data from SHIRE");
+               }
+               req.setAttribute("shire", getSHIRE(req));
+               req.setAttribute("target", getTarget(req));
+               req.setAttribute("encodedShire", URLEncoder.encode(getSHIRE(req)));
+               req.setAttribute("encodedTarget", URLEncoder.encode(getTarget(req)));
+
+               RequestDispatcher rd = req.getRequestDispatcher("/wayf.jsp");
+               try {
+                       rd.forward(req, res);
+               } catch (IOException ioe) {
+                       throw new WayfException(
+                               "Problem displaying WAYF UI." + ioe.toString());
+               } catch (ServletException se) {
+                       throw new WayfException(
+                               "Problem displaying WAYF UI." + se.toString());
+               }
+       }
+
+       private void handleSearch(HttpServletRequest req, HttpServletResponse res)
+               throws WayfException {
+
+               if (req.getParameter("string") != null) {
+                       Origin[] origins =
+                               WayfConfig.getWAYFData().seachForMatchingOrigins(
+                                       req.getParameter("string"));
+                       if (origins.length != 0) {
+                               req.setAttribute("searchresults", origins);
+                       } else {
+                               req.setAttribute("searchResultsEmpty", "true");
+                       }
+               }
+               handleLookup(req, res);
+
+       }
+
+       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)
+               throws WayfException {
+
+               String handleService =
+                       WayfConfig.getWAYFData().lookupHSbyName(req.getParameter("origin"));
+               if (handleService == null) {
+                       handleLookup(req, res);
+               } else {
+                       addHsToCache(req, res, handleService);
+                       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,
+               String handleService)
+               throws WayfException {
+
+               String shire = getSHIRE(req);
+               String target = getTarget(req);
+
+               try {
+                       res.sendRedirect(
+                               handleService
+                                       + "?target="
+                                       + URLEncoder.encode(target)
+                                       + "&shire="
+                                       + URLEncoder.encode(shire));
+               } catch (IOException ioe) {
+                       throw new WayfException(
+                               "Error forwarding to HS: " + ioe.toString());
+               }
+
+       }
+
+       private void handleError(
+               HttpServletRequest req,
+               HttpServletResponse res,
+               WayfException we) {
+
+               log.error("WAYF Failure: " + we.toString());
+               req.setAttribute("errorText", we.toString());
+               RequestDispatcher rd = req.getRequestDispatcher("/wayferror.jsp");
+
+               try {
+                       rd.forward(req, res);
+               } catch (IOException ioe) {
+                       log.error(
+                               "Problem trying to display WAYF error page: " + ioe.toString());
+               } catch (ServletException se) {
+                       log.error(
+                               "Problem trying to display WAYF error page: " + se.toString());
+               }
+       }
+
+       private String getSHIRE(HttpServletRequest req) throws WayfException {
+
+               String shire = (String) req.getAttribute("shire");
+               if (req.getParameter("shire") != null) {
+                       shire = req.getParameter("shire");
+               }
+               if (shire == null) {
+                       throw new WayfException("Invalid data from SHIRE: No acceptance URL received.");
+               }
+               return shire;
+       }
+
+       private String getTarget(HttpServletRequest req) throws WayfException {
+
+               String target = (String) req.getAttribute("target");
+               if (req.getParameter("target") != null) {
+                       target = req.getParameter("target");
+               }
+               if (target == null) {
+                       throw new WayfException("Invalid data from SHIRE: No target URL received.");
+               }
+               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
index d34e6b5..2b82fa9 100755 (executable)
@@ -21,7 +21,7 @@
        <servlet>
                <servlet-name>WAYF</servlet-name>
                <display-name>WAYF</display-name>
-               <servlet-class>edu.internet2.middleware.shibboleth.wayf.WayfHandler</servlet-class>
+               <servlet-class>edu.internet2.middleware.shibboleth.wayf.WayfService</servlet-class>
                <init-param>
                        <param-name>WAYFConfigFileLocation</param-name>
                        <param-value>/WEB-INF/conf/shibboleth.xml</param-value>