FileSystemArpRepository now does schema checking.
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / aa / arp / provider / FileSystemArpRepository.java
1 /* 
2  * The Shibboleth License, Version 1. 
3  * Copyright (c) 2002 
4  * University Corporation for Advanced Internet Development, Inc. 
5  * All rights reserved
6  * 
7  * 
8  * Redistribution and use in source and binary forms, with or without 
9  * modification, are permitted provided that the following conditions are met:
10  * 
11  * Redistributions of source code must retain the above copyright notice, this 
12  * list of conditions and the following disclaimer.
13  * 
14  * Redistributions in binary form must reproduce the above copyright notice, 
15  * this list of conditions and the following disclaimer in the documentation 
16  * and/or other materials provided with the distribution, if any, must include 
17  * the following acknowledgment: "This product includes software developed by 
18  * the University Corporation for Advanced Internet Development 
19  * <http://www.ucaid.edu>Internet2 Project. Alternately, this acknowledegement 
20  * may appear in the software itself, if and wherever such third-party 
21  * acknowledgments normally appear.
22  * 
23  * Neither the name of Shibboleth nor the names of its contributors, nor 
24  * Internet2, nor the University Corporation for Advanced Internet Development, 
25  * Inc., nor UCAID may be used to endorse or promote products derived from this 
26  * software without specific prior written permission. For written permission, 
27  * please contact shibboleth@shibboleth.org
28  * 
29  * Products derived from this software may not be called Shibboleth, Internet2, 
30  * UCAID, or the University Corporation for Advanced Internet Development, nor 
31  * may Shibboleth appear in their name, without prior written permission of the 
32  * University Corporation for Advanced Internet Development.
33  * 
34  * 
35  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
36  * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
37  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
38  * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK 
39  * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE. 
40  * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY 
41  * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT, 
42  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
43  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
44  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
47  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  */
49
50 package edu.internet2.middleware.shibboleth.aa.arp.provider;
51
52 import java.io.File;
53 import java.io.IOException;
54 import java.io.InputStream;
55 import java.security.Principal;
56 import java.util.Properties;
57
58 import org.apache.log4j.Logger;
59 import org.apache.xerces.parsers.DOMParser;
60 import org.w3c.dom.Element;
61 import org.xml.sax.EntityResolver;
62 import org.xml.sax.ErrorHandler;
63 import org.xml.sax.InputSource;
64 import org.xml.sax.SAXException;
65 import org.xml.sax.SAXParseException;
66
67 import edu.internet2.middleware.shibboleth.aa.arp.Arp;
68 import edu.internet2.middleware.shibboleth.aa.arp.ArpRepository;
69 import edu.internet2.middleware.shibboleth.aa.arp.ArpRepositoryException;
70 import edu.internet2.middleware.shibboleth.common.ShibResource;
71
72 /**
73  * Simple <code>ArpRepository</code> implementation that uses a filesystem for storage.
74  * 
75  * @author Walter Hoehn (wassa@columbia.edu)
76  */
77
78 public class FileSystemArpRepository extends BaseArpRepository implements ArpRepository {
79
80         private static Logger log = Logger.getLogger(FileSystemArpRepository.class.getName());
81         private final String siteArpFileName = "arp.site.xml";
82
83         private String dataStorePath;
84
85         public FileSystemArpRepository(Properties props) throws ArpRepositoryException {
86                 super(props);
87                 if (props
88                         .getProperty(
89                                 "edu.internet2.middleware.shibboleth.aa.arp.provider.FileSystemArpRepository.Path",
90                                 null)
91                         == null) {
92                         log.error(
93                                 "Cannot initialize FileSystemArpRepository: attribute (edu.internet2.middleware.shibboleth.aa.arp.provider.FileSystemArpRepository.Path) not specified");
94                         throw new ArpRepositoryException("Cannot initialize FileSystemArpRepository");
95                 }
96
97                 try {
98                         File givenPath =
99                                 new ShibResource(
100                                         props.getProperty(
101                                                 "edu.internet2.middleware.shibboleth.aa.arp.provider.FileSystemArpRepository.Path"),
102                                         this.getClass())
103                                         .getFile();
104
105                         if (!givenPath.isDirectory()) {
106                                 log.error(
107                                         "Cannot initialize FileSystemArpRepository: specified path is not a directory: ("
108                                                 + givenPath.getPath()
109                                                 + ").");
110                                 throw new ArpRepositoryException("Cannot initialize FileSystemArpRepository");
111                         }
112
113                         dataStorePath =
114                                 props.getProperty(
115                                         "edu.internet2.middleware.shibboleth.aa.arp.provider.FileSystemArpRepository.Path");
116                         if (!dataStorePath.endsWith("/")) {
117                                 dataStorePath += "/";
118                         }
119                         log.info("Initializing File System Arp Repository with a root of (" + dataStorePath + ").");
120                 } catch (Exception e) {
121                         log.error(
122                                 "Cannot initialize FileSystemArpRepository: error accessing path: ("
123                                         + props.getProperty(
124                                                 "edu.internet2.middleware.shibboleth.aa.arp.provider.FileSystemArpRepository.Path")
125                                         + "): "
126                                         + e);
127                         throw new ArpRepositoryException("Cannot initialize FileSystemArpRepository");
128                 }
129         }
130         /**
131          * @see edu.internet2.middleware.shibboleth.aa.arp.ArpRepository#remove(Arp)
132          */
133         public void remove(Arp arp) throws ArpRepositoryException {
134                 throw new ArpRepositoryException("Remove not implemented for FileSystemArpRepository.");
135         }
136
137         /**
138          * @see edu.internet2.middleware.shibboleth.aa.arp.ArpRepository#update(Arp)
139          */
140         public void update(Arp arp) throws ArpRepositoryException {
141                 throw new ArpRepositoryException("Update not implemented for FileSystemArpRepository.");
142         }
143
144         /**
145          * @see edu.internet2.middleware.shibboleth.aa.arp.provider.BaseArpRepository#retrieveSiteArpXml()
146          */
147         protected Element retrieveSiteArpXml() throws IOException, SAXException {
148
149                 String fileName = dataStorePath + siteArpFileName;
150                 log.debug("Attempting to load site ARP from: (" + fileName + ").");
151                 return retrieveArpXml(fileName);
152
153         }
154
155         private Element retrieveArpXml(String fileName) throws SAXException, IOException {
156
157                 try {
158                         ShibResource resource = new ShibResource(fileName, this.getClass());
159                         if (!resource.getFile().exists()) {
160                                 log.debug("No ARP found.");
161                                 return null;
162                         }
163
164                         DOMParser parser = new DOMParser();
165                         parser.setFeature("http://xml.org/sax/features/validation", true);
166                         parser.setFeature("http://apache.org/xml/features/validation/schema", true);
167                         parser.setEntityResolver(new EntityResolver() {
168                                 public InputSource resolveEntity(String publicId, String systemId)
169                                         throws SAXException {
170
171                                         if (systemId.endsWith("shibboleth-arp-1.0.xsd")) {
172                                                 InputStream stream;
173                                                 try {
174                                                         return new InputSource(new ShibResource("/schemas/shibboleth-arp-1.0.xsd", this.getClass()).getInputStream());
175                                                 } catch (IOException e) {
176                                                         throw new SAXException("Could not load entity: " + e);
177                                                 }
178                                         } else {
179                                                 return null;
180                                         }
181                                 }
182                         });
183
184                         parser.setErrorHandler(new ErrorHandler() {
185                                 public void error(SAXParseException arg0) throws SAXException {
186                                         throw new SAXException("Error parsing xml file: " + arg0);
187                                 }
188                                 public void fatalError(SAXParseException arg0) throws SAXException {
189                                         throw new SAXException("Error parsing xml file: " + arg0);
190                                 }
191                                 public void warning(SAXParseException arg0) throws SAXException {
192                                         throw new SAXException("Error parsing xml file: " + arg0);
193                                 }
194                         });
195                         parser.parse(new InputSource(resource.getInputStream()));
196                         return parser.getDocument().getDocumentElement();
197
198                 } catch (ShibResource.ResourceNotAvailableException e) {
199                         log.debug("No ARP found.");
200                         return null;
201                 }
202         }
203         /**
204          * @see edu.internet2.middleware.shibboleth.aa.arp.provider.BaseArpRepository#retrieveUserArpXml(Principal)
205          */
206         protected Element retrieveUserArpXml(Principal principal) throws IOException, SAXException {
207                 String fileName =
208                         dataStorePath
209                                 + "arp.user."
210                                 + principal.getName()
211                                 + ".xml";
212                 log.debug(
213                         "Attempting to load user (" + principal.getName() + ") ARP from: (" + fileName + ").");
214                 return retrieveArpXml(fileName);
215         }
216
217 }