Fix old namespace defaults.
[shibboleth/cpp-sp.git] / shibsp / attribute / NameIDAttribute.h
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  * @file shibsp/attribute/NameIDAttribute.h
19  * 
20  * An Attribute whose values are relations of a value and a scope.
21  */
22
23 #ifndef __shibsp_nameidattr_h__
24 #define __shibsp_nameidattr_h__
25
26 #include <shibsp/attribute/Attribute.h>
27 #include <xmltooling/exceptions.h>
28
29 namespace shibsp {
30
31 #if defined (_MSC_VER)
32     #pragma warning( push )
33     #pragma warning( disable : 4251 )
34 #endif
35
36     /** Default serialization format for NameIDs */
37     #define DEFAULT_NAMEID_FORMATTER    "$Name!!$NameQualifier!!$SPNameQualifier"
38
39     /**
40      * An Attribute whose values are derived from or mappable to a SAML NameID.
41      */
42     class SHIBSP_API NameIDAttribute : public Attribute
43     {
44     public:
45         /**
46          * Constructor
47          * 
48          * @param id    Attribute identifier
49          */
50         NameIDAttribute(const char* id, const char* formatter=DEFAULT_NAMEID_FORMATTER)
51             : Attribute(id), m_formatter(formatter) {
52         }
53
54         /**
55          * Constructs based on a remoted NameIDAttribute.
56          * 
57          * @param in    input object containing marshalled NameIDAttribute
58          */
59         NameIDAttribute(DDF& in) : Attribute(in) {
60             const char* pch;
61             DDF val = in.first().first();
62             while (val.name()) {
63                 m_values.push_back(Value());
64                 Value& v = m_values.back();
65                 v.m_Name = val.name();
66                 pch = val["Format"].string();
67                 if (pch)
68                     v.m_Format = pch;
69                 pch = val["NameQualifier"].string();
70                 if (pch)
71                     v.m_NameQualifier = pch;
72                 pch = val["SPNameQualifier"].string();
73                 if (pch)
74                     v.m_SPNameQualifier = pch;
75                 pch = val["SPProvidedID"].string();
76                 if (pch)
77                     v.m_SPProvidedID = pch;
78                 val = in.first().next();
79             }
80         }
81         
82         virtual ~NameIDAttribute() {}
83         
84         /**
85          * Holds all the fields associated with a NameID.
86          */
87         struct SHIBSP_API Value
88         {
89             std::string m_Name;
90             std::string m_Format;
91             std::string m_NameQualifier;
92             std::string m_SPNameQualifier;
93             std::string m_SPProvidedID;
94         };
95         
96         /**
97          * Returns the set of values encoded as UTF-8 strings.
98          * 
99          * <p>Each compound value is a pair containing the simple value and the scope. 
100          * 
101          * @return  a mutable vector of the values
102          */
103         std::vector<Value>& getValues() {
104             return m_values;
105         }
106         
107         size_t valueCount() const {
108             return m_values.size();
109         }
110         
111         void clearSerializedValues() {
112             m_serialized.clear();
113         }
114         
115         const std::vector<std::string>& getSerializedValues() const {
116             if (m_serialized.empty()) {
117                 for (std::vector<Value>::const_iterator i=m_values.begin(); i!=m_values.end(); ++i) {
118                     // This is kind of a hack, but it's a good way to reuse some code.
119                     xmltooling::XMLToolingException e(
120                         m_formatter,
121                         xmltooling::namedparams(
122                             5,
123                             "Name", i->m_Name.c_str(),
124                             "Format", i->m_Format.c_str(),
125                             "NameQualifier", i->m_NameQualifier.c_str(),
126                             "SPNameQualifier", i->m_SPNameQualifier.c_str(),
127                             "SPProvidedID", i->m_SPProvidedID.c_str()
128                             )
129                         );
130                     m_serialized.push_back(e.what());
131                 }
132             }
133             return Attribute::getSerializedValues();
134         }
135     
136         DDF marshall() const {
137             DDF ddf = Attribute::marshall();
138             ddf.name("NameID");
139             DDF vlist = ddf.first();
140             for (std::vector<Value>::const_iterator i=m_values.begin(); i!=m_values.end(); ++i) {
141                 DDF val = DDF(i->m_Name.c_str()).structure();
142                 if (!i->m_Format.empty())
143                     val.addmember("Format").string(i->m_Format.c_str());
144                 if (!i->m_NameQualifier.empty())
145                     val.addmember("NameQualifier").string(i->m_NameQualifier.c_str());
146                 if (!i->m_SPNameQualifier.empty())
147                     val.addmember("SPNameQualifier").string(i->m_SPNameQualifier.c_str());
148                 if (!i->m_SPProvidedID.empty())
149                     val.addmember("SPProvidedID").string(i->m_SPProvidedID.c_str());
150                 vlist.add(val);
151             }
152             return ddf;
153         }
154     
155     private:
156         std::vector<Value> m_values;
157         std::string m_formatter;
158     };
159
160 #if defined (_MSC_VER)
161     #pragma warning( pop )
162 #endif
163
164 };
165
166 #endif /* __shibsp_nameidattr_h__ */