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