boost changes and header fixes
[shibboleth/cpp-opensaml.git] / saml / saml1 / core / impl / ProtocolsImpl.cpp
index 6fe96d0..b9aa3a7 100644 (file)
@@ -1,17 +1,21 @@
-/*
- *  Copyright 2001-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.
  */
 
 /**
@@ -36,6 +40,7 @@
 #include <xmltooling/util/XMLHelper.h>
 
 #include <ctime>
+#include <boost/lexical_cast.hpp>
 #include <xercesc/util/XMLUniDefs.hpp>
 
 using namespace opensaml::saml1p;
@@ -66,7 +71,7 @@ namespace opensaml {
             public AbstractXMLObjectMarshaller,
             public AbstractXMLObjectUnmarshaller
         {
-            xmltooling::QName* m_qname;
+            mutable xmltooling::QName* m_qname;
         public:
             virtual ~RespondWithImpl() {
                 delete m_qname;
@@ -82,6 +87,9 @@ namespace opensaml {
             }
 
             xmltooling::QName* getQName() const {
+                if (!m_qname && getDOM() && getDOM()->getTextContent()) {
+                    m_qname = XMLHelper::getNodeValueAsQName(getDOM());
+                }
                 return m_qname;
             }
 
@@ -91,8 +99,9 @@ namespace opensaml {
                     auto_ptr_XMLCh temp(m_qname->toString().c_str());
                     setTextContent(temp.get());
                 }
-                else
+                else {
                     setTextContent(nullptr);
+                }
             }
 
             IMPL_XMLOBJECT_CLONE(RespondWith);
@@ -109,7 +118,7 @@ namespace opensaml {
 
             QueryImpl(const QueryImpl& src) : AbstractXMLObject(src), AnyElementImpl(src) {}
 
-            IMPL_XMLOBJECT_CLONE(Query);
+            IMPL_XMLOBJECT_CLONE_EX(Query);
         };
 
         class SAML_DLLLOCAL SubjectQueryImpl : public virtual SubjectQuery,
@@ -123,10 +132,12 @@ namespace opensaml {
                 m_children.push_back(nullptr);
                 m_pos_Subject=m_children.begin();
             }
+
         protected:
             SubjectQueryImpl() {
                 init();
             }
+
         public:
             virtual ~SubjectQueryImpl() {}
 
@@ -138,10 +149,21 @@ namespace opensaml {
             SubjectQueryImpl(const SubjectQueryImpl& src)
                     : AbstractXMLObject(src), AbstractComplexElement(src), AbstractDOMCachingXMLObject(src) {
                 init();
+            }
+
+            void _clone(const SubjectQueryImpl& src) {
                 if (src.getSubject())
                     setSubject(src.getSubject()->cloneSubject());
             }
 
+            SubjectQuery* cloneSubjectQuery() const {
+                return dynamic_cast<SubjectQuery*>(clone());
+            }
+
+            Query* cloneQuery() const {
+                return dynamic_cast<Query*>(clone());
+            }
+
             IMPL_TYPED_CHILD(Subject);
 
         protected:
@@ -156,28 +178,27 @@ namespace opensaml {
             void init() {
                 m_AuthenticationMethod=nullptr;
             }
+
         public:
             virtual ~AuthenticationQueryImpl() {
                 XMLString::release(&m_AuthenticationMethod);
             }
 
             AuthenticationQueryImpl(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const xmltooling::QName* schemaType)
-                : AbstractXMLObject(nsURI, localName, prefix, schemaType) {
+                    : AbstractXMLObject(nsURI, localName, prefix, schemaType) {
                 init();
             }
 
             AuthenticationQueryImpl(const AuthenticationQueryImpl& src) : AbstractXMLObject(src), SubjectQueryImpl(src) {
                 init();
-                setAuthenticationMethod(src.getAuthenticationMethod());
             }
 
-            IMPL_XMLOBJECT_CLONE(AuthenticationQuery);
-            SubjectQuery* cloneSubjectQuery() const {
-                return cloneAuthenticationQuery();
-            }
-            Query* cloneQuery() const {
-                return cloneAuthenticationQuery();
+            void _clone(const AuthenticationQueryImpl& src) {
+                SubjectQueryImpl::_clone(src);
+                setAuthenticationMethod(src.getAuthenticationMethod());
             }
+
+            IMPL_XMLOBJECT_CLONE_EX(AuthenticationQuery);
             IMPL_STRING_ATTRIB(AuthenticationMethod);
 
         protected:
@@ -197,34 +218,32 @@ namespace opensaml {
             void init() {
                 m_Resource=nullptr;
             }
+
         public:
             virtual ~AttributeQueryImpl() {
                 XMLString::release(&m_Resource);
             }
 
             AttributeQueryImpl(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const xmltooling::QName* schemaType)
-                : AbstractXMLObject(nsURI, localName, prefix, schemaType) {
+                    : AbstractXMLObject(nsURI, localName, prefix, schemaType) {
                 init();
             }
 
             AttributeQueryImpl(const AttributeQueryImpl& src) : AbstractXMLObject(src), SubjectQueryImpl(src) {
                 init();
+            }
+
+            void _clone(const AttributeQueryImpl& src) {
+                SubjectQueryImpl::_clone(src);
                 setResource(src.getResource());
-                VectorOf(AttributeDesignator) v=getAttributeDesignators();
                 for (vector<AttributeDesignator*>::const_iterator i=src.m_AttributeDesignators.begin(); i!=src.m_AttributeDesignators.end(); i++) {
                     if (*i) {
-                        v.push_back((*i)->cloneAttributeDesignator());
+                        getAttributeDesignators().push_back((*i)->cloneAttributeDesignator());
                     }
                 }
             }
 
-            IMPL_XMLOBJECT_CLONE(AttributeQuery);
-            SubjectQuery* cloneSubjectQuery() const {
-                return cloneAttributeQuery();
-            }
-            Query* cloneQuery() const {
-                return cloneAttributeQuery();
-            }
+            IMPL_XMLOBJECT_CLONE_EX(AttributeQuery);
             IMPL_STRING_ATTRIB(Resource);
             IMPL_TYPED_CHILDREN(AttributeDesignator,m_children.end());
 
@@ -254,36 +273,34 @@ namespace opensaml {
                 m_pos_Evidence=m_pos_Subject;
                 ++m_pos_Evidence;
             }
+
         public:
             virtual ~AuthorizationDecisionQueryImpl() {
                 XMLString::release(&m_Resource);
             }
 
             AuthorizationDecisionQueryImpl(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const xmltooling::QName* schemaType)
-                : AbstractXMLObject(nsURI, localName, prefix, schemaType) {
+                    : AbstractXMLObject(nsURI, localName, prefix, schemaType) {
                 init();
             }
 
             AuthorizationDecisionQueryImpl(const AuthorizationDecisionQueryImpl& src) : AbstractXMLObject(src), SubjectQueryImpl(src) {
                 init();
+            }
+
+            void _clone(const AuthorizationDecisionQueryImpl& src) {
+                SubjectQueryImpl::_clone(src);
                 setResource(src.getResource());
                 if (src.getEvidence())
                     setEvidence(src.getEvidence()->cloneEvidence());
-                VectorOf(Action) v=getActions();
                 for (vector<Action*>::const_iterator i=src.m_Actions.begin(); i!=src.m_Actions.end(); i++) {
                     if (*i) {
-                        v.push_back((*i)->cloneAction());
+                        getActions().push_back((*i)->cloneAction());
                     }
                 }
             }
 
-            IMPL_XMLOBJECT_CLONE(AuthorizationDecisionQuery);
-            SubjectQuery* cloneSubjectQuery() const {
-                return cloneAuthorizationDecisionQuery();
-            }
-            Query* cloneQuery() const {
-                return cloneAuthorizationDecisionQuery();
-            }
+            IMPL_XMLOBJECT_CLONE_EX(AuthorizationDecisionQuery);
             IMPL_STRING_ATTRIB(Resource);
             IMPL_TYPED_CHILD(Evidence);
             IMPL_TYPED_CHILDREN(Action, m_pos_Evidence);
@@ -320,6 +337,7 @@ namespace opensaml {
                 m_Signature=nullptr;
                 m_pos_Signature=m_children.begin();
             }
+
         protected:
             RequestAbstractTypeImpl() {
                 init();
@@ -339,24 +357,28 @@ namespace opensaml {
             RequestAbstractTypeImpl(const RequestAbstractTypeImpl& src)
                     : AbstractXMLObject(src), AbstractComplexElement(src), AbstractDOMCachingXMLObject(src) {
                 init();
+            }
+
+            //IMPL_TYPED_CHILD(Signature);
+            // Need customized setter.
+
+        protected:
+            void _clone(const RequestAbstractTypeImpl& src) {
                 setMinorVersion(src.m_MinorVersion);
                 setRequestID(src.getRequestID());
                 setIssueInstant(src.getIssueInstant());
                 if (src.getSignature())
                     setSignature(src.getSignature()->cloneSignature());
-                VectorOf(RespondWith) v=getRespondWiths();
                 for (vector<RespondWith*>::const_iterator i=src.m_RespondWiths.begin(); i!=src.m_RespondWiths.end(); i++) {
                     if (*i) {
-                        v.push_back((*i)->cloneRespondWith());
+                        getRespondWiths().push_back((*i)->cloneRespondWith());
                     }
                 }
             }
 
-            //IMPL_TYPED_CHILD(Signature);
-            // Need customized setter.
-        protected:
             Signature* m_Signature;
             list<XMLObject*>::iterator m_pos_Signature;
+
         public:
             Signature* getSignature() const {
                 return m_Signature;
@@ -370,6 +392,10 @@ namespace opensaml {
                     m_Signature->setContentReference(new opensaml::ContentReference(*this));
             }
 
+            RequestAbstractType* cloneRequestAbstractType() const {
+                return dynamic_cast<RequestAbstractType*>(clone());
+            }
+
             IMPL_INTEGER_ATTRIB(MinorVersion);
             IMPL_STRING_ATTRIB(RequestID);    // have to special-case getXMLID
             const XMLCh* getXMLID() const {
@@ -388,6 +414,11 @@ namespace opensaml {
             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(nullptr,MAJORVERSION,XML_ONE);
@@ -449,6 +480,7 @@ namespace opensaml {
                 m_pos_Query=m_pos_Signature;
                 ++m_pos_Query;
             }
+
         public:
             virtual ~RequestImpl() {}
 
@@ -459,26 +491,25 @@ namespace opensaml {
 
             RequestImpl(const RequestImpl& src) : AbstractXMLObject(src), RequestAbstractTypeImpl(src) {
                 init();
+            }
+
+            void _clone(const RequestImpl& src) {
+                RequestAbstractTypeImpl::_clone(src);
                 if (src.getQuery())
                     setQuery(src.getQuery()->cloneQuery());
-                VectorOf(AssertionIDReference) v=getAssertionIDReferences();
                 for (vector<AssertionIDReference*>::const_iterator i=src.m_AssertionIDReferences.begin(); i!=src.m_AssertionIDReferences.end(); i++) {
                     if (*i) {
-                        v.push_back((*i)->cloneAssertionIDReference());
+                        getAssertionIDReferences().push_back((*i)->cloneAssertionIDReference());
                     }
                 }
-                VectorOf(AssertionArtifact) v2=getAssertionArtifacts();
                 for (vector<AssertionArtifact*>::const_iterator i=src.m_AssertionArtifacts.begin(); i!=src.m_AssertionArtifacts.end(); i++) {
                     if (*i) {
-                        v2.push_back((*i)->cloneAssertionArtifact());
+                        getAssertionArtifacts().push_back((*i)->cloneAssertionArtifact());
                     }
                 }
             }
 
-            IMPL_XMLOBJECT_CLONE(Request);
-            RequestAbstractType* cloneRequestAbstractType() const {
-                return cloneRequest();
-            }
+            IMPL_XMLOBJECT_CLONE_EX(Request);
             IMPL_TYPED_CHILD(Query);
 
             SubjectQuery* getSubjectQuery() const {
@@ -531,6 +562,7 @@ namespace opensaml {
                 m_StatusCode=nullptr;
                 m_pos_StatusCode=m_children.begin();
             }
+
         public:
             virtual ~StatusCodeImpl() {
                 delete m_Value;
@@ -583,9 +615,11 @@ namespace opensaml {
 
             StatusDetailImpl(const StatusDetailImpl& src)
                     : AbstractXMLObject(src), AbstractComplexElement(src), AbstractDOMCachingXMLObject(src) {
-                VectorOf(XMLObject) v=getUnknownXMLObjects();
-                for (vector<XMLObject*>::const_iterator i=src.m_UnknownXMLObjects.begin(); i!=src.m_UnknownXMLObjects.end(); ++i)
-                    v.push_back((*i)->clone());
+                for (vector<XMLObject*>::const_iterator i=src.m_UnknownXMLObjects.begin(); i!=src.m_UnknownXMLObjects.end(); ++i) {
+                    if (*i) {
+                        getUnknownXMLObjects().push_back((*i)->clone());
+                    }
+                }
             }
 
             IMPL_XMLOBJECT_CLONE(StatusDetail);
@@ -616,6 +650,7 @@ namespace opensaml {
                 m_pos_StatusDetail=m_pos_StatusMessage;
                 ++m_pos_StatusDetail;
             }
+
         public:
             virtual ~StatusImpl() {}
 
@@ -665,10 +700,12 @@ namespace opensaml {
                 m_Signature=nullptr;
                 m_pos_Signature=m_children.begin();
             }
+
         protected:
             ResponseAbstractTypeImpl() {
                 init();
             }
+
         public:
             virtual ~ResponseAbstractTypeImpl() {
                 XMLString::release(&m_MinorVersion);
@@ -679,13 +716,19 @@ namespace opensaml {
             }
 
             ResponseAbstractTypeImpl(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const xmltooling::QName* schemaType)
-                : AbstractXMLObject(nsURI, localName, prefix, schemaType) {
+                    : AbstractXMLObject(nsURI, localName, prefix, schemaType) {
                 init();
             }
 
             ResponseAbstractTypeImpl(const ResponseAbstractTypeImpl& src)
                     : AbstractXMLObject(src), AbstractComplexElement(src), AbstractDOMCachingXMLObject(src) {
                 init();
+            }
+
+            //IMPL_TYPED_CHILD(Signature);
+            // Need customized setter.
+        protected:
+            void _clone(const ResponseAbstractTypeImpl& src) {
                 setMinorVersion(src.m_MinorVersion);
                 setResponseID(src.getResponseID());
                 setInResponseTo(src.getInResponseTo());
@@ -695,9 +738,6 @@ namespace opensaml {
                     setSignature(src.getSignature()->cloneSignature());
             }
 
-            //IMPL_TYPED_CHILD(Signature);
-            // Need customized setter.
-        protected:
             Signature* m_Signature;
             list<XMLObject*>::iterator m_pos_Signature;
         public:
@@ -713,6 +753,10 @@ namespace opensaml {
                     m_Signature->setContentReference(new opensaml::ContentReference(*this));
             }
 
+            ResponseAbstractType* cloneResponseAbstractType() const {
+                return dynamic_cast<ResponseAbstractType*>(clone());
+            }
+
             IMPL_INTEGER_ATTRIB(MinorVersion);
             IMPL_STRING_ATTRIB(ResponseID);    // have to special-case getXMLID
             const XMLCh* getXMLID() const {
@@ -732,6 +776,11 @@ namespace opensaml {
             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(nullptr,MAJORVERSION,XML_ONE);
@@ -796,30 +845,31 @@ namespace opensaml {
                 m_pos_Status=m_pos_Signature;
                 ++m_pos_Status;
             }
+
         public:
             virtual ~ResponseImpl() {}
 
             ResponseImpl(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const xmltooling::QName* schemaType)
-                : AbstractXMLObject(nsURI, localName, prefix, schemaType) {
+                    : AbstractXMLObject(nsURI, localName, prefix, schemaType) {
                 init();
             }
 
             ResponseImpl(const ResponseImpl& src) : AbstractXMLObject(src), ResponseAbstractTypeImpl(src) {
                 init();
+            }
+
+            void _clone(const ResponseImpl& src) {
+                ResponseAbstractTypeImpl::_clone(src);
                 if (src.getStatus())
                     setStatus(src.getStatus()->cloneStatus());
-                VectorOf(saml1::Assertion) v=getAssertions();
                 for (vector<saml1::Assertion*>::const_iterator i=src.m_Assertions.begin(); i!=src.m_Assertions.end(); i++) {
                     if (*i) {
-                        v.push_back((*i)->cloneAssertion());
+                        getAssertions().push_back((*i)->cloneAssertion());
                     }
                 }
             }
 
-            IMPL_XMLOBJECT_CLONE(Response);
-            ResponseAbstractType* cloneResponseAbstractType() const {
-                return cloneResponse();
-            }
+            IMPL_XMLOBJECT_CLONE_EX(Response);
             IMPL_TYPED_CHILD(Status);
             IMPL_TYPED_FOREIGN_CHILDREN(Assertion,saml1,m_children.end());