Add ability to generate additional metadata content based on config.
[shibboleth/cpp-sp.git] / shibsp / attribute / resolver / impl / ChainingAttributeExtractor.cpp
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  * ChainingAttributeExtractor.cpp
19  *
20  * Chains together multiple AttributeExtractor plugins.
21  */
22
23 #include "internal.h"
24 #include "Application.h"
25 #include "ServiceProvider.h"
26 #include "attribute/Attribute.h"
27 #include "attribute/resolver/AttributeExtractor.h"
28
29 #include <xercesc/util/XMLUniDefs.hpp>
30 #include <xmltooling/util/XMLHelper.h>
31
32 using namespace shibsp;
33 using namespace opensaml::saml2md;
34 using namespace xmltooling;
35 using namespace std;
36
37 namespace shibsp {
38
39     class SHIBSP_DLLLOCAL ChainingAttributeExtractor : public AttributeExtractor
40     {
41     public:
42         ChainingAttributeExtractor(const DOMElement* e);
43         virtual ~ChainingAttributeExtractor() {
44             for_each(m_extractors.begin(), m_extractors.end(), xmltooling::cleanup<AttributeExtractor>());
45         }
46
47         Lockable* lock() {
48             return this;
49         }
50         void unlock() {
51         }
52
53         void extractAttributes(
54             const Application& application,
55             const RoleDescriptor* issuer,
56             const XMLObject& xmlObject,
57             vector<Attribute*>& attributes
58             ) const;
59
60         void getAttributeIds(vector<string>& attributes) const {
61             for (vector<AttributeExtractor*>::const_iterator i=m_extractors.begin(); i!=m_extractors.end(); ++i) {
62                 Locker locker(*i);
63                 (*i)->getAttributeIds(attributes);
64             }
65         }
66
67         void generateMetadata(SPSSODescriptor& role) const {
68             for (vector<AttributeExtractor*>::const_iterator i=m_extractors.begin(); i!=m_extractors.end(); ++i) {
69                 Locker locker(*i);
70                 (*i)->generateMetadata(role);
71             }
72         }
73
74     private:
75         vector<AttributeExtractor*> m_extractors;
76     };
77
78     static const XMLCh _AttributeExtractor[] =  UNICODE_LITERAL_18(A,t,t,r,i,b,u,t,e,E,x,t,r,a,c,t,o,r);
79     static const XMLCh _type[] =                UNICODE_LITERAL_4(t,y,p,e);
80
81     SHIBSP_DLLLOCAL PluginManager<AttributeExtractor,string,const DOMElement*>::Factory DelegationAttributeExtractorFactory;
82     SHIBSP_DLLLOCAL PluginManager<AttributeExtractor,string,const DOMElement*>::Factory KeyDescriptorAttributeExtractorFactory;
83     SHIBSP_DLLLOCAL PluginManager<AttributeExtractor,string,const DOMElement*>::Factory XMLAttributeExtractorFactory;
84     AttributeExtractor* SHIBSP_DLLLOCAL ChainingExtractorFactory(const DOMElement* const & e)
85     {
86         return new ChainingAttributeExtractor(e);
87     }
88 };
89
90 void SHIBSP_API shibsp::registerAttributeExtractors()
91 {
92     SPConfig::getConfig().AttributeExtractorManager.registerFactory(DELEGATION_ATTRIBUTE_EXTRACTOR, DelegationAttributeExtractorFactory);
93     SPConfig::getConfig().AttributeExtractorManager.registerFactory(KEYDESCRIPTOR_ATTRIBUTE_EXTRACTOR, KeyDescriptorAttributeExtractorFactory);
94     SPConfig::getConfig().AttributeExtractorManager.registerFactory(XML_ATTRIBUTE_EXTRACTOR, XMLAttributeExtractorFactory);
95     SPConfig::getConfig().AttributeExtractorManager.registerFactory(CHAINING_ATTRIBUTE_EXTRACTOR, ChainingExtractorFactory);
96 }
97
98 AttributeExtractor::AttributeExtractor()
99 {
100 }
101
102 AttributeExtractor::~AttributeExtractor()
103 {
104 }
105
106 void AttributeExtractor::generateMetadata(SPSSODescriptor& role) const
107 {
108 }
109
110 ChainingAttributeExtractor::ChainingAttributeExtractor(const DOMElement* e)
111 {
112     SPConfig& conf = SPConfig::getConfig();
113
114     // Load up the chain of handlers.
115     e = XMLHelper::getFirstChildElement(e, _AttributeExtractor);
116     while (e) {
117         string t(XMLHelper::getAttrString(e, nullptr, _type));
118         if (!t.empty()) {
119             try {
120                 Category::getInstance(SHIBSP_LOGCAT".AttributeExtractor.Chaining").info(
121                     "building AttributeExtractor of type (%s)...", t.c_str()
122                     );
123                 m_extractors.push_back(conf.AttributeExtractorManager.newPlugin(t.c_str(), e));
124             }
125             catch (exception& ex) {
126                 Category::getInstance(SHIBSP_LOGCAT".AttributeExtractor.Chaining").error(
127                     "caught exception processing embedded AttributeExtractor element: %s", ex.what()
128                     );
129             }
130         }
131         e = XMLHelper::getNextSiblingElement(e, _AttributeExtractor);
132     }
133 }
134
135 void ChainingAttributeExtractor::extractAttributes(
136     const Application& application, const RoleDescriptor* issuer, const XMLObject& xmlObject, vector<Attribute*>& attributes
137     ) const
138 {
139     for (vector<AttributeExtractor*>::const_iterator i=m_extractors.begin(); i!=m_extractors.end(); ++i) {
140         Locker locker(*i);
141         (*i)->extractAttributes(application, issuer, xmlObject, attributes);
142     }
143 }