2 * Copyright 2001-2007 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * BlacklistMetadataFilter.cpp
20 * Removes blacklisted entities from a metadata instance
24 #include "saml2/metadata/Metadata.h"
25 #include "saml2/metadata/MetadataFilter.h"
27 #include <xmltooling/logging.h>
28 #include <xmltooling/util/NDC.h>
30 using namespace opensaml::saml2md;
31 using namespace xmltooling::logging;
32 using namespace xmltooling;
38 class SAML_DLLLOCAL BlacklistMetadataFilter : public MetadataFilter
41 BlacklistMetadataFilter(const DOMElement* e);
42 ~BlacklistMetadataFilter() {}
44 const char* getId() const { return BLACKLIST_METADATA_FILTER; }
45 void doFilter(XMLObject& xmlObject) const;
48 void doFilter(EntitiesDescriptor& entities) const;
50 bool found(const XMLCh* id) const {
54 return m_set.count(id)==1;
56 auto_ptr_char id2(id);
57 return m_set.count(id2.get())==1;
68 MetadataFilter* SAML_DLLLOCAL BlacklistMetadataFilterFactory(const DOMElement* const & e)
70 return new BlacklistMetadataFilter(e);
76 static const XMLCh Exclude[] = UNICODE_LITERAL_7(E,x,c,l,u,d,e);
78 BlacklistMetadataFilter::BlacklistMetadataFilter(const DOMElement* e)
80 e = XMLHelper::getFirstChildElement(e);
82 if (XMLString::equals(e->getLocalName(), Exclude) && e->hasChildNodes()) {
84 m_set.insert(e->getFirstChild()->getNodeValue());
86 auto_ptr_char id(e->getFirstChild()->getNodeValue());
87 m_set.insert(id.get());
90 e = XMLHelper::getNextSiblingElement(e);
94 void BlacklistMetadataFilter::doFilter(XMLObject& xmlObject) const
101 EntitiesDescriptor& entities = dynamic_cast<EntitiesDescriptor&>(xmlObject);
102 if (found(entities.getName()))
103 throw MetadataFilterException("BlacklistMetadataFilter instructed to filter the root/only group in the metadata.");
111 EntityDescriptor& entity = dynamic_cast<EntityDescriptor&>(xmlObject);
112 if (found(entity.getEntityID()))
113 throw MetadataFilterException("BlacklistMetadataFilter instructed to filter the root/only entity in the metadata.");
119 throw MetadataFilterException("BlacklistMetadataFilter was given an improper metadata instance to filter.");
122 void BlacklistMetadataFilter::doFilter(EntitiesDescriptor& entities) const
124 Category& log=Category::getInstance(SAML_LOGCAT".MetadataFilter.Blacklist");
126 VectorOf(EntityDescriptor) v=entities.getEntityDescriptors();
127 for (VectorOf(EntityDescriptor)::size_type i=0; i<v.size(); ) {
128 const XMLCh* id=v[i]->getEntityID();
130 auto_ptr_char id2(id);
131 log.info("filtering out blacklisted entity (%s)", id2.get());
132 v.erase(v.begin() + i);
139 VectorOf(EntitiesDescriptor) w=entities.getEntitiesDescriptors();
140 for (VectorOf(EntitiesDescriptor)::size_type j=0; j<w.size(); ) {
141 const XMLCh* name=w[j]->getName();
143 auto_ptr_char name2(name);
144 log.info("filtering out blacklisted group (%s)", name2.get());
145 w.erase(w.begin() + j);