Add chaining resolver.
[shibboleth/cpp-sp.git] / shibsp / attribute / resolver / impl / ChainingAttributeResolver.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  * ChainingAttributeResolver.cpp
19  * 
20  * Chains together multiple AttributeResolver plugins.
21  */
22
23 #include "internal.h"
24 #include "Application.h"
25 #include "ServiceProvider.h"
26 #include "attribute/Attribute.h"
27 #include "attribute/resolver/AttributeResolver.h"
28 #include "attribute/resolver/ResolutionContext.h"
29
30 #include <log4cpp/Category.hh>
31 #include <xercesc/util/XMLUniDefs.hpp>
32 #include <xmltooling/util/XMLHelper.h>
33
34 using namespace shibsp;
35 using namespace opensaml::saml2;
36 using namespace opensaml::saml2md;
37 using namespace xmltooling;
38 using namespace log4cpp;
39 using namespace std;
40
41 namespace shibsp {
42
43     struct SHIBSP_DLLLOCAL ChainingContext : public ResolutionContext
44     {
45         ~ChainingContext() {
46             for_each(m_contexts.begin(), m_contexts.end(), xmltooling::cleanup<ResolutionContext>());
47             for_each(m_attributes.begin(), m_attributes.end(), cleanup_pair<string,shibsp::Attribute>());
48             for_each(m_assertions.begin(), m_assertions.end(), xmltooling::cleanup<opensaml::Assertion>());
49         }
50
51         multimap<string,shibsp::Attribute*>& getResolvedAttributes() {
52             return m_attributes;
53         }
54         vector<opensaml::Assertion*>& getResolvedAssertions() {
55             return m_assertions;
56         }
57
58         vector<ResolutionContext*> m_contexts;
59         multimap<string,shibsp::Attribute*> m_attributes;
60         vector<opensaml::Assertion*> m_assertions;
61     };
62
63     class SHIBSP_DLLLOCAL ChainingAttributeResolver : public AttributeResolver
64     {
65     public:
66         ChainingAttributeResolver(const DOMElement* e);
67         virtual ~ChainingAttributeResolver() {
68             for_each(m_resolvers.begin(), m_resolvers.end(), xmltooling::cleanup<AttributeResolver>());
69         }
70         
71         Lockable* lock() {
72             for_each(m_resolvers.begin(), m_resolvers.end(), mem_fun(&AttributeResolver::lock));
73             return this;
74         }
75         void unlock() {
76             for_each(m_resolvers.begin(), m_resolvers.end(), mem_fun(&AttributeResolver::unlock));
77         }
78         
79         ResolutionContext* createResolutionContext(
80             const Application& application,
81             const EntityDescriptor* issuer,
82             const NameID* nameid,
83             const vector<const opensaml::Assertion*>* tokens=NULL,
84             const multimap<string,shibsp::Attribute*>* attributes=NULL
85             ) const {
86             auto_ptr<ChainingContext> chain(new ChainingContext());
87             for (vector<AttributeResolver*>::const_iterator i=m_resolvers.begin(); i!=m_resolvers.end(); ++i)
88                 chain->m_contexts.push_back((*i)->createResolutionContext(application, issuer, nameid, tokens, attributes));
89             return chain.release();
90         }
91
92         ResolutionContext* createResolutionContext(const Application& application, const Session& session) const {
93             auto_ptr<ChainingContext> chain(new ChainingContext());
94             for (vector<AttributeResolver*>::const_iterator i=m_resolvers.begin(); i!=m_resolvers.end(); ++i)
95                 chain->m_contexts.push_back((*i)->createResolutionContext(application, session));
96             return chain.release();
97         }
98
99         void resolveAttributes(ResolutionContext& ctx) const;
100
101     private:
102         vector<AttributeResolver*> m_resolvers;
103     };
104
105     static const XMLCh _AttributeResolver[] =   UNICODE_LITERAL_17(A,t,t,r,i,b,u,t,e,R,e,s,o,l,v,e,r);
106     static const XMLCh _type[] =                UNICODE_LITERAL_4(t,y,p,e);
107
108     AttributeResolver* SHIBSP_DLLLOCAL ChainingAttributeResolverFactory(const DOMElement* & e)
109     {
110         return new ChainingAttributeResolver(e);
111     }
112 };
113
114 ChainingAttributeResolver::ChainingAttributeResolver(const DOMElement* e)
115 {
116     SPConfig& conf = SPConfig::getConfig();
117
118     // Load up the chain of handlers.
119     e = e ? XMLHelper::getFirstChildElement(e, _AttributeResolver) : NULL;
120     while (e) {
121         auto_ptr_char type(e->getAttributeNS(NULL,_type));
122         if (type.get() && *(type.get())) {
123             try {
124                 m_resolvers.push_back(conf.AttributeResolverManager.newPlugin(type.get(),e));
125             }
126             catch (exception& ex) {
127                 Category::getInstance(SHIBSP_LOGCAT".AttributeResolver").error(
128                     "caught exception processing embedded AttributeResolver element: %s", ex.what()
129                     );
130             }
131         }
132         e = XMLHelper::getNextSiblingElement(e, _AttributeResolver);
133     }
134 }
135
136 void ChainingAttributeResolver::resolveAttributes(ResolutionContext& ctx) const
137 {
138     ChainingContext& chain = dynamic_cast<ChainingContext&>(ctx);
139     vector<ResolutionContext*>::iterator ictx = chain.m_contexts.begin();
140     for (vector<AttributeResolver*>::const_iterator i=m_resolvers.begin(); i!=m_resolvers.end(); ++i, ++ictx) {
141         (*i)->resolveAttributes(*(*ictx));
142         chain.getResolvedAttributes().insert((*ictx)->getResolvedAttributes().begin(), (*ictx)->getResolvedAttributes().end());
143         (*ictx)->getResolvedAttributes().clear();
144         chain.getResolvedAssertions().insert(chain.getResolvedAssertions().end(), (*ictx)->getResolvedAssertions().begin(), (*ictx)->getResolvedAssertions().end());
145         (*ictx)->getResolvedAssertions().clear();
146     }
147 }