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 * DynamicMetadataProvider.cpp
20 * Base class for caching metadata providers.
24 #include "binding/SAMLArtifact.h"
25 #include "saml2/metadata/Metadata.h"
26 #include "saml2/metadata/DynamicMetadataProvider.h"
28 #include <xercesc/framework/Wrapper4InputSource.hpp>
29 #include <xercesc/framework/URLInputSource.hpp>
30 #include <xercesc/util/XMLUniDefs.hpp>
31 #include <xmltooling/logging.h>
32 #include <xmltooling/util/XMLHelper.h>
34 using namespace opensaml::saml2md;
35 using namespace xmltooling::logging;
36 using namespace xmltooling;
39 static const XMLCh validate[] = UNICODE_LITERAL_8(v,a,l,i,d,a,t,e);
43 MetadataProvider* SAML_DLLLOCAL DynamicMetadataProviderFactory(const DOMElement* const & e)
45 return new DynamicMetadataProvider(e);
50 DynamicMetadataProvider::DynamicMetadataProvider(const DOMElement* e)
51 : AbstractMetadataProvider(e), m_lock(RWLock::create())
53 const XMLCh* flag=e ? e->getAttributeNS(NULL,validate) : NULL;
54 m_validate=(XMLString::equals(flag,xmlconstants::XML_TRUE) || XMLString::equals(flag,xmlconstants::XML_ONE));
57 DynamicMetadataProvider::~DynamicMetadataProvider()
59 // Each entity in the map is unique (no multimap semantics), so this is safe.
60 clearDescriptorIndex(true);
64 pair<const EntityDescriptor*,const RoleDescriptor*> DynamicMetadataProvider::getEntityDescriptor(const Criteria& criteria) const
66 // Check cache while holding the read lock.
67 pair<const EntityDescriptor*,const RoleDescriptor*> entity = AbstractMetadataProvider::getEntityDescriptor(criteria);
68 if (entity.first) // even if the role isn't found, we're done
72 if (criteria.entityID_ascii)
73 name = criteria.entityID_ascii;
74 else if (criteria.entityID_unicode) {
75 auto_ptr_char temp(criteria.entityID_unicode);
78 else if (criteria.artifact)
79 name = criteria.artifact->getSource();
83 Category& log = Category::getInstance(SAML_LOGCAT".MetadataProvider.Dynamic");
84 log.info("resolving metadata for (%s)", name.c_str());
87 auto_ptr<EntityDescriptor> entity2(resolve(name.c_str()));
89 // Filter it, which may throw.
90 doFilters(*entity2.get());
92 log.info("caching resolved metadata for (%s)", name.c_str());
94 // Translate cacheDuration into validUntil.
95 if (entity2->getCacheDuration())
96 entity2->setValidUntil(time(NULL) + entity2->getCacheDurationEpoch());
98 // Upgrade our lock so we can cache the new metadata.
105 // Make sure we clear out any existing copies, including stale metadata or if somebody snuck in.
106 index(entity2.release(), SAMLTIME_MAX, true);
108 // Downgrade back to a read lock.
113 return getEntityDescriptor(criteria);
116 EntityDescriptor* DynamicMetadataProvider::resolve(const char* entityID) const
119 DOMDocument* doc=NULL;
120 auto_ptr_XMLCh widenit(entityID);
121 URLInputSource src(widenit.get());
122 Wrapper4InputSource dsrc(&src,false);
124 doc=XMLToolingConfig::getConfig().getValidatingParser().parse(dsrc);
126 doc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
128 // Wrap the document for now.
129 XercesJanitor<DOMDocument> docjanitor(doc);
131 // Unmarshall objects, binding the document.
132 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
133 docjanitor.release();
135 // Make sure it's metadata.
136 EntityDescriptor* entity = dynamic_cast<EntityDescriptor*>(xmlObject.get());
138 throw MetadataException(
139 "Root of metadata instance not recognized: $1", params(1,xmlObject->getElementQName().toString().c_str())
145 catch (XMLException& e) {
146 auto_ptr_char msg(e.getMessage());
147 Category::getInstance(SAML_LOGCAT".MetadataProvider.Dynamic").error(
148 "Xerces error while resolving entityID (%s): %s", entityID, msg.get()
150 throw MetadataException(msg.get());
152 catch (exception& e) {
153 Category::getInstance(SAML_LOGCAT".MetadataProvider.Dynamic").error(
154 "error while resolving entityID (%s): %s", entityID, e.what()