Setup validation for WAYF configuration files.
authorwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Wed, 19 Jun 2002 16:45:01 +0000 (16:45 +0000)
committerwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Wed, 19 Jun 2002 16:45:01 +0000 (16:45 +0000)
Changed so that validation failures cause servlet shutdown.
Updated javadoc.
Extracted two methods for clarity.

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

src/edu/internet2/middleware/shibboleth/wayf/PassThruErrorHandler.java [new file with mode: 0755]
src/edu/internet2/middleware/shibboleth/wayf/WayfService.java
webApplication/WEB-INF/conf/sites.xml

diff --git a/src/edu/internet2/middleware/shibboleth/wayf/PassThruErrorHandler.java b/src/edu/internet2/middleware/shibboleth/wayf/PassThruErrorHandler.java
new file mode 100755 (executable)
index 0000000..f661deb
--- /dev/null
@@ -0,0 +1,36 @@
+package edu.internet2.middleware.shibboleth.wayf;
+
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+
+/**
+ * Sax <code>ErrorHandler</code> that simply passes all errors up as new 
+ * exceptions.
+ */
+
+public class PassThruErrorHandler implements ErrorHandler {
+
+       /**
+        * @see org.xml.sax.ErrorHandler#error(SAXParseException)
+        */
+       public void error(SAXParseException arg0) throws SAXException {
+               throw new SAXException("Error parsing xml file: " + arg0);
+       }
+
+       /**
+        * @see org.xml.sax.ErrorHandler#fatalError(SAXParseException)
+        */
+       public void fatalError(SAXParseException arg0) throws SAXException {
+               throw new SAXException("Error parsing xml file: " + arg0);
+       }
+
+       /**
+        * @see org.xml.sax.ErrorHandler#warning(SAXParseException)
+        */
+       public void warning(SAXParseException arg0) throws SAXException {
+               throw new SAXException("Error parsing xml file: " + arg0);
+       }
+
+}
+
index 7328793..1eb2c49 100755 (executable)
@@ -39,20 +39,26 @@ public class WayfService extends HttpServlet {
                log.info("Initializing WAYF.");
                loadInitParams();
                log.info("Loading configuration from file.");
                log.info("Initializing WAYF.");
                loadInitParams();
                log.info("Loading configuration from file.");
+               configure();
+               initViewConfig();
+               log.info("WAYF initialization completed.");
+       }
+
+       /**
+        * Populates WayfConfig and WayfOrigins objects from file contents.
+        */
+       private void configure() throws UnavailableException {
+
                InputStream is = getServletContext().getResourceAsStream(wayfConfigFileLocation);
                WayfConfigDigester digester = new WayfConfigDigester(getServletContext());
                InputStream is = getServletContext().getResourceAsStream(wayfConfigFileLocation);
                WayfConfigDigester digester = new WayfConfigDigester(getServletContext());
-               
                InputStream siteIs = getServletContext().getResourceAsStream(siteConfigFileLocation);
                OriginSitesDigester siteDigester = new OriginSitesDigester(getServletContext());
                InputStream siteIs = getServletContext().getResourceAsStream(siteConfigFileLocation);
                OriginSitesDigester siteDigester = new OriginSitesDigester(getServletContext());
-               
+
                try {
                try {
-                       //digester.setValidating(true);
+                       digester.setValidating(true);
+                       digester.setErrorHandler(new PassThruErrorHandler());
                        config = (WayfConfig) digester.parse(is);
                        config = (WayfConfig) digester.parse(is);
-                       
-                       //siteDigester.setValidating(true);
-                       originConfig = (WayfOrigins) siteDigester.parse(siteIs);
-                       
-                       
+
                } catch (SAXException se) {
                        log.fatal("Error parsing WAYF configuration file.", se);
                        throw new UnavailableException("Error parsing WAYF configuration file.");
                } catch (SAXException se) {
                        log.fatal("Error parsing WAYF configuration file.", se);
                        throw new UnavailableException("Error parsing WAYF configuration file.");
@@ -61,15 +67,33 @@ public class WayfService extends HttpServlet {
                        throw new UnavailableException("Error reading WAYF configuration file.");
                }
 
                        throw new UnavailableException("Error reading WAYF configuration file.");
                }
 
-               //Setup appliation-wide beans from config
+               try {
+                       siteDigester.setValidating(true);
+                       siteDigester.setErrorHandler(new PassThruErrorHandler());
+                       originConfig = (WayfOrigins) siteDigester.parse(siteIs);
+
+               } catch (SAXException se) {
+                       log.fatal("Error parsing site file.", se);
+                       throw new UnavailableException("Error parsing site file.");
+               } catch (IOException ioe) {
+                       log.fatal("Error reading site file.", ioe);
+                       throw new UnavailableException("Error reading site file.");
+               }
+       }
+       /**
+        * Setup application-wide beans for view
+        */
+       private void initViewConfig() {
                getServletContext().setAttribute("originsets", originConfig.getOriginSets());
                getServletContext().setAttribute("supportContact", config.getSupportContact());
                getServletContext().setAttribute("helpText", config.getHelpText());
                getServletContext().setAttribute("searchResultEmptyText", config.getSearchResultEmptyText());
                getServletContext().setAttribute("logoLocation", config.getLogoLocation());
                getServletContext().setAttribute("originsets", originConfig.getOriginSets());
                getServletContext().setAttribute("supportContact", config.getSupportContact());
                getServletContext().setAttribute("helpText", config.getHelpText());
                getServletContext().setAttribute("searchResultEmptyText", config.getSearchResultEmptyText());
                getServletContext().setAttribute("logoLocation", config.getLogoLocation());
-               log.info("WAYF initialization completed.");
        }
 
        }
 
+       /**
+        * Reads parameters from web.xml <init-param /> construct.
+        */
        private void loadInitParams() {
 
                wayfConfigFileLocation = getServletConfig().getInitParameter("WAYFConfigFileLocation");
        private void loadInitParams() {
 
                wayfConfigFileLocation = getServletConfig().getInitParameter("WAYFConfigFileLocation");
@@ -95,7 +119,7 @@ public class WayfService extends HttpServlet {
                res.setHeader("Cache-Control", "no-cache");
                res.setHeader("Pragma", "no-cache");
                res.setDateHeader("Expires", 0);
                res.setHeader("Cache-Control", "no-cache");
                res.setHeader("Pragma", "no-cache");
                res.setDateHeader("Expires", 0);
-               
+
                //Decide how to route the request based on query string
                String requestType = req.getParameter("action");
                if (requestType == null) {
                //Decide how to route the request based on query string
                String requestType = req.getParameter("action");
                if (requestType == null) {
@@ -107,7 +131,10 @@ public class WayfService extends HttpServlet {
                                WayfCacheFactory.getInstance(config.getCacheType()).deleteHsFromCache(req, res);
                                handleLookup(req, res);
                        } else if (WayfCacheFactory.getInstance(config.getCacheType()).hasCachedHS(req)) {
                                WayfCacheFactory.getInstance(config.getCacheType()).deleteHsFromCache(req, res);
                                handleLookup(req, res);
                        } else if (WayfCacheFactory.getInstance(config.getCacheType()).hasCachedHS(req)) {
-                               handleRedirect(req, res, WayfCacheFactory.getInstance(config.getCacheType()).getCachedHS(req));
+                               handleRedirect(
+                                       req,
+                                       res,
+                                       WayfCacheFactory.getInstance(config.getCacheType()).getCachedHS(req));
                        } else if (requestType.equals("search")) {
                                handleSearch(req, res);
                        } else if (requestType.equals("selection")) {
                        } else if (requestType.equals("search")) {
                                handleSearch(req, res);
                        } else if (requestType.equals("selection")) {
@@ -129,7 +156,7 @@ public class WayfService extends HttpServlet {
                req.setAttribute("target", getTarget(req));
                req.setAttribute("encodedShire", URLEncoder.encode(getSHIRE(req)));
                req.setAttribute("encodedTarget", URLEncoder.encode(getTarget(req)));
                req.setAttribute("target", getTarget(req));
                req.setAttribute("encodedShire", URLEncoder.encode(getSHIRE(req)));
                req.setAttribute("encodedTarget", URLEncoder.encode(getTarget(req)));
-               
+
                log.debug("Displaying WAYF selection page.");
                RequestDispatcher rd = req.getRequestDispatcher("/wayf.jsp");
                try {
                log.debug("Displaying WAYF selection page.");
                RequestDispatcher rd = req.getRequestDispatcher("/wayf.jsp");
                try {
@@ -169,7 +196,7 @@ public class WayfService extends HttpServlet {
 
        private void handleRedirect(HttpServletRequest req, HttpServletResponse res, String handleService)
                throws WayfException {
 
        private void handleRedirect(HttpServletRequest req, HttpServletResponse res, String handleService)
                throws WayfException {
-               
+
                String shire = getSHIRE(req);
                String target = getTarget(req);
                log.info("Redirecting to selected Handle Service");
                String shire = getSHIRE(req);
                String target = getTarget(req);
                log.info("Redirecting to selected Handle Service");
index bf3842b..0cb7677 100755 (executable)
@@ -1,4 +1,7 @@
-<Sites xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://shibboleth.internet2.edu/wayf/alpha-2/wayfconfig.xsd">
+<Sites xmlns="urn:mace:shibboleth:1.0" 
+       xmlns:ds="http://www.w3.org/2000/09/xmldsig#" 
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
+       xsi:schemaLocation="urn:mace:shibboleth:1.0 http://shibboleth.internet2.edu/wayf/alpha-2/shibboleth.xsd">
 <SiteGroup Name="Test">
        <OriginSite Name="demo.edu">
                <Alias>Demo State University</Alias>
 <SiteGroup Name="Test">
        <OriginSite Name="demo.edu">
                <Alias>Demo State University</Alias>