VS10 solution files, convert from NULL macro to nullptr.
[shibboleth/sp.git] / shibsp / attribute / DOMAttributeDecoder.cpp
1 /*
2  *  Copyright 2009-2010 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  * DOMAttributeDecoder.cpp
19  *
20  * Decodes a DOM into an ExtensibleAttribute.
21  */
22
23 #include "internal.h"
24 #include "attribute/AttributeDecoder.h"
25 #include "attribute/ExtensibleAttribute.h"
26
27 #include <saml/saml1/core/Assertions.h>
28 #include <saml/saml2/core/Assertions.h>
29 #include <xmltooling/util/XMLHelper.h>
30
31 using namespace shibsp;
32 using namespace opensaml;
33 using namespace xmltooling;
34 using namespace std;
35
36 namespace shibsp {
37     class SHIBSP_DLLLOCAL DOMAttributeDecoder : virtual public AttributeDecoder
38     {
39     public:
40         DOMAttributeDecoder(const DOMElement* e);
41         ~DOMAttributeDecoder() {}
42
43         Attribute* decode(
44             const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
45             ) const;
46
47     private:
48         DDF convert(DOMElement* e, bool nameit=true) const;
49         auto_ptr_char m_formatter;
50         map<pair<xstring,xstring>,string> m_tagMap;
51     };
52
53     AttributeDecoder* SHIBSP_DLLLOCAL DOMAttributeDecoderFactory(const DOMElement* const & e)
54     {
55         return new DOMAttributeDecoder(e);
56     }
57
58     static const XMLCh Mapping[] =  UNICODE_LITERAL_7(M,a,p,p,i,n,g);
59     static const XMLCh _from[] =    UNICODE_LITERAL_4(f,r,o,m);
60     static const XMLCh _to[] =      UNICODE_LITERAL_2(t,o);
61     static const XMLCh formatter[] =      UNICODE_LITERAL_9(f,o,r,m,a,t,t,e,r);
62 };
63
64 DOMAttributeDecoder::DOMAttributeDecoder(const DOMElement* e)
65     : AttributeDecoder(e), m_formatter(e ? e->getAttributeNS(nullptr,formatter) : nullptr)
66 {
67     Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.DOM");
68
69     e = e ? XMLHelper::getFirstChildElement(e, Mapping) : nullptr;
70     while (e) {
71         if (e->hasAttributeNS(nullptr, _from) && e->hasAttributeNS(nullptr, _to)) {
72             auto_ptr<xmltooling::QName> f(XMLHelper::getNodeValueAsQName(e->getAttributeNodeNS(nullptr, _from)));
73             auto_ptr_char t(e->getAttributeNS(nullptr, _to));
74             if (f.get() && t.get() && *t.get()) {
75                 if (log.isDebugEnabled())
76                     log.debug("mapping (%s) to (%s)", f->toString().c_str(), t.get());
77                 m_tagMap.insert(
78                     pair< const pair<xstring,xstring>,string>(
79                         pair<xstring,xstring>(f->getLocalPart(), f->hasNamespaceURI() ? f->getNamespaceURI() : &chNull),
80                         t.get()
81                         )
82                     );
83             }
84         }
85         e = XMLHelper::getNextSiblingElement(e, Mapping);
86     }
87 }
88
89 Attribute* DOMAttributeDecoder::decode(
90     const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
91     ) const
92 {
93     Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.DOM");
94
95     if (!xmlObject || !XMLString::equals(saml1::Attribute::LOCAL_NAME, xmlObject->getElementQName().getLocalPart())) {
96         log.warn("XMLObject type not recognized by DOMAttributeDecoder, no values returned");
97         return nullptr;
98     }
99
100     auto_ptr<ExtensibleAttribute> attr(new ExtensibleAttribute(ids, m_formatter.get()));
101     DDF dest = attr->getValues();
102     vector<XMLObject*>::const_iterator v,stop;
103
104     const saml2::Attribute* saml2attr = dynamic_cast<const saml2::Attribute*>(xmlObject);
105     if (saml2attr) {
106         const vector<XMLObject*>& values = saml2attr->getAttributeValues();
107         v = values.begin();
108         stop = values.end();
109         if (log.isDebugEnabled()) {
110             auto_ptr_char n(saml2attr->getName());
111             log.debug(
112                 "decoding ExtensibleAttribute (%s) from SAML 2 Attribute (%s) with %lu value(s)",
113                 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
114                 );
115         }
116     }
117     else {
118         const saml1::Attribute* saml1attr = dynamic_cast<const saml1::Attribute*>(xmlObject);
119         if (saml1attr) {
120             const vector<XMLObject*>& values = saml1attr->getAttributeValues();
121             v = values.begin();
122             stop = values.end();
123             if (log.isDebugEnabled()) {
124                 auto_ptr_char n(saml1attr->getAttributeName());
125                 log.debug(
126                     "decoding ExtensibleAttribute (%s) from SAML 1 Attribute (%s) with %lu value(s)",
127                     ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
128                     );
129             }
130         }
131         else {
132             log.warn("XMLObject type not recognized by DOMAttributeDecoder, no values returned");
133             return nullptr;
134         }
135     }
136
137     for (; v!=stop; ++v) {
138         DOMElement* e = (*v)->getDOM();
139         if (e) {
140             DDF converted = convert(e, false);
141             if (!converted.isnull())
142                 dest.add(converted);
143         }
144         else
145             log.warn("skipping AttributeValue without a backing DOM");
146     }
147
148     return dest.integer() ? _decode(attr.release()) : nullptr;
149 }
150
151 DDF DOMAttributeDecoder::convert(DOMElement* e, bool nameit) const
152 {
153     const XMLCh* nsURI;
154     const XMLCh* local;
155     map<pair<xstring,xstring>,string>::const_iterator mapping;
156     DDF obj = DDF(nullptr).structure();
157
158     if (nameit) {
159         // Name this structure.
160         nsURI = e->getNamespaceURI();
161         local = e->getLocalName();
162         mapping = m_tagMap.find(pair<xstring,xstring>(local,nsURI));
163         if (mapping == m_tagMap.end()) {
164             auto_ptr_char temp(local);
165             obj.name(temp.get());
166         }
167         else {
168             obj.name(mapping->second.c_str());
169         }
170     }
171
172     // Process non-xmlns attributes.
173     DOMNamedNodeMap* attrs = e->getAttributes();
174     for (XMLSize_t a = attrs->getLength(); a > 0; --a) {
175         DOMNode* attr = attrs->item(a-1);
176         nsURI = attr->getNamespaceURI();
177         if (XMLString::equals(nsURI, xmlconstants::XMLNS_NS))
178             continue;
179         local = attr->getLocalName();
180         mapping = m_tagMap.find(pair<xstring,xstring>(local, nsURI ? nsURI : &chNull));
181         if (mapping == m_tagMap.end()) {
182             auto_ptr_char temp(local);
183             obj.addmember(temp.get()).string(toUTF8(attr->getNodeValue(), true), false);
184         }
185         else {
186             obj.addmember(mapping->second.c_str()).string(toUTF8(attr->getNodeValue(), true), false);
187         }
188     }
189
190     DOMElement* child = XMLHelper::getFirstChildElement(e);
191     if (!child && e->hasChildNodes() && e->getFirstChild()->getNodeType() == DOMNode::TEXT_NODE) {
192         // Attach a _text member if a text node is present.
193         obj.addmember("_string").string(toUTF8(e->getFirstChild()->getNodeValue(), true), false);
194     }
195     else {
196         while (child) {
197             // Convert the child element.
198             DDF converted = convert(child);
199             if (!converted.isnull()) {
200                 // Now identify it and attach it.
201                 if (obj[converted.name()].isnull()) {
202                     // We're a new child, so just attach as a structure member.
203                     obj.add(converted);
204                 }
205                 else if (obj[converted.name()].islist()) {
206                     // We're already a repeating child, so add it to the list.
207                     obj[converted.name()].add(converted);
208                 }
209                 else if (obj[converted.name()].isstruct()) {
210                     // This is the complex case where we see a child for the second
211                     // time and have to convert a structure member into a named list.
212                     DDF newlist = DDF(converted.name()).list();
213                     newlist.add(obj[converted.name()].remove());
214                     newlist.add(converted);
215                     obj.add(newlist);
216                 }
217             }
218             child = XMLHelper::getNextSiblingElement(child);
219         }
220     }
221
222     // If we're empty, just delete.
223     if (obj.integer() == 0)
224         obj.destroy();
225     return obj;
226 }