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


/* SAMLAudienceRestrictionCondition.cpp - SAML audience condition implementation

   Scott Cantor
   5/27/02

   $History:$
*/

#include "internal.h"

using namespace saml;
using namespace std;


SAMLAudienceRestrictionCondition::SAMLAudienceRestrictionCondition(const Iterator<const XMLCh*>& audiences)

{
    RTTI(SAMLAudienceRestrictionCondition);
    while (audiences.hasNext())
        m_audiences.push_back(XMLString::replicate(audiences.next()));
}

SAMLAudienceRestrictionCondition::SAMLAudienceRestrictionCondition(DOMElement* e)
{
    RTTI(SAMLAudienceRestrictionCondition);
    fromDOM(e);
}

SAMLAudienceRestrictionCondition::SAMLAudienceRestrictionCondition(istream& in) : SAMLCondition(in)
{
    RTTI(SAMLAudienceRestrictionCondition);
    fromDOM(m_document->getDocumentElement());
}


SAMLAudienceRestrictionCondition::~SAMLAudienceRestrictionCondition()
{
    if (m_bOwnStrings)
    {
        for (vector<const XMLCh*>::const_iterator i=m_audiences.begin(); i!=m_audiences.end(); i++)
            delete[] const_cast<XMLCh*>(*i);
    }
}

SAMLObject* SAMLAudienceRestrictionCondition::clone() const
{
    return new SAMLAudienceRestrictionCondition(m_audiences);
}

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

    if (SAMLConfig::getConfig().strict_dom_checking)
    {
        if (XMLString::compareString(XML::SAML_NS,e->getNamespaceURI()))
            throw MalformedException(SAMLException::RESPONDER,"SAMLAudienceRestrictionCondition::fromDOM() missing saml namespace on root element");

        if (XMLString::compareString(L(AudienceRestrictionCondition),e->getLocalName()))
        {
            auto_ptr<saml::QName> type(saml::QName::getQNameAttribute(e,XML::XSI_NS,L(type)));
            if (XMLString::compareString(L(Condition),e->getLocalName()) ||
                !type.get() || XMLString::compareString(XML::SAML_NS,type->getNamespaceURI()) ||
                XMLString::compareString(L(AudienceRestrictionConditionType),type->getLocalName()))
                throw MalformedException(SAMLException::RESPONDER, "SAMLAudienceRestrictionCondition::fromDOM() requires saml:AudienceRestrictionCondition at root");
        }
    }
    m_root=e;
    m_bOwnStrings=false;

    // Iterate over audiences.
    DOMNodeList* nlist = e->getElementsByTagNameNS(XML::SAML_NS,L(Audience));
    for (int i=0; i<nlist->getLength(); i++)
        m_audiences.push_back(nlist->item(i)->getFirstChild()->getNodeValue());
}

DOMNode* SAMLAudienceRestrictionCondition::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 a Condition.
    DOMElement* c=doc->createElementNS(XML::SAML_NS,L(AudienceRestrictionCondition));
    if (xmlns)
        c->setAttributeNS(XML::XMLNS_NS,L(xmlns),XML::SAML_NS);

    for (vector<const XMLCh*>::const_iterator i=m_audiences.begin(); i!=m_audiences.end(); i++)
        c->appendChild(doc->createElementNS(XML::SAML_NS,L(Audience)))->appendChild(doc->createTextNode(*i));

    return m_root=c;
}

bool SAMLAudienceRestrictionCondition::eval(const Iterator<const XMLCh*>& audiences) const
{
    if (m_audiences.empty())
        return true;
    else if (audiences.size()==0)
        return false;

    while (audiences.hasNext())
    {
        const XMLCh* current=audiences.next();
        for (vector<const XMLCh*>::const_iterator i=m_audiences.begin(); i!=m_audiences.end(); i++)
        {
            if (!XMLString::compareString(current,*i))
                return true;
        }
    }
    return false;
}

bool SAMLAudienceRestrictionCondition::eval(const Iterator<xstring>& audiences) const
{
    if (m_audiences.empty())
        return true;
    else if (audiences.size()==0)
        return false;

    while (audiences.hasNext())
    {
        const xstring& current=audiences.next();
        for (vector<const XMLCh*>::const_iterator i=m_audiences.begin(); i!=m_audiences.end(); i++)
        {
            if (!XMLString::compareString(current.c_str(),*i))
                return true;
        }
    }
    return false;
}
