WebHeaderCollection
{
if (!ExtensionMethods_1.StringHelper.IsNullOrEmpty(this.AnchorMailbox)) {
webHeaderCollection.Set(ServiceRequestBase.AnchorMailboxHeaderName, this.AnchorMailbox);
webHeaderCollection.Set(ServiceRequestBase.ExplicitLogonUserHeaderName, this.AnchorMailbox);
}
};
ServiceRequestBase.prototype.GetXmlElementName = function () { throw new Error("abstract method, must override"); };
ServiceRequestBase.prototype.GetMinimumRequiredServerVersion = function () { throw new Error("abstract method, must override"); };
ServiceRequestBase.prototype.GetResponseXmlElementName = function () { throw new Error("abstract method, must override"); };
//ParseResponse(reader: EwsServiceXmlReader): any { throw new Error("abstract method, must override"); }
ServiceRequestBase.prototype.ParseResponse = function (jsonBody
JsonObject
{
var serviceResponse = new ServiceResponse_1.ServiceResponse();
serviceResponse.LoadFromXmlJsObject(jsonBody, this.Service);
return serviceResponse;
};
ServiceRequestBase.prototype.WriteElementsToXml = function (writer) { throw new Error("abstract method, must override"); };
//#endregion
//BuildEwsHttpWebRequest(): IEwsHttpWebRequest { throw new Error("Could not implemented."); }
ServiceRequestBase.prototype.BuildXHR = function () {
var request = this.Service.PrepareHttpWebRequest(this.GetXmlElementName());
//try
//{
// IEwsHttpWebRequest request = this.Service.PrepareHttpWebRequest(this.GetXmlElementName());
// this.Service.TraceHttpRequestHeaders(TraceFlags.EwsRequestHttpHeaders, request);
// bool needSignature = this.Service.Credentials != null && this.Service.Credentials.NeedSignature;
// bool needTrace = this.Service.IsTraceEnabledFor(TraceFlags.EwsRequest);
// // If tracing is enabled, we generate the request in-memory so that we
// // can pass it along to the ITraceListener. Then we copy the stream to
// // the request stream.
// if (needSignature || needTrace) {
// this.TraceAndEmitRequest(request, needSignature, needTrace);
// }
// else {
this.EmitRequest(request);
// }
return request;
//}
//catch (WebException ex)
//{
// if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null) {
// this.ProcessWebException(ex);
// }
// // Wrap exception if the above code block didn't throw
// throw new ServiceRequestException(string.Format(Strings.ServiceRequestFailed, ex.Message), ex);
//}
//catch (IOException e)
//{
// // Wrap exception.
// throw new ServiceRequestException(string.Format(Strings.ServiceRequestFailed, e.Message), e);
//}
};
ServiceRequestBase.prototype.BuildResponseObjectFromJson = function (jsObject) {
if (jsObject["Header"]) {
this.ReadSoapHeader(jsObject["Header"]);
}
return this.ParseResponse(jsObject[XmlElementNames_1.XmlElementNames.SOAPBodyElementName]);
};
//CreateJsonHeaders(): JsonObject { throw new Error("Could not implemented."); }
//CreateJsonRequest(): JsonObject { throw new Error("Could not implemented."); }
ServiceRequestBase.prototype.EmitRequest = function (request
IEwsHttpWebRequest
{
if (this.Service.RenderingMethod === RenderingMode_1.RenderingMode.Xml) {
var writer = new EwsServiceXmlWriter_1.EwsServiceXmlWriter(this.service); //writer.Service
this.WriteToXml(writer);
request.data = writer.GetXML();
}
else if (this.Service.RenderingMethod === RenderingMode_1.RenderingMode.JSON) {
//JsonObject requestObject = this.CreateJsonRequest();
//using(Stream serviceRequestStream = this.GetWebRequestStream(request))
//{
// requestObject.SerializeToJson(serviceRequestStream);
//}
debugger;
}
};
//EndGetEwsHttpWebResponse(request: IEwsHttpWebRequest, asyncResult: any /*System.IAsyncResult*/): IEwsHttpWebResponse { throw new Error("Could not implemented."); }
ServiceRequestBase.prototype.GetEwsHttpWebResponse = function (request
IEwsHttpWebRequest
{ return this.service.GetXHRApi.xhr(request); };
ServiceRequestBase.prototype.GetRequestedServiceVersionString = function () {
if (this.Service.Exchange2007CompatibilityMode && this.Service.RequestedServerVersion == ExchangeVersion_1.ExchangeVersion.Exchange2007_SP1) {
return "Exchange2007";
}
else {
return ExchangeVersion_1.ExchangeVersion[this.Service.RequestedServerVersion];
}
};
//GetResponseStream(response: IEwsHttpWebResponse): any /*System.IO.Stream*/ { throw new Error("Could not implemented."); }
//GetResponseStream(response: IEwsHttpWebResponse, readTimeout: number):any /*System.IO.Stream*/{ throw new Error("ServiceRequestBase.ts - GetResponseStream : Not implemented.");}
//GetWebRequestStream(request: IEwsHttpWebRequest): any /*System.IO.Stream*/ { throw new Error("Could not implemented."); }
ServiceRequestBase.prototype.ProcessWebException = function (webException) {
if (webException.response) {
//IEwsHttpWebResponse httpWebResponse = this.Service.HttpWebRequestFactory.CreateExceptionResponse(webException);
var soapFaultDetails = null;
if (webException.status == 500
System.Net.HttpStatusCode.InternalServerError
{
//this.Service.ProcessHttpResponseHeaders(TraceFlags.EwsResponseHttpHeaders, httpWebResponse);
// If tracing is enabled, we read the entire response into a MemoryStream so that we
// can pass it along to the ITraceListener. Then we parse the response from the
// MemoryStream.
//if (this.Service.IsTraceEnabledFor(TraceFlags.EwsResponse)) {
// using(MemoryStream memoryStream = new MemoryStream())
// {
// using(Stream serviceResponseStream = ServiceRequestBase.GetResponseStream(httpWebResponse))
// {
// // Copy response to in-memory stream and reset position to start.
// EwsUtilities.CopyStream(serviceResponseStream, memoryStream);
// memoryStream.Position = 0;
// }
// if (this.Service.RenderingMethod == ExchangeService.RenderingMode.Xml) {
// this.TraceResponseXml(httpWebResponse, memoryStream);
// EwsServiceXmlReader reader = new EwsServiceXmlReader(memoryStream, this.Service);
// soapFaultDetails = this.ReadSoapFault(reader);
// }
// else if (this.Service.RenderingMethod == ExchangeService.RenderingMode.JSON) {
// this.TraceResponseJson(httpWebResponse, memoryStream);
// try {
// JsonObject jsonSoapFault = new JsonParser(memoryStream).Parse();
// soapFaultDetails = this.ReadSoapFault(jsonSoapFault);
// }
// catch (ServiceJsonDeserializationException) {
// // If no valid JSON response was returned, just return null SoapFault details
// }
// }
// else {
// throw new InvalidOperationException();
// }
// }
//}
//else {
// using(Stream stream = ServiceRequestBase.GetResponseStream(httpWebResponse))
// {
// if (this.Service.RenderingMethod == ExchangeService.RenderingMode.Xml) {
// EwsServiceXmlReader reader = new EwsServiceXmlReader(stream, this.Service);
// soapFaultDetails = this.ReadSoapFault(reader);
// }
// else if (this.Service.RenderingMethod == ExchangeService.RenderingMode.JSON) {
// try {
// JsonObject jsonSoapFault = new JsonParser(stream).Parse();
// soapFaultDetails = this.ReadSoapFault(jsonSoapFault);
// }
// catch (ServiceJsonDeserializationException) {
// // If no valid JSON response was returned, just return null SoapFault details
// }
// }
// else {
// throw new InvalidOperationException();
// }
// }
//}
//todo: fix tracing and other operations here
var reader = new EwsServiceXmlReader_1.EwsServiceXmlReader(webException.responseText, this.Service);
soapFaultDetails = this.ReadSoapFault(reader);
if (soapFaultDetails != null) {
//todo: implement soap fault error throw
this.SoapFaultDetails = soapFaultDetails;
}
}
else {
//todo: fix this
debugger;
this.Service.ProcessHttpErrorResponse(webException, webException);
}
}
};
ServiceRequestBase.prototype.ReadResponseXmlJsObject = function (jsObject) {
if (jsObject[XmlElementNames_1.XmlElementNames.SOAPHeaderElementName]) {
this.ReadSoapHeader(jsObject[XmlElementNames_1.XmlElementNames.SOAPHeaderElementName]);
}
if (!jsObject[XmlElementNames_1.XmlElementNames.SOAPBodyElementName]) {
throw new Error("invalid soap message");
}
var serviceResponse;
jsObject = jsObject[XmlElementNames_1.XmlElementNames.SOAPBodyElementName];
jsObject = jsObject[this.GetResponseXmlElementName()];
serviceResponse = this.ParseResponse(jsObject);
return serviceResponse;
};
ServiceRequestBase.prototype.ReadSoapFault = function (reader) {
var soapFaultDetails = null;
debugger;
try {
//this.ReadXmlDeclaration(reader);
reader.Read();
if (reader.LocalName != XmlElementNames_1.XmlElementNames.SOAPEnvelopeElementName) {
return soapFaultDetails;
}
// EWS can sometimes return SOAP faults using the SOAP 1.2 namespace. Get the
// namespace URI from the envelope element and use it for the rest of the parsing.
// If it's not 1.1 or 1.2, we can't continue.
var soapNamespace = EwsUtilities_1.EwsUtilities.GetNamespaceFromUri(reader.NamespaceUri);
if (soapNamespace == XmlNamespace_1.XmlNamespace.NotSpecified) {
return soapFaultDetails;
}
reader.Read();
// EWS doesn't always return a SOAP header. If this response contains a header element,
// read the server version information contained in the header.
if (reader.IsElement(soapNamespace, XmlElementNames_1.XmlElementNames.SOAPHeaderElementName)) {
do {
reader.Read();
if (reader.IsElement(XmlNamespace_1.XmlNamespace.Types, XmlElementNames_1.XmlElementNames.ServerVersionInfo)) {
this.Service.ServerInfo = ExchangeServerInfo_1.ExchangeServerInfo.Parse(reader);
}
} while (reader.HasRecursiveParent(XmlElementNames_1.XmlElementNames.SOAPHeaderElementName));
}
// Parse the fault element contained within the SOAP body.
if (reader.IsElement(soapNamespace, XmlElementNames_1.XmlElementNames.SOAPBodyElementName)) {
do {
reader.Read();
// Parse Fault element
if (reader.IsElement(soapNamespace, XmlElementNames_1.XmlElementNames.SOAPFaultElementName)) {
soapFaultDetails = SoapFaultDetails_1.SoapFaultDetails.Parse(reader, soapNamespace);
}
} while (reader.HasRecursiveParent(XmlElementNames_1.XmlElementNames.SOAPBodyElementName));
}
}
catch (XmlException) {
}
return soapFaultDetails;
};
ServiceRequestBase.prototype.ReadSoapHeader = function (jsObject) {
if (jsObject[XmlElementNames_1.XmlElementNames.ServerVersionInfo]) {
this.Service.ServerInfo = ExchangeServerInfo_1.ExchangeServerInfo.Parse(jsObject[XmlElementNames_1.XmlElementNames.ServerVersionInfo]);
}
};
ServiceRequestBase.prototype.ThrowIfNotSupportedByRequestedServerVersion = function () {
if (this.Service.RequestedServerVersion < this.GetMinimumRequiredServerVersion()) {
throw new ServiceVersionException_1.ServiceVersionException(ExtensionMethods_1.StringHelper.Format(Strings_1.Strings.RequestIncompatibleWithRequestVersion, this.GetXmlElementName(), ExchangeVersion_1.ExchangeVersion[this.GetMinimumRequiredServerVersion()]), null);
}
};
//TraceAndEmitRequest(request: IEwsHttpWebRequest, needSignature: boolean, needTrace: boolean): any { throw new Error("Could not implemented."); }
//TraceJsonRequest(requestObject: JsonObject): any { throw new Error("Could not implemented."); }
//TraceResponseJson(response: IEwsHttpWebResponse, memoryStream: any): any { throw new Error("Could not implemented."); }
//TraceResponseXml(response: IEwsHttpWebResponse, memoryStream: any): any { throw new Error("Could not implemented."); }
//TraceXmlRequest(memoryStream: any): any { throw new Error("Could not implemented."); }
ServiceRequestBase.prototype.Validate = function () { this.Service.Validate(); };
ServiceRequestBase.prototype.ValidateAndEmitRequest = function (request) {
this.Validate();
//var request = this.BuildXHR();
if (this.service.SendClientLatencies) {
var clientStatisticsToAdd = '';
//lock(clientStatisticsCache)
//{
if (ServiceRequestBase.clientStatisticsCache.length > 0) {
clientStatisticsToAdd = ServiceRequestBase.clientStatisticsCache[0];
ServiceRequestBase.clientStatisticsCache.splice(0, 1);
}
//}
if (!ExtensionMethods_1.StringHelper.IsNullOrEmpty(clientStatisticsToAdd)) {
if (request.headers[ServiceRequestBase.ClientStatisticsRequestHeader]) {
request.headers[ServiceRequestBase.ClientStatisticsRequestHeader] =
request.headers[ServiceRequestBase.ClientStatisticsRequestHeader] + clientStatisticsToAdd;
}
else {
request.headers[ServiceRequestBase.ClientStatisticsRequestHeader] = clientStatisticsToAdd;
}
}
}
//var startTime = Date.now();// DateTime.UtcNow;
//var response = XHR(request);
EwsLogging_1.EwsLogging.DebugLog("sending ews request");
EwsLogging_1.EwsLogging.DebugLog(request, true);
return this.service.GetXHRApi.xhr(request);
//try
//{
// var response = this.GetEwsHttpWebResponse(request);
//}
//finally {
// if (this.service.SendClientLatencies) {
// int clientSideLatency = (int)(DateTime.UtcNow - startTime).TotalMilliseconds;
// string requestId = string.Empty;
// string soapAction = this.GetType().Name.Replace("Request", string.Empty);
// if (response != null && response.Headers != null) {
// foreach(string requestIdHeader in ServiceRequestBase.RequestIdResponseHeaders)
// {
// string requestIdValue = response.Headers.Get(requestIdHeader);
// if (!string.IsNullOrEmpty(requestIdValue)) {
// requestId = requestIdValue;
// break;
// }
// }
// }
// StringBuilder sb = new StringBuilder();
// sb.Append("MessageId=");
// sb.Append(requestId);
// sb.Append(",ResponseTime=");
// sb.Append(clientSideLatency);
// sb.Append(",SoapAction=");
// sb.Append(soapAction);
// sb.Append(";");
// lock(clientStatisticsCache)
// {
// clientStatisticsCache.Add(sb.ToString());
// }
// }
//}
//return response;
};
//WrapStream(responseStream: any /*System.IO.Stream*/, contentEncoding: string): any /*System.IO.Stream*/ { throw new Error("Could not implemented."); }
/// <summary>
/// Writes XML attributes.
/// </summary>
/// <remarks>
/// Subexport class will override if it has XML attributes.
/// </remarks>
/// <param name="writer">The writer.</param>
ServiceRequestBase.prototype.WriteAttributesToXml = function (writer) { };
/// <summary>
/// Writes XML body.
/// </summary>
/// <param name="writer">The writer.</param>
ServiceRequestBase.prototype.WriteBodyToXml = function (writer) {
writer.WriteStartElement(XmlNamespace_1.XmlNamespace.Messages, this.GetXmlElementName());
this.WriteAttributesToXml(writer);
this.WriteElementsToXml(writer);
writer.WriteEndElement(); // m:this.GetXmlElementName()
};
//#region HttpWebRequest-based implementation
/// <summary>
/// Writes XML.
/// </summary>
/// <param name="writer">The writer.</param>
ServiceRequestBase.prototype.WriteToXml = function (writer) {
writer.WriteStartElement(XmlNamespace_1.XmlNamespace.Soap, XmlElementNames_1.XmlElementNames.SOAPEnvelopeElementName);
writer.WriteAttributeValue("xmlns", EwsUtilities_1.EwsUtilities.EwsXmlSchemaInstanceNamespacePrefix, EwsUtilities_1.EwsUtilities.EwsXmlSchemaInstanceNamespace);
writer.WriteAttributeValue("xmlns", EwsUtilities_1.EwsUtilities.EwsMessagesNamespacePrefix, EwsUtilities_1.EwsUtilities.EwsMessagesNamespace);
writer.WriteAttributeValue("xmlns", EwsUtilities_1.EwsUtilities.EwsTypesNamespacePrefix, EwsUtilities_1.EwsUtilities.EwsTypesNamespace);
if (writer.RequireWSSecurityUtilityNamespace) {
writer.WriteAttributeValue("xmlns", EwsUtilities_1.EwsUtilities.WSSecurityUtilityNamespacePrefix, EwsUtilities_1.EwsUtilities.WSSecurityUtilityNamespace);
}
writer.WriteStartElement(XmlNamespace_1.XmlNamespace.Soap, XmlElementNames_1.XmlElementNames.SOAPHeaderElementName);
if (this.Service.Credentials != null) {
//todo: fix extra soap headers if required
this.Service.Credentials.EmitExtraSoapHeaderNamespaceAliases(null);
}
// Emit the RequestServerVersion header
if (!this.Service.SuppressXmlVersionHeader) {
writer.WriteStartElement(XmlNamespace_1.XmlNamespace.Types, XmlElementNames_1.XmlElementNames.RequestServerVersion);
writer.WriteAttributeValue(XmlAttributeNames_1.XmlAttributeNames.Version, this.GetRequestedServiceVersionString());
writer.WriteEndElement(); // RequestServerVersion
}
// Against Exchange 2007 SP1, we always emit the simplified time zone header. It adds very little to
// the request, so bandwidth consumption is not an issue. Against Exchange 2010 and above, we emit
// the full time zone header but only when the request actually needs it.
//
// The exception to this is if we are in Exchange2007 Compat Mode, in which case we should never emit
// the header. (Note: Exchange2007 Compat Mode is enabled for testability purposes only.)
//
if ((this.Service.RequestedServerVersion == ExchangeVersion_1.ExchangeVersion.Exchange2007_SP1 || this.EmitTimeZoneHeader) &&
(!this.Service.Exchange2007CompatibilityMode)) {
writer.WriteStartElement(XmlNamespace_1.XmlNamespace.Types, XmlElementNames_1.XmlElementNames.TimeZoneContext);
this.Service.TimeZoneDefinition.WriteToXml(writer);
writer.WriteEndElement(); // TimeZoneContext
writer.IsTimeZoneHeaderEmitted = true;
}
// Emit the MailboxCulture header
if (this.Service.PreferredCulture != null) {
//todo: fix preferred culture.
writer.WriteElementValue(XmlNamespace_1.XmlNamespace.Types, XmlElementNames_1.XmlElementNames.MailboxCulture, this.Service.PreferredCulture.Name);
}
// Emit the DateTimePrecision header
if (this.Service.DateTimePrecision != DateTimePrecision_1.DateTimePrecision.Default) {
writer.WriteElementValue(XmlNamespace_1.XmlNamespace.Types, XmlElementNames_1.XmlElementNames.DateTimePrecision, DateTimePrecision_1.DateTimePrecision[this.Service.DateTimePrecision]);
}
// Emit the ExchangeImpersonation header
if (this.Service.ImpersonatedUserId != null) {
this.Service.ImpersonatedUserId.WriteToXml(writer);
}
else if (this.Service.PrivilegedUserId != null) {
//todo: find scenario if this is used anywhere, did not find any reference in MSDN
this.Service.PrivilegedUserId.WriteToXml(writer, this.Service.RequestedServerVersion);
}
else if (this.Service.ManagementRoles != null) {
this.Service.ManagementRoles.WriteToXml(writer);
}
if (this.Service.Credentials != null) {
this.Service.Credentials.SerializeExtraSoapHeaders(writer, this.GetXmlElementName());
}
this.Service.DoOnSerializeCustomSoapHeaders(writer);
writer.WriteEndElement(); // soap:Header
writer.WriteStartElement(XmlNamespace_1.XmlNamespace.Soap, XmlElementNames_1.XmlElementNames.SOAPBodyElementName);
this.WriteBodyToXml(writer);
writer.WriteEndElement(); // soap:Body
writer.WriteEndElement(); // soap:Envelope
};
//#region private static and const
//ref:this may be from newer code overall i am using, update when updating from ews base code library //todo:
///
/// The two contants below are used to set the AnchorMailbox and ExplicitLogonUser values
/// in the request header.
///
///
/// Note: Setting this values will route the request directly to the backend hosting the
/// AnchorMailbox. These headers should be used primarily for UnifiedGroup scenario where
/// a request needs to be routed directly to the group mailbox versus the user mailbox.
///
ServiceRequestBase.AnchorMailboxHeaderName = "X-AnchorMailbox";
ServiceRequestBase.ExplicitLogonUserHeaderName = "X-OWA-ExplicitLogonUser";
ServiceRequestBase.XMLSchemaNamespace = "http://www.w3.org/2001/XMLSchema";
ServiceRequestBase.XMLSchemaInstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance";
ServiceRequestBase.ClientStatisticsRequestHeader = "X-ClientStatistics";
ServiceRequestBase.RequestIdResponseHeaders = ["RequestId", "request-id"];
ServiceRequestBase.clientStatisticsCache = []; //System.Collections.Generic.List<string>;
return ServiceRequestBase;
})();
exports.ServiceRequestBase = ServiceRequestBase;