Let XML resource loader handle "validity".
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / XMLMetadataProvider.cpp
1 /*
2  *  Copyright 2001-2007 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * XMLMetadataProvider.cpp
19  * 
20  * Supplies metadata from an XML file
21  */
22
23 #include "internal.h"
24 #include "saml2/metadata/Metadata.h"
25 #include "saml2/metadata/MetadataFilter.h"
26 #include "saml2/metadata/AbstractMetadataProvider.h"
27
28 #include <xmltooling/util/NDC.h>
29 #include <xmltooling/util/ReloadableXMLFile.h>
30
31 using namespace opensaml::saml2md;
32 using namespace xmltooling;
33 using namespace log4cpp;
34 using namespace std;
35
36 #if defined (_MSC_VER)
37     #pragma warning( push )
38     #pragma warning( disable : 4250 )
39 #endif
40
41 namespace opensaml {
42     namespace saml2md {
43
44         class SAML_DLLLOCAL XMLMetadataProvider : public AbstractMetadataProvider, public ReloadableXMLFile
45         {
46         public:
47             XMLMetadataProvider(const DOMElement* e)
48                 : AbstractMetadataProvider(e), ReloadableXMLFile(e, Category::getInstance(SAML_LOGCAT".MetadataProvider")),
49                     m_object(NULL) {
50             }
51             virtual ~XMLMetadataProvider() {
52                 delete m_object;
53             }
54
55             void init() {
56                 load(); // guarantees an exception or the metadata is loaded
57             }
58             
59             const XMLObject* getMetadata() const {
60                 return m_object;
61             }
62
63         protected:
64             pair<bool,DOMElement*> load();
65
66         private:
67             void index();
68         
69             XMLObject* m_object;
70         }; 
71
72         MetadataProvider* SAML_DLLLOCAL XMLMetadataProviderFactory(const DOMElement* const & e)
73         {
74             return new XMLMetadataProvider(e);
75         }
76
77     };
78 };
79
80 #if defined (_MSC_VER)
81     #pragma warning( pop )
82 #endif
83
84 pair<bool,DOMElement*> XMLMetadataProvider::load()
85 {
86     // Load from source using base class.
87     pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
88     
89     // If we own it, wrap it for now.
90     XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);
91             
92     // Unmarshall objects, binding the document.
93     auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(raw.second, true));
94     docjanitor.release();
95
96     if (!dynamic_cast<const EntitiesDescriptor*>(xmlObject.get()) && !dynamic_cast<const EntityDescriptor*>(xmlObject.get()))
97         throw MetadataException(
98             "Root of metadata instance not recognized: $1", params(1,xmlObject->getElementQName().toString().c_str())
99             );
100     
101     // Preprocess the metadata.
102     doFilters(*xmlObject.get());
103     xmlObject->releaseThisAndChildrenDOM();
104     xmlObject->setDocument(NULL);
105     
106     // Swap it in.
107     bool changed = m_object!=NULL;
108     delete m_object;
109     m_object = xmlObject.release();
110     index();
111     if (changed)
112         emitChangeEvent();
113     return make_pair(false,(DOMElement*)NULL);
114 }
115
116 void XMLMetadataProvider::index()
117 {
118     clearDescriptorIndex();
119     EntitiesDescriptor* group=dynamic_cast<EntitiesDescriptor*>(m_object);
120     if (group) {
121         AbstractMetadataProvider::index(group, SAMLTIME_MAX);
122         return;
123     }
124     EntityDescriptor* site=dynamic_cast<EntityDescriptor*>(m_object);
125     AbstractMetadataProvider::index(site, SAMLTIME_MAX);
126 }