/*
 * The OpenSAML License, Version 1.
 * Copyright (c) 2002
 * University Corporation for Advanced Internet Development, Inc.
 * All rights reserved
 *
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution, if any, must include
 * the following acknowledgment: "This product includes software developed by
 * the University Corporation for Advanced Internet Development
 * <http://www.ucaid.edu>Internet2 Project. Alternately, this acknowledegement
 * may appear in the software itself, if and wherever such third-party
 * acknowledgments normally appear.
 *
 * Neither the name of OpenSAML nor the names of its contributors, nor
 * Internet2, nor the University Corporation for Advanced Internet Development,
 * Inc., nor UCAID may be used to endorse or promote products derived from this
 * software without specific prior written permission. For written permission,
 * please contact opensaml@opensaml.org
 *
 * Products derived from this software may not be called OpenSAML, Internet2,
 * UCAID, or the University Corporation for Advanced Internet Development, nor
 * may OpenSAML appear in their name, without prior written permission of the
 * University Corporation for Advanced Internet Development.
 *
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK
 * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY
 * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


/* SAMLAuthorityBinding.cpp - SAML authority binding implementation

   Scott Cantor
   5/9/02

   $History:$
*/

#include "internal.h"

using namespace saml;
using namespace std;


SAMLAuthorityBinding::SAMLAuthorityBinding(const saml::QName& kind, const XMLCh* binding, const XMLCh* location)

{
    RTTI(SAMLAuthorityBinding);
    if (!binding || !*binding || !location || !*location)
        throw MalformedException(SAMLException::RESPONDER,"SAMLAuthorityBinding() requires binding and location");
    m_kind=new saml::QName(kind);
    m_binding=XMLString::replicate(binding);
    m_location=XMLString::replicate(location);
}

SAMLAuthorityBinding::SAMLAuthorityBinding(DOMElement* e) : m_kind(NULL), m_binding(NULL), m_location(NULL)
{
    RTTI(SAMLAuthorityBinding);
    fromDOM(e);
}

SAMLAuthorityBinding::SAMLAuthorityBinding(istream& in) : SAMLObject(in), m_kind(NULL), m_binding(NULL), m_location(NULL)
{
    RTTI(SAMLAuthorityBinding);
    fromDOM(m_document->getDocumentElement());
}

SAMLAuthorityBinding::~SAMLAuthorityBinding()
{
    if (m_bOwnStrings)
    {
        delete[] const_cast<XMLCh*>(m_binding);
        delete[] const_cast<XMLCh*>(m_location);
    }
    delete m_kind;
}

SAMLObject* SAMLAuthorityBinding::clone() const
{
    return new SAMLAuthorityBinding(*m_kind,m_binding,m_location);
}

void SAMLAuthorityBinding::fromDOM(DOMElement* e)
{
    SAMLObject::fromDOM(e);

    if (SAMLConfig::getConfig().strict_dom_checking &&
        (XMLString::compareString(XML::SAML_NS,e->getNamespaceURI()) || XMLString::compareString(L(AuthorityBinding),e->getLocalName())))
        throw MalformedException("SAMLAuthorityBinding::fromDOM() requires saml:AuthorityBinding at root");
    m_root=e;
    m_bOwnStrings=false;

    m_kind=saml::QName::getQNameAttribute(e,NULL,L(AuthorityKind));
    m_binding = e->getAttributeNS(NULL,L(Binding));
    m_location = e->getAttributeNS(NULL,L(Location));
}

DOMNode* SAMLAuthorityBinding::toDOM(DOMDocument* doc, bool xmlns) const
{
    SAMLObject::toDOM(doc,xmlns);
    if (m_root)
    {
        if (xmlns)
            static_cast<DOMElement*>(m_root)->setAttributeNS(XML::XMLNS_NS,L(xmlns),XML::SAML_NS);
        return m_root;
    }
    if (!doc)
        doc=m_document;

    // Construct an AuthorityBinding.
    DOMElement* ab=doc->createElementNS(XML::SAML_NS,L(AuthorityBinding));
    if (xmlns)
        ab->setAttributeNS(XML::XMLNS_NS,L(xmlns),XML::SAML_NS);
    ab->setAttributeNS(NULL,L(Binding),m_binding);
    ab->setAttributeNS(NULL,L(Location),m_location);

    xstring kind;
    if (XMLString::compareString(XML::SAMLP_NS,m_kind->getNamespaceURI()))
    {
        kind+=chLatin_k + chLatin_i + chLatin_n + chLatin_d + chColon + m_kind->getLocalName();
        xstring nskind=L(xmlns);
        nskind+=chColon + chLatin_k + chLatin_i + chLatin_n + chLatin_d;
        ab->setAttributeNS(XML::XMLNS_NS,nskind.c_str(),m_kind->getNamespaceURI());
    }
    else
        kind=kind + chLatin_s + chLatin_a + chLatin_m + chLatin_l + chLatin_p + chColon +
            m_kind->getLocalName();

    ab->setAttributeNS(NULL,L(AuthorityKind),kind.c_str());

    return m_root=ab;
}
