2 * Copyright 2001-2007 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * XMLMetadataProvider.cpp
20 * Supplies metadata from an XML file
24 #include "saml2/metadata/Metadata.h"
25 #include "saml2/metadata/MetadataFilter.h"
26 #include "saml2/metadata/AbstractMetadataProvider.h"
28 #include <xmltooling/util/NDC.h>
29 #include <xmltooling/util/ReloadableXMLFile.h>
30 #include <xmltooling/validation/ValidatorSuite.h>
32 using namespace opensaml::saml2md;
33 using namespace xmltooling::logging;
34 using namespace xmltooling;
37 #if defined (_MSC_VER)
38 #pragma warning( push )
39 #pragma warning( disable : 4250 )
45 class SAML_DLLLOCAL XMLMetadataProvider : public AbstractMetadataProvider, public ReloadableXMLFile
48 XMLMetadataProvider(const DOMElement* e)
49 : AbstractMetadataProvider(e), ReloadableXMLFile(e, Category::getInstance(SAML_LOGCAT".MetadataProvider.XML")),
50 m_object(NULL), m_maxCacheDuration(m_reloadInterval) {
52 virtual ~XMLMetadataProvider() {
57 load(); // guarantees an exception or the metadata is loaded
60 const XMLObject* getMetadata() const {
65 pair<bool,DOMElement*> load();
68 using AbstractMetadataProvider::index;
72 time_t m_maxCacheDuration;
75 MetadataProvider* SAML_DLLLOCAL XMLMetadataProviderFactory(const DOMElement* const & e)
77 return new XMLMetadataProvider(e);
83 #if defined (_MSC_VER)
84 #pragma warning( pop )
87 pair<bool,DOMElement*> XMLMetadataProvider::load()
89 // Load from source using base class.
90 pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
92 // If we own it, wrap it for now.
93 XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);
95 // Unmarshall objects, binding the document.
96 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(raw.second, true));
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())
104 // Preprocess the metadata (even if we schema-validated).
106 SchemaValidators.validate(xmlObject.get());
108 catch (exception& ex) {
109 m_log.error("metadata intance failed manual validation checking: %s", ex.what());
110 throw MetadataException("Metadata instance failed manual validation checking.");
113 doFilters(*xmlObject.get());
114 xmlObject->releaseThisAndChildrenDOM();
115 xmlObject->setDocument(NULL);
118 bool changed = m_object!=NULL;
120 m_object = xmlObject.release();
125 // If a remote resource, reduce the reload interval if cacheDuration is set.
127 const CacheableSAMLObject* cacheable = dynamic_cast<const CacheableSAMLObject*>(m_object);
128 if (cacheable && cacheable->getCacheDuration() && cacheable->getCacheDurationEpoch() < m_maxCacheDuration)
129 m_reloadInterval = cacheable->getCacheDurationEpoch();
131 m_reloadInterval = m_maxCacheDuration;
134 return make_pair(false,(DOMElement*)NULL);
137 void XMLMetadataProvider::index()
139 time_t exp = SAMLTIME_MAX;
141 clearDescriptorIndex();
142 EntitiesDescriptor* group=dynamic_cast<EntitiesDescriptor*>(m_object);
144 if (!m_local && group->getCacheDuration())
145 exp = time(NULL) + group->getCacheDurationEpoch();
146 AbstractMetadataProvider::index(group, exp);
149 EntityDescriptor* site=dynamic_cast<EntityDescriptor*>(m_object);
150 if (!m_local && site->getCacheDuration())
151 exp = time(NULL) + site->getCacheDurationEpoch();
152 AbstractMetadataProvider::index(site, exp);