Esempio n. 1
0
        public virtual async Task <EurekaHttpResponse> StatusUpdateAsync(string appName, string id, InstanceStatus newStatus, InstanceInfo info)
        {
            if (string.IsNullOrEmpty(appName))
            {
                throw new ArgumentException(nameof(appName));
            }

            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException(nameof(id));
            }

            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }


            var queryArgs = new Dictionary <string, string>()
            {
                { "value", newStatus.ToString() },
                { "lastDirtyTimestamp", DateTimeConversions.ToJavaMillis(new DateTime(info.LastDirtyTimestamp, DateTimeKind.Utc)).ToString() }
            };

            HttpClient client     = GetHttpClient(_config);
            var        requestUri = GetRequestUri(_serviceUrl + "apps/" + appName + "/" + id + "/status", queryArgs);
            var        request    = GetRequestMessage(HttpMethod.Put, requestUri);

#if NET451
            // If certificate validation is disabled, inject a callback to handle properly
            RemoteCertificateValidationCallback prevValidator = null;
            if (!_config.ValidateCertificates)
            {
                prevValidator = ServicePointManager.ServerCertificateValidationCallback;
                ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
            }
#endif
            try
            {
                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    _logger?.LogDebug("StatusUpdateAsync {0}, status: {1}", requestUri.ToString(), response.StatusCode);
                    EurekaHttpResponse resp = new EurekaHttpResponse(response.StatusCode);
                    resp.Headers = response.Headers;
                    return(resp);
                }
            }
            catch (Exception e)
            {
                _logger?.LogError("StatusUpdateAsync Exception: {0}", e);
                throw;
            }
#if NET451
            finally
            {
                ServicePointManager.ServerCertificateValidationCallback = prevValidator;
            }
#endif
        }
Esempio n. 2
0
        protected virtual async Task <EurekaHttpResponse <Applications> > DoGetApplicationsAsync(string path, ISet <string> regions)
        {
            string regionParams = CommaDelimit(regions);

            var queryArgs = new Dictionary <string, string>();

            if (regionParams != null)
            {
                queryArgs.Add("regions", regionParams);
            }

            HttpClient client     = GetHttpClient(_config);
            var        requestUri = GetRequestUri(_serviceUrl + path, queryArgs);
            var        request    = GetRequestMessage(HttpMethod.Get, requestUri);

#if NET451
            // If certificate validation is disabled, inject a callback to handle properly
            RemoteCertificateValidationCallback prevValidator = null;
            if (!_config.ValidateCertificates)
            {
                prevValidator = ServicePointManager.ServerCertificateValidationCallback;
                ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
            }
#endif
            try
            {
                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    Stream stream = await response.Content.ReadAsStreamAsync();

                    JsonApplicationsRoot jroot = JsonApplicationsRoot.Deserialize(stream);

                    Applications appsResp = null;
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        if (jroot != null)
                        {
                            appsResp = Applications.FromJsonApplications(jroot.Applications);
                        }
                    }
                    _logger?.LogDebug("DoGetApplicationsAsync {0}, status: {1}, applications: {2}",
                                      requestUri.ToString(), response.StatusCode, ((appsResp != null) ? appsResp.ToString() : "null"));
                    EurekaHttpResponse <Applications> resp = new EurekaHttpResponse <Applications>(response.StatusCode, appsResp);
                    resp.Headers = response.Headers;
                    return(resp);
                }
            }
            catch (Exception e)
            {
                _logger?.LogError("DoGetApplicationsAsync Exception: {0}", e);
                throw;
            }
#if NET451
            finally
            {
                ServicePointManager.ServerCertificateValidationCallback = prevValidator;
            }
#endif
        }
Esempio n. 3
0
        public virtual async Task <EurekaHttpResponse> DeleteStatusOverrideAsync(string appName, string id, InstanceInfo info)
        {
            if (string.IsNullOrEmpty(appName))
            {
                throw new ArgumentException(nameof(appName));
            }

            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException(nameof(id));
            }

            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }


            var queryArgs = new Dictionary <string, string>()
            {
                { "lastDirtyTimestamp", DateTimeConversions.ToJavaMillis(new DateTime(info.LastDirtyTimestamp, DateTimeKind.Utc)).ToString() }
            };

            HttpClient client     = GetHttpClient(_config);
            var        requestUri = GetRequestUri(_serviceUrl + "apps/" + appName + "/" + id + "/status", queryArgs);
            var        request    = GetRequestMessage(HttpMethod.Delete, requestUri);

#if NET452
            // If certificate validation is disabled, inject a callback to handle properly
            RemoteCertificateValidationCallback prevValidator = null;
            SecurityProtocolType prevProtocols = (SecurityProtocolType)0;
            ConfigureCertificateValidatation(out prevProtocols, out prevValidator);
#endif
            try
            {
                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    _logger?.LogDebug("DeleteStatusOverrideAsync {0}, status: {1}", requestUri.ToString(), response.StatusCode);
                    EurekaHttpResponse resp = new EurekaHttpResponse(response.StatusCode);
                    resp.Headers = response.Headers;
                    return(resp);
                }
            }
            catch (Exception e)
            {
                _logger?.LogError("DeleteStatusOverrideAsync Exception: {0}", e);
                throw;
            }
            finally
            {
                DisposeHttpClient(client);
#if NET452
                RestoreCertificateValidation(prevProtocols, prevValidator);
#endif
            }
        }
Esempio n. 4
0
        public virtual async Task <EurekaHttpResponse <Application> > GetApplicationAsync(string appName)
        {
            if (string.IsNullOrEmpty(appName))
            {
                throw new ArgumentException(nameof(appName));
            }

            HttpClient client     = GetHttpClient(_config);
            var        requestUri = GetRequestUri(_serviceUrl + "apps/" + appName);
            var        request    = GetRequestMessage(HttpMethod.Get, requestUri);

#if NET452
            // If certificate validation is disabled, inject a callback to handle properly
            RemoteCertificateValidationCallback prevValidator = null;
            if (!_config.ValidateCertificates)
            {
                prevValidator = ServicePointManager.ServerCertificateValidationCallback;
                ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
            }
#endif
            try
            {
                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    Stream stream = await response.Content.ReadAsStreamAsync();

                    JsonApplicationRoot jroot = JsonApplicationRoot.Deserialize(stream);

                    Application appResp = null;
                    if (jroot != null)
                    {
                        appResp = Application.FromJsonApplication(jroot.Application);
                    }
                    _logger?.LogDebug("GetApplicationAsync {0}, status: {1}, application: {2}",
                                      requestUri.ToString(), response.StatusCode, ((appResp != null) ? appResp.ToString() : "null"));
                    EurekaHttpResponse <Application> resp = new EurekaHttpResponse <Application>(response.StatusCode, appResp);
                    resp.Headers = response.Headers;
                    return(resp);
                }
            }
            catch (Exception e)
            {
                _logger?.LogError("GetApplicationAsync Exception: {0}", e);
                throw;
            }
            finally
            {
                DisposeHttpClient(client);
#if NET452
                ServicePointManager.ServerCertificateValidationCallback = prevValidator;
#endif
            }
        }
Esempio n. 5
0
        protected virtual async Task <EurekaHttpResponse <InstanceInfo> > DoGetInstanceAsync(string path)
        {
            var        requestUri = GetRequestUri(_serviceUrl + path);
            var        request    = GetRequestMessage(HttpMethod.Get, requestUri);
            HttpClient client     = GetHttpClient(_config);

#if NET452
            // If certificate validation is disabled, inject a callback to handle properly
            RemoteCertificateValidationCallback prevValidator = null;
            if (!_config.ValidateCertificates)
            {
                prevValidator = ServicePointManager.ServerCertificateValidationCallback;
                ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
            }
#endif
            try
            {
                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    Stream stream = await response.Content.ReadAsStreamAsync();

                    JsonInstanceInfoRoot jroot = JsonInstanceInfoRoot.Deserialize(stream);

                    InstanceInfo infoResp = null;
                    if (jroot != null)
                    {
                        infoResp = InstanceInfo.FromJsonInstance(jroot.Instance);
                    }
                    _logger?.LogDebug("DoGetInstanceAsync {0}, status: {1}, instanceInfo: {2}",
                                      requestUri.ToString(), response.StatusCode, ((infoResp != null) ? infoResp.ToString() : "null"));
                    EurekaHttpResponse <InstanceInfo> resp = new EurekaHttpResponse <InstanceInfo>(response.StatusCode, infoResp);
                    resp.Headers = response.Headers;
                    return(resp);
                }
            }
            catch (Exception e)
            {
                _logger?.LogError("DoGetInstanceAsync Exception: {0}", e);
                throw;
            }

            finally
            {
                DisposeHttpClient(client);
#if NET452
                ServicePointManager.ServerCertificateValidationCallback = prevValidator;
#endif
            }
        }
Esempio n. 6
0
        public virtual async Task <EurekaHttpResponse> CancelAsync(string appName, string id)
        {
            if (string.IsNullOrEmpty(appName))
            {
                throw new ArgumentException(nameof(appName));
            }

            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException(nameof(id));
            }

            HttpClient client     = GetHttpClient(_config);
            var        requestUri = GetRequestUri(_serviceUrl + "apps/" + appName + "/" + id);
            var        request    = GetRequestMessage(HttpMethod.Delete, requestUri);

#if NET452
            // If certificate validation is disabled, inject a callback to handle properly
            RemoteCertificateValidationCallback prevValidator = null;
            if (!_config.ValidateCertificates)
            {
                prevValidator = ServicePointManager.ServerCertificateValidationCallback;
                ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
            }
#endif
            try
            {
                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    _logger?.LogDebug("CancelAsync {0}, status: {1}", requestUri.ToString(), response.StatusCode);
                    EurekaHttpResponse resp = new EurekaHttpResponse(response.StatusCode);
                    resp.Headers = response.Headers;
                    return(resp);
                }
            }
            catch (Exception e)
            {
                _logger?.LogError("CancelAsync Exception: {0}", e);
                throw;
            }
            finally
            {
                DisposeHttpClient(client);
#if NET452
                ServicePointManager.ServerCertificateValidationCallback = prevValidator;
#endif
            }
        }
Esempio n. 7
0
        public virtual async Task <EurekaHttpResponse> RegisterAsync(InstanceInfo info)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            HttpClient client     = GetHttpClient(_config);
            var        requestUri = GetRequestUri(_serviceUrl + "apps/" + info.AppName);
            var        request    = GetRequestMessage(HttpMethod.Post, requestUri);

#if NET452
            // If certificate validation is disabled, inject a callback to handle properly
            RemoteCertificateValidationCallback prevValidator = null;
            if (!_config.ValidateCertificates)
            {
                prevValidator = ServicePointManager.ServerCertificateValidationCallback;
                ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
            }
#endif
            try
            {
                request.Content = GetRequestContent(new JsonInstanceInfoRoot(info.ToJsonInstance()));

                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    _logger?.LogDebug("RegisterAsync {0}, status: {1}", requestUri.ToString(), response.StatusCode);
                    EurekaHttpResponse resp = new EurekaHttpResponse(response.StatusCode);
                    resp.Headers = response.Headers;
                    return(resp);
                }
            }
            catch (Exception e)
            {
                _logger?.LogError("RegisterAsync Exception:", e);
                throw;
            }

            finally
            {
                DisposeHttpClient(client);
#if NET452
                ServicePointManager.ServerCertificateValidationCallback = prevValidator;
#endif
            }
        }
Esempio n. 8
0
        public virtual async Task <EurekaHttpResponse> CancelAsync(string appName, string id)
        {
            if (string.IsNullOrEmpty(appName))
            {
                throw new ArgumentException(nameof(appName));
            }

            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException(nameof(id));
            }

            HttpClient client     = GetHttpClient(_config);
            var        requestUri = GetRequestUri(_serviceUrl + "apps/" + appName + "/" + id);
            var        request    = GetRequestMessage(HttpMethod.Delete, requestUri);

#if NET452
            // If certificate validation is disabled, inject a callback to handle properly
            RemoteCertificateValidationCallback prevValidator = null;
            SecurityProtocolType prevProtocols = (SecurityProtocolType)0;
            ConfigureCertificateValidatation(out prevProtocols, out prevValidator);
#endif
            try
            {
                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    _logger?.LogDebug("CancelAsync {0}, status: {1}", requestUri.ToString(), response.StatusCode);
                    EurekaHttpResponse resp = new EurekaHttpResponse(response.StatusCode);
                    resp.Headers = response.Headers;
                    return(resp);
                }
            }
            catch (Exception e)
            {
                _logger?.LogError("CancelAsync Exception: {0}", e);
                throw;
            }
            finally
            {
                DisposeHttpClient(client);
#if NET452
                RestoreCertificateValidation(prevProtocols, prevValidator);
#endif
            }
        }
Esempio n. 9
0
        public virtual async Task <EurekaHttpResponse <InstanceInfo> > SendHeartBeatAsync(string appName, string id, InstanceInfo info, InstanceStatus overriddenStatus)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            if (string.IsNullOrEmpty(appName))
            {
                throw new ArgumentException(nameof(appName));
            }

            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException(nameof(id));
            }

            var queryArgs = new Dictionary <string, string>()
            {
                { "status", info.Status.ToString() },
                { "lastDirtyTimestamp", DateTimeConversions.ToJavaMillis(new DateTime(info.LastDirtyTimestamp, DateTimeKind.Utc)).ToString() }
            };

            if (overriddenStatus != InstanceStatus.UNKNOWN)
            {
                queryArgs.Add("overriddenstatus", overriddenStatus.ToString());
            }

            HttpClient client     = GetHttpClient(_config);
            var        requestUri = GetRequestUri(_serviceUrl + "apps/" + info.AppName + "/" + id, queryArgs);
            var        request    = GetRequestMessage(HttpMethod.Put, requestUri);

#if NET451
            // If certificate validation is disabled, inject a callback to handle properly
            RemoteCertificateValidationCallback prevValidator = null;
            if (!_config.ValidateCertificates)
            {
                prevValidator = ServicePointManager.ServerCertificateValidationCallback;
                ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
            }
#endif
            try
            {
                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    Stream stream = await response.Content.ReadAsStreamAsync();

                    JsonInstanceInfo jinfo = JsonInstanceInfo.Deserialize(stream);



                    InstanceInfo infoResp = null;
                    if (jinfo != null)
                    {
                        infoResp = InstanceInfo.FromJsonInstance(jinfo);
                    }

                    _logger?.LogDebug("SendHeartbeatAsync {0}, status: {1}, instanceInfo: {2}",
                                      requestUri.ToString(), response.StatusCode, ((infoResp != null) ? infoResp.ToString() : "null"));
                    EurekaHttpResponse <InstanceInfo> resp = new EurekaHttpResponse <InstanceInfo>(response.StatusCode, infoResp);
                    resp.Headers = response.Headers;
                    return(resp);
                }
            }
            catch (Exception e)
            {
                _logger?.LogError("SendHeartbeatAsync Exception: {0}", e);
                throw;
            }
#if NET451
            finally
            {
                ServicePointManager.ServerCertificateValidationCallback = prevValidator;
            }
#endif
        }