79f1133ce72154df4c7eb6a62fa42d8667c03aac
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / common / OriginConfig.java
1 /*
2  * The Shibboleth License, Version 1. Copyright (c) 2002 University Corporation for Advanced Internet Development, Inc.
3  * All rights reserved Redistribution and use in source and binary forms, with or without modification, are permitted
4  * provided that the following conditions are met: Redistributions of source code must retain the above copyright
5  * notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the
6  * above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other
7  * materials provided with the distribution, if any, must include the following acknowledgment: "This product includes
8  * software developed by the University Corporation for Advanced Internet Development <http://www.ucaid.edu> Internet2
9  * Project. Alternately, this acknowledegement may appear in the software itself, if and wherever such third-party
10  * acknowledgments normally appear. Neither the name of Shibboleth nor the names of its contributors, nor Internet2,
11  * nor the University Corporation for Advanced Internet Development, Inc., nor UCAID may be used to endorse or promote
12  * products derived from this software without specific prior written permission. For written permission, please
13  * contact shibboleth@shibboleth.org Products derived from this software may not be called Shibboleth, Internet2,
14  * UCAID, or the University Corporation for Advanced Internet Development, nor may Shibboleth appear in their name,
15  * without prior written permission of the University Corporation for Advanced Internet Development. THIS SOFTWARE IS
16  * PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES,
17  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
18  * NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS
19  * WITH LICENSEE. IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY CORPORATION FOR ADVANCED
20  * INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
23  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 package edu.internet2.middleware.shibboleth.common;
28
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.StringTokenizer;
32
33 import javax.servlet.ServletContext;
34
35 import org.apache.log4j.Logger;
36 import org.apache.xerces.parsers.DOMParser;
37 import org.w3c.dom.Document;
38 import org.xml.sax.EntityResolver;
39 import org.xml.sax.ErrorHandler;
40 import org.xml.sax.InputSource;
41 import org.xml.sax.SAXException;
42 import org.xml.sax.SAXParseException;
43
44 /**
45  * Constructs a DOM tree for the origin configuration XML file.
46  * 
47  * @author Walter Hoehn
48  * @author Noah Levitt
49  */
50 public class OriginConfig {
51
52         private static Logger   log                                     = Logger.getLogger(OriginConfig.class);
53         private static Document originConfig            = null;
54         private static String   originConfigFile        = null;
55
56         // never returns null
57         private static String getOriginConfigFile(ServletContext context) {
58                 if (context.getInitParameter("OriginConfigFile") != null) {
59                         return context.getInitParameter("OriginConfigFile");
60                 } else {
61                         return "/conf/origin.xml";
62                 }
63         }
64
65         /**
66          * Loads the Origin Configuration file into a DOM tree.
67          * 
68          * @param configFileLocation
69          *            URL of the configuration file
70          * @return the DOM Document
71          * @throws ShibbolethConfigurationException
72          *             if there was an error loading the file
73          */
74         public static synchronized Document getOriginConfig(String configFileLocation)
75                         throws ShibbolethConfigurationException {
76                 if (configFileLocation.equals(originConfigFile)) {
77                         return originConfig;
78                 } else if (originConfigFile == null) {
79                         originConfigFile = configFileLocation;
80                 } else {
81                         log.error("Previously read origin configuration from (" + originConfigFile + "), re-reading from ("
82                                         + configFileLocation + "). This probably indicates a bug in shibboleth.");
83                         originConfigFile = configFileLocation;
84                 }
85
86                 DOMParser parser = new DOMParser();
87
88                 try {
89                         parser.setFeature("http://xml.org/sax/features/validation", true);
90                         parser.setFeature("http://apache.org/xml/features/validation/schema", true);
91
92                         parser.setEntityResolver(new EntityResolver() {
93
94                                 public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
95                                         log.debug("Resolving entity for System ID: " + systemId);
96                                         if (systemId != null) {
97                                                 StringTokenizer tokenString = new StringTokenizer(systemId, "/");
98                                                 String xsdFile = "";
99                                                 while (tokenString.hasMoreTokens()) {
100                                                         xsdFile = tokenString.nextToken();
101                                                 }
102                                                 if (xsdFile.endsWith(".xsd")) {
103                                                         InputStream stream;
104                                                         try {
105                                                                 stream = new ShibResource("/schemas/" + xsdFile, OriginConfig.class).getInputStream();
106                                                         } catch (IOException ioe) {
107                                                                 log.error("Error loading schema: " + xsdFile + ": " + ioe);
108                                                                 return null;
109                                                         }
110                                                         if (stream != null) {
111                                                                 return new InputSource(stream);
112                                                         }
113                                                 }
114                                         }
115                                         return null;
116                                 }
117                         });
118
119                         parser.setErrorHandler(new ErrorHandler() {
120
121                                 public void error(SAXParseException arg0) throws SAXException {
122                                         throw new SAXException("Error parsing xml file: " + arg0);
123                                 }
124
125                                 public void fatalError(SAXParseException arg0) throws SAXException {
126                                         throw new SAXException("Error parsing xml file: " + arg0);
127                                 }
128
129                                 public void warning(SAXParseException arg0) throws SAXException {
130                                         throw new SAXException("Error parsing xml file: " + arg0);
131                                 }
132                         });
133
134                 } catch (SAXException e) {
135                         log.error("Unable to setup a workable XML parser: " + e);
136                         throw new ShibbolethConfigurationException("Unable to setup a workable XML parser.");
137                 }
138
139                 log.debug("Loading Configuration from (" + originConfigFile + ").");
140
141                 try {
142                         parser.parse(new InputSource(new ShibResource(originConfigFile, OriginConfig.class).getInputStream()));
143                 } catch (SAXException e) {
144                         log.error("Error while parsing origin configuration: " + e);
145                         throw new ShibbolethConfigurationException("Error while parsing origin configuration:" + e);
146                 } catch (IOException e) {
147                         log.error("Could not load origin configuration: " + e);
148                         throw new ShibbolethConfigurationException("Could not load origin configuration.");
149                 }
150
151                 originConfig = parser.getDocument();
152
153                 return originConfig;
154         }
155
156         /**
157          * Loads the Origin Configuration file into a DOM tree.
158          * 
159          * @param context
160          *            {@link ServletContext}from which to figure out the location of origin.xml
161          * @return the DOM Document
162          * @throws ShibbolethConfigurationException
163          *             if there was an error loading the file
164          */
165         public static Document getOriginConfig(ServletContext context) throws ShibbolethConfigurationException {
166                 return getOriginConfig(getOriginConfigFile(context));
167
168         }
169 }