Boost changes
[shibboleth/cpp-sp.git] / shibsp / attribute / resolver / impl / ChainingAttributeExtractor.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * ChainingAttributeExtractor.cpp
23  *
24  * Chains together multiple AttributeExtractor plugins.
25  */
26
27 #include "internal.h"
28 #include "Application.h"
29 #include "ServiceProvider.h"
30 #include "attribute/Attribute.h"
31 #include "attribute/resolver/AttributeExtractor.h"
32
33 #include <boost/ptr_container/ptr_vector.hpp>
34 #include <xercesc/util/XMLUniDefs.hpp>
35 #include <xmltooling/util/XMLHelper.h>
36
37 using namespace shibsp;
38 using namespace opensaml::saml2md;
39 using namespace xmltooling;
40 using namespace boost;
41 using namespace std;
42
43 namespace shibsp {
44
45     class SHIBSP_DLLLOCAL ChainingAttributeExtractor : public AttributeExtractor
46     {
47     public:
48         ChainingAttributeExtractor(const DOMElement* e);
49         virtual ~ChainingAttributeExtractor() {}
50
51         Lockable* lock() {
52             return this;
53         }
54         void unlock() {
55         }
56
57         void extractAttributes(
58             const Application& application,
59             const RoleDescriptor* issuer,
60             const XMLObject& xmlObject,
61             vector<Attribute*>& attributes
62             ) const {
63             for (ptr_vector<AttributeExtractor>::iterator i = m_extractors.begin(); i != m_extractors.end(); ++i) {
64                 Locker locker(&(*i));
65                 i->extractAttributes(application, issuer, xmlObject, attributes);
66             }
67         }
68
69         void getAttributeIds(vector<string>& attributes) const {
70             for (ptr_vector<AttributeExtractor>::iterator i = m_extractors.begin(); i != m_extractors.end(); ++i) {
71                 Locker locker(&(*i));
72                 i->getAttributeIds(attributes);
73             }
74         }
75
76         void generateMetadata(SPSSODescriptor& role) const {
77             for (ptr_vector<AttributeExtractor>::iterator i = m_extractors.begin(); i != m_extractors.end(); ++i) {
78                 Locker locker(&(*i));
79                 i->generateMetadata(role);
80             }
81         }
82
83     private:
84         mutable ptr_vector<AttributeExtractor> m_extractors;
85     };
86
87     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);
88     static const XMLCh _type[] =                UNICODE_LITERAL_4(t,y,p,e);
89
90     SHIBSP_DLLLOCAL PluginManager<AttributeExtractor,string,const DOMElement*>::Factory AssertionAttributeExtractorFactory;
91     SHIBSP_DLLLOCAL PluginManager<AttributeExtractor,string,const DOMElement*>::Factory DelegationAttributeExtractorFactory;
92     SHIBSP_DLLLOCAL PluginManager<AttributeExtractor,string,const DOMElement*>::Factory KeyDescriptorAttributeExtractorFactory;
93     SHIBSP_DLLLOCAL PluginManager<AttributeExtractor,string,const DOMElement*>::Factory XMLAttributeExtractorFactory;
94     AttributeExtractor* SHIBSP_DLLLOCAL ChainingExtractorFactory(const DOMElement* const & e)
95     {
96         return new ChainingAttributeExtractor(e);
97     }
98 };
99
100 void SHIBSP_API shibsp::registerAttributeExtractors()
101 {
102     SPConfig::getConfig().AttributeExtractorManager.registerFactory(ASSERTION_ATTRIBUTE_EXTRACTOR, AssertionAttributeExtractorFactory);
103     SPConfig::getConfig().AttributeExtractorManager.registerFactory(DELEGATION_ATTRIBUTE_EXTRACTOR, DelegationAttributeExtractorFactory);
104     SPConfig::getConfig().AttributeExtractorManager.registerFactory(KEYDESCRIPTOR_ATTRIBUTE_EXTRACTOR, KeyDescriptorAttributeExtractorFactory);
105     SPConfig::getConfig().AttributeExtractorManager.registerFactory(XML_ATTRIBUTE_EXTRACTOR, XMLAttributeExtractorFactory);
106     SPConfig::getConfig().AttributeExtractorManager.registerFactory(CHAINING_ATTRIBUTE_EXTRACTOR, ChainingExtractorFactory);
107 }
108
109 AttributeExtractor::AttributeExtractor()
110 {
111 }
112
113 AttributeExtractor::~AttributeExtractor()
114 {
115 }
116
117 void AttributeExtractor::generateMetadata(SPSSODescriptor& role) const
118 {
119 }
120
121 ChainingAttributeExtractor::ChainingAttributeExtractor(const DOMElement* e)
122 {
123     SPConfig& conf = SPConfig::getConfig();
124
125     // Load up the chain of handlers.
126     e = XMLHelper::getFirstChildElement(e, _AttributeExtractor);
127     while (e) {
128         string t(XMLHelper::getAttrString(e, nullptr, _type));
129         if (!t.empty()) {
130             try {
131                 Category::getInstance(SHIBSP_LOGCAT".AttributeExtractor.Chaining").info(
132                     "building AttributeExtractor of type (%s)...", t.c_str()
133                     );
134                 auto_ptr<AttributeExtractor> np(conf.AttributeExtractorManager.newPlugin(t.c_str(), e));
135                 m_extractors.push_back(np);
136             }
137             catch (exception& ex) {
138                 Category::getInstance(SHIBSP_LOGCAT".AttributeExtractor.Chaining").error(
139                     "caught exception processing embedded AttributeExtractor element: %s", ex.what()
140                     );
141             }
142         }
143         e = XMLHelper::getNextSiblingElement(e, _AttributeExtractor);
144     }
145 }