TSystems.RELOAD.Enroll.ReloadConfigResolve.GetConfigDocument C# (CSharp) Method

GetConfigDocument() public method

public GetConfigDocument ( ) : TextReader
return TextReader
    public TextReader GetConfigDocument() {
      TextReader tr_xml = null;

      if (!ReloadGlobals.ForceLocalConfig) {
        /* load static settings of enrollment server, in this case dynamic resolution is skipped */
        configuration_url = ReloadGlobals.ConfigurationServer;

        /* Determine Configuration server */
        if (configuration_url == "" || configuration_url == null) {
          int iRetries = 3;

          for (int i = 0; i < iRetries; i++) {
            configuration_url = new ReloadConfigResolve(m_ReloadConfig).ResolveConfigurationServer(m_ReloadConfig.OverlayName);

            if (configuration_url != null) {
              m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_TLS, String.Format("Configuration server URL as of DNS SRV: '{0}'", configuration_url));
              break;
            }
          }
        }

        HttpWebResponse httpWebesponse = null;

        if (configuration_url == null) {
          configuration_url = String.Format("https://{0}/.well-known/p2psip-enroll", m_ReloadConfig.OverlayName);
          m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_WARNING, String.Format("DNS SRV failed, set configuration server URL to {0}.", configuration_url));
        }

        try {
          m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_INFO, String.Format("Calling configuration server: {0}", configuration_url));
          //Create a Web-Request to an URL

          // HTTPS is required as specified in draft-ietf-p2psip-base-26 section 3.6.1

          HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(configuration_url);

#if !WINDOWS_PHONE
          // Additional HttpWebRequest parameters are not supported
          httpWebRequest.Timeout = ReloadGlobals.WEB_REQUEST_TIMEOUT;

          // SSL is also not supported

          if (ReloadGlobals.IgnoreSSLErrors)
            httpWebRequest.AuthenticationLevel = AuthenticationLevel.None;

          // Test for MITM Attacks!
          //if (m_ReloadConfig.DontCheckSSLCert)
          //  ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(OnCheckSSLCert);
#endif

          //Send Web-Request and receive a Web-Response
          httpWebesponse = (HttpWebResponse)httpWebRequest.GetResponse();

          m_fEnrollmentserverAvailable = true;
        }
        catch (WebException ex) {
          m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ERROR, "Configuration server not available: " + ex.Message + "using local configuration");
        }

        //Translate data from the Web-Response to a string
        if (m_fEnrollmentserverAvailable) {
          Stream dataStream = httpWebesponse.GetResponseStream();
          tr_xml = new StreamReader(dataStream, Encoding.UTF8);
          m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_TOPO, "Successfully downloaded Configuration document");
        }
      }

      // if Configuration file retrieval failed, then read from local file
      if (tr_xml == null) {
#if COMPACT_FRAMEWORK
                // Check for OsVersion to remove URI prefix if it is not WindowsCE.
                string basex = Assembly.GetExecutingAssembly().GetName().CodeBase.ToString();
                string applicationDirectory = Path.GetDirectoryName(basex);
                tr_xml = new StreamReader(applicationDirectory + @"\\reload_enroll_config.xml");
#else
        //tr_xml = new StreamReader(@"reload_enroll_config.xml");
          tr_xml = new StreamReader(@"..\..\config\config-reload-selfsigned.xml");
#endif
      }

      return tr_xml;
    }