private void TraceWebException(WebException 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.GetResponseStream()) { // Copy response to in-memory stream and reset position to start. EwsUtilities.CopyStream(responseStream, memoryStream); memoryStream.Position = 0; } this.TraceResponse((HttpWebResponse)e.Response, memoryStream); } } }
/// <summary> /// Traces the and emits the request. /// </summary> /// <param name="request">The request.</param> /// <param name="needSignature"></param> /// <param name="needTrace"></param> private void TraceAndEmitRequest(IEwsHttpWebRequest request, bool needSignature, bool needTrace) { using (MemoryStream memoryStream = new MemoryStream()) { using (EwsServiceXmlWriter writer = new EwsServiceXmlWriter(this.Service, memoryStream)) { writer.RequireWSSecurityUtilityNamespace = needSignature; this.WriteToXml(writer); } if (needSignature) { this.service.Credentials.Sign(memoryStream); } if (needTrace) { this.TraceXmlRequest(memoryStream); } using (Stream serviceRequestStream = this.GetWebRequestStream(request)) { EwsUtilities.CopyStream(memoryStream, serviceRequestStream); } } }
/// <summary> /// Reads the response with error handling /// </summary> /// <param name="response">The response.</param> /// <returns>Service response.</returns> private object ReadResponse(IEwsHttpWebResponse response) { object serviceResponse; try { this.Service.ProcessHttpResponseHeaders(TraceFlags.EwsResponseHttpHeaders, response); // 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(response)) { // Copy response to in-memory stream and reset position to start. EwsUtilities.CopyStream(serviceResponseStream, memoryStream); memoryStream.Position = 0; } this.TraceResponseXml(response, memoryStream); serviceResponse = this.ReadResponseXml(memoryStream, response.Headers); } } else { using (Stream responseStream = ServiceRequestBase.GetResponseStream(response)) { serviceResponse = this.ReadResponseXml(responseStream, response.Headers); } } } catch (WebException e) { if (e.Response != null) { IEwsHttpWebResponse exceptionResponse = this.Service.HttpWebRequestFactory.CreateExceptionResponse(e); this.Service.ProcessHttpResponseHeaders(TraceFlags.EwsResponseHttpHeaders, exceptionResponse); } throw new ServiceRequestException(string.Format(Strings.ServiceRequestFailed, e.Message), e); } catch (IOException e) { // Wrap exception. throw new ServiceRequestException(string.Format(Strings.ServiceRequestFailed, e.Message), e); } finally { if (response != null) { response.Close(); } } return(serviceResponse); }
/* * /// <summary> * /// Gets a <see cref="T:System.IO.Stream"/> object to use to write request data. * /// </summary> * /// <returns> * /// A <see cref="T:System.IO.Stream"/> to use to write request data. * /// </returns> * Stream IEwsHttpWebRequest.GetRequestStream() * { * // return this.request.GetRequestStream(); * return this.contentStream; * } */ void IEwsHttpWebRequest.SetRequestStream(Stream requestContent) { MemoryStream memStream = new MemoryStream(); EwsUtilities.CopyStream(requestContent, memStream); memStream.Position = 0; using (StreamReader reader = new StreamReader(memStream)) { this.requestContent = reader.ReadToEnd(); } }
/// <summary> /// Processes the web exception. /// </summary> /// <param name="webException">The web exception.</param> private void ProcessWebException(WebException webException) { if (webException.Response != null) { IEwsHttpWebResponse httpWebResponse = this.Service.HttpWebRequestFactory.CreateExceptionResponse(webException); SoapFaultDetails soapFaultDetails; if (httpWebResponse.StatusCode == HttpStatusCode.InternalServerError) { // 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.AutodiscoverRequest)) { using (MemoryStream memoryStream = new MemoryStream()) { using (Stream serviceResponseStream = AutodiscoverRequest.GetResponseStream(httpWebResponse)) { // Copy response to in-memory stream and reset position to start. EwsUtilities.CopyStream(serviceResponseStream, memoryStream); memoryStream.Position = 0; } this.Service.TraceResponse(httpWebResponse, memoryStream); EwsXmlReader reader = new EwsXmlReader(memoryStream); soapFaultDetails = this.ReadSoapFault(reader); } } else { using (Stream stream = AutodiscoverRequest.GetResponseStream(httpWebResponse)) { EwsXmlReader reader = new EwsXmlReader(stream); soapFaultDetails = this.ReadSoapFault(reader); } } if (soapFaultDetails != null) { throw new ServiceResponseException(new ServiceResponse(soapFaultDetails)); } } else { this.Service.ProcessHttpErrorResponse(httpWebResponse, webException); } } }
/// <summary> /// Traces the and emits the request. /// </summary> /// <param name="request">The request.</param> /// <param name="needSignature"></param> /// <param name="needTrace"></param> private void TraceAndEmitRequest(IEwsHttpWebRequest request, bool needSignature, bool needTrace) { if (this.service.RenderingMethod == ExchangeService.RenderingMode.Xml) { using (MemoryStream memoryStream = new MemoryStream()) { using (EwsServiceXmlWriter writer = new EwsServiceXmlWriter(this.Service, memoryStream)) { writer.RequireWSSecurityUtilityNamespace = needSignature; this.WriteToXml(writer); } if (needSignature) { this.service.Credentials.Sign(memoryStream); } if (needTrace) { this.TraceXmlRequest(memoryStream); } using (Stream serviceRequestStream = this.GetWebRequestStream(request)) { EwsUtilities.CopyStream(memoryStream, serviceRequestStream); } } } else if (this.service.RenderingMethod == ExchangeService.RenderingMode.JSON) { JsonObject requestObject = this.CreateJsonRequest(); this.TraceJsonRequest(requestObject); using (Stream serviceRequestStream = this.GetWebRequestStream(request)) { requestObject.SerializeToJson(serviceRequestStream); } } }
/// <summary> /// Processes the web exception. /// </summary> /// <param name="webException">The web exception.</param> private void ProcessWebException(WebException webException) { if (webException.Response != null) { 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 = 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 = 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.ProcessWebException", "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> /// Executes this instance. /// </summary> /// <returns></returns> internal AutodiscoverResponse InternalExecute() { this.Validate(); try { IEwsHttpWebRequest request = this.Service.PrepareHttpWebRequestForUrl(this.Url); this.Service.TraceHttpRequestHeaders(TraceFlags.AutodiscoverRequestHttpHeaders, request); bool needSignature = this.Service.Credentials != null && this.Service.Credentials.NeedSignature; bool needTrace = this.Service.IsTraceEnabledFor(TraceFlags.AutodiscoverRequest); using (MemoryStream memoryStream = new MemoryStream()) { using (EwsServiceXmlWriter writer = new EwsServiceXmlWriter(this.Service, memoryStream)) { writer.RequireWSSecurityUtilityNamespace = needSignature; this.WriteSoapRequest( this.Url, writer); } if (needSignature) { this.service.Credentials.Sign(memoryStream); } if (needTrace) { memoryStream.Position = 0; this.Service.TraceXml(TraceFlags.AutodiscoverRequest, memoryStream); } request.SetRequestStream(memoryStream); } using (IEwsHttpWebResponse webResponse = request.GetResponse()) { if (AutodiscoverRequest.IsRedirectionResponse(webResponse)) { AutodiscoverResponse response = this.CreateRedirectionResponse(webResponse); if (response != null) { return(response); } else { throw new ServiceRemoteException(Strings.InvalidRedirectionResponseReturned); } } using (Stream responseStream = AutodiscoverRequest.GetResponseStream(webResponse)) { using (MemoryStream memoryStream = new MemoryStream()) { // Copy response stream to in-memory stream and reset to start EwsUtilities.CopyStream(responseStream, memoryStream); memoryStream.Position = 0; this.Service.TraceResponse(webResponse, memoryStream); EwsXmlReader ewsXmlReader = new EwsXmlReader(memoryStream); // WCF may not generate an XML declaration. ewsXmlReader.Read(); if (ewsXmlReader.NodeType == XmlNodeType.XmlDeclaration) { ewsXmlReader.ReadStartElement(XmlNamespace.Soap, XmlElementNames.SOAPEnvelopeElementName); } else if ((ewsXmlReader.NodeType != XmlNodeType.Element) || (ewsXmlReader.LocalName != XmlElementNames.SOAPEnvelopeElementName) || (ewsXmlReader.NamespaceUri != EwsUtilities.GetNamespaceUri(XmlNamespace.Soap))) { throw new ServiceXmlDeserializationException(Strings.InvalidAutodiscoverServiceResponse); } this.ReadSoapHeaders(ewsXmlReader); AutodiscoverResponse response = this.ReadSoapBody(ewsXmlReader); ewsXmlReader.ReadEndElement(XmlNamespace.Soap, XmlElementNames.SOAPEnvelopeElementName); if (response.ErrorCode == AutodiscoverErrorCode.NoError) { return(response); } else { throw new AutodiscoverResponseException(response.ErrorCode, response.ErrorMessage); } } } } } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null) { IEwsHttpWebResponse httpWebResponse = this.Service.HttpWebRequestFactory.CreateExceptionResponse(ex); if (AutodiscoverRequest.IsRedirectionResponse(httpWebResponse)) { this.Service.ProcessHttpResponseHeaders( TraceFlags.AutodiscoverResponseHttpHeaders, httpWebResponse); AutodiscoverResponse response = this.CreateRedirectionResponse(httpWebResponse); if (response != null) { return(response); } } else { this.ProcessWebException(ex); } } // Wrap exception if the above code block didn't throw throw new ServiceRequestException(string.Format(Strings.ServiceRequestFailed, ex.Message), ex); } catch (XmlException ex) { this.Service.TraceMessage( TraceFlags.AutodiscoverConfiguration, string.Format("XML parsing error: {0}", ex.Message)); // Wrap exception throw new ServiceRequestException(string.Format(Strings.ServiceRequestFailed, ex.Message), ex); } catch (IOException ex) { this.Service.TraceMessage( TraceFlags.AutodiscoverConfiguration, string.Format("I/O error: {0}", ex.Message)); // Wrap exception throw new ServiceRequestException(string.Format(Strings.ServiceRequestFailed, ex.Message), ex); } }