https://issues.shibboleth.net/jira/browse/CPPOST-64
[shibboleth/cpp-opensaml.git] / saml / saml1 / core / impl / ProtocolsImpl.cpp
index abcaa77..28a5fe1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2009 Internet2
+ *  Copyright 2001-2010 Internet2
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
 #include <xmltooling/io/AbstractXMLObjectMarshaller.h>
 #include <xmltooling/io/AbstractXMLObjectUnmarshaller.h>
 #include <xmltooling/signature/Signature.h>
+#include <xmltooling/util/DateTime.h>
 #include <xmltooling/util/XMLHelper.h>
 
 #include <ctime>
@@ -65,22 +66,25 @@ namespace opensaml {
             public AbstractXMLObjectMarshaller,
             public AbstractXMLObjectUnmarshaller
         {
-            xmltooling::QName* m_qname;
+            mutable xmltooling::QName* m_qname;
         public:
             virtual ~RespondWithImpl() {
                 delete m_qname;
             }
 
             RespondWithImpl(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const xmltooling::QName* schemaType)
-                : AbstractXMLObject(nsURI, localName, prefix, schemaType), m_qname(NULL) {
+                : AbstractXMLObject(nsURI, localName, prefix, schemaType), m_qname(nullptr) {
             }
 
             RespondWithImpl(const RespondWithImpl& src)
-                    : AbstractXMLObject(src), AbstractSimpleElement(src), AbstractDOMCachingXMLObject(src), m_qname(NULL) {
+                    : AbstractXMLObject(src), AbstractSimpleElement(src), AbstractDOMCachingXMLObject(src), m_qname(nullptr) {
                 setQName(src.getQName());
             }
 
             xmltooling::QName* getQName() const {
+                if (!m_qname && getDOM() && getDOM()->getTextContent()) {
+                    m_qname = XMLHelper::getNodeValueAsQName(getDOM());
+                }
                 return m_qname;
             }
 
@@ -90,8 +94,9 @@ namespace opensaml {
                     auto_ptr_XMLCh temp(m_qname->toString().c_str());
                     setTextContent(temp.get());
                 }
-                else
-                    setTextContent(NULL);
+                else {
+                    setTextContent(nullptr);
+                }
             }
 
             IMPL_XMLOBJECT_CLONE(RespondWith);
@@ -118,8 +123,8 @@ namespace opensaml {
             public AbstractXMLObjectUnmarshaller
         {
             void init() {
-                m_Subject=NULL;
-                m_children.push_back(NULL);
+                m_Subject=nullptr;
+                m_children.push_back(nullptr);
                 m_pos_Subject=m_children.begin();
             }
         protected:
@@ -153,7 +158,7 @@ namespace opensaml {
         class SAML_DLLLOCAL AuthenticationQueryImpl : public virtual AuthenticationQuery, public SubjectQueryImpl
         {
             void init() {
-                m_AuthenticationMethod=NULL;
+                m_AuthenticationMethod=nullptr;
             }
         public:
             virtual ~AuthenticationQueryImpl() {
@@ -181,12 +186,12 @@ namespace opensaml {
 
         protected:
             void marshallAttributes(DOMElement* domElement) const {
-                MARSHALL_STRING_ATTRIB(AuthenticationMethod,AUTHENTICATIONMETHOD,NULL);
+                MARSHALL_STRING_ATTRIB(AuthenticationMethod,AUTHENTICATIONMETHOD,nullptr);
                 SubjectQueryImpl::marshallAttributes(domElement);
             }
 
             void processAttribute(const DOMAttr* attribute) {
-                PROC_STRING_ATTRIB(AuthenticationMethod,AUTHENTICATIONMETHOD,NULL);
+                PROC_STRING_ATTRIB(AuthenticationMethod,AUTHENTICATIONMETHOD,nullptr);
                 SubjectQueryImpl::processAttribute(attribute);
             }
         };
@@ -194,7 +199,7 @@ namespace opensaml {
         class SAML_DLLLOCAL AttributeQueryImpl : public virtual AttributeQuery, public SubjectQueryImpl
         {
             void init() {
-                m_Resource=NULL;
+                m_Resource=nullptr;
             }
         public:
             virtual ~AttributeQueryImpl() {
@@ -229,7 +234,7 @@ namespace opensaml {
 
         protected:
             void marshallAttributes(DOMElement* domElement) const {
-                MARSHALL_STRING_ATTRIB(Resource,RESOURCE,NULL);
+                MARSHALL_STRING_ATTRIB(Resource,RESOURCE,nullptr);
                 SubjectQueryImpl::marshallAttributes(domElement);
             }
 
@@ -239,7 +244,7 @@ namespace opensaml {
             }
 
             void processAttribute(const DOMAttr* attribute) {
-                PROC_STRING_ATTRIB(Resource,RESOURCE,NULL);
+                PROC_STRING_ATTRIB(Resource,RESOURCE,nullptr);
                 SubjectQueryImpl::processAttribute(attribute);
             }
         };
@@ -247,9 +252,9 @@ namespace opensaml {
         class SAML_DLLLOCAL AuthorizationDecisionQueryImpl : public virtual AuthorizationDecisionQuery, public SubjectQueryImpl
         {
             void init() {
-                m_Resource=NULL;
-                m_Evidence=NULL;
-                m_children.push_back(NULL);
+                m_Resource=nullptr;
+                m_Evidence=nullptr;
+                m_children.push_back(nullptr);
                 m_pos_Evidence=m_pos_Subject;
                 ++m_pos_Evidence;
             }
@@ -289,7 +294,7 @@ namespace opensaml {
 
         protected:
             void marshallAttributes(DOMElement* domElement) const {
-                MARSHALL_STRING_ATTRIB(Resource,RESOURCE,NULL);
+                MARSHALL_STRING_ATTRIB(Resource,RESOURCE,nullptr);
                 SubjectQueryImpl::marshallAttributes(domElement);
             }
 
@@ -300,7 +305,7 @@ namespace opensaml {
             }
 
             void processAttribute(const DOMAttr* attribute) {
-                PROC_STRING_ATTRIB(Resource,RESOURCE,NULL);
+                PROC_STRING_ATTRIB(Resource,RESOURCE,nullptr);
                 SubjectQueryImpl::processAttribute(attribute);
             }
         };
@@ -312,11 +317,11 @@ namespace opensaml {
             public AbstractXMLObjectUnmarshaller
         {
             void init() {
-                m_MinorVersion=NULL;
-                m_RequestID=NULL;
-                m_IssueInstant=NULL;
-                m_children.push_back(NULL);
-                m_Signature=NULL;
+                m_MinorVersion=nullptr;
+                m_RequestID=nullptr;
+                m_IssueInstant=nullptr;
+                m_children.push_back(nullptr);
+                m_Signature=nullptr;
                 m_pos_Signature=m_children.begin();
             }
         protected:
@@ -373,36 +378,46 @@ namespace opensaml {
             IMPL_STRING_ATTRIB(RequestID);    // have to special-case getXMLID
             const XMLCh* getXMLID() const {
                 pair<bool,int> v = getMinorVersion();
-                return (!v.first || v.second > 0) ? m_RequestID : NULL;
+                return (!v.first || v.second > 0) ? m_RequestID : nullptr;
             }
             const XMLCh* getID() const {
                 return getRequestID();
             }
+            void releaseDOM() const {
+                if (getDOM())
+                    getDOM()->removeAttributeNS(nullptr, REQUESTID_ATTRIB_NAME);
+                AbstractDOMCachingXMLObject::releaseDOM();
+            }
             IMPL_DATETIME_ATTRIB(IssueInstant,0);
             IMPL_TYPED_CHILDREN(RespondWith,m_pos_Signature);
 
         protected:
+            void prepareForMarshalling() const {
+                if (m_Signature)
+                    declareNonVisibleNamespaces();
+            }
+
             void marshallAttributes(DOMElement* domElement) const {
                 static const XMLCh MAJORVERSION[] = UNICODE_LITERAL_12(M,a,j,o,r,V,e,r,s,i,o,n);
-                domElement->setAttributeNS(NULL,MAJORVERSION,XML_ONE);
+                domElement->setAttributeNS(nullptr,MAJORVERSION,XML_ONE);
                 if (!m_MinorVersion)
                     const_cast<RequestAbstractTypeImpl*>(this)->m_MinorVersion=XMLString::replicate(XML_ONE);
-                MARSHALL_INTEGER_ATTRIB(MinorVersion,MINORVERSION,NULL);
+                MARSHALL_INTEGER_ATTRIB(MinorVersion,MINORVERSION,nullptr);
                 if (!m_RequestID)
                     const_cast<RequestAbstractTypeImpl*>(this)->m_RequestID=SAMLConfig::getConfig().generateIdentifier();
-                domElement->setAttributeNS(NULL, REQUESTID_ATTRIB_NAME, m_RequestID);
+                domElement->setAttributeNS(nullptr, REQUESTID_ATTRIB_NAME, m_RequestID);
                 if (*m_MinorVersion!=chDigit_0) {
 #ifdef XMLTOOLING_XERCESC_BOOLSETIDATTRIBUTE
-                    domElement->setIdAttributeNS(NULL, REQUESTID_ATTRIB_NAME, true);
+                    domElement->setIdAttributeNS(nullptr, REQUESTID_ATTRIB_NAME, true);
 #else
-                    domElement->setIdAttributeNS(NULL, REQUESTID_ATTRIB_NAME);
+                    domElement->setIdAttributeNS(nullptr, REQUESTID_ATTRIB_NAME);
 #endif
                 }
                 if (!m_IssueInstant) {
-                    const_cast<RequestAbstractTypeImpl*>(this)->m_IssueInstantEpoch=time(NULL);
+                    const_cast<RequestAbstractTypeImpl*>(this)->m_IssueInstantEpoch=time(nullptr);
                     const_cast<RequestAbstractTypeImpl*>(this)->m_IssueInstant=new DateTime(m_IssueInstantEpoch);
                 }
-                MARSHALL_DATETIME_ATTRIB(IssueInstant,ISSUEINSTANT,NULL);
+                MARSHALL_DATETIME_ATTRIB(IssueInstant,ISSUEINSTANT,nullptr);
             }
 
             void processChildElement(XMLObject* childXMLObject, const DOMElement* root) {
@@ -416,30 +431,30 @@ namespace opensaml {
                 AbstractXMLObjectUnmarshaller::unmarshallAttributes(domElement);
                 if (m_RequestID && (!m_MinorVersion || *m_MinorVersion!=chDigit_0)) {
 #ifdef XMLTOOLING_XERCESC_BOOLSETIDATTRIBUTE
-                    const_cast<DOMElement*>(domElement)->setIdAttributeNS(NULL, REQUESTID_ATTRIB_NAME, true);
+                    const_cast<DOMElement*>(domElement)->setIdAttributeNS(nullptr, REQUESTID_ATTRIB_NAME, true);
 #else
-                    const_cast<DOMElement*>(domElement)->setIdAttributeNS(NULL, REQUESTID_ATTRIB_NAME);
+                    const_cast<DOMElement*>(domElement)->setIdAttributeNS(nullptr, REQUESTID_ATTRIB_NAME);
 #endif
                 }
             }
 
             void processAttribute(const DOMAttr* attribute) {
                 static const XMLCh MAJORVERSION[] = UNICODE_LITERAL_12(M,a,j,o,r,V,e,r,s,i,o,n);
-                if (XMLHelper::isNodeNamed(attribute,NULL,MAJORVERSION)) {
+                if (XMLHelper::isNodeNamed(attribute,nullptr,MAJORVERSION)) {
                     if (!XMLString::equals(attribute->getValue(),XML_ONE))
                         throw UnmarshallingException("Request has invalid major version.");
                 }
-                PROC_INTEGER_ATTRIB(MinorVersion,MINORVERSION,NULL);
-                PROC_STRING_ATTRIB(RequestID,REQUESTID,NULL);
-                PROC_DATETIME_ATTRIB(IssueInstant,ISSUEINSTANT,NULL);
+                PROC_INTEGER_ATTRIB(MinorVersion,MINORVERSION,nullptr);
+                PROC_STRING_ATTRIB(RequestID,REQUESTID,nullptr);
+                PROC_DATETIME_ATTRIB(IssueInstant,ISSUEINSTANT,nullptr);
             }
         };
 
         class SAML_DLLLOCAL RequestImpl : public virtual Request, public RequestAbstractTypeImpl
         {
             void init() {
-                m_children.push_back(NULL);
-                m_Query=NULL;
+                m_children.push_back(nullptr);
+                m_Query=nullptr;
                 m_pos_Query=m_pos_Signature;
                 ++m_pos_Query;
             }
@@ -520,9 +535,9 @@ namespace opensaml {
             public AbstractXMLObjectUnmarshaller
         {
             void init() {
-                m_Value=NULL;
-                m_children.push_back(NULL);
-                m_StatusCode=NULL;
+                m_Value=nullptr;
+                m_children.push_back(nullptr);
+                m_StatusCode=nullptr;
                 m_pos_StatusCode=m_children.begin();
             }
         public:
@@ -549,7 +564,7 @@ namespace opensaml {
 
         protected:
             void marshallAttributes(DOMElement* domElement) const {
-                MARSHALL_QNAME_ATTRIB(Value,VALUE,NULL);
+                MARSHALL_QNAME_ATTRIB(Value,VALUE,nullptr);
             }
 
             void processChildElement(XMLObject* childXMLObject, const DOMElement* root) {
@@ -558,7 +573,7 @@ namespace opensaml {
             }
 
             void processAttribute(const DOMAttr* attribute) {
-                PROC_QNAME_ATTRIB(Value,VALUE,NULL);
+                PROC_QNAME_ATTRIB(Value,VALUE,nullptr);
             }
         };
 
@@ -598,15 +613,15 @@ namespace opensaml {
             public AbstractXMLObjectUnmarshaller
         {
             void init() {
-                m_children.push_back(NULL);
-                m_children.push_back(NULL);
-                m_children.push_back(NULL);
-                m_StatusCode=NULL;
+                m_children.push_back(nullptr);
+                m_children.push_back(nullptr);
+                m_children.push_back(nullptr);
+                m_StatusCode=nullptr;
                 m_pos_StatusCode=m_children.begin();
-                m_StatusMessage=NULL;
+                m_StatusMessage=nullptr;
                 m_pos_StatusMessage=m_pos_StatusCode;
                 ++m_pos_StatusMessage;
-                m_StatusDetail=NULL;
+                m_StatusDetail=nullptr;
                 m_pos_StatusDetail=m_pos_StatusMessage;
                 ++m_pos_StatusDetail;
             }
@@ -650,13 +665,13 @@ namespace opensaml {
             public AbstractXMLObjectUnmarshaller
         {
             void init() {
-                m_MinorVersion=NULL;
-                m_ResponseID=NULL;
-                m_InResponseTo=NULL;
-                m_IssueInstant=NULL;
-                m_Recipient=NULL;
-                m_children.push_back(NULL);
-                m_Signature=NULL;
+                m_MinorVersion=nullptr;
+                m_ResponseID=nullptr;
+                m_InResponseTo=nullptr;
+                m_IssueInstant=nullptr;
+                m_Recipient=nullptr;
+                m_children.push_back(nullptr);
+                m_Signature=nullptr;
                 m_pos_Signature=m_children.begin();
             }
         protected:
@@ -711,39 +726,49 @@ namespace opensaml {
             IMPL_STRING_ATTRIB(ResponseID);    // have to special-case getXMLID
             const XMLCh* getXMLID() const {
                 pair<bool,int> v = getMinorVersion();
-                return (!v.first || v.second > 0) ? m_ResponseID : NULL;
+                return (!v.first || v.second > 0) ? m_ResponseID : nullptr;
             }
             const XMLCh* getID() const {
                 return getResponseID();
             }
+            void releaseDOM() const {
+                if (getDOM())
+                    getDOM()->removeAttributeNS(nullptr, RESPONSEID_ATTRIB_NAME);
+                AbstractDOMCachingXMLObject::releaseDOM();
+            }
             IMPL_STRING_ATTRIB(InResponseTo);
             IMPL_DATETIME_ATTRIB(IssueInstant,0);
             IMPL_STRING_ATTRIB(Recipient);
 
         protected:
+            void prepareForMarshalling() const {
+                if (m_Signature)
+                    declareNonVisibleNamespaces();
+            }
+
             void marshallAttributes(DOMElement* domElement) const {
                 static const XMLCh MAJORVERSION[] = UNICODE_LITERAL_12(M,a,j,o,r,V,e,r,s,i,o,n);
-                domElement->setAttributeNS(NULL,MAJORVERSION,XML_ONE);
+                domElement->setAttributeNS(nullptr,MAJORVERSION,XML_ONE);
                 if (!m_MinorVersion)
                     const_cast<ResponseAbstractTypeImpl*>(this)->m_MinorVersion=XMLString::replicate(XML_ONE);
-                MARSHALL_INTEGER_ATTRIB(MinorVersion,MINORVERSION,NULL);
+                MARSHALL_INTEGER_ATTRIB(MinorVersion,MINORVERSION,nullptr);
                 if (!m_ResponseID)
                     const_cast<ResponseAbstractTypeImpl*>(this)->m_ResponseID=SAMLConfig::getConfig().generateIdentifier();
-                domElement->setAttributeNS(NULL, RESPONSEID_ATTRIB_NAME, m_ResponseID);
+                domElement->setAttributeNS(nullptr, RESPONSEID_ATTRIB_NAME, m_ResponseID);
                 if (*m_MinorVersion!=chDigit_0) {
 #ifdef XMLTOOLING_XERCESC_BOOLSETIDATTRIBUTE
-                    domElement->setIdAttributeNS(NULL, RESPONSEID_ATTRIB_NAME, true);
+                    domElement->setIdAttributeNS(nullptr, RESPONSEID_ATTRIB_NAME, true);
 #else
-                    domElement->setIdAttributeNS(NULL, RESPONSEID_ATTRIB_NAME);
+                    domElement->setIdAttributeNS(nullptr, RESPONSEID_ATTRIB_NAME);
 #endif
                 }
-                MARSHALL_STRING_ATTRIB(InResponseTo,INRESPONSETO,NULL);
+                MARSHALL_STRING_ATTRIB(InResponseTo,INRESPONSETO,nullptr);
                 if (!m_IssueInstant) {
-                    const_cast<ResponseAbstractTypeImpl*>(this)->m_IssueInstantEpoch=time(NULL);
+                    const_cast<ResponseAbstractTypeImpl*>(this)->m_IssueInstantEpoch=time(nullptr);
                     const_cast<ResponseAbstractTypeImpl*>(this)->m_IssueInstant=new DateTime(m_IssueInstantEpoch);
                 }
-                MARSHALL_DATETIME_ATTRIB(IssueInstant,ISSUEINSTANT,NULL);
-                MARSHALL_STRING_ATTRIB(Recipient,RECIPIENT,NULL);
+                MARSHALL_DATETIME_ATTRIB(IssueInstant,ISSUEINSTANT,nullptr);
+                MARSHALL_STRING_ATTRIB(Recipient,RECIPIENT,nullptr);
             }
 
             void processChildElement(XMLObject* childXMLObject, const DOMElement* root) {
@@ -756,32 +781,32 @@ namespace opensaml {
                 AbstractXMLObjectUnmarshaller::unmarshallAttributes(domElement);
                 if (m_ResponseID && (!m_MinorVersion || *m_MinorVersion!=chDigit_0)) {
 #ifdef XMLTOOLING_XERCESC_BOOLSETIDATTRIBUTE
-                    const_cast<DOMElement*>(domElement)->setIdAttributeNS(NULL, RESPONSEID_ATTRIB_NAME, true);
+                    const_cast<DOMElement*>(domElement)->setIdAttributeNS(nullptr, RESPONSEID_ATTRIB_NAME, true);
 #else
-                    const_cast<DOMElement*>(domElement)->setIdAttributeNS(NULL, RESPONSEID_ATTRIB_NAME);
+                    const_cast<DOMElement*>(domElement)->setIdAttributeNS(nullptr, RESPONSEID_ATTRIB_NAME);
 #endif
                 }
             }
 
             void processAttribute(const DOMAttr* attribute) {
                 static const XMLCh MAJORVERSION[] = UNICODE_LITERAL_12(M,a,j,o,r,V,e,r,s,i,o,n);
-                if (XMLHelper::isNodeNamed(attribute,NULL,MAJORVERSION)) {
+                if (XMLHelper::isNodeNamed(attribute,nullptr,MAJORVERSION)) {
                     if (!XMLString::equals(attribute->getValue(),XML_ONE))
                         throw UnmarshallingException("Response has invalid major version.");
                 }
-                PROC_INTEGER_ATTRIB(MinorVersion,MINORVERSION,NULL);
-                PROC_STRING_ATTRIB(ResponseID,RESPONSEID,NULL);
-                PROC_STRING_ATTRIB(InResponseTo,INRESPONSETO,NULL);
-                PROC_DATETIME_ATTRIB(IssueInstant,ISSUEINSTANT,NULL);
-                PROC_STRING_ATTRIB(Recipient,RECIPIENT,NULL);
+                PROC_INTEGER_ATTRIB(MinorVersion,MINORVERSION,nullptr);
+                PROC_STRING_ATTRIB(ResponseID,RESPONSEID,nullptr);
+                PROC_STRING_ATTRIB(InResponseTo,INRESPONSETO,nullptr);
+                PROC_DATETIME_ATTRIB(IssueInstant,ISSUEINSTANT,nullptr);
+                PROC_STRING_ATTRIB(Recipient,RECIPIENT,nullptr);
             }
         };
 
         class SAML_DLLLOCAL ResponseImpl : public virtual Response, public ResponseAbstractTypeImpl
         {
             void init() {
-                m_children.push_back(NULL);
-                m_Status=NULL;
+                m_children.push_back(nullptr);
+                m_Status=nullptr;
                 m_pos_Status=m_pos_Signature;
                 ++m_pos_Status;
             }