2 * Copyright 2009-2010 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 * XMLAttributeDecoder.cpp
20 * Decodes arbitrary XML into an XMLAttribute.
24 #include "attribute/AttributeDecoder.h"
25 #include "attribute/XMLAttribute.h"
27 #include <saml/saml1/core/Assertions.h>
28 #include <saml/saml2/core/Assertions.h>
29 #include <xmltooling/util/XMLHelper.h>
31 using namespace shibsp;
32 using namespace opensaml;
33 using namespace xmltooling;
37 class SHIBSP_DLLLOCAL XMLAttributeDecoder : virtual public AttributeDecoder
40 XMLAttributeDecoder(const DOMElement* e) : AttributeDecoder(e) {}
41 ~XMLAttributeDecoder() {}
44 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
48 DDF convert(DOMElement* e, bool nameit=true) const;
49 auto_ptr_char m_formatter;
50 map<pair<xstring,xstring>,string> m_tagMap;
53 AttributeDecoder* SHIBSP_DLLLOCAL XMLAttributeDecoderFactory(const DOMElement* const & e)
55 return new XMLAttributeDecoder(e);
60 Attribute* XMLAttributeDecoder::decode(
61 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
67 Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.XML");
69 auto_ptr<XMLAttribute> attr(new XMLAttribute(ids));
70 vector<string>& dest = attr->getValues();
72 // Handle any non-Attribute object directly.
73 if (!xmlObject || !XMLString::equals(saml1::Attribute::LOCAL_NAME, xmlObject->getElementQName().getLocalPart())) {
74 DOMElement* e = xmlObject->getDOM();
76 if (log.isDebugEnabled()) {
78 "decoding XMLAttribute (%s) from XMLObject (%s)",
80 (xmlObject->getSchemaType() ? xmlObject->getSchemaType()->toString() : xmlObject->getElementQName().toString()).c_str()
83 dest.push_back(string());
84 XMLHelper::serialize(e, dest.back());
87 log.warn("skipping XMLObject without a backing DOM");
89 return dest.empty() ? nullptr : _decode(attr.release());
92 vector<XMLObject*>::const_iterator v,stop;
94 const saml2::Attribute* saml2attr = dynamic_cast<const saml2::Attribute*>(xmlObject);
96 const vector<XMLObject*>& values = saml2attr->getAttributeValues();
99 if (log.isDebugEnabled()) {
100 auto_ptr_char n(saml2attr->getName());
102 "decoding XMLAttribute (%s) from SAML 2 Attribute (%s) with %lu value(s)",
103 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
108 const saml1::Attribute* saml1attr = dynamic_cast<const saml1::Attribute*>(xmlObject);
110 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
113 if (log.isDebugEnabled()) {
114 auto_ptr_char n(saml1attr->getAttributeName());
116 "decoding XMLAttribute (%s) from SAML 1 Attribute (%s) with %lu value(s)",
117 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
122 log.warn("XMLObject type not recognized by XMLAttributeDecoder, no values returned");
127 for (; v!=stop; ++v) {
128 DOMElement* e = (*v)->getDOM();
130 dest.push_back(string());
131 XMLHelper::serialize(e, dest.back());
134 log.warn("skipping AttributeValue without a backing DOM");
137 return dest.empty() ? nullptr : _decode(attr.release());