Attribute filtering code.
[shibboleth/sp.git] / shibsp / attribute / filtering / impl / ChainingAttributeFilter.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  * ChainingAttributeFilter.cpp
19  * 
20  * Chains together multiple AttributeFilter plugins.
21  */
22
23 #include "internal.h"
24 #include "attribute/filtering/AttributeFilter.h"
25 #include "attribute/filtering/FilteringContext.h"
26
27 #include <log4cpp/Category.hh>
28 #include <xercesc/util/XMLUniDefs.hpp>
29 #include <xmltooling/util/XMLHelper.h>
30
31 using namespace shibsp;
32 using namespace xmltooling;
33 using namespace log4cpp;
34 using namespace std;
35
36 namespace shibsp {
37
38     class SHIBSP_DLLLOCAL ChainingAttributeFilter : public AttributeFilter
39     {
40     public:
41         ChainingAttributeFilter(const DOMElement* e);
42         virtual ~ChainingAttributeFilter() {
43             for_each(m_filters.begin(), m_filters.end(), xmltooling::cleanup<AttributeFilter>());
44         }
45         
46         Lockable* lock() {
47             return this;
48         }
49         void unlock() {
50         }
51         
52         void filterAttributes(const FilteringContext& context, multimap<string,Attribute*>& attributes) const {
53             for (vector<AttributeFilter*>::const_iterator i=m_filters.begin(); i!=m_filters.end(); ++i) {
54                 Locker locker(*i);
55                 (*i)->filterAttributes(context, attributes);
56             }
57         }
58
59     private:
60         vector<AttributeFilter*> m_filters;
61     };
62
63     static const XMLCh _AttributeFilter[] = UNICODE_LITERAL_15(A,t,t,r,i,b,u,t,e,F,i,l,t,e,r);
64     static const XMLCh _type[] =            UNICODE_LITERAL_4(t,y,p,e);
65
66     AttributeFilter* SHIBSP_DLLLOCAL ChainingAttributeFilterFactory(const DOMElement* const & e)
67     {
68         return new ChainingAttributeFilter(e);
69     }
70 };
71
72 ChainingAttributeFilter::ChainingAttributeFilter(const DOMElement* e)
73 {
74     SPConfig& conf = SPConfig::getConfig();
75
76     // Load up the chain of handlers.
77     e = e ? XMLHelper::getFirstChildElement(e, _AttributeFilter) : NULL;
78     while (e) {
79         auto_ptr_char type(e->getAttributeNS(NULL,_type));
80         if (type.get() && *(type.get())) {
81             try {
82                 m_filters.push_back(conf.AttributeFilterManager.newPlugin(type.get(),e));
83             }
84             catch (exception& ex) {
85                 Category::getInstance(SHIBSP_LOGCAT".AttributeFilter").error(
86                     "caught exception processing embedded AttributeFilter element: %s", ex.what()
87                     );
88             }
89         }
90         e = XMLHelper::getNextSiblingElement(e, _AttributeFilter);
91     }
92 }