Handle 32-bit time size
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / MetadataProvider.cpp
1 /*
2  *  Copyright 2001-2006 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  * MetadataProvider.cpp
19  * 
20  * Registration of factories for built-in providers
21  */
22
23 #include "internal.h"
24 #include "saml2/metadata/MetadataProvider.h"
25
26 #include <log4cpp/Category.hh>
27 #include <xmltooling/util/NDC.h>
28
29 using namespace opensaml::saml2md;
30 using namespace xmltooling;
31 using namespace log4cpp;
32 using namespace std;
33
34 static const XMLCh Blacklist[] =                    UNICODE_LITERAL_23(B,l,a,c,k,l,i,s,t,M,e,t,a,d,a,t,a,F,i,l,t,e,r);
35 static const XMLCh Exclude[] =                      UNICODE_LITERAL_7(E,x,c,l,u,d,e);
36 static const XMLCh Include[] =                      UNICODE_LITERAL_7(I,n,c,l,u,d,e);
37 static const XMLCh GenericMetadataFilter[] =        UNICODE_LITERAL_14(M,e,t,a,d,a,t,a,F,i,l,t,e,r);
38 static const XMLCh type[] =                         UNICODE_LITERAL_4(t,y,p,e);
39 static const XMLCh Whitelist[] =                    UNICODE_LITERAL_23(W,h,i,t,e,l,i,s,t,M,e,t,a,d,a,t,a,F,i,l,t,e,r);
40
41 MetadataProvider::MetadataProvider(const DOMElement* e)
42 {
43 #ifdef _DEBUG
44     NDC ndc("MetadataProvider");
45 #endif
46     SAMLConfig& conf=SAMLConfig::getConfig();
47     
48     // Locate any default recognized filters.
49     try {
50         DOMElement* child = e ? XMLHelper::getFirstChildElement(e) : NULL;
51         while (child) {
52             if (XMLString::equals(child->getLocalName(),GenericMetadataFilter)) {
53                 auto_ptr_char t(child->getAttributeNS(NULL,type));
54                 if (t.get())
55                     m_filters.push_back(conf.MetadataFilterManager.newPlugin(t.get(),child));
56             }
57             else if (XMLString::equals(child->getLocalName(),Whitelist)) {
58                 m_filters.push_back(conf.MetadataFilterManager.newPlugin(WHITELIST_METADATA_FILTER,child));
59             }
60             else if (XMLString::equals(child->getLocalName(),Blacklist)) {
61                 m_filters.push_back(conf.MetadataFilterManager.newPlugin(BLACKLIST_METADATA_FILTER,child));
62             }
63             else if (XMLString::equals(child->getLocalName(),Include)) {
64                 m_filters.push_back(conf.MetadataFilterManager.newPlugin(WHITELIST_METADATA_FILTER,e));
65             }
66             else if (XMLString::equals(child->getLocalName(),Exclude)) {
67                 m_filters.push_back(conf.MetadataFilterManager.newPlugin(BLACKLIST_METADATA_FILTER,e));
68             }
69             child = XMLHelper::getNextSiblingElement(child);
70         }
71     }
72     catch (XMLToolingException& ex) {
73         Category::getInstance(SAML_LOGCAT".Metadata").error("caught exception while installing filters: %s", ex.what());
74         for_each(m_filters.begin(),m_filters.end(),xmltooling::cleanup<MetadataFilter>());
75         throw;
76     }
77 }
78
79 MetadataProvider::~MetadataProvider()
80 {
81     for_each(m_filters.begin(),m_filters.end(),xmltooling::cleanup<MetadataFilter>());
82 }
83
84 void MetadataProvider::doFilters(XMLObject& xmlObject) const
85 {
86 #ifdef _DEBUG
87     NDC ndc("doFilters");
88 #endif
89     Category& log=Category::getInstance(SAML_LOGCAT".Metadata");
90     for (std::vector<MetadataFilter*>::const_iterator i=m_filters.begin(); i!=m_filters.end(); i++) {
91         log.info("applying metadata filter (%s)", (*i)->getId());
92         (*i)->doFilter(xmlObject);
93     }
94 }
95
96 namespace opensaml {
97     namespace saml2md {
98         SAML_DLLLOCAL PluginManager<MetadataProvider,const DOMElement*>::Factory FilesystemMetadataProviderFactory; 
99         SAML_DLLLOCAL PluginManager<MetadataFilter,const DOMElement*>::Factory BlacklistMetadataFilterFactory; 
100         SAML_DLLLOCAL PluginManager<MetadataFilter,const DOMElement*>::Factory WhitelistMetadataFilterFactory; 
101     };
102 };
103
104 void SAML_API opensaml::saml2md::registerMetadataProviders()
105 {
106     SAMLConfig::getConfig().MetadataProviderManager.registerFactory(FILESYSTEM_METADATA_PROVIDER, FilesystemMetadataProviderFactory);
107 }
108
109 void SAML_API opensaml::saml2md::registerMetadataFilters()
110 {
111     SAMLConfig::getConfig().MetadataFilterManager.registerFactory(BLACKLIST_METADATA_FILTER, BlacklistMetadataFilterFactory);
112     SAMLConfig::getConfig().MetadataFilterManager.registerFactory(WHITELIST_METADATA_FILTER, WhitelistMetadataFilterFactory);
113 }