Esempio n. 1
0
        public async Task <string> ResetKeys(string tenantId)
        {
            HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.InternalServerError);

            int reverseProxyPort = await FrontEndHelper.FrontEndHelper.GetReverseProxyPortAsync();

            HttpServiceUriBuilder builder = new HttpServiceUriBuilder()
            {
                PortNumber  = reverseProxyPort,
                ServiceName = $"{tenantId}/{TenantApplicationAdminServiceName}/api/keys/key1"
            };

            HttpServiceUriBuilder builder2 = new HttpServiceUriBuilder()
            {
                PortNumber  = reverseProxyPort,
                ServiceName = $"{tenantId}/{TenantApplicationAdminServiceName}/api/keys/key2"
            };

            HttpResponseMessage topicResponseMessage;

            using (HttpClient httpClient = new HttpClient())
            {
                topicResponseMessage = await httpClient.GetAsync(builder.Build());

                topicResponseMessage = await httpClient.GetAsync(builder2.Build());
            }

            return("keys reset");
        }
Esempio n. 2
0
        public async Task <HttpResponseMessage> AddSubscriber(string tenantId, string topicName, string subscriberName)
        {
            HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.InternalServerError);

            int reverseProxyPort = await FrontEndHelper.FrontEndHelper.GetReverseProxyPortAsync();

            HttpServiceUriBuilder builder = new HttpServiceUriBuilder()
            {
                PortNumber  = reverseProxyPort,
                ServiceName = $"{tenantId}/{TenantApplicationAdminServiceName}/api/{topicName}/subscribers/{subscriberName}"
            };

            HttpResponseMessage topicResponseMessage;

            using (HttpClient httpClient = new HttpClient())
            {
                topicResponseMessage = await httpClient.PutAsync(builder.Build(), null);
            }

            if (topicResponseMessage != null && topicResponseMessage.IsSuccessStatusCode)
            {
                responseMessage.StatusCode = HttpStatusCode.OK;
            }
            else
            {
                responseMessage.StatusCode   = HttpStatusCode.InternalServerError;
                responseMessage.ReasonPhrase = topicResponseMessage?.ReasonPhrase ?? "Internal error";
            }

            return(responseMessage);
        }
Esempio n. 3
0
        public void TargetDefault()
        {
            HttpServiceUriBuilder target = new HttpServiceUriBuilder()
                                           .SetServiceName("fabric:/test");

            Assert.Equal <HttpServiceUriTarget>(HttpServiceUriTarget.Default, target.Target);
            Assert.Equal <string>(this.GetUrl("http", "fabric", "test"), target.Build().ToString());
        }
Esempio n. 4
0
        public void SetFQServiceAndBuild()
        {
            Uri expected = new Uri("fabric:/app/my/service");
            HttpServiceUriBuilder target = new HttpServiceUriBuilder()
                                           .SetServiceName(expected);

            Assert.Equal <Uri>(expected, target.ServiceName);
            Assert.Equal <string>(this.GetUrl("http", "fabric", "app/my/service"), target.Build().ToString());
        }
Esempio n. 5
0
        public void SetNullHostAndBuild()
        {
            HttpServiceUriBuilder target = new HttpServiceUriBuilder()
                                           .SetHost(null)
                                           .SetServiceName("fabric:/my/service");

            Assert.Equal <string>(null, target.Host);
            Assert.Equal <string>(this.GetUrl("http", "fabric", "my/service"), target.Build().ToString());
        }
Esempio n. 6
0
        public void SetSchemeAndBuild(string scheme)
        {
            HttpServiceUriBuilder target = new HttpServiceUriBuilder()
                                           .SetScheme("https")
                                           .SetServiceName("fabric:/my/service");

            Assert.Equal <string>("https", target.Scheme);
            Assert.Equal <string>(this.GetUrl("https", "fabric", "my/service"), target.Build().ToString());
        }
Esempio n. 7
0
        public void SetPartitionKeyString()
        {
            HttpServiceUriBuilder target = new HttpServiceUriBuilder()
                                           .SetServiceName("fabric:/test")
                                           .SetPartitionKey("pKey");

            Assert.Equal <string>("pKey", target.PartitionKey.Value as string);
            Assert.Equal <string>(this.GetUrl("http", "fabric", "test", "pKey"), target.Build().ToString());
        }
Esempio n. 8
0
        // POST api/tenantId/topicName
        //public async Task<HttpResponseMessage> Post(string tenantId, string topicName)
        //{
        //    HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.InternalServerError);

        //    // Assuming the message body will contain the content for the data to put to the topic.
        //    string messageBody = await this.Request.Content.ReadAsStringAsync();

        //    HttpServiceUriBuilder builder = new HttpServiceUriBuilder()
        //    {
        //        PortNumber = await GetReverseProxyPortAsync(),
        //        ServiceName = $"{tenantId}/{TenantApplicationTopicServiceName}/{topicName}/api/"
        //    };

        //    HttpResponseMessage topicResponseMessage;
        //    using (HttpClient httpClient = new HttpClient())
        //    {
        //        HttpContent postContent = new StringContent(messageBody);
        //        postContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

        //        topicResponseMessage = await httpClient.PostAsync(builder.Build(), postContent);
        //    }

        //    if (topicResponseMessage != null && topicResponseMessage.IsSuccessStatusCode)
        //    {
        //        responseMessage.StatusCode = HttpStatusCode.Accepted;

        //        var msg = await topicResponseMessage.Content.ReadAsStringAsync();

        //        System.Diagnostics.Debug.WriteLine($"Received response of '{msg}'.");
        //    }
        //    else
        //    {
        //        responseMessage.StatusCode = topicResponseMessage?.StatusCode ?? HttpStatusCode.InternalServerError;
        //        responseMessage.ReasonPhrase = topicResponseMessage?.ReasonPhrase ?? "Internal error";
        //    }

        //    return responseMessage;
        //}

        // GET api/tenantId/topicName/subscriber
        public async Task <HttpResponseMessage> Get(string tenantId, string topicName, string subscriberName)
        {
            try
            {
                ServiceEventSource.Current.Message($"[Router] Message read for {tenantId}/{topicName}/{subscriberName}");

                HttpServiceUriBuilder builder = new HttpServiceUriBuilder()
                {
                    PortNumber  = await FrontEndHelper.FrontEndHelper.GetReverseProxyPortAsync(),
                    ServiceName = $"{tenantId}/{TenantApplicationTopicServiceName}/{topicName}/{subscriberName}/api"
                };

                HttpResponseMessage msg;
                PubSubMessage       pubSubMsg = null;
                using (HttpClient httpClient = new HttpClient())
                {
                    string pubSubMsgJson = await httpClient.GetStringAsync(builder.Build());

                    if (!String.IsNullOrEmpty(pubSubMsgJson))
                    {
                        pubSubMsg = JsonConvert.DeserializeObject <PubSubMessage>(pubSubMsgJson);
                    }
                }

                HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.OK);
                if (pubSubMsg != null)
                {
                    string msgJson = JsonConvert.SerializeObject(pubSubMsg.Message,
                                                                 new JsonSerializerSettings
                    {
                        Formatting = Formatting.Indented
                    });
                    responseMessage.Content = new StringContent(msgJson);
                }
                else
                {
                    responseMessage.StatusCode = HttpStatusCode.NoContent;
                }

                return(responseMessage);

                //using remoting
                //var topicSvc = ServiceProxy.Create<ISubscriberService>(new Uri($"fabric:/{tenantId}/{TenantApplicationTopicServiceName}/{topicName}/{subscriberName}"));
                //var msg = await topicSvc.Pop();
                //string msgJson = JsonConvert.SerializeObject(msg.Content,
                //    new JsonSerializerSettings
                //    {
                //        Formatting = Formatting.Indented
                //    });
                //responseMessage.Content = new StringContent(msgJson);
            }
            catch (Exception ex)
            {
                return(new HttpResponseMessage(HttpStatusCode.InternalServerError));
            }
        }
Esempio n. 9
0
        public void SetNullUriServiceAndBuild()
        {
            Uri serviceName = null;

            HttpServiceUriBuilder target = new HttpServiceUriBuilder()
                                           .SetServiceName("fabric:/my/app");

            target.SetServiceName(serviceName);

            Assert.Null(target.ServiceName);
            Assert.Throws <UriFormatException>(() => target.Build());
        }
Esempio n. 10
0
        public async Task <HttpResponseMessage> Post(string tenantId, string topicName, string message)
        {
            try
            {
                ServiceEventSource.Current.Message($"[Router] New Message received for {tenantId}/{topicName}: {message}");

                HttpServiceUriBuilder builder = new HttpServiceUriBuilder()
                {
                    PortNumber  = await FrontEndHelper.FrontEndHelper.GetReverseProxyPortAsync(),
                    ServiceName = $"{tenantId}/{TenantApplicationTopicServiceName}/{topicName}/api"
                };

                HttpResponseMessage msg;
                PubSubMessage       pubsubMessage = new PubSubMessage()
                {
                    Message = message
                };
                using (HttpClient httpClient = new HttpClient())
                {
                    string msgJson = JsonConvert.SerializeObject(pubsubMessage,
                                                                 new JsonSerializerSettings
                    {
                        Formatting = Formatting.Indented
                    });
                    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header

                    msg = await httpClient.PostAsync(
                        builder.Build(),
                        new StringContent(msgJson, Encoding.UTF8, "application/json")
                        );
                }

                HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.OK);
                if (!msg.IsSuccessStatusCode)
                {
                    responseMessage.StatusCode = HttpStatusCode.InternalServerError;
                }

                return(responseMessage);

                //using remoting

                //var msg = new PubSubMessage() { Message = message };
                //var topicSvc = ServiceProxy.Create<ITopicService>(new Uri($"fabric:/{tenantId}/{TenantApplicationTopicServiceName}/{topicName}"));
                //await topicSvc.Push(msg);
            }
            catch (Exception ex)
            {
                return(new HttpResponseMessage(HttpStatusCode.InternalServerError));
            }
        }
Esempio n. 11
0
        public async Task <HttpResponseMessage> GetSubscribers(string tenantId, string topicName)
        {
            HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.InternalServerError);

            int reverseProxyPort = await FrontEndHelper.FrontEndHelper.GetReverseProxyPortAsync();

            HttpServiceUriBuilder builder = new HttpServiceUriBuilder()
            {
                PortNumber = reverseProxyPort,

                ServiceName = $"{tenantId}/{TenantApplicationAdminServiceName}/api/{topicName}/subscribers"
            };

            HttpResponseMessage response;

            using (HttpClient httpClient = new HttpClient())
            {
                response = await httpClient.GetAsync(builder.Build());
            }

            IList <string> subscribers = new List <string>();

            if (response != null && response.IsSuccessStatusCode)
            {
                var msg = await response.Content.ReadAsStringAsync();

                dynamic x = JArray.Parse(msg);
                foreach (dynamic node in x)
                {
                    string serviceName = node.serviceName;
                    serviceName = serviceName.Split('/').LastOrDefault();
                    subscribers.Add(serviceName);
                }

                string subscribersNames = JsonConvert.SerializeObject(subscribers,
                                                                      new JsonSerializerSettings
                {
                    Formatting = Formatting.Indented
                });
                responseMessage.Content    = new StringContent(subscribersNames);
                responseMessage.StatusCode = HttpStatusCode.OK;
            }
            else
            {
                responseMessage.StatusCode   = HttpStatusCode.InternalServerError;
                responseMessage.ReasonPhrase = response?.ReasonPhrase ?? "Internal error";
            }

            return(responseMessage);
        }
Esempio n. 12
0
        public async Task <string> DeleteTopic(string tenantId, string TopicName)
        {
            HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.InternalServerError);

            int reverseProxyPort = await FrontEndHelper.FrontEndHelper.GetReverseProxyPortAsync();

            HttpServiceUriBuilder builder = new HttpServiceUriBuilder()
            {
                PortNumber  = reverseProxyPort,
                ServiceName = $"{tenantId}/{TenantApplicationAdminServiceName}/api/topics/" + TopicName
            };

            HttpResponseMessage topicResponseMessage;

            using (HttpClient httpClient = new HttpClient())
            {
                topicResponseMessage = await httpClient.DeleteAsync(builder.Build());
            }

            return("deleted");
        }
Esempio n. 13
0
        public async Task <string> CreateTopic(string tenantId, string TopicName)
        {
            //var key = HttpContext.Current.Request.Headers.GetValues("x-request-key").FirstOrDefault();

            HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.InternalServerError);

            int reverseProxyPort = await FrontEndHelper.FrontEndHelper.GetReverseProxyPortAsync();

            HttpServiceUriBuilder builder = new HttpServiceUriBuilder()
            {
                PortNumber  = reverseProxyPort,
                ServiceName = $"{tenantId}/{TenantApplicationAdminServiceName}/api/topics/" + TopicName
            };

            HttpResponseMessage topicResponseMessage;

            using (HttpClient httpClient = new HttpClient())
            {
                topicResponseMessage = await httpClient.PutAsync(builder.Build(), null);
            }

            return("created");
        }
Esempio n. 14
0
        public void SetAllTheThings()
        {
            HttpServiceUriBuilder target = new HttpServiceUriBuilder()
                                           .SetScheme("https")
                                           .SetHost("fabric")
                                           .SetServiceName("fabric:/app/service")
                                           .SetPartitionKey(34)
                                           .SetTarget(HttpServiceUriTarget.Secondary)
                                           .SetEndpointName("myendpoint")
                                           .SetServicePathAndQuery("path/to/nowhere?value=1");

            Assert.Equal <string>("https", target.Scheme);
            Assert.Equal <string>("fabric", target.Host);
            Assert.Equal <Uri>(new Uri("fabric:/app/service"), target.ServiceName);
            Assert.Equal <long>(34, (long)target.PartitionKey.Value);
            Assert.Equal <HttpServiceUriTarget>(HttpServiceUriTarget.Secondary, target.Target);
            Assert.Equal <string>("myendpoint", target.EndpointName);
            Assert.Equal <string>("path/to/nowhere?value=1", target.ServicePathAndQuery);
            Assert.Equal <string>("https:/fabric/app/service/#/34/secondary/myendpoint/path/to/nowhere?value=1", target.Build().ToString());
        }
Esempio n. 15
0
        public void SetPathAndQuery(string pathAndQuery)
        {
            HttpServiceUriBuilder target = new HttpServiceUriBuilder()
                                           .SetServiceName("fabric:/test")
                                           .SetServicePathAndQuery(pathAndQuery);

            Assert.Equal <string>(pathAndQuery, target.ServicePathAndQuery);
            Assert.Equal <string>(this.GetUrl("http", "fabric", "test", pathAndQuery: pathAndQuery), target.Build().ToString());
        }
Esempio n. 16
0
        public void SetEndpointName()
        {
            HttpServiceUriBuilder target = new HttpServiceUriBuilder()
                                           .SetServiceName("fabric:/test")
                                           .SetEndpointName("myendpoint");

            Assert.Equal <string>("myendpoint", target.EndpointName);
            Assert.Equal <string>(this.GetUrl("http", "fabric", "test", endpoint: "myendpoint"), target.Build().ToString());
        }
Esempio n. 17
0
        public void SetTarget(HttpServiceUriTarget uriTarget)
        {
            HttpServiceUriBuilder target = new HttpServiceUriBuilder()
                                           .SetServiceName("fabric:/test")
                                           .SetTarget(uriTarget);

            Assert.Equal <HttpServiceUriTarget>(uriTarget, target.Target);
            Assert.Equal <string>(this.GetUrl("http", "fabric", "test", target: uriTarget.ToString()), target.Build().ToString());
        }
Esempio n. 18
0
        public void SetPartitionKeyInt64()
        {
            HttpServiceUriBuilder target = new HttpServiceUriBuilder()
                                           .SetServiceName("fabric:/test")
                                           .SetPartitionKey(long.MaxValue);

            Assert.Equal <long>(long.MaxValue, (long)target.PartitionKey.Value);
            Assert.Equal <string>(this.GetUrl("http", "fabric", "test", long.MaxValue.ToString()), target.Build().ToString());
        }