4e9f674ffc97956bf8fd5572813825fec9ecd9e8
[shibboleth/cpp-sp.git] / shibsp / attribute / Attribute.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  * shibsp/attribute/Attribute.cpp
19  *
20  * A resolved attribute.
21  */
22
23 #include "internal.h"
24 #include "SPConfig.h"
25 #ifndef SHIBSP_LITE
26 # include "attribute/AttributeDecoder.h"
27 #endif
28 #include "attribute/SimpleAttribute.h"
29 #include "attribute/ScopedAttribute.h"
30 #include "attribute/NameIDAttribute.h"
31 #include "attribute/ExtensibleAttribute.h"
32 #include "util/SPConstants.h"
33
34 #include <xercesc/util/XMLUniDefs.hpp>
35
36 using namespace shibsp;
37 using namespace xmltooling;
38 using namespace std;
39
40 namespace shibsp {
41
42     SHIBSP_DLLLOCAL Attribute* SimpleAttributeFactory(DDF& in) {
43         return new SimpleAttribute(in);
44     }
45
46     SHIBSP_DLLLOCAL Attribute* ScopedAttributeFactory(DDF& in) {
47         return new ScopedAttribute(in);
48     }
49
50     SHIBSP_DLLLOCAL Attribute* NameIDAttributeFactory(DDF& in) {
51         return new NameIDAttribute(in);
52     }
53
54     SHIBSP_DLLLOCAL Attribute* ExtensibleAttributeFactory(DDF& in) {
55         return new ExtensibleAttribute(in);
56     }
57
58 #ifndef SHIBSP_LITE
59     SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory StringAttributeDecoderFactory;
60     SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory ScopedAttributeDecoderFactory;
61     SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory NameIDAttributeDecoderFactory;
62     SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory NameIDFromScopedAttributeDecoderFactory;
63     SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory KeyInfoAttributeDecoderFactory;
64     SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory DOMAttributeDecoderFactory;
65
66     static const XMLCh _StringAttributeDecoder[] = UNICODE_LITERAL_22(S,t,r,i,n,g,A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
67     static const XMLCh _ScopedAttributeDecoder[] = UNICODE_LITERAL_22(S,c,o,p,e,d,A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
68     static const XMLCh _NameIDAttributeDecoder[] = UNICODE_LITERAL_22(N,a,m,e,I,D,A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
69     static const XMLCh _NameIDFromScopedAttributeDecoder[] = UNICODE_LITERAL_32(N,a,m,e,I,D,F,r,o,m,S,c,o,p,e,d,A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
70     static const XMLCh _KeyInfoAttributeDecoder[] =UNICODE_LITERAL_23(K,e,y,I,n,f,o,A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
71     static const XMLCh _DOMAttributeDecoder[] =    UNICODE_LITERAL_19(D,O,M,A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
72
73     static const XMLCh caseSensitive[] =           UNICODE_LITERAL_13(c,a,s,e,S,e,n,s,i,t,i,v,e);
74 #endif
75 };
76
77 #ifndef SHIBSP_LITE
78 xmltooling::QName shibsp::StringAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _StringAttributeDecoder);
79 xmltooling::QName shibsp::ScopedAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _ScopedAttributeDecoder);
80 xmltooling::QName shibsp::NameIDAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _NameIDAttributeDecoder);
81 xmltooling::QName shibsp::NameIDFromScopedAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _NameIDFromScopedAttributeDecoder);
82 xmltooling::QName shibsp::KeyInfoAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _KeyInfoAttributeDecoder);
83 xmltooling::QName shibsp::DOMAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _DOMAttributeDecoder);
84
85 void shibsp::registerAttributeDecoders()
86 {
87     SPConfig& conf = SPConfig::getConfig();
88     conf.AttributeDecoderManager.registerFactory(StringAttributeDecoderType, StringAttributeDecoderFactory);
89     conf.AttributeDecoderManager.registerFactory(ScopedAttributeDecoderType, ScopedAttributeDecoderFactory);
90     conf.AttributeDecoderManager.registerFactory(NameIDAttributeDecoderType, NameIDAttributeDecoderFactory);
91     conf.AttributeDecoderManager.registerFactory(NameIDFromScopedAttributeDecoderType, NameIDFromScopedAttributeDecoderFactory);
92     conf.AttributeDecoderManager.registerFactory(KeyInfoAttributeDecoderType, KeyInfoAttributeDecoderFactory);
93     conf.AttributeDecoderManager.registerFactory(DOMAttributeDecoderType, DOMAttributeDecoderFactory);
94 }
95
96 AttributeDecoder::AttributeDecoder(const DOMElement *e) : m_caseSensitive(true)
97 {
98     if (e) {
99         const XMLCh* flag = e->getAttributeNS(NULL,caseSensitive);
100         if (flag && (*flag == chLatin_f || *flag == chDigit_0))
101             m_caseSensitive = false;
102     }
103 }
104 #endif
105
106 void shibsp::registerAttributeFactories()
107 {
108     Attribute::registerFactory("", SimpleAttributeFactory);
109     Attribute::registerFactory("Simple", SimpleAttributeFactory);
110     Attribute::registerFactory("Scoped", ScopedAttributeFactory);
111     Attribute::registerFactory("NameID", NameIDAttributeFactory);
112     Attribute::registerFactory("Extensible", ExtensibleAttributeFactory);
113 }
114
115 map<string,Attribute::AttributeFactory*> Attribute::m_factoryMap;
116
117 Attribute* Attribute::unmarshall(DDF& in)
118 {
119     map<string,AttributeFactory*>::const_iterator i = m_factoryMap.find(in.name() ? in.name() : "");
120     if (i == m_factoryMap.end())
121         throw AttributeException("No registered factory for Attribute of type ($1).", params(1,in.name()));
122     return (i->second)(in);
123 }