/// <summary>
        /// Read XML element.
        /// </summary>
        /// <param name="xmlNamespace">The XML namespace.</param>
        /// <param name="localName">Name of the local.</param>
        /// <param name="nodeType">Type of the node.</param>
        private void InternalReadElement(
            XmlNamespace xmlNamespace,
            string localName,
            XmlNodeType nodeType)
        {
            if (xmlNamespace == XmlNamespace.NotSpecified)
            {
                this.InternalReadElement(
                    string.Empty,
                    localName,
                    nodeType);
            }
            else
            {
                this.Read(nodeType);

                if ((this.LocalName != localName) || (this.NamespaceUri != EwsUtilities.GetNamespaceUri(xmlNamespace)))
                {
                    throw new ServiceXmlDeserializationException(
                              string.Format(
                                  Strings.UnexpectedElement,
                                  EwsUtilities.GetNamespacePrefix(xmlNamespace),
                                  localName,
                                  nodeType,
                                  this.xmlReader.Name,
                                  this.NodeType));
                }
            }
        }
Exemple #2
0
 /// <summary>
 /// Writes the start element.
 /// </summary>
 /// <param name="xmlNamespace">The XML namespace.</param>
 /// <param name="localName">The local name of the element.</param>
 public void WriteStartElement(XmlNamespace xmlNamespace, string localName)
 {
     this.xmlWriter.WriteStartElement(
         EwsUtilities.GetNamespacePrefix(xmlNamespace),
         localName,
         EwsUtilities.GetNamespaceUri(xmlNamespace));
 }
Exemple #3
0
        /// <summary>
        /// Read XML element.
        /// </summary>
        /// <param name="xmlNamespace">The XML namespace.</param>
        /// <param name="localName">Name of the local.</param>
        /// <param name="nodeType">Type of the node.</param>
        private async System.Threading.Tasks.Task InternalReadElementAsync(
            XmlNamespace xmlNamespace,
            string localName,
            XmlNodeType nodeType,
            CancellationToken token)
        {
            if (xmlNamespace == XmlNamespace.NotSpecified)
            {
                this.InternalReadElement(
                    string.Empty,
                    localName,
                    nodeType);
            }
            else
            {
                await this.ReadAsync(nodeType, token);

                if ((this.LocalName != localName) || (this.NamespaceUri != EwsUtilities.GetNamespaceUri(xmlNamespace)))
                {
                    throw new ServiceXmlDeserializationException(
                              string.Format(
                                  Strings.UnexpectedElement,
                                  EwsUtilities.GetNamespacePrefix(xmlNamespace),
                                  localName,
                                  nodeType,
                                  this.xmlReader.Name,
                                  this.NodeType));
                }
            }
        }
 /// <summary>
 /// Reads the attribute value.
 /// </summary>
 /// <param name="xmlNamespace">The XML namespace.</param>
 /// <param name="attributeName">Name of the attribute.</param>
 /// <returns>Attribute value.</returns>
 public string ReadAttributeValue(XmlNamespace xmlNamespace, string attributeName)
 {
     if (xmlNamespace == XmlNamespace.NotSpecified)
     {
         return(this.ReadAttributeValue(attributeName));
     }
     else
     {
         return(this.xmlReader.GetAttribute(attributeName, EwsUtilities.GetNamespaceUri(xmlNamespace)));
     }
 }
 /// <summary>
 /// Reads to the next descendant element with the specified local name and namespace.
 /// </summary>
 /// <param name="xmlNamespace">The namespace of the element you with to move to.</param>
 /// <param name="localName">The local name of the element you wish to move to.</param>
 public void ReadToDescendant(XmlNamespace xmlNamespace, string localName)
 {
     this.xmlReader.ReadToDescendant(localName, EwsUtilities.GetNamespaceUri(xmlNamespace));
 }
 /// <summary>
 /// Determines whether current element is a end element.
 /// </summary>
 /// <param name="xmlNamespace">The XML namespace.</param>
 /// <param name="localName">Name of the local.</param>
 /// <returns>
 ///     <c>true</c> if current element is an end element; otherwise, <c>false</c>.
 /// </returns>
 public bool IsEndElement(XmlNamespace xmlNamespace, string localName)
 {
     return((this.LocalName == localName) && (this.NodeType == XmlNodeType.EndElement) &&
            ((this.NamespacePrefix == EwsUtilities.GetNamespacePrefix(xmlNamespace)) ||
             (this.NamespaceUri == EwsUtilities.GetNamespaceUri(xmlNamespace))));
 }
 /// <summary>
 /// Determines whether current element is a start element.
 /// </summary>
 /// <param name="xmlNamespace">The XML namespace.</param>
 /// <param name="localName">Name of the local.</param>
 /// <returns>
 ///     <c>true</c> if current element is a start element; otherwise, <c>false</c>.
 /// </returns>
 public bool IsStartElement(XmlNamespace xmlNamespace, string localName)
 {
     return((this.LocalName == localName) && this.IsStartElement() &&
            ((this.NamespacePrefix == EwsUtilities.GetNamespacePrefix(xmlNamespace)) ||
             (this.NamespaceUri == EwsUtilities.GetNamespaceUri(xmlNamespace))));
 }
        /// <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);
            }
        }