GetSettings<TGetSettingsResponseCollection, TSettingName> method

Future<TGetSettingsResponseCollection> GetSettings<TGetSettingsResponseCollection, TSettingName>(
  1. List<String> identities,
  2. List<TSettingName> settings,
  3. ExchangeVersion? requestedVersion,
  4. GetSettingsMethod<TGetSettingsResponseCollection, TSettingName> getSettingsMethod,
  5. GetDomainCallback getDomainMethod,
)
Either the domains or the SMTP addresses of the users. The settings. Requested version of the Exchange service. The method to use. The method to calculate the domain value.

Implementation

/// </summary>
/// <typeparam name="TGetSettingsResponseCollection">Type of response collection to return.</typeparam>
/// <typeparam name="TSettingName">Type of setting name.</typeparam>
/// <param name="identities">Either the domains or the SMTP addresses of the users.</param>
/// <param name="settings">The settings.</param>
/// <param name="requestedVersion">Requested version of the Exchange service.</param>
/// <param name="getSettingsMethod">The method to use.</param>
/// <param name="getDomainMethod">The method to calculate the domain value.</param>
/// <returns></returns>
/* private */
Future<TGetSettingsResponseCollection>
GetSettings<TGetSettingsResponseCollection, TSettingName>(
  List<String> identities,
  List<TSettingName> settings,
  ExchangeVersion? requestedVersion,
  GetSettingsMethod<TGetSettingsResponseCollection, TSettingName>
  getSettingsMethod,
  GetDomainCallback getDomainMethod,
) async {
  TGetSettingsResponseCollection response;

  // Autodiscover service only exists in E14 or later.
  if (RequestedServerVersion.index <
      _MinimumRequestVersionForAutoDiscoverSoapService.index) {
    throw ServiceVersionException("""string.Format(
                      Strings.AutodiscoverServiceIncompatibleWithRequestVersion,
                      MinimumRequestVersionForAutoDiscoverSoapService)""");
  }

  // If Url is specified, call service directly.
  if (Url != null) {
    OutParam<Uri> autodiscoverUrl = OutParam();
    autodiscoverUrl.param = Url;

    response = await getSettingsMethod(
      identities,
      settings,
      requestedVersion,
      autodiscoverUrl,
    );

    Url = autodiscoverUrl.param;
    return response;
  }
  // If Domain is specified, determine endpoint Url and call service.
  else if (!StringUtils.IsNullOrEmpty(Domain)) {
    OutParam<String> hostOutParam = OutParam()..param = Domain;
    OutParam<Uri> autodiscoverUrl = OutParam();
    autodiscoverUrl.param = await _GetAutodiscoverEndpointUrl(hostOutParam);
    Domain = hostOutParam.param;
    response = await getSettingsMethod(
      identities,
      settings,
      requestedVersion,
      autodiscoverUrl,
    );

    // If we got this far, response was successful, set Url.
    Url = autodiscoverUrl.param;
    return response;
  }
  // No Url or Domain specified, need to figure out which endpoint(s) to try.
  else {
    // Assume caller is not inside the Intranet, regardless of whether SCP Urls
    // were returned or not. SCP Urls are only relevent if one of them returns
    // valid Autodiscover settings.
    IsExternal = true;

    OutParam<Uri> autodiscoverUrlOutParam = OutParam();

    String domainName = getDomainMethod();
    OutParam<int> scpHostCountOutParam = OutParam();
    List<String> hosts = GetAutodiscoverServiceHosts(
      domainName,
      scpHostCountOutParam,
    );

    if (hosts.isEmpty) {
      throw ServiceValidationException(
        "Strings.AutodiscoverServiceRequestRequiresDomainOrUrl",
      );
    }

    for (
      int currentHostIndex = 0;
      currentHostIndex < hosts.length;
      currentHostIndex++
    ) {
      OutParam<String> hostOutParam = OutParam()
        ..param = hosts[currentHostIndex];
      bool isScpHost = currentHostIndex < scpHostCountOutParam.param!;

      if (await _TryGetAutodiscoverEndpointUrl(
        hostOutParam,
        autodiscoverUrlOutParam,
      )) {
        try {
          response = await getSettingsMethod(
            identities,
            settings,
            requestedVersion,
            autodiscoverUrlOutParam,
          );

          // If we got this far, the response was successful, set Url.
          Url = autodiscoverUrlOutParam.param;

          // Not external if Autodiscover endpoint found via SCP returned the settings.
          if (isScpHost) {
            IsExternal = false;
          }

          return response;
        } catch (_) {
          // skip (AutodiscoverResponseException, ServiceRequestException, ...)
        }
      }
    }

    // Next-to-last chance: try unauthenticated GET over HTTP to be redirected to appropriate service endpoint.
    autodiscoverUrlOutParam.param = await _GetRedirectUrl(domainName);
    if ((autodiscoverUrlOutParam.param != null) &&
        CallRedirectionUrlValidationCallback(
          autodiscoverUrlOutParam.param.toString(),
        ) &&
        await _TryGetAutodiscoverEndpointUrl(
          OutParam()..param = autodiscoverUrlOutParam.param!.host,
          autodiscoverUrlOutParam,
        )) {
      response = await getSettingsMethod(
        identities,
        settings,
        requestedVersion,
        autodiscoverUrlOutParam,
      );

      // If we got this far, the response was successful, set Url.
      Url = autodiscoverUrlOutParam.param;

      return response;
    }

    // Last Chance: try to read autodiscover SRV Record from DNS. If we find one, use
    // the hostname returned to construct an Autodiscover endpoint URL.
    // TODO: Implement SVR Record from DNS
    //                autodiscoverUrl = this.GetRedirectionUrlFromDnsSrvRecord(domainName);
    //                if ((autodiscoverUrl != null) &&
    //                    this.CallRedirectionUrlValidationCallback(autodiscoverUrl.ToString()) &&
    //                        this.TryGetAutodiscoverEndpointUrl(autodiscoverUrl.Host, out autodiscoverUrl))
    //                {
    //                    response = getSettingsMethod(
    //                                    identities,
    //                                    settings,
    //                                    requestedVersion,
    //                                    ref autodiscoverUrl);
    //
    //                    // If we got this far, the response was successful, set Url.
    //                    this.Url = autodiscoverUrl;
    //
    //                    return response;
    //                }
    //                else
    //                {
    throw AutodiscoverLocalException("Strings.AutodiscoverCouldNotBeLocated");
    //                }
  }
}