// soap12testhdrUltRcvObj.cpp : Implementation of Csoap12testhdrUltRcvObj

#include "stdafx.h"
#include "soap12testhdrUltRcvObj.h"

#include "..\soap_source\xmlparser.h"
#include "..\soap_source\charencode.h"
#include "..\wmsoapmsg\wmsoapmsg_i.c"
#include "..\wmsoapmsg\wmsoapmsg.h"
#include "..\soap_source\soap.h"

static wchar_t *g_pwszHeaderNS = L"http://example.org/ts-tests";

// Csoap12testhdrUltRcvObj

STDMETHODIMP Csoap12testhdrUltRcvObj::OnTestMUHeaderBlock(BSTR bstrNamespace, BSTR bstrLocalName, VARIANT_BOOL *pResult)
{
    *pResult = VARIANT_FALSE;
    if (wcscmp(bstrNamespace, g_pwszHeaderNS) == 0)
    {
        if (wcscmp(bstrLocalName, L"echoOkUltimateReceiver") == 0)
        {
            *pResult = VARIANT_TRUE;
            return S_OK;
        }
    }
    return S_OK;
}

STDMETHODIMP Csoap12testhdrUltRcvObj::OnProcessHeader(VARIANT SOAPMsgInterface, VARIANT_BOOL *pResult)
{
	// set up, assume success
	m_bAbortProcessing = false;
	*pResult = VARIANT_TRUE;
	// get interface ptr
	IDispatch *pIDispatch = SOAPMsgInterface.pdispVal;
	Iwmsoapmsg2 *pIwmsoapmsg;
	HRESULT hRes = pIDispatch->QueryInterface(IID_Iwmsoapmsg2, (void **)&pIwmsoapmsg);
	if (hRes != S_OK)
	{
		return E_FAIL;
	}
	VARIANT_BOOL bResult;
	pIwmsoapmsg->IsRequestPhase(&bResult);
	if (bResult == VARIANT_TRUE)
	{
		// this is request phase
		if (ProcessRequest(pIwmsoapmsg) == false)
		{
            m_bAbortProcessing = true;
		}
		pIwmsoapmsg->Release();
		// abort processing?
		if (m_bAbortProcessing == true)
		{
			// set out parameter to signal abort
			*pResult = VARIANT_FALSE;
		}
		return S_OK;
	}
	// response phase
	if (ProcessResponse(pIwmsoapmsg) == false)
	{
        m_bAbortProcessing = true;
	}
	pIwmsoapmsg->Release();
	if (m_bAbortProcessing == true)
	{
		// set out parameter to signal abort
		*pResult = VARIANT_FALSE;
	}
	return S_OK;
}

bool Csoap12testhdrUltRcvObj::ProcessRequest(Iwmsoapmsg2 *pIwmsoapmsg)
{
    // reset flags
    m_bEchoOkFound = false;

    // get "echoOk" header element
	BSTR bstrNamespaceURI = SysAllocString(g_pwszHeaderNS);
	BSTR bstrLocalName = SysAllocString(L"echoOkUltimateReceiver");
	BSTR bstrXML;
	wstring wstrXML;
	VARIANT_BOOL bResult;
	HRESULT hRes = pIwmsoapmsg->GetHeaderElemLiteral(bstrNamespaceURI, bstrLocalName, &bstrXML, &bResult);
	SysFreeString(bstrLocalName);
	SysFreeString(bstrNamespaceURI);
	if (hRes != S_OK)
	{
		return false;
	}
    if (bResult == VARIANT_TRUE)
    {
	    wstrXML = bstrXML;
	    SysFreeString(bstrXML);
	    // set up for parsing
	    WMXMLParser Parser;
	    bool bRtn = true;
	    // parse the XML fragment representing the header entry
	    try
	    {
		    Parser.ParseXML_WideString(wstrXML, NULL);
	    }
	    catch(int e)
	    {
		    int x = e;
		    return false;
	    }
	    // get the WMXMLElement pointer representing root node of parse tree
	    WMXMLElement *pRoot = Parser.DetachDocRoot();
	    if (pRoot == NULL)
	    {
		    return false;
	    }
        // was this targeted at role "none"?
        wstring wstrRole;
        bool bIgnoreIt = false;
        if (pRoot->GetAttribute(g_pwszSoap12EnvelopeNS, wstring(L"role"), wstrRole) == true)
        {
            if (wstrRole == g_pwszSoap12RoleNone)
            {
                bIgnoreIt = true;
            }
        }
        if (bIgnoreIt == false)
        {
	        m_wstrEchoOk = pRoot->GetContent();
            m_bEchoOkFound = true;
        }
	    delete pRoot;
    }
	return true;
}

bool Csoap12testhdrUltRcvObj::ProcessResponse(Iwmsoapmsg2 *pIwmsoapmsg)
{
    if (m_bEchoOkFound == true)
    {
	    // create new header entry
	    WMXMLElement *pResponseOk = new WMXMLElement(wstring(L"test:responseOkUltimateReceiver"));
	    if (pResponseOk == NULL)
	    {
		    return false;
	    }
		pResponseOk->SetNSPrefix(wstring(L"test"), wstring(g_pwszHeaderNS));
    	pResponseOk->SetNSPrefix(wstring(L"SOAP-ENV"), wstring(g_pwszSoap12EnvelopeNS));
		pResponseOk->AppendContent(m_wstrEchoOk);

	    // now serialize
	    wstring wstrBuffer;
	    pResponseOk->Serialize(NULL, wstrBuffer);
	    delete pResponseOk;

	    BSTR bstrBuffer = SysAllocString(wstrBuffer.c_str());
	    BSTR bstrNamespaceURI = SysAllocString(g_pwszHeaderNS);
	    BSTR bstrLocalName = SysAllocString(L"responseOk");
	    VARIANT_BOOL bMustUnderstand = VARIANT_FALSE;
	    BSTR bstrActorURI = SysAllocString(L"");
	    BSTR bstrEncodingStyle = SysAllocString(L"");
	    VARIANT_BOOL bResult;
		pIwmsoapmsg->AddHeaderElemLiteral(  bstrNamespaceURI,
											bstrLocalName,
											bstrBuffer,
											bMustUnderstand,
											bstrActorURI,
											bstrEncodingStyle,
											&bResult);
	    SysFreeString(bstrNamespaceURI);
	    SysFreeString(bstrLocalName);
	    SysFreeString(bstrActorURI);
	    SysFreeString(bstrEncodingStyle);
	    SysFreeString(bstrBuffer);
        if (bResult == VARIANT_FALSE)
        {
            return false;
        }
    }
	return true;
}

