private void TraceWebException(EwsHttpClientException e) { // If there wasn't a response, there's nothing to trace. if (e.Response == null) { if (this.TraceEnabled) { string logMessage = string.Format( "Exception Received when sending Windows Live token request: {0}", e); this.traceListener.Trace("WindowsLiveResponse", logMessage); } return; } // 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.TraceEnabled) { using (MemoryStream memoryStream = new MemoryStream()) { //using (Stream responseStream = e.Response.Content.ReadAsStreamAsync()) //{ // // Copy response to in-memory stream and reset position to start. // EwsUtilities.CopyStream(responseStream, memoryStream); // memoryStream.Position = 0; //} //this.TraceResponse(e.Response, memoryStream); } } }
/// <summary> /// Processes an HTTP error response /// </summary> /// <param name="httpWebResponse">The HTTP web response.</param> /// <param name="webException">The web exception.</param> /// <param name="responseHeadersTraceFlag">The trace flag for response headers.</param> /// <param name="responseTraceFlag">The trace flag for responses.</param> /// <remarks> /// This method doesn't handle 500 ISE errors. This is handled by the caller since /// 500 ISE typically indicates that a SOAP fault has occurred and the handling of /// a SOAP fault is currently service specific. /// </remarks> internal void InternalProcessHttpErrorResponse( IEwsHttpWebResponse httpWebResponse, EwsHttpClientException webException, TraceFlags responseHeadersTraceFlag, TraceFlags responseTraceFlag) { EwsUtilities.Assert( httpWebResponse.StatusCode != HttpStatusCode.InternalServerError, "ExchangeServiceBase.InternalProcessHttpErrorResponse", "InternalProcessHttpErrorResponse does not handle 500 ISE errors, the caller is supposed to handle this."); this.ProcessHttpResponseHeaders(responseHeadersTraceFlag, httpWebResponse); // Deal with new HTTP error code indicating that account is locked. // The "unlock" URL is returned as the status description in the response. if (httpWebResponse.StatusCode == ExchangeServiceBase.AccountIsLocked) { string location = httpWebResponse.StatusDescription; Uri accountUnlockUrl = null; if (Uri.IsWellFormedUriString(location, UriKind.Absolute)) { accountUnlockUrl = new Uri(location); } this.TraceMessage(responseTraceFlag, string.Format("Account is locked. Unlock URL is {0}", accountUnlockUrl)); throw new AccountIsLockedException( string.Format(Strings.AccountIsLocked, accountUnlockUrl), accountUnlockUrl, webException); } }
/// <summary> /// Creates response from a EwsHttpClientException. /// </summary> /// <param name="exception">The exception.</param> /// <returns>Instance of IEwsHttpWebResponse.</returns> IEwsHttpWebResponse IEwsHttpWebRequestFactory.CreateExceptionResponse(EwsHttpClientException exception) { EwsUtilities.ValidateParam(exception, "exception"); if (exception.Response == null) { throw new InvalidOperationException("The exception does not contain response."); } return(new EwsHttpWebResponse(exception.Response)); }
/// <summary> private static async Task <GetUserPhotoResponse> GetResultOrDefault(Func <Task <object> > serviceResponseFactory) { try { return((GetUserPhotoResponse) await serviceResponseFactory().ConfigureAwait(false)); } catch (ServiceRequestException ex) { // 404 is a valid return code in the case of GetUserPhoto when the photo is // not found, so it is necessary to catch this exception here. EwsHttpClientException webException = ex.InnerException as EwsHttpClientException; if (webException != null) { var errorResponse = webException.Response; if (errorResponse != null && errorResponse.StatusCode == HttpStatusCode.NotFound) { return(GetUserPhotoRequest.GetNotFoundResponse()); } } throw; } }
/// <summary> /// Processes the web exception. /// </summary> /// <param name="webException">The web exception.</param> private async System.Threading.Tasks.Task ProcessEwsHttpClientException(EwsHttpClientException webException) { if (webException.Response != null) { using (IEwsHttpWebResponse httpWebResponse = this.Service.HttpWebRequestFactory.CreateExceptionResponse(webException)) { SoapFaultDetails soapFaultDetails = null; if (httpWebResponse.StatusCode == 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 = await ServiceRequestBase.GetResponseStream(httpWebResponse)) { // Copy response to in-memory stream and reset position to start. EwsUtilities.CopyStream(serviceResponseStream, memoryStream); memoryStream.Position = 0; } this.TraceResponseXml(httpWebResponse, memoryStream); EwsServiceXmlReader reader = new EwsServiceXmlReader(memoryStream, this.Service); soapFaultDetails = this.ReadSoapFault(reader); } } else { using (Stream stream = await ServiceRequestBase.GetResponseStream(httpWebResponse)) { EwsServiceXmlReader reader = new EwsServiceXmlReader(stream, this.Service); soapFaultDetails = this.ReadSoapFault(reader); } } if (soapFaultDetails != null) { switch (soapFaultDetails.ResponseCode) { case ServiceError.ErrorInvalidServerVersion: throw new ServiceVersionException(Strings.ServerVersionNotSupported); case ServiceError.ErrorSchemaValidation: // If we're talking to an E12 server (8.00.xxxx.xxx), a schema validation error is the same as a version mismatch error. // (Which only will happen if we send a request that's not valid for E12). if ((this.Service.ServerInfo != null) && (this.Service.ServerInfo.MajorVersion == 8) && (this.Service.ServerInfo.MinorVersion == 0)) { throw new ServiceVersionException(Strings.ServerVersionNotSupported); } break; case ServiceError.ErrorIncorrectSchemaVersion: // This shouldn't happen. It indicates that a request wasn't valid for the version that was specified. EwsUtilities.Assert( false, "ServiceRequestBase.ProcessEwsHttpClientException", "Exchange server supports requested version but request was invalid for that version"); break; case ServiceError.ErrorServerBusy: throw new ServerBusyException(new ServiceResponse(soapFaultDetails)); default: // Other error codes will be reported as remote error break; } // General fall-through case: throw a ServiceResponseException throw new ServiceResponseException(new ServiceResponse(soapFaultDetails)); } } else { this.Service.ProcessHttpErrorResponse(httpWebResponse, webException); } } } }
/// <summary> /// Processes an HTTP error response. /// </summary> /// <param name="httpWebResponse">The HTTP web response.</param> /// <param name="webException">The web exception.</param> internal abstract void ProcessHttpErrorResponse(IEwsHttpWebResponse httpWebResponse, EwsHttpClientException webException);