Exemplo n.º 1
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 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("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:", e);
                throw;
            }
#if NET451
            finally
            {
                ServicePointManager.ServerCertificateValidationCallback = prevValidator;
            }
#endif
        }
Exemplo n.º 2
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 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();

                    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:", e);
                throw;
            }
#if NET451
            finally
            {
                ServicePointManager.ServerCertificateValidationCallback = prevValidator;
            }
#endif
        }
Exemplo n.º 3
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 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();

                    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:", e);
                throw;
            }
#if NET451
            finally
            {
                ServicePointManager.ServerCertificateValidationCallback = prevValidator;
            }
#endif
        }
Exemplo n.º 4
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 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("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:", e);
                throw;
            }
#if NET451
            finally
            {
                ServicePointManager.ServerCertificateValidationCallback = prevValidator;
            }
#endif
        }
Exemplo n.º 5
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 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
            {
                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;
            }
#if NET451
            finally
            {
                ServicePointManager.ServerCertificateValidationCallback = prevValidator;
            }
#endif
        }
Exemplo n.º 6
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:", e);
                throw;
            }
#if NET451
            finally
            {
                ServicePointManager.ServerCertificateValidationCallback = prevValidator;
            }
#endif
        }