Pulled up parser loading into a base class.
authorwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Fri, 30 Jan 2004 05:49:40 +0000 (05:49 +0000)
committerwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Fri, 30 Jan 2004 05:49:40 +0000 (05:49 +0000)
git-svn-id: https://subversion.switch.ch/svn/shibboleth/java-idp/trunk@869 ab3bd59b-922f-494d-bb5f-6f0a3c29deca

src/edu/internet2/middleware/shibboleth/aa/AAServlet.java
src/edu/internet2/middleware/shibboleth/common/OriginComponent.java [new file with mode: 0644]
src/edu/internet2/middleware/shibboleth/hs/HandleServlet.java

index 51b54bf..259de5f 100755 (executable)
@@ -48,7 +48,6 @@
 package edu.internet2.middleware.shibboleth.aa;
 
 import java.io.IOException;
-import java.io.InputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.security.Principal;
@@ -57,11 +56,9 @@ import java.util.Arrays;
 import java.util.Collections;
 import java.util.Date;
 import java.util.Iterator;
-import java.util.StringTokenizer;
 
 import javax.servlet.ServletException;
 import javax.servlet.UnavailableException;
-import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
@@ -84,11 +81,8 @@ import org.opensaml.SAMLStatement;
 import org.opensaml.SAMLSubject;
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
-import org.xml.sax.EntityResolver;
-import org.xml.sax.ErrorHandler;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
-import org.xml.sax.SAXParseException;
 
 import sun.misc.BASE64Decoder;
 import edu.internet2.middleware.shibboleth.aa.arp.ArpEngine;
@@ -99,6 +93,7 @@ import edu.internet2.middleware.shibboleth.common.AuthNPrincipal;
 import edu.internet2.middleware.shibboleth.common.NameIdentifierMapping;
 import edu.internet2.middleware.shibboleth.common.NameIdentifierMappingException;
 import edu.internet2.middleware.shibboleth.common.NameMapper;
+import edu.internet2.middleware.shibboleth.common.OriginComponent;
 import edu.internet2.middleware.shibboleth.common.RelyingParty;
 import edu.internet2.middleware.shibboleth.common.SAMLBindingFactory;
 import edu.internet2.middleware.shibboleth.common.ServiceProviderMapper;
@@ -111,7 +106,7 @@ import edu.internet2.middleware.shibboleth.common.ShibbolethOriginConfig;
  * @author Walter Hoehn
  */
 
-public class AAServlet extends HttpServlet {
+public class AAServlet extends OriginComponent {
 
        private AAConfig configuration;
        protected AAResponder responder;
@@ -146,8 +141,6 @@ public class AAServlet extends HttpServlet {
        }
        protected void loadConfiguration() throws ShibbolethConfigurationException {
 
-               //TODO could maybe factor some of the common stuff up a level.
-
                DOMParser parser = loadParser(true);
 
                String originConfigFile = getInitParameter("OriginConfigFile");
@@ -221,63 +214,6 @@ public class AAServlet extends HttpServlet {
                }
 
        }
-       private DOMParser loadParser(boolean schemaChecking) throws ShibbolethConfigurationException {
-
-               DOMParser parser = new DOMParser();
-
-               if (!schemaChecking) {
-                       return parser;
-               }
-
-               try {
-                       parser.setFeature("http://xml.org/sax/features/validation", true);
-                       parser.setFeature("http://apache.org/xml/features/validation/schema", true);
-
-                       parser.setEntityResolver(new EntityResolver() {
-                               public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
-                                       log.debug("Resolving entity for System ID: " + systemId);
-                                       if (systemId != null) {
-                                               StringTokenizer tokenString = new StringTokenizer(systemId, "/");
-                                               String xsdFile = "";
-                                               while (tokenString.hasMoreTokens()) {
-                                                       xsdFile = tokenString.nextToken();
-                                               }
-                                               if (xsdFile.endsWith(".xsd")) {
-                                                       InputStream stream;
-                                                       try {
-                                                               stream = new ShibResource("/schemas/" + xsdFile, this.getClass()).getInputStream();
-                                                       } catch (IOException ioe) {
-                                                               log.error("Error loading schema: " + xsdFile + ": " + ioe);
-                                                               return null;
-                                                       }
-                                                       if (stream != null) {
-                                                               return new InputSource(stream);
-                                                       }
-                                               }
-                                       }
-                                       return null;
-                               }
-                       });
-
-                       parser.setErrorHandler(new ErrorHandler() {
-                               public void error(SAXParseException arg0) throws SAXException {
-                                       throw new SAXException("Error parsing xml file: " + arg0);
-                               }
-                               public void fatalError(SAXParseException arg0) throws SAXException {
-                                       throw new SAXException("Error parsing xml file: " + arg0);
-                               }
-                               public void warning(SAXParseException arg0) throws SAXException {
-                                       throw new SAXException("Error parsing xml file: " + arg0);
-                               }
-                       });
-
-               } catch (SAXException e) {
-                       log.error("Unable to setup a workable XML parser: " + e);
-                       throw new ShibbolethConfigurationException("Unable to setup a workable XML parser.");
-               }
-               return parser;
-       }
-
        public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 
                MDC.put("serviceId", "[AA] " + new SAMLIdentifier().toString());
diff --git a/src/edu/internet2/middleware/shibboleth/common/OriginComponent.java b/src/edu/internet2/middleware/shibboleth/common/OriginComponent.java
new file mode 100644 (file)
index 0000000..d3ba484
--- /dev/null
@@ -0,0 +1,128 @@
+/*
+ * The Shibboleth License, Version 1. Copyright (c) 2002 University Corporation
+ * for Advanced Internet Development, Inc. All rights reserved
+ * 
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution, if any, must include
+ * the following acknowledgment: "This product includes software developed by
+ * the University Corporation for Advanced Internet Development
+ * <http://www.ucaid.edu> Internet2 Project. Alternately, this acknowledegement
+ * may appear in the software itself, if and wherever such third-party
+ * acknowledgments normally appear.
+ * 
+ * Neither the name of Shibboleth nor the names of its contributors, nor
+ * Internet2, nor the University Corporation for Advanced Internet Development,
+ * Inc., nor UCAID may be used to endorse or promote products derived from this
+ * software without specific prior written permission. For written permission,
+ * please contact shibboleth@shibboleth.org
+ * 
+ * Products derived from this software may not be called Shibboleth, Internet2,
+ * UCAID, or the University Corporation for Advanced Internet Development, nor
+ * may Shibboleth appear in their name, without prior written permission of the
+ * University Corporation for Advanced Internet Development.
+ * 
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
+ * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK
+ * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY
+ * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package edu.internet2.middleware.shibboleth.common;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.StringTokenizer;
+
+import javax.servlet.http.HttpServlet;
+
+import org.apache.log4j.Logger;
+import org.apache.xerces.parsers.DOMParser;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+
+/**
+ * @author Walter Hoehn
+ */
+public abstract class OriginComponent extends HttpServlet {
+
+       private static Logger log = Logger.getLogger(OriginComponent.class.getName());
+
+       protected DOMParser loadParser(boolean schemaChecking) throws ShibbolethConfigurationException {
+
+               DOMParser parser = new DOMParser();
+
+               if (!schemaChecking) {
+                       return parser;
+               }
+
+               try {
+                       parser.setFeature("http://xml.org/sax/features/validation", true);
+                       parser.setFeature("http://apache.org/xml/features/validation/schema", true);
+
+                       parser.setEntityResolver(new EntityResolver() {
+                               public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
+                                       log.debug("Resolving entity for System ID: " + systemId);
+                                       if (systemId != null) {
+                                               StringTokenizer tokenString = new StringTokenizer(systemId, "/");
+                                               String xsdFile = "";
+                                               while (tokenString.hasMoreTokens()) {
+                                                       xsdFile = tokenString.nextToken();
+                                               }
+                                               if (xsdFile.endsWith(".xsd")) {
+                                                       InputStream stream;
+                                                       try {
+                                                               stream = new ShibResource("/schemas/" + xsdFile, this.getClass()).getInputStream();
+                                                       } catch (IOException ioe) {
+                                                               log.error("Error loading schema: " + xsdFile + ": " + ioe);
+                                                               return null;
+                                                       }
+                                                       if (stream != null) {
+                                                               return new InputSource(stream);
+                                                       }
+                                               }
+                                       }
+                                       return null;
+                               }
+                       });
+
+                       parser.setErrorHandler(new ErrorHandler() {
+                               public void error(SAXParseException arg0) throws SAXException {
+                                       throw new SAXException("Error parsing xml file: " + arg0);
+                               }
+                               public void fatalError(SAXParseException arg0) throws SAXException {
+                                       throw new SAXException("Error parsing xml file: " + arg0);
+                               }
+                               public void warning(SAXParseException arg0) throws SAXException {
+                                       throw new SAXException("Error parsing xml file: " + arg0);
+                               }
+                       });
+
+               } catch (SAXException e) {
+                       log.error("Unable to setup a workable XML parser: " + e);
+                       throw new ShibbolethConfigurationException("Unable to setup a workable XML parser.");
+               }
+               return parser;
+       }
+
+}
index d75e3bc..e5a25b0 100644 (file)
 package edu.internet2.middleware.shibboleth.hs;
 
 import java.io.IOException;
-import java.io.InputStream;
 import java.util.Collections;
 import java.util.Date;
-import java.util.StringTokenizer;
 
 import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletException;
 import javax.servlet.UnavailableException;
-import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
 import org.apache.log4j.MDC;
-import org.apache.log4j.Level;
 import org.apache.xerces.parsers.DOMParser;
 import org.doomdark.uuid.UUIDGenerator;
 import org.opensaml.QName;
@@ -73,26 +70,23 @@ import org.opensaml.SAMLNameIdentifier;
 import org.opensaml.SAMLResponse;
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
-import org.xml.sax.EntityResolver;
-import org.xml.sax.ErrorHandler;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
-import org.xml.sax.SAXParseException;
 
 import sun.misc.BASE64Decoder;
-import edu.internet2.middleware.shibboleth.common.*;
 import edu.internet2.middleware.shibboleth.common.AuthNPrincipal;
 import edu.internet2.middleware.shibboleth.common.Credentials;
 import edu.internet2.middleware.shibboleth.common.NameIdentifierMapping;
 import edu.internet2.middleware.shibboleth.common.NameIdentifierMappingException;
+import edu.internet2.middleware.shibboleth.common.OriginComponent;
 import edu.internet2.middleware.shibboleth.common.RelyingParty;
 import edu.internet2.middleware.shibboleth.common.ServiceProviderMapper;
 import edu.internet2.middleware.shibboleth.common.ServiceProviderMapperException;
 import edu.internet2.middleware.shibboleth.common.ShibPOSTProfile;
 import edu.internet2.middleware.shibboleth.common.ShibResource;
-import edu.internet2.middleware.shibboleth.common.ShibbolethOriginConfig;
+import edu.internet2.middleware.shibboleth.common.ShibbolethConfigurationException;
 
-public class HandleServlet extends HttpServlet {
+public class HandleServlet extends OriginComponent {
 
        private static Logger log = Logger.getLogger(HandleServlet.class.getName());
        private static Logger transactionLog = Logger.getLogger("edu.internet2.middleware.shibboleth.TRANSACTION");
@@ -173,63 +167,6 @@ public class HandleServlet extends HttpServlet {
 
        }
 
-       private DOMParser loadParser(boolean schemaChecking) throws ShibbolethConfigurationException {
-
-               DOMParser parser = new DOMParser();
-
-               if (!schemaChecking) {
-                       return parser;
-               }
-
-               try {
-                       parser.setFeature("http://xml.org/sax/features/validation", true);
-                       parser.setFeature("http://apache.org/xml/features/validation/schema", true);
-
-                       parser.setEntityResolver(new EntityResolver() {
-                               public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
-                                       log.debug("Resolving entity for System ID: " + systemId);
-                                       if (systemId != null) {
-                                               StringTokenizer tokenString = new StringTokenizer(systemId, "/");
-                                               String xsdFile = "";
-                                               while (tokenString.hasMoreTokens()) {
-                                                       xsdFile = tokenString.nextToken();
-                                               }
-                                               if (xsdFile.endsWith(".xsd")) {
-                                                       InputStream stream;
-                                                       try {
-                                                               stream = new ShibResource("/schemas/" + xsdFile, this.getClass()).getInputStream();
-                                                       } catch (IOException ioe) {
-                                                               log.error("Error loading schema: " + xsdFile + ": " + ioe);
-                                                               return null;
-                                                       }
-                                                       if (stream != null) {
-                                                               return new InputSource(stream);
-                                                       }
-                                               }
-                                       }
-                                       return null;
-                               }
-                       });
-
-                       parser.setErrorHandler(new ErrorHandler() {
-                               public void error(SAXParseException arg0) throws SAXException {
-                                       throw new SAXException("Error parsing xml file: " + arg0);
-                               }
-                               public void fatalError(SAXParseException arg0) throws SAXException {
-                                       throw new SAXException("Error parsing xml file: " + arg0);
-                               }
-                               public void warning(SAXParseException arg0) throws SAXException {
-                                       throw new SAXException("Error parsing xml file: " + arg0);
-                               }
-                       });
-
-               } catch (SAXException e) {
-                       log.error("Unable to setup a workable XML parser: " + e);
-                       throw new ShibbolethConfigurationException("Unable to setup a workable XML parser.");
-               }
-               return parser;
-       }
-
        public void init() throws ServletException {
                super.init();
                MDC.put("serviceId", "[HS] Core");