adcdee8dd802f6809bb94b9a69e759969f96d184
[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::logging;
33 using namespace xmltooling;
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         static const XMLCh requireValidUntil[] = UNICODE_LITERAL_17(r,e,q,u,i,r,e,V,a,l,i,d,U,n,t,i,l);
45
46         class SAML_DLLLOCAL XMLMetadataProvider : public AbstractMetadataProvider, public ReloadableXMLFile
47         {
48         public:
49             XMLMetadataProvider(const DOMElement* e)
50                 : AbstractMetadataProvider(e), ReloadableXMLFile(e, Category::getInstance(SAML_LOGCAT".MetadataProvider.XML")),
51                     m_object(NULL), m_requireValidUntil(false) {
52                 const XMLCh* flag = e ? e->getAttributeNS(NULL,requireValidUntil) : NULL;
53                 m_requireValidUntil = (flag && (*flag == chLatin_t || *flag == chDigit_1));
54             }
55             virtual ~XMLMetadataProvider() {
56                 delete m_object;
57             }
58
59             void init() {
60                 load(); // guarantees an exception or the metadata is loaded
61             }
62
63             const XMLObject* getMetadata() const {
64                 return m_object;
65             }
66
67         protected:
68             pair<bool,DOMElement*> load();
69
70         private:
71             using AbstractMetadataProvider::index;
72             void index();
73
74             XMLObject* m_object;
75             bool m_requireValidUntil;
76         };
77
78         MetadataProvider* SAML_DLLLOCAL XMLMetadataProviderFactory(const DOMElement* const & e)
79         {
80             return new XMLMetadataProvider(e);
81         }
82
83     };
84 };
85
86 #if defined (_MSC_VER)
87     #pragma warning( pop )
88 #endif
89
90 pair<bool,DOMElement*> XMLMetadataProvider::load()
91 {
92     // Load from source using base class.
93     pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
94
95     // If we own it, wrap it for now.
96     XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);
97
98     // Unmarshall objects, binding the document.
99     auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(raw.second, true));
100     docjanitor.release();
101
102     if (!dynamic_cast<const EntitiesDescriptor*>(xmlObject.get()) && !dynamic_cast<const EntityDescriptor*>(xmlObject.get()))
103         throw MetadataException(
104             "Root of metadata instance not recognized: $1", params(1,xmlObject->getElementQName().toString().c_str())
105             );
106
107     if (m_requireValidUntil) {
108         const TimeBoundSAMLObject* tbo = dynamic_cast<const TimeBoundSAMLObject*>(xmlObject.get());
109         if (!tbo || tbo->getValidUntil() == NULL)
110             throw MetadataException("Root of metadata instance does not have validUntil atttribute.");
111     }
112
113     // Preprocess the metadata.
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     return make_pair(false,(DOMElement*)NULL);
126 }
127
128 void XMLMetadataProvider::index()
129 {
130     clearDescriptorIndex();
131     EntitiesDescriptor* group=dynamic_cast<EntitiesDescriptor*>(m_object);
132     if (group) {
133         AbstractMetadataProvider::index(group, SAMLTIME_MAX);
134         return;
135     }
136     EntityDescriptor* site=dynamic_cast<EntityDescriptor*>(m_object);
137     AbstractMetadataProvider::index(site, SAMLTIME_MAX);
138 }