Remove non-xstring code.
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / BlacklistMetadataFilter.cpp
1 /*
2  *  Copyright 2001-2009 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  * BlacklistMetadataFilter.cpp
19  * 
20  * Removes blacklisted entities from a metadata instance
21  */
22
23 #include "internal.h"
24 #include "saml2/metadata/Metadata.h"
25 #include "saml2/metadata/MetadataFilter.h"
26
27 #include <xmltooling/logging.h>
28 #include <xmltooling/util/NDC.h>
29
30 using namespace opensaml::saml2md;
31 using namespace xmltooling::logging;
32 using namespace xmltooling;
33 using namespace std;
34
35 namespace opensaml {
36     namespace saml2md {
37                 
38         class SAML_DLLLOCAL BlacklistMetadataFilter : public MetadataFilter
39         {
40         public:
41             BlacklistMetadataFilter(const DOMElement* e);
42             ~BlacklistMetadataFilter() {}
43             
44             const char* getId() const { return BLACKLIST_METADATA_FILTER; }
45             void doFilter(XMLObject& xmlObject) const;
46
47         private:
48             void doFilter(EntitiesDescriptor& entities) const;
49             
50             bool found(const XMLCh* id) const {
51                 if (!id)
52                     return false;
53                 return m_set.count(id)==1;
54             }
55
56             set<xstring> m_set;
57         }; 
58
59         MetadataFilter* SAML_DLLLOCAL BlacklistMetadataFilterFactory(const DOMElement* const & e)
60         {
61             return new BlacklistMetadataFilter(e);
62         }
63
64     };
65 };
66
67 static const XMLCh Exclude[] =  UNICODE_LITERAL_7(E,x,c,l,u,d,e);
68
69 BlacklistMetadataFilter::BlacklistMetadataFilter(const DOMElement* e)
70 {
71     e = XMLHelper::getFirstChildElement(e);
72     while (e) {
73         if (XMLString::equals(e->getLocalName(), Exclude) && e->hasChildNodes()) {
74             m_set.insert(e->getFirstChild()->getNodeValue());
75         }
76         e = XMLHelper::getNextSiblingElement(e);
77     }
78 }
79
80 void BlacklistMetadataFilter::doFilter(XMLObject& xmlObject) const
81 {
82 #ifdef _DEBUG
83     NDC ndc("doFilter");
84 #endif
85     
86     try {
87         EntitiesDescriptor& entities = dynamic_cast<EntitiesDescriptor&>(xmlObject);
88         if (found(entities.getName()))
89             throw MetadataFilterException("BlacklistMetadataFilter instructed to filter the root/only group in the metadata.");
90         doFilter(entities);
91         return;
92     }
93     catch (bad_cast) {
94     }
95
96     try {
97         EntityDescriptor& entity = dynamic_cast<EntityDescriptor&>(xmlObject);
98         if (found(entity.getEntityID()))
99             throw MetadataFilterException("BlacklistMetadataFilter instructed to filter the root/only entity in the metadata.");
100         return;
101     }
102     catch (bad_cast) {
103     }
104      
105     throw MetadataFilterException("BlacklistMetadataFilter was given an improper metadata instance to filter.");
106 }
107
108 void BlacklistMetadataFilter::doFilter(EntitiesDescriptor& entities) const
109 {
110     Category& log=Category::getInstance(SAML_LOGCAT".MetadataFilter.Blacklist");
111     
112     VectorOf(EntityDescriptor) v=entities.getEntityDescriptors();
113     for (VectorOf(EntityDescriptor)::size_type i=0; i<v.size(); ) {
114         const XMLCh* id=v[i]->getEntityID();
115         if (found(id)) {
116             auto_ptr_char id2(id);
117             log.info("filtering out blacklisted entity (%s)", id2.get());
118             v.erase(v.begin() + i);
119         }
120         else {
121             i++;
122         }
123     }
124     
125     VectorOf(EntitiesDescriptor) w=entities.getEntitiesDescriptors();
126     for (VectorOf(EntitiesDescriptor)::size_type j=0; j<w.size(); ) {
127         const XMLCh* name=w[j]->getName();
128         if (found(name)) {
129             auto_ptr_char name2(name);
130             log.info("filtering out blacklisted group (%s)", name2.get());
131             w.erase(w.begin() + j);
132         }
133         else {
134             doFilter(*(w[j]));
135             j++;
136         }
137     }
138 }