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 * AttributeValueStringFunctor.cpp
20 * A match function that matches the value of an attribute against the specified value.
24 #include "exceptions.h"
25 #include "attribute/Attribute.h"
26 #include "attribute/filtering/FilteringContext.h"
27 #include "attribute/filtering/FilterPolicyContext.h"
29 using namespace shibsp;
34 static const XMLCh attributeID[] = UNICODE_LITERAL_11(a,t,t,r,i,b,u,t,e,I,D);
35 static const XMLCh value[] = UNICODE_LITERAL_5(v,a,l,u,e);
38 * A match function that matches the value of an attribute against the specified value.
40 class SHIBSP_DLLLOCAL AttributeValueStringFunctor : public MatchFunctor
42 xmltooling::auto_ptr_char m_value;
43 xmltooling::auto_ptr_char m_attributeID;
45 bool hasValue(const FilteringContext& filterContext) const;
46 bool matches(const Attribute& attribute, size_t index) const;
49 AttributeValueStringFunctor(const DOMElement* e)
50 : m_value(e ? e->getAttributeNS(NULL,value) : NULL), m_attributeID(e ? e->getAttributeNS(NULL,attributeID) : NULL) {
51 if (!m_value.get() || !*m_value.get())
52 throw ConfigurationException("AttributeValueString MatchFunctor requires non-empty value attribute.");
55 bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
56 if (!m_attributeID.get() || !*m_attributeID.get())
57 throw AttributeFilteringException("No attributeID specified.");
58 return hasValue(filterContext);
61 bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
62 if (!m_attributeID.get() || !*m_attributeID.get() || XMLString::equals(m_attributeID.get(), attribute.getId()))
63 return matches(attribute, index);
64 return hasValue(filterContext);
68 MatchFunctor* SHIBSP_DLLLOCAL AttributeValueStringFactory(const std::pair<const FilterPolicyContext*,const DOMElement*>& p)
70 return new AttributeValueStringFunctor(p.second);
75 bool AttributeValueStringFunctor::hasValue(const FilteringContext& filterContext) const
78 pair<multimap<string,Attribute*>::const_iterator,multimap<string,Attribute*>::const_iterator> attrs =
79 filterContext.getAttributes().equal_range(m_attributeID.get());
80 for (; attrs.first != attrs.second; ++attrs.first) {
81 count = attrs.first->second->valueCount();
82 for (size_t index = 0; index < count; ++index) {
83 if (matches(*(attrs.first->second), index))
90 bool AttributeValueStringFunctor::matches(const Attribute& attribute, size_t index) const
92 const char* val = attribute.getString(index);
95 if (attribute.isCaseSensitive())
96 return !strcmp(m_value.get(), val);
98 #ifdef HAVE_STRCASECMP
99 return !strcasecmp(m_value.get(), val);
101 return !stricmp(m_value.get(), val);