// interopechohdrObj.cpp : Implementation of CinteropechohdrObj
//
// Copyright (c) 2002 Robert Cunnings. All rights reserved.
//
// You may copy, modify, distribute or publish this code free of charge.
//
// NO WARRANTY: This software is provided AS IS, without warranty of any kind.
//
//////////////////////////////////////////////////////////////////////
//
// CinteropechohdrObj class - implementation of header handler for 
// Round 2 interop "Group C" service.
// 
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "interopechohdrObj.h"

#include "..\wmsoapmsg\wmsoapmsg_i.c"
#include "..\wmsoapmsg\wmsoapmsg.h"
#include "..\wmsoapstruct\wmsoapstruct.h"
#include "..\wmsoapstruct\wmsoapstruct_i.c"
#include <string>
using namespace std;

static wchar_t *g_pwszHeaderNS = L"http://soapinterop.org/echoheader/";

// CinteropechohdrObj

STDMETHODIMP CinteropechohdrObj::OnProcessHeader(VARIANT SOAPMsgInterface, VARIANT_BOOL *pResult)
{
	// get interface ptr
	IDispatch *pIDispatch = SOAPMsgInterface.pdispVal;
	Iwmsoapmsg2 *pIwmsoapmsg;
	HRESULT hRes = pIDispatch->QueryInterface(IID_Iwmsoapmsg2, (void **)&pIwmsoapmsg);
	if (hRes != S_OK)
	{
		// trouble
		*pResult = VARIANT_FALSE;
		return S_OK;
	}
	// ok
	*pResult = VARIANT_TRUE;
	VARIANT_BOOL bResult;
	pIwmsoapmsg->IsRequestPhase(&bResult);
	// check message phase
	if (bResult == VARIANT_FALSE)
	{
		// this is response phase... insert headers
		BSTR bstrTargetActorURI = SysAllocString(L"");
		BSTR bstrNamespaceURI = SysAllocString(g_pwszHeaderNS);
		BSTR bstrEchoString = SysAllocString(L"echoMeStringResponse");
		BSTR bstrEchoStruct = SysAllocString(L"echoMeStructResponse");
		vector<_variant_t>::iterator iter;
		for (iter = m_arrStringHeaders.begin();
				iter != m_arrStringHeaders.end();
				iter++)
		{
			pIwmsoapmsg->AddHeaderElem(bstrNamespaceURI, bstrEchoString, *iter, VARIANT_FALSE, bstrTargetActorURI, &bResult);
		}
		for (iter = m_arrStructHeaders.begin();
				iter != m_arrStructHeaders.end();
				iter++)
		{
			pIwmsoapmsg->AddHeaderElem(bstrNamespaceURI, bstrEchoStruct, *iter, VARIANT_FALSE, bstrTargetActorURI, &bResult);
		}
		SysFreeString(bstrEchoString);
		SysFreeString(bstrEchoStruct);
		SysFreeString(bstrNamespaceURI);
		SysFreeString(bstrTargetActorURI);
		pIwmsoapmsg->Release();
		return S_OK;
	}
	long lPosition = 0;
	_variant_t varTemp;

	BSTR bstrNamespaceURI = SysAllocString(g_pwszHeaderNS);
	BSTR bstrLocalName = SysAllocString(L"echoMeStringRequest");
	while (true)
	{
		pIwmsoapmsg->GetHeaderElem(bstrNamespaceURI, bstrLocalName, &varTemp, &bResult);
		if (bResult == VARIANT_TRUE)
		{
			m_arrStringHeaders.push_back(_variant_t(varTemp));
			varTemp.Clear();
		}
		else
		{
			break;
		}
	}
	SysFreeString(bstrLocalName);
	bstrLocalName = SysAllocString(L"echoMeStructRequest");
	while (true)
	{
		pIwmsoapmsg->GetHeaderElem(bstrNamespaceURI, bstrLocalName, &varTemp, &bResult);
		if (bResult == VARIANT_TRUE)
		{
			m_arrStructHeaders.push_back(_variant_t(varTemp));
			varTemp.Clear();
		}
		else
		{
			break;
		}
	}
	SysFreeString(bstrLocalName);
	SysFreeString(bstrNamespaceURI);
	pIwmsoapmsg->Release();
	return S_OK;
}

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