InternalExecute method

Future<AutodiscoverResponse> InternalExecute()
Executes this instance.

Implementation

Future<AutodiscoverResponse> InternalExecute() async {
  this.Validate();

  try {
    IEwsHttpWebRequest request = await this
        .Service
        .PrepareHttpWebRequestForUrl(this.Url!, false, false);

    this.Service.TraceHttpRequestHeaders(
        TraceFlags.AutodiscoverRequestHttpHeaders, request);

    bool needSignature = this.Service.Credentials != null &&
        this.Service.Credentials!.NeedSignature;
    bool needTrace =
        this.Service.IsTraceEnabledFor(TraceFlags.AutodiscoverRequest);

//                using (Stream requestStream = request.GetRequestStream())
//                {
    StreamConsumer<List<int>> requestStream =
        await request.GetRequestStream();
    MemoryStream memoryStream = new MemoryStream();
//                    using (MemoryStream memoryStream = new MemoryStream())
//                    {
    EwsServiceXmlWriter writer =
        new EwsServiceXmlWriter(this.Service, memoryStream);
    writer.RequireWSSecurityUtilityNamespace = needSignature;
    this.WriteSoapRequest(this.Url, writer);

//                            await writer.Flush();
    await writer.Dispose();

    if (needSignature) {
      this._service.Credentials!.Sign(memoryStream);
    }

    if (needTrace) {
      memoryStream.Position = 0;
      this.Service.TraceXml(TraceFlags.AutodiscoverRequest, memoryStream);
    }

    await EwsUtilities.CopyStream(memoryStream, requestStream);

    await memoryStream.close();
    await requestStream.close();

    IEwsHttpWebResponse webResponse = await request.GetResponse();

    try {
      if (AutodiscoverRequest.IsRedirectionResponse(webResponse)) {
        AutodiscoverResponse? response =
            this.CreateRedirectionResponse(webResponse);
        if (response != null) {
          return response;
        } else {
          throw new ServiceRemoteException(
              "Strings.InvalidRedirectionResponseReturned");
        }
      }

      Stream responseStream =
          AutodiscoverRequest.GetResponseStream(webResponse);

      MemoryStream memoryStream2 = new MemoryStream();

      try {
        // Copy response stream to in-memory stream and reset to start
        await EwsUtilities.CopyStream(
            responseStream as Stream<List<int>>, memoryStream2);
        memoryStream2.Position = 0;

        this.Service.TraceResponse(webResponse, memoryStream2);

//                            EwsXmlReader ewsXmlReader = new EwsXmlReader(memoryStream2);
        EwsServiceXmlReader ewsXmlReader =
            await EwsServiceXmlReader.Create(memoryStream2, this.Service);

        // WCF may not generate an XML declaration.
        await ewsXmlReader.Read();
        if (ewsXmlReader.NodeType == XmlNodeType.XmlDeclaration) {
          await ewsXmlReader.ReadStartElementWithNamespace(
              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");
        }

        await this.ReadSoapHeaders(ewsXmlReader);

        AutodiscoverResponse response = await this.ReadSoapBody(ewsXmlReader);

        await ewsXmlReader.ReadEndElementWithNamespace(
            XmlNamespace.Soap, XmlElementNames.SOAPEnvelopeElementName);

        if (response.ErrorCode == AutodiscoverErrorCode.NoError) {
          return response;
        } else {
          throw new AutodiscoverResponseException(
              response.ErrorCode, response.ErrorMessage!);
        }
      } finally {
        await memoryStream2.close();
      }
//                        await responseStream.close();
    } finally {
      webResponse.Close();
    }
  } on WebException catch (ex, stacktrace) {
    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 {
        await this.ProcessWebException(ex);
      }
    }

    // Wrap exception if the above code block didn't throw
    throw new ServiceRequestException(
        "ServiceRequestFailed(${ex.message})", ex, stacktrace);
  } on XmlException catch (ex, stacktrace) {
    this.Service.TraceMessage(
          TraceFlags.AutodiscoverConfiguration,
          "XML parsing error: $ex",
        );

    // Wrap exception
    throw new ServiceRequestException(
        "ServiceRequestFailed($ex)", ex, stacktrace);
  } on IOException catch (ex, stacktrace) {
    this
        .Service
        .TraceMessage(TraceFlags.AutodiscoverConfiguration, "I/O error: $ex");

    // Wrap exception
    throw new ServiceRequestException(
        "ServiceRequestFailed($ex)", ex, stacktrace);
  }
}