9e60e27411de5ff7ad0cec3cd1a52332411e4e17
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / AbstractMetadataProvider.h
1 /*
2  *  Copyright 2001-2010 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  * @file saml/saml2/metadata/AbstractMetadataProvider.h
19  * 
20  * Base class for caching metadata providers.
21  */
22
23 #ifndef __saml2_absmetadataprov_h__
24 #define __saml2_absmetadataprov_h__
25
26 #include <saml/saml2/metadata/ObservableMetadataProvider.h>
27
28 #include <ctime>
29 #include <map>
30 #include <vector>
31 #include <string>
32
33 namespace xmltooling {
34     class XMLTOOL_API Credential;
35     class XMLTOOL_API CredentialCriteria;
36     class XMLTOOL_API KeyInfoResolver;
37     class XMLTOOL_API Mutex;
38 };
39
40 namespace opensaml {
41     namespace saml2md {
42         
43         class SAML_API MetadataFilter;
44
45 #if defined (_MSC_VER)
46         #pragma warning( push )
47         #pragma warning( disable : 4251 )
48 #endif
49
50         /**
51          * Base class for caching metadata providers.
52          */
53         class SAML_API AbstractMetadataProvider : public ObservableMetadataProvider
54         {
55         protected:
56             /**
57              * Constructor.
58              * 
59              * If a DOM is supplied, a set of default logic will be used to identify
60              * and build a KeyInfoResolver plugin and install it into the provider.
61              * 
62              * The following XML content is supported:
63              * 
64              * <ul>
65              *  <li>&lt;KeyInfoResolver&gt; elements with a type attribute
66              * </ul>
67              * 
68              * XML namespaces are ignored in the processing of these elements.
69              * 
70              * @param e DOM to supply configuration for provider
71              */
72             AbstractMetadataProvider(const xercesc::DOMElement* e=nullptr);
73             
74         public:
75             virtual ~AbstractMetadataProvider();
76             
77             using MetadataProvider::getEntityDescriptor;
78             using MetadataProvider::getEntitiesDescriptor;
79
80             void emitChangeEvent() const;
81             std::pair<const EntityDescriptor*,const RoleDescriptor*> getEntityDescriptor(const Criteria& criteria) const;
82             const EntitiesDescriptor* getEntitiesDescriptor(const char* name, bool requireValidMetadata=true) const;
83             const xmltooling::Credential* resolve(const xmltooling::CredentialCriteria* criteria=nullptr) const;
84             std::vector<const xmltooling::Credential*>::size_type resolve(
85                 std::vector<const xmltooling::Credential*>& results, const xmltooling::CredentialCriteria* criteria=nullptr
86                 ) const;
87
88         protected:
89             /** Embedded KeyInfoResolver instance. */
90             xmltooling::KeyInfoResolver* m_resolver;
91
92             /**
93              * Loads an entity into the cache for faster lookup.
94              * <p>This includes processing known reverse lookup strategies for artifacts.
95              * The validUntil parameter will contain the smallest value found on output.
96              * 
97              * @param site          entity definition
98              * @param validUntil    maximum expiration time of the entity definition
99              * @param replace       true iff existing entries for the same entity should be cleared/replaced
100              */
101             virtual void indexEntity(EntityDescriptor* site, time_t& validUntil, bool replace=false) const;
102
103             /**
104              * Loads a group of entities into the cache for faster lookup.
105              * <p>The validUntil parameter will contain the smallest value found on output.
106              * 
107              * @param group         group definition
108              * @param validUntil    maximum expiration time of the group definition
109              */
110             virtual void indexGroup(EntitiesDescriptor* group, time_t& validUntil) const;
111
112             /**
113              * @deprecated
114              * Loads an entity into the cache for faster lookup.
115              * <p>This includes processing known reverse lookup strategies for artifacts.
116              * 
117              * @param site          entity definition
118              * @param validUntil    maximum expiration time of the entity definition
119              * @param replace       true iff existing entries for the same entity should be cleared/replaced
120              */
121             virtual void index(EntityDescriptor* site, time_t validUntil, bool replace=false) const;
122
123             /**
124              * @deprecated
125              * Loads a group of entities into the cache for faster lookup.
126              * 
127              * @param group         group definition
128              * @param validUntil    maximum expiration time of the group definition
129              */
130             virtual void index(EntitiesDescriptor* group, time_t validUntil) const;
131
132             /**
133              * Clear the cache of known entities and groups.
134              *
135              * @param freeSites true iff the objects cached in the site map should be freed.
136              */
137             virtual void clearDescriptorIndex(bool freeSites=false);
138
139         private:
140             typedef std::multimap<std::string,const EntityDescriptor*> sitemap_t;
141             typedef std::multimap<std::string,const EntitiesDescriptor*> groupmap_t;
142             mutable sitemap_t m_sites;
143             mutable sitemap_t m_sources;
144             mutable groupmap_t m_groups;
145
146             mutable xmltooling::Mutex* m_credentialLock;
147             typedef std::map< const RoleDescriptor*, std::vector<xmltooling::Credential*> > credmap_t;
148             mutable credmap_t m_credentialMap;
149             const credmap_t::mapped_type& resolveCredentials(const RoleDescriptor& role) const;
150         };
151
152 #if defined (_MSC_VER)
153         #pragma warning( pop )
154         #pragma warning( disable : 4251 )
155 #endif
156         
157     };
158 };
159
160 #endif /* __saml2_absmetadataprov_h__ */