8214e3a0b3c410fbbeb0c386e345bddd9e8790a3
[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 #include <xmltooling/validation/ValidatorSuite.h>
31
32 using namespace opensaml::saml2md;
33 using namespace xmltooling::logging;
34 using namespace xmltooling;
35 using namespace std;
36
37 #if defined (_MSC_VER)
38     #pragma warning( push )
39     #pragma warning( disable : 4250 )
40 #endif
41
42 namespace opensaml {
43     namespace saml2md {
44
45         class SAML_DLLLOCAL XMLMetadataProvider : public AbstractMetadataProvider, public ReloadableXMLFile
46         {
47         public:
48             XMLMetadataProvider(const DOMElement* e)
49                 : AbstractMetadataProvider(e), ReloadableXMLFile(e, Category::getInstance(SAML_LOGCAT".MetadataProvider.XML")),
50                     m_object(NULL), m_maxCacheDuration(m_reloadInterval) {
51             }
52             virtual ~XMLMetadataProvider() {
53                 delete m_object;
54             }
55
56             void init() {
57                 load(); // guarantees an exception or the metadata is loaded
58             }
59
60             const XMLObject* getMetadata() const {
61                 return m_object;
62             }
63
64         protected:
65             pair<bool,DOMElement*> load();
66
67         private:
68             using AbstractMetadataProvider::index;
69             void index();
70
71             XMLObject* m_object;
72             time_t m_maxCacheDuration;
73         };
74
75         MetadataProvider* SAML_DLLLOCAL XMLMetadataProviderFactory(const DOMElement* const & e)
76         {
77             return new XMLMetadataProvider(e);
78         }
79
80     };
81 };
82
83 #if defined (_MSC_VER)
84     #pragma warning( pop )
85 #endif
86
87 pair<bool,DOMElement*> XMLMetadataProvider::load()
88 {
89     // Load from source using base class.
90     pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
91
92     // If we own it, wrap it for now.
93     XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);
94
95     // Unmarshall objects, binding the document.
96     auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(raw.second, true));
97     docjanitor.release();
98
99     if (!dynamic_cast<const EntitiesDescriptor*>(xmlObject.get()) && !dynamic_cast<const EntityDescriptor*>(xmlObject.get()))
100         throw MetadataException(
101             "Root of metadata instance not recognized: $1", params(1,xmlObject->getElementQName().toString().c_str())
102             );
103
104     // Preprocess the metadata.
105     if (!m_validate) {
106         try {
107             SchemaValidators.validate(xmlObject.get());
108         }
109         catch (exception& ex) {
110             m_log.error("metadata intance failed manual schema validation checking: ", ex.what());
111             throw MetadataException("Metadata instance failed manual schema validation checking.");
112         }
113     }
114     doFilters(*xmlObject.get());
115     xmlObject->releaseThisAndChildrenDOM();
116     xmlObject->setDocument(NULL);
117
118     // Swap it in.
119     bool changed = m_object!=NULL;
120     delete m_object;
121     m_object = xmlObject.release();
122     index();
123     if (changed)
124         emitChangeEvent();
125
126     // If a remote resource, reduce the reload interval if cacheDuration is set.
127     if (!m_local) {
128         const CacheableSAMLObject* cacheable = dynamic_cast<const CacheableSAMLObject*>(m_object);
129         if (cacheable && cacheable->getCacheDuration() && cacheable->getCacheDurationEpoch() < m_maxCacheDuration)
130             m_reloadInterval = cacheable->getCacheDurationEpoch();
131         else
132             m_reloadInterval = m_maxCacheDuration;
133     }
134
135     return make_pair(false,(DOMElement*)NULL);
136 }
137
138 void XMLMetadataProvider::index()
139 {
140     time_t exp = SAMLTIME_MAX;
141
142     clearDescriptorIndex();
143     EntitiesDescriptor* group=dynamic_cast<EntitiesDescriptor*>(m_object);
144     if (group) {
145         if (!m_local && group->getCacheDuration())
146             exp = time(NULL) + group->getCacheDurationEpoch();
147         AbstractMetadataProvider::index(group, exp);
148         return;
149     }
150     EntityDescriptor* site=dynamic_cast<EntityDescriptor*>(m_object);
151     if (!m_local && site->getCacheDuration())
152         exp = time(NULL) + site->getCacheDurationEpoch();
153     AbstractMetadataProvider::index(site, exp);
154 }