Dynamic metadata provider implementation.
[shibboleth/opensaml2.git] / saml / saml2 / metadata / impl / DynamicMetadataProvider.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  * DynamicMetadataProvider.cpp
19  * 
20  * Base class for caching metadata providers.
21  */
22
23 #include "internal.h"
24 #include "saml2/metadata/Metadata.h"
25 #include "saml2/metadata/DynamicMetadataProvider.h"
26
27 #include <xercesc/framework/Wrapper4InputSource.hpp>\r
28 #include <xercesc/framework/URLInputSource.hpp>\r
29 #include <xercesc/util/XMLUniDefs.hpp>\r
30 #include <xmltooling/logging.h>\r
31 #include <xmltooling/util/XMLHelper.h>
32
33 using namespace opensaml::saml2md;
34 using namespace xmltooling::logging;
35 using namespace xmltooling;
36 using namespace std;
37
38 static const XMLCh validate[] = UNICODE_LITERAL_8(v,a,l,i,d,a,t,e);\r
39
40 namespace opensaml {
41     namespace saml2md {
42         MetadataProvider* SAML_DLLLOCAL DynamicMetadataProviderFactory(const DOMElement* const & e)
43         {
44             return new DynamicMetadataProvider(e);
45         }
46     };
47 };
48
49 DynamicMetadataProvider::DynamicMetadataProvider(const DOMElement* e)
50     : AbstractMetadataProvider(e), m_lock(RWLock::create())
51 {
52     const XMLCh* flag=e ? e->getAttributeNS(NULL,validate) : NULL;\r
53     m_validate=(XMLString::equals(flag,xmlconstants::XML_TRUE) || XMLString::equals(flag,xmlconstants::XML_ONE));\r
54 }
55
56 DynamicMetadataProvider::~DynamicMetadataProvider()
57 {
58     // Each entity in the map is unique (no multimap semantics), so this is safe.
59     clearDescriptorIndex(true);
60     delete m_lock;
61 }
62
63 const EntityDescriptor* DynamicMetadataProvider::getEntityDescriptor(const char* name, bool strict) const
64 {
65     // Check cache while holding the read lock.
66     const EntityDescriptor* entity = AbstractMetadataProvider::getEntityDescriptor(name, strict);
67     if (entity)
68         return entity;
69
70     Category& log = Category::getInstance(SAML_LOGCAT".MetadataProvider.Dynamic");
71     log.info("resolving metadata for (%s)", name);
72
73     // Try resolving it.
74     auto_ptr<EntityDescriptor> entity2(resolve(name));
75
76     // Filter it, which may throw.
77     doFilters(*entity2.get());
78
79     log.info("caching resolved metadata for (%s)", name);
80
81     // Translate cacheDuration into validUntil.
82     if (entity2->getCacheDuration())
83         entity2->setValidUntil(time(NULL) + entity2->getCacheDurationEpoch());
84
85     // Upgrade our lock so we can cache the new metadata.
86     m_lock->unlock();
87     m_lock->wrlock();
88
89     // Notify observers.
90     emitChangeEvent();
91
92     // Make sure we clear out any existing copies, including stale metadata or if somebody snuck in.
93     index(entity2.release(), SAMLTIME_MAX, true);
94
95     // Downgrade back to a read lock.
96     m_lock->unlock();
97     m_lock->rdlock();
98
99     // Rinse and repeat.
100     return getEntityDescriptor(name, strict);
101 }
102
103 EntityDescriptor* DynamicMetadataProvider::resolve(const char* entityID) const
104 {
105     try {\r
106         DOMDocument* doc=NULL;\r
107         auto_ptr_XMLCh widenit(entityID);\r
108         URLInputSource src(widenit.get());\r
109         Wrapper4InputSource dsrc(&src,false);\r
110         if (m_validate)\r
111             doc=XMLToolingConfig::getConfig().getValidatingParser().parse(dsrc);\r
112         else\r
113             doc=XMLToolingConfig::getConfig().getParser().parse(dsrc);\r
114 \r
115         // Wrap the document for now.
116         XercesJanitor<DOMDocument> docjanitor(doc);
117                 
118         // Unmarshall objects, binding the document.
119         auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
120         docjanitor.release();
121
122         // Make sure it's metadata.
123         EntityDescriptor* entity = dynamic_cast<EntityDescriptor*>(xmlObject.get());
124         if (!entity) {
125             throw MetadataException(
126                 "Root of metadata instance not recognized: $1", params(1,xmlObject->getElementQName().toString().c_str())
127                 );
128         }
129         xmlObject.release();\r
130         return entity;\r
131     }\r
132     catch (XMLException& e) {\r
133         auto_ptr_char msg(e.getMessage());\r
134         Category::getInstance(SAML_LOGCAT".MetadataProvider.Dynamic").error(\r
135             "Xerces error while resolving entityID (%s): %s", entityID, msg.get()\r
136             );\r
137         throw MetadataException(msg.get());\r
138     }\r
139     catch (exception& e) {\r
140         Category::getInstance(SAML_LOGCAT".MetadataProvider.Dynamic").error(\r
141             "error while resolving entityID (%s): %s", entityID, e.what()\r
142             );\r
143         throw;\r
144     }\r
145 }