d4d1bd0af029aca64d51f99ba9233bcde0eefa90
[shibboleth/sp.git] / shibsp / attribute / filtering / impl / AttributeRequesterRegexFunctor.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  * AttributeRequesterRegexFunctor.cpp
19  * 
20  * A match function that evaluates to true if the Attribute requester matches the provided regular\r
21  * expression.
22  */
23
24 #include "internal.h"
25 #include "exceptions.h"
26 #include "attribute/filtering/FilteringContext.h"
27 #include "attribute/filtering/FilterPolicyContext.h"
28
29 #include <xercesc/util/regx/RegularExpression.hpp>
30
31 namespace shibsp {
32
33     static const XMLCh options[] =  UNICODE_LITERAL_7(o,p,t,i,o,n,s);
34     static const XMLCh regex[] =    UNICODE_LITERAL_5(r,e,g,e,x);
35     
36     /**
37      * A match function that evaluates to true if the Attribute requester matches the provided regular\r
38      * expression.
39      */
40     class SHIBSP_DLLLOCAL AttributeRequesterRegexFunctor : public MatchFunctor
41     {
42         RegularExpression* m_regex;
43     public:
44         AttributeRequesterRegexFunctor(const DOMElement* e) {
45             const XMLCh* r = e ? e->getAttributeNS(NULL,regex) : NULL;
46             if (!r || !*r)
47                 throw ConfigurationException("AttributeRequesterRegex MatchFunctor requires non-empty regex attribute.");
48             try {
49                 m_regex = new RegularExpression(r, e->getAttributeNS(NULL,options));
50             }
51             catch (XMLException& ex) {\r
52                 xmltooling::auto_ptr_char temp(ex.getMessage());\r
53                 throw ConfigurationException(temp.get());\r
54             }\r
55         }
56
57         virtual ~AttributeRequesterRegexFunctor() {
58             delete m_regex;
59         }
60
61         bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
62             return m_regex->matches(filterContext.getAttributeRequester());
63         }
64
65         bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
66             return m_regex->matches(filterContext.getAttributeRequester());
67         }
68     };
69
70     MatchFunctor* SHIBSP_DLLLOCAL AttributeRequesterRegexFactory(const std::pair<const FilterPolicyContext*,const DOMElement*>& p)
71     {
72         return new AttributeRequesterRegexFunctor(p.second);
73     }
74
75 };