fa519a0ce4e2f53c21a815378bcdf067f13ed9c7
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / aa / arp / provider / FileSystemArpRepository.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.aa.arp.provider;
49
50 import java.io.File;
51 import java.io.IOException;
52 import java.security.Principal;
53
54 import org.apache.log4j.Logger;
55 import org.apache.xerces.parsers.DOMParser;
56 import org.w3c.dom.Element;
57 import org.w3c.dom.Node;
58 import org.w3c.dom.NodeList;
59 import org.xml.sax.EntityResolver;
60 import org.xml.sax.ErrorHandler;
61 import org.xml.sax.InputSource;
62 import org.xml.sax.SAXException;
63 import org.xml.sax.SAXParseException;
64
65 import edu.internet2.middleware.shibboleth.aa.arp.Arp;
66 import edu.internet2.middleware.shibboleth.aa.arp.ArpRepository;
67 import edu.internet2.middleware.shibboleth.aa.arp.ArpRepositoryException;
68 import edu.internet2.middleware.shibboleth.common.ShibResource;
69 import edu.internet2.middleware.shibboleth.common.ShibbolethOriginConfig;
70
71 /**
72  * Simple <code>ArpRepository</code> implementation that uses a filesystem
73  * 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(Element config) throws ArpRepositoryException {
86                 super(config);
87
88                 NodeList itemElements = config.getElementsByTagNameNS(ShibbolethOriginConfig.originConfigNamespace, "Path");
89
90                 if (itemElements.getLength() > 1) {
91                         log.warn(
92                                 "Encountered multiple <Path> configuration elements for the File System ARP Repository.  Using first...");
93                 }
94                 Node tnode = itemElements.item(0).getFirstChild();
95                 String path = null;
96                 if (tnode != null && tnode.getNodeType() == Node.TEXT_NODE) {
97                         path = tnode.getNodeValue();
98                 }
99                 if (path == null || path.equals("")) {
100                         log.error("ARP repository path not specified.");
101                         throw new ArpRepositoryException("Cannot initialize FileSystemArpRepository: <ArpRepository> element must contain a <Path> element.");
102                 }
103
104                 try {
105                         File realPath = new ShibResource(path, this.getClass()).getFile();
106
107                         if (!realPath.isDirectory()) {
108                                 log.error(
109                                         "Cannot initialize FileSystemArpRepository: specified path is not a directory: ("
110                                                 + realPath.getPath()
111                                                 + ").");
112                                 throw new ArpRepositoryException("Cannot initialize FileSystemArpRepository");
113                         }
114
115                         dataStorePath = 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("Cannot initialize FileSystemArpRepository: error accessing path: (" + path + "): " + e);
122                         throw new ArpRepositoryException("Cannot initialize FileSystemArpRepository");
123                 }
124         }
125         /**
126          * @see edu.internet2.middleware.shibboleth.aa.arp.ArpRepository#remove(Arp)
127          */
128         public void remove(Arp arp) throws ArpRepositoryException {
129                 throw new ArpRepositoryException("Remove not implemented for FileSystemArpRepository.");
130         }
131
132         /**
133          * @see edu.internet2.middleware.shibboleth.aa.arp.ArpRepository#update(Arp)
134          */
135         public void update(Arp arp) throws ArpRepositoryException {
136                 throw new ArpRepositoryException("Update not implemented for FileSystemArpRepository.");
137         }
138
139         /**
140          * @see edu.internet2.middleware.shibboleth.aa.arp.provider.BaseArpRepository#retrieveSiteArpXml()
141          */
142         protected Element retrieveSiteArpXml() throws IOException, SAXException {
143
144                 String fileName = dataStorePath + siteArpFileName;
145                 log.debug("Attempting to load site ARP from: (" + fileName + ").");
146                 return retrieveArpXml(fileName);
147
148         }
149
150         private Element retrieveArpXml(String fileName) throws SAXException, IOException {
151
152                 try {
153                         ShibResource resource = new ShibResource(fileName, this.getClass());
154                         if (!resource.getFile().exists()) {
155                                 log.debug("No ARP found.");
156                                 return null;
157                         }
158
159                         DOMParser parser = new DOMParser();
160                         parser.setFeature("http://xml.org/sax/features/validation", true);
161                         parser.setFeature("http://apache.org/xml/features/validation/schema", true);
162                         parser.setEntityResolver(new EntityResolver() {
163                                 public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
164
165                                         if (systemId.endsWith("shibboleth-arp-1.0.xsd")) {
166                                                 try {
167                                                         return new InputSource(
168                                                                 new ShibResource("/schemas/shibboleth-arp-1.0.xsd", this.getClass()).getInputStream());
169                                                 } catch (IOException e) {
170                                                         throw new SAXException("Could not load entity: " + e);
171                                                 }
172                                         } else {
173                                                 return null;
174                                         }
175                                 }
176                         });
177
178                         parser.setErrorHandler(new ErrorHandler() {
179                                 public void error(SAXParseException arg0) throws SAXException {
180                                         throw new SAXException("Error parsing xml file: " + arg0);
181                                 }
182                                 public void fatalError(SAXParseException arg0) throws SAXException {
183                                         throw new SAXException("Error parsing xml file: " + arg0);
184                                 }
185                                 public void warning(SAXParseException arg0) throws SAXException {
186                                         throw new SAXException("Error parsing xml file: " + arg0);
187                                 }
188                         });
189                         parser.parse(new InputSource(resource.getInputStream()));
190                         return parser.getDocument().getDocumentElement();
191
192                 } catch (ShibResource.ResourceNotAvailableException e) {
193                         log.debug("No ARP found.");
194                         return null;
195                 }
196         }
197         /**
198          * @see edu.internet2.middleware.shibboleth.aa.arp.provider.BaseArpRepository#retrieveUserArpXml(Principal)
199          */
200         protected Element retrieveUserArpXml(Principal principal) throws IOException, SAXException {
201                 String fileName = dataStorePath + "arp.user." + principal.getName() + ".xml";
202                 log.debug("Attempting to load user (" + principal.getName() + ") ARP from: (" + fileName + ").");
203                 return retrieveArpXml(fileName);
204         }
205
206 }