SSPCPP-616 - clean up concatenated string literals
[shibboleth/cpp-sp.git] / shibsp / attribute / DOMAttributeDecoder.cpp
index 33509e3..2aad842 100644 (file)
@@ -1,17 +1,21 @@
-/*
- *  Copyright 2009-2010 Internet2
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
  *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+ * UCAID licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the
+ * License at
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
  */
 
 /**
@@ -40,13 +44,20 @@ namespace shibsp {
         DOMAttributeDecoder(const DOMElement* e);
         ~DOMAttributeDecoder() {}
 
+        // deprecated method
         Attribute* decode(
             const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
+            ) const {
+            return decode(nullptr, ids, xmlObject, assertingParty, relyingParty);
+        }
+
+        Attribute* decode(
+            const GenericRequest*, const vector<string>&, const XMLObject*, const char* assertingParty=nullptr, const char* relyingParty=nullptr
             ) const;
 
     private:
         DDF convert(DOMElement* e, bool nameit=true) const;
-        auto_ptr_char m_formatter;
+        string m_formatter;
         map<pair<xstring,xstring>,string> m_tagMap;
     };
 
@@ -58,15 +69,15 @@ namespace shibsp {
     static const XMLCh Mapping[] =  UNICODE_LITERAL_7(M,a,p,p,i,n,g);
     static const XMLCh _from[] =    UNICODE_LITERAL_4(f,r,o,m);
     static const XMLCh _to[] =      UNICODE_LITERAL_2(t,o);
-    static const XMLCh formatter[] =      UNICODE_LITERAL_9(f,o,r,m,a,t,t,e,r);
+    static const XMLCh formatter[] =UNICODE_LITERAL_9(f,o,r,m,a,t,t,e,r);
 };
 
 DOMAttributeDecoder::DOMAttributeDecoder(const DOMElement* e)
-    : AttributeDecoder(e), m_formatter(e ? e->getAttributeNS(nullptr,formatter) : nullptr)
+    : AttributeDecoder(e), m_formatter(XMLHelper::getAttrString(e, nullptr, formatter))
 {
-    Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.DOM");
+    Category& log = Category::getInstance(SHIBSP_LOGCAT ".AttributeDecoder.DOM");
 
-    e = e ? XMLHelper::getFirstChildElement(e, Mapping) : nullptr;
+    e = XMLHelper::getFirstChildElement(e, Mapping);
     while (e) {
         if (e->hasAttributeNS(nullptr, _from) && e->hasAttributeNS(nullptr, _to)) {
             auto_ptr<xmltooling::QName> f(XMLHelper::getNodeValueAsQName(e->getAttributeNodeNS(nullptr, _from)));
@@ -87,25 +98,23 @@ DOMAttributeDecoder::DOMAttributeDecoder(const DOMElement* e)
 }
 
 Attribute* DOMAttributeDecoder::decode(
-    const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
+    const GenericRequest* request, const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
     ) const
 {
-    Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.DOM");
+    Category& log = Category::getInstance(SHIBSP_LOGCAT ".AttributeDecoder.DOM");
 
-    if (!xmlObject || !XMLString::equals(saml1::Attribute::LOCAL_NAME, xmlObject->getElementQName().getLocalPart())) {
-        log.warn("XMLObject type not recognized by DOMAttributeDecoder, no values returned");
+    if (!xmlObject)
         return nullptr;
-    }
 
-    auto_ptr<ExtensibleAttribute> attr(new ExtensibleAttribute(ids, m_formatter.get()));
+    auto_ptr<ExtensibleAttribute> attr(new ExtensibleAttribute(ids, m_formatter.c_str()));
     DDF dest = attr->getValues();
-    vector<XMLObject*>::const_iterator v,stop;
+    vector<XMLObject*> genericObjectWrapper;    // used to support stand-alone object decoding
+    pair<vector<XMLObject*>::const_iterator,vector<XMLObject*>::const_iterator> valrange;
 
     const saml2::Attribute* saml2attr = dynamic_cast<const saml2::Attribute*>(xmlObject);
     if (saml2attr) {
         const vector<XMLObject*>& values = saml2attr->getAttributeValues();
-        v = values.begin();
-        stop = values.end();
+        valrange = valueRange(request, values);
         if (log.isDebugEnabled()) {
             auto_ptr_char n(saml2attr->getName());
             log.debug(
@@ -118,8 +127,7 @@ Attribute* DOMAttributeDecoder::decode(
         const saml1::Attribute* saml1attr = dynamic_cast<const saml1::Attribute*>(xmlObject);
         if (saml1attr) {
             const vector<XMLObject*>& values = saml1attr->getAttributeValues();
-            v = values.begin();
-            stop = values.end();
+            valrange = valueRange(request, values);
             if (log.isDebugEnabled()) {
                 auto_ptr_char n(saml1attr->getAttributeName());
                 log.debug(
@@ -129,20 +137,22 @@ Attribute* DOMAttributeDecoder::decode(
             }
         }
         else {
-            log.warn("XMLObject type not recognized by DOMAttributeDecoder, no values returned");
-            return nullptr;
+            log.debug("decoding arbitrary XMLObject type (%s)", xmlObject->getElementQName().toString().c_str());
+            genericObjectWrapper.push_back(const_cast<XMLObject*>(xmlObject));
+            valrange.first = genericObjectWrapper.begin();
+            valrange.second = genericObjectWrapper.end();
         }
     }
 
-    for (; v!=stop; ++v) {
-        DOMElement* e = (*v)->getDOM();
+    for (; valrange.first != valrange.second; ++valrange.first) {
+        DOMElement* e = (*valrange.first)->getDOM();
         if (e) {
             DDF converted = convert(e, false);
             if (!converted.isnull())
                 dest.add(converted);
         }
         else
-            log.warn("skipping AttributeValue without a backing DOM");
+            log.warn("skipping XMLObject without a backing DOM");
     }
 
     return dest.integer() ? _decode(attr.release()) : nullptr;
@@ -190,7 +200,7 @@ DDF DOMAttributeDecoder::convert(DOMElement* e, bool nameit) const
     DOMElement* child = XMLHelper::getFirstChildElement(e);
     if (!child && e->hasChildNodes() && e->getFirstChild()->getNodeType() == DOMNode::TEXT_NODE) {
         // Attach a _text member if a text node is present.
-        obj.addmember("_string").string(toUTF8(e->getFirstChild()->getNodeValue(), true), false);
+        obj.addmember("_string").string(toUTF8(e->getFirstChild()->getTextContent(), true), false);
     }
     else {
         while (child) {