2 * Copyright 2001-2010 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.
20 * A MatchFunctor that negates the result of a contained functor.
24 #include "exceptions.h"
25 #include "attribute/filtering/FilterPolicyContext.h"
26 #include "attribute/filtering/MatchFunctor.h"
27 #include "util/SPConstants.h"
29 #include <xercesc/util/XMLUniDefs.hpp>
30 #include <xmltooling/util/XMLHelper.h>
32 using namespace shibsp;
33 using namespace xmltooling;
39 * A MatchFunctor that negates the result of a contained functor.
41 class SHIBSP_DLLLOCAL NotMatchFunctor : public MatchFunctor
44 NotMatchFunctor(const pair<const FilterPolicyContext*,const DOMElement*>& p);
46 bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
48 return !(m_functor->evaluatePolicyRequirement(filterContext));
52 bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
54 return !(m_functor->evaluatePermitValue(filterContext, attribute, index));
59 MatchFunctor* buildFunctor(const DOMElement* e, const FilterPolicyContext* functorMap);
61 const MatchFunctor* m_functor;
64 MatchFunctor* SHIBSP_DLLLOCAL NotMatchFunctorFactory(const pair<const FilterPolicyContext*,const DOMElement*>& p)
66 return new NotMatchFunctor(p);
69 static XMLCh _id[] = UNICODE_LITERAL_2(i,d);
70 static XMLCh _ref[] = UNICODE_LITERAL_3(r,e,f);
71 static XMLCh Rule[] = UNICODE_LITERAL_4(R,u,l,e);
72 static XMLCh RuleReference[] = UNICODE_LITERAL_13(R,u,l,e,R,e,f,e,r,e,n,c,e);
75 NotMatchFunctor::NotMatchFunctor(const pair<const FilterPolicyContext*,const DOMElement*>& p) : m_functor(nullptr)
77 const DOMElement* e = XMLHelper::getFirstChildElement(p.second);
79 if (XMLHelper::isNodeNamed(e, shibspconstants::SHIB2ATTRIBUTEFILTER_MF_BASIC_NS, Rule)) {
80 m_functor = buildFunctor(e, p.first);
82 else if (XMLHelper::isNodeNamed(e, shibspconstants::SHIB2ATTRIBUTEFILTER_MF_BASIC_NS, RuleReference)) {
83 string ref = XMLHelper::getAttrString(e, nullptr, _ref);
85 multimap<string,MatchFunctor*>::const_iterator rule = p.first->getMatchFunctors().find(ref);
86 m_functor = (rule!=p.first->getMatchFunctors().end()) ? rule->second : nullptr;
92 throw ConfigurationException("No child Rule installed into NotMatchFunctor.");
95 MatchFunctor* NotMatchFunctor::buildFunctor(const DOMElement* e, const FilterPolicyContext* functorMap)
97 // We'll track and map IDs just for consistency, but don't require them or worry about dups.
98 string id = XMLHelper::getAttrString(e, nullptr, _id);
99 if (!id.empty() && functorMap->getMatchFunctors().count(id))
102 auto_ptr<xmltooling::QName> type(XMLHelper::getXSIType(e));
104 throw ConfigurationException("Child Rule found with no xsi:type.");
106 MatchFunctor* func = SPConfig::getConfig().MatchFunctorManager.newPlugin(*type.get(), make_pair(functorMap,e));
107 functorMap->getMatchFunctors().insert(multimap<string,MatchFunctor*>::value_type(id, func));