📄 contactsformathandler.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
#include "ContactsFormatHandler.h"
#include "RequestParameters.h"
#include "MemTracking.h"
#include "Settings.h"
#include "Tracing.h"
#include "Includes.h"
#include "Utilities.h"
#include "SecurityUtils.h"
static ParseParameters s_ContactsParseParameters;
static ParsePropertyMapping s_ContactsPropertyMapping[NUM_CONTACTS_PROPERTIES + 1];
/*------------------------------------------------------------------------------
CContactsFormatHandler::CContactsFormatHandler
Constructor
------------------------------------------------------------------------------*/
CContactsFormatHandler::CContactsFormatHandler() : CGenericFormatHandler()
{
MemTrackAdd();
m_strServerName = L"";
m_strFilterFirstName = L"";
m_strFilterLastName = L"";
}
/*------------------------------------------------------------------------------
CContactsFormatHandler::~CContactsFormatHandler
Destructor
------------------------------------------------------------------------------*/
CContactsFormatHandler::~CContactsFormatHandler()
{
MemTrackRemove();
//the ce::wstring member var's clean themselves up
}
/*------------------------------------------------------------------------------
CContactsFormatHandler::BuildParseTable
Builds the parsing table from registry values
------------------------------------------------------------------------------*/
/* static */HRESULT CContactsFormatHandler::BuildParseTable()
{
s_ContactsParseParameters.c_wszNewDataRecordName = c_SettingContactsNewDataRecord;
s_ContactsPropertyMapping[0].idx = CONTACTS_PARSE_DISPLAYNAME;
s_ContactsPropertyMapping[0].c_wszProperty = (const WCHAR*)c_SettingContactsDispName;
s_ContactsPropertyMapping[1].idx = CONTACTS_PARSE_WORKPHONE;
s_ContactsPropertyMapping[1].c_wszProperty = (const WCHAR*)c_SettingContactsWorkPhone;
s_ContactsPropertyMapping[2].idx = CONTACTS_PARSE_HOMEPHONE;
s_ContactsPropertyMapping[2].c_wszProperty = (const WCHAR*)c_SettingContactsHomePhone;
s_ContactsPropertyMapping[3].idx = CONTACTS_PARSE_MOBILE;
s_ContactsPropertyMapping[3].c_wszProperty = (const WCHAR*)c_SettingContactsMobile;
s_ContactsPropertyMapping[4].idx = CONTACTS_PARSE_EMAIL;
s_ContactsPropertyMapping[4].c_wszProperty = (const WCHAR*)c_SettingContactsEmail;
s_ContactsPropertyMapping[5].idx = CONTACTS_PARSE_HOMEADDR;
s_ContactsPropertyMapping[5].c_wszProperty = (const WCHAR*)c_SettingContactsHomeAddr;
s_ContactsPropertyMapping[6].idx = CONTACTS_PARSE_WORKADDR;
s_ContactsPropertyMapping[6].c_wszProperty = (const WCHAR*)c_SettingContactsWorkAddr;
s_ContactsPropertyMapping[7].idx = INVALID_PARSE_INDEX;
s_ContactsPropertyMapping[7].c_wszProperty = NULL;
s_ContactsParseParameters.c_rgPropertyMappings = s_ContactsPropertyMapping;
return S_OK;
}
/*------------------------------------------------------------------------------
CContactsFormatHandler::Initialize
Initialized the format handler and the superclass
Parameters:
wszServerName: Name of the exchange server
wszUsername: Username
wszPassword: Password
pvParams: (VOID*) ContactsSearchCriteria
Returns (HRESULT): indicating success or failure of the call
------------------------------------------------------------------------------*/
HRESULT CContactsFormatHandler::Initialize(
const WCHAR * wszServerName,
VOID * pvParams
)
{
//Initialize the super class with the appropriate parse mapping table
HRESULT hr = CGenericFormatHandler::Initialize(&s_ContactsParseParameters);
ContactsSearchCriteria *pCriteria = (ContactsSearchCriteria*)pvParams;
PREFAST_ASSERT(pCriteria != NULL);
if (SUCCEEDED(hr))
{
//wstr's assign does a strcpy
if (!m_strServerName.assign(wszServerName))
{
hr = E_OUTOFMEMORY;
}
}
if (SUCCEEDED(hr))
{
if (
(pCriteria->wszFirstName && !m_strFilterFirstName.assign(pCriteria->wszFirstName)) ||
(pCriteria->wszLastName && !m_strFilterLastName.assign(pCriteria->wszLastName))
)
{
hr = E_OUTOFMEMORY;
}
}
return hr;
}
/*------------------------------------------------------------------------------
CContactsFormatHandler::GetFormattedHttpParameters
Get the
Method: SEARCH
Body: <xml>
Url: <server>/exchange/<user>/contacts/
------------------------------------------------------------------------------*/
HRESULT CContactsFormatHandler::GetFormattedHttpParameters(
BSTR * pbstrMethod,
BSTR * pbstrUrl,
BSTR * pbstrBody
)
{
//check initialization
if (m_strServerName[0] == L'\0')
{
return OWAEC_E_NOSERVER;
}
//check params of the trusted internal function
PREFAST_ASSERT(
pbstrMethod != NULL &&
pbstrUrl != NULL &&
pbstrBody != NULL
);
HRESULT hr = S_OK;
WCHAR wszUsername[MAX_PATH] = L"";
ce::wstring wstrBuffer;
wstrBuffer.reserve(MAX_PATH*3);
//Alloc the method
*pbstrMethod = SysAllocString(c_SettingContactsMethod);
if (*pbstrMethod == NULL)
{
hr = E_OUTOFMEMORY;
goto exit;
}
//allocate the url
hr = GetLoggedInUserInformation(
wszUsername,
ARRAYSIZE(wszUsername),
CRED_ITEM_USERNAME
);
if (FAILED(hr) || wszUsername[0] == L'\0')
{
return OWAEC_E_NOUSER;
}
hr = StringCchPrintf (
wstrBuffer.get_buffer(),
wstrBuffer.capacity(),
c_SettingContactsUrlFmt,
(const WCHAR*)m_strServerName.get_buffer(),
wszUsername
);
if (FAILED(hr))
{
ASSERT(FALSE);
goto exit;
}
*pbstrUrl = SysAllocString(wstrBuffer.get_buffer());
if (*pbstrUrl == NULL)
{
hr = E_OUTOFMEMORY;
goto exit;
}
//allocatethe the Body
wstrBuffer.clear();
hr = StringCchPrintf(
wstrBuffer.get_buffer(),
wstrBuffer.capacity(),
c_SettingContactsBodyStartFmt,
wszUsername
);
if (FAILED(hr))
{
ASSERT(FALSE);
goto exit;
}
if (m_strFilterFirstName[0])
{
ce::auto_bstr bstrEncodedFilterFirstName;
hr = EncodeString(
m_strFilterFirstName,
&bstrEncodedFilterFirstName,
FALSE //BODY of request - not URL
);
if (FAILED(hr))
{
ASSERT(FALSE);
goto exit;
}
if(!wstrBuffer.append(bstrEncodedFilterFirstName))
{
hr = E_OUTOFMEMORY;
goto exit;
}
}
if (m_strFilterLastName[0])
{
ce::auto_bstr bstrEncodedFilterLastName;
hr = EncodeString(
m_strFilterLastName,
&bstrEncodedFilterLastName,
FALSE //BODY of request - not URL
);
if (FAILED(hr))
{
ASSERT(FALSE);
goto exit;
}
if(!wstrBuffer.append(bstrEncodedFilterLastName))
{
hr = E_OUTOFMEMORY;
goto exit;
}
}
if(!wstrBuffer.append(c_SettingContactsBodyEndFmt))
{
hr = E_OUTOFMEMORY;
goto exit;
}
//allocate the buffer
*pbstrBody = SysAllocString(wstrBuffer.get_buffer());
if (*pbstrBody == NULL)
{
hr = E_OUTOFMEMORY;
goto exit;
}
exit:
//if we are out of memory - clear all the strings
if (FAILED(hr))
{
//SysFreeString(NULL) is legal
SysFreeString(*pbstrBody);
SysFreeString(*pbstrMethod);
SysFreeString(*pbstrUrl);
}
return hr;
}
/*------------------------------------------------------------------------------
CContactsFormatHandler::SetAdditionalHeaders
Sets "Content-Type: text/xml" for the contacts request
------------------------------------------------------------------------------*/
HRESULT CContactsFormatHandler::SetAdditionalHeaders(
IXMLHTTPRequest * piRequest
)
{
PREFAST_ASSERT(piRequest != NULL);
HRESULT hr = AddLocaleHeader(
piRequest
);
if (FAILED(hr))
{
ASSERT(FALSE);
return hr;
}
return CGenericFormatHandler::SetAdditionalHeadersFromValue(
piRequest,
c_SettingContactsAdditionalHeaderName,
c_SettingContactsAdditionalHeaderValue
);
}
/*------------------------------------------------------------------------------
CContactsFormatHandler::GetDataRecordSize
Returns the size of an datarecord for this type of request
------------------------------------------------------------------------------*/
INT CContactsFormatHandler::GetDataRecordSize()
{
return NUM_CONTACTS_PROPERTIES;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -