/*
 * 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.
 */


/* SAMLAttributeQuery.cpp - SAML attribute query implementation

   Scott Cantor
   5/21/02

   $History:$
*/

#include "internal.h"

using namespace saml;
using namespace std;


SAMLAttributeQuery::SAMLAttributeQuery(SAMLSubject* subject, const XMLCh* resource, const Iterator<SAMLAttribute*>& designators)
{
    RTTI(SAMLAttributeQuery);
    if (!subject)
        throw SAMLException(SAMLException::REQUESTER,"SAMLAttributeQuery() requires subject");
    m_subject=subject;
    m_resource=XMLString::replicate(resource);
    while (designators.hasNext())
        m_designators.push_back(designators.next());
}

SAMLAttributeQuery::SAMLAttributeQuery(DOMElement* e) : m_subject(NULL), m_resource(NULL)
{
    RTTI(SAMLAttributeQuery);
    fromDOM(e);
}

SAMLAttributeQuery::SAMLAttributeQuery(istream& in) : SAMLQuery(in), m_subject(NULL), m_resource(NULL)
{
    RTTI(SAMLAttributeQuery);
    fromDOM(m_document->getDocumentElement());
}

SAMLAttributeQuery::~SAMLAttributeQuery()
{
    if (m_bOwnStrings)
        delete[] const_cast<XMLCh*>(m_resource);
    delete m_subject;
    for (vector<SAMLAttribute*>::const_iterator i=m_designators.begin(); i!=m_designators.end(); i++)
        delete (*i);
}

SAMLObject* SAMLAttributeQuery::clone() const
{
    return new SAMLAttributeQuery(static_cast<SAMLSubject*>(m_subject->clone()),m_resource,getDesignators().clone());
}

void SAMLAttributeQuery::fromDOM(DOMElement* e)
{
    saml::NDC ndc("fromDOM");
    SAMLObject::fromDOM(e);

    if (SAMLConfig::getConfig().strict_dom_checking)
    {
        if (XMLString::compareString(XML::SAMLP_NS,e->getNamespaceURI()))
            throw MalformedException(SAMLException::REQUESTER,"SAMLAttributeQuery::fromDOM() root element isn't in samlp namespace");
        if (XMLString::compareString(L(AttributeQuery),e->getLocalName()))
        {
            auto_ptr<saml::QName> type(saml::QName::getQNameAttribute(e,XML::XSI_NS,L(type)));
            if ((XMLString::compareString(L(Query),e->getLocalName()) && XMLString::compareString(L(SubjectQuery),e->getLocalName())) ||
                    !type.get() || XMLString::compareString(XML::SAMLP_NS,type->getNamespaceURI()) ||
                    XMLString::compareString(L(AttributeQueryType),type->getLocalName()))
                throw MalformedException(SAMLException::REQUESTER,"SAMLAttributeQuery::fromDOM() missing samlp:AttributeQuery element at root");
        }
    }

    m_root=e;
    m_bOwnStrings=false;

    m_resource = e->getAttributeNS(NULL,L(Resource));

    DOMNodeList* nlist = e->getElementsByTagNameNS(XML::SAML_NS,L(AttributeDesignator));
    for (int i=0; nlist && i<nlist->getLength(); i++)
        m_designators.push_back(new SAMLAttribute((DOMElement*)nlist->item(i)));
}

DOMNode* SAMLAttributeQuery::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::SAMLP_NS);
        return m_root;
    }
    if (!doc)
        doc=m_document;
    // Construct an AttributeQuery.
    DOMElement* q=doc->createElementNS(XML::SAMLP_NS,L(AttributeQuery));
    if (xmlns)
        q->setAttributeNS(XML::XMLNS_NS,L(xmlns),XML::SAMLP_NS);
    if (m_resource && *m_resource)
        q->setAttributeNS(NULL,L(Resource),m_resource);

    q->appendChild(m_subject->toDOM(doc));

    for (vector<SAMLAttribute*>::const_iterator i=m_designators.begin(); i!=m_designators.end(); i++)
    {
        DOMElement* desig = doc->createElementNS(XML::SAML_NS,L(AttributeDesignator));
        desig->setAttributeNS(XML::XMLNS_NS,L(xmlns),XML::SAML_NS);
        desig->setAttributeNS(NULL,L(AttributeName),(*i)->getName());
        desig->setAttributeNS(NULL,L(AttributeNamespace),(*i)->getNamespace());
        q->appendChild(desig);
    }

    return m_root=q;
}
