Exemplo n.º 1
0
        // Delete the Channel specified by channelId'
        public void DeleteChannel(string channelId, ref string responseContent, ref HttpStatusCode statusCode, ref string reasonPhrase)
        {
            try
            {
                // Repace percent encoding characters
                channelId = System.Uri.UnescapeDataString(channelId);

                // Create a new Database Handler
                DatabaseHandler myDatabaseHandler = new DatabaseHandler();

                // SQL Statement to read channel with matching channelId
                string  sqlStatement   = "Select * from Channels where Channel_Id = '" + channelId + "'";
                DataSet channelDataset = myDatabaseHandler.Select(sqlStatement);

                // Skip deleting the channel if no record returned
                if (channelDataset.Tables[0].Rows.Count != 0)
                {
                    // SQL Statement to delete the channel with matching channelId
                    sqlStatement = "Delete from Channels where Channel_Id = '" + channelId + "'";
                    myDatabaseHandler.Delete(sqlStatement);

                    string channelUUID = channelDataset.Tables[0].Rows[0]["Channel_UUID"].ToString();

                    // Get Azure Bus SAS token string from Web.Config
                    string ServiceBusConnectionString = ConfigurationManager.ConnectionStrings["AzureBus"].ConnectionString;
                    // Create a new Azure Management
                    AzureManagement myAzureManagement = new AzureManagement();
                    // Delete the Azure Bus topic in the form of UUID corresponding to channel record
                    // ISBM channels are mapped to Azure Bus topics
                    myAzureManagement.DeleteTopic(ServiceBusConnectionString, channelUUID);

                    // Set HTTP status code and reason phrase
                    // 2xx for success
                    statusCode   = (HttpStatusCode)204;
                    reasonPhrase = "Channel successfully deleted.";
                }
                else
                {
                    // Set HTTP status code and reason phrase
                    // 4xx for Client Error
                    statusCode   = (HttpStatusCode)404;
                    reasonPhrase = "The channel doesn't exist";
                }
            }

            catch (Exception e)
            {
                // Set HTTP status code and reason phrase
                // 5xx for server error not defined by ISBM
                statusCode   = (HttpStatusCode)500;
                reasonPhrase = "Internal Server Error";
            }
        }
Exemplo n.º 2
0
        // Create a new channel with the specified URI path fragment.
        public void CreateChannel(string body, ref string responseContent, ref HttpStatusCode statusCode, ref string reasonPhrase)
        {
            try
            {
                // Load HTTP request content into Newtonsoft JObject
                JObject jsonBody       = JObject.Parse(body);
                JArray  securityTokens = (JArray)jsonBody["securityTokens"];

                string channelId   = (string)jsonBody["uri"];
                string channelType = (string)jsonBody["channelType"];
                string description = (string)jsonBody["description"];

                // Create a new Database Handler
                DatabaseHandler myDatabaseHandler = new DatabaseHandler();

                // SQL Statement to read channel with matching channelId
                string  sqlStatement   = "Select * from Channels where Channel_Id = '" + channelId + "'";
                DataSet channelDataset = myDatabaseHandler.Select(sqlStatement);

                // Skip creating channel if it already exists
                if (channelDataset.Tables[0].Rows.Count == 0)
                {
                    string channelUUID = Guid.NewGuid().ToString();

                    // SQL Statement to create the new channel on database
                    sqlStatement = "Insert into Channels (Channel_UUID, Channel_Id, Channel_Type, Description) values ('" + channelUUID + "', '" + channelId + "', '" + channelType + "', '" + description + "')";
                    myDatabaseHandler.Insert(sqlStatement);

                    // Get Azure Bus SAS token string from Web.Config
                    string ServiceBusConnectionString = ConfigurationManager.ConnectionStrings["AzureBus"].ConnectionString;
                    // Create a new Azure Management
                    AzureManagement myAzureManagement = new AzureManagement();
                    // Create an Azure Bus topic in the form of UUID corresponding to channel record
                    // ISBM channels are mapped to Azure Bus topics
                    myAzureManagement.CreateTopic(ServiceBusConnectionString, channelUUID);

                    // Set HTTP status code and reason phrase
                    //2xx for success
                    statusCode   = (HttpStatusCode)201;
                    reasonPhrase = "The newly created Channel, excluding any configured security tokens.";
                }
                else
                {
                    // Set HTTP status code and reason phrase
                    // 4xx for Client Error
                    statusCode   = (HttpStatusCode)409;
                    reasonPhrase = "Could not create the channel, URI already exists.";
                }

                return;
            }

            catch (Exception e)
            {
                // Set HTTP status code and reason phrase
                // 5xx for server error not defined by ISBM
                statusCode   = (HttpStatusCode)500;
                reasonPhrase = "Internal Server Error";

                return;
            }
        }
Exemplo n.º 3
0
        // Closes a session of any type
        public void CloseSession(string sessionId, ref string responseContent, ref HttpStatusCode statusCode, ref string reasonPhrase)
        {
            try
            {
                // Create a new Database Handler
                DatabaseHandler myDatabaseHandler = new DatabaseHandler();

                // SQL Statement to read session with matching sessionId
                string  sqlStatement    = "Select * from Sessions where Session_UUID = '" + sessionId + "'";
                DataSet SessionsDataset = myDatabaseHandler.Select(sqlStatement);

                // Skip closing the session if no record returned
                if (SessionsDataset.Tables[0].Rows.Count != 0)
                {
                    string status = SessionsDataset.Tables[0].Rows[0]["Status"].ToString();

                    string channelUUID = SessionsDataset.Tables[0].Rows[0]["Channel_UUID"].ToString();

                    // Skip closing the session if the status is not "Open"
                    if (status == "Open")
                    {
                        // SQL Statement to update session status to "Close" with matching sessionId
                        sqlStatement = "Update Sessions set Status = 'Close' where Session_UUID = '" + sessionId + "'";
                        myDatabaseHandler.Update(sqlStatement);

                        // Get Azure Bus SAS token string from Web.Config
                        string ServiceBusConnectionString = ConfigurationManager.ConnectionStrings["AzureBus"].ConnectionString;
                        // Create a new Azure Management
                        AzureManagement myAzureManagement = new AzureManagement();
                        // Delete the Azure Bus topic Subscriptions in the form of UUID corresponding to channel record
                        // ISBM channels are mapped to Azure Bus topics
                        myAzureManagement.DeleteTopicSubscriptions(ServiceBusConnectionString, channelUUID, sessionId);

                        // Set HTTP status code and reason phrase
                        // 2xx for success
                        statusCode   = (HttpStatusCode)204;
                        reasonPhrase = "Session is successfully closed.";
                    }

                    else
                    {
                        // Set HTTP status code and reason phrase
                        // 4xx for Client Error
                        statusCode   = (HttpStatusCode)410;
                        reasonPhrase = "The session is closed.This is optional, 404 could be returned instead.";
                    }
                }

                else
                {
                    // Set HTTP status code and reason phrase
                    // 4xx for Client Error
                    statusCode   = (HttpStatusCode)404;
                    reasonPhrase = "The session does not exist or has been closed.";
                }
            }

            catch (Exception e)
            {
                // Set HTTP status code and reason phrase
                // 5xx for server error not defined by ISBM
                statusCode   = (HttpStatusCode)500;
                reasonPhrase = "Internal Server Error";

                return;
            }
        }
Exemplo n.º 4
0
        // Opens a subscription session for a channel.
        public void OpenConsumerPublicationSession(string channelId, string body, ref string responseContent, ref HttpStatusCode statusCode, ref string reasonPhrase)
        {
            try
            {
                // Replace percent encoding characters
                channelId = System.Uri.UnescapeDataString(channelId);

                // Create a new Database Handler
                DatabaseHandler myDatabaseHandler = new DatabaseHandler();

                // SQL Statement to read channel with matching channelId
                string  sqlStatement   = "Select * from Channels where Channel_Id = '" + channelId + "'";
                DataSet channelDataset = myDatabaseHandler.Select(sqlStatement);

                // Skip opening a publication session if no record returned
                if (channelDataset.Tables[0].Rows.Count != 0)
                {
                    string channelType = "";
                    channelType = channelDataset.Tables[0].Rows[0]["Channel_Type"].ToString();
                    // Skip opening a publication session if channelType is not of type Publication
                    if (channelType == "Publication")
                    {
                        string subscribedBy = "Consumer";
                        string status       = "Open";

                        string sessionUUID = Guid.NewGuid().ToString();
                        string channelUUID = channelDataset.Tables[0].Rows[0]["Channel_UUID"].ToString();

                        // SQL Statement to create the new session on database
                        sqlStatement = "Insert into Sessions (Session_UUID, Channel_UUID, Channel_Type, Subscribed_By, Status) values ('" + sessionUUID + "', '" + channelUUID + "', '" + channelType + "', '" + subscribedBy + "', '" + status + "')";
                        myDatabaseHandler.Insert(sqlStatement);

                        // Load HTTP request content into Newtonsoft JObject
                        JObject jsonBody = JObject.Parse(body);
                        JArray  topics   = (JArray)jsonBody["topics"];
                        int     length   = topics.Count;

                        // Create a record for each topic on the database
                        // Topic filtering will be done by the ISBM adapter in the future release
                        for (int i = 0; i < length; i++)
                        {
                            string topic = (string)topics[i];

                            // SQL Statement to create the new session topic on database
                            sqlStatement = "Insert into SessionTopics (Session_UUID, Topic) values ('" + sessionUUID + "', '" + topic + "')";
                            myDatabaseHandler.Insert(sqlStatement);
                        }

                        // Format response content using Newtonsoft JObject
                        JObject jsonSessionId = new JObject();
                        jsonSessionId.AddFirst(new JProperty("sessionId", sessionUUID));
                        // Generate JSON string from the jArray object
                        responseContent = JsonConvert.SerializeObject(jsonSessionId);

                        // Get Azure Bus SAS token string from Web.Config
                        string ServiceBusConnectionString = ConfigurationManager.ConnectionStrings["AzureBus"].ConnectionString;
                        // Create a new Azure Management
                        AzureManagement myAzureManagement = new AzureManagement();
                        // Create an Azure Bus topic subscription in the form of UUID corresponding to channel record
                        // ISBM channels are mapped to Azure Bus topics
                        myAzureManagement.CreateTopicSubscriptions(ServiceBusConnectionString, channelUUID, sessionUUID);

                        // Set HTTP status code and reason phrase
                        // 2xx for success
                        statusCode   = (HttpStatusCode)201;
                        reasonPhrase = "The subscription session has been successfully opened on the channel. Only the SessionID is to be returned.";
                    }
                    else
                    {
                        // Set HTTP status code and reason phrase
                        // 4xx for Client Error
                        statusCode   = (HttpStatusCode)422;
                        reasonPhrase = "The Channel is not of type Publication.";
                    }
                }
                else
                {
                    // Set HTTP status code and reason phrase
                    // 4xx for Client Error
                    statusCode   = (HttpStatusCode)404;
                    reasonPhrase = "The Channel does not exists.";
                }
            }

            catch (Exception e)
            {
                // Set HTTP status code and reason phrase
                // 5xx for server error not defined by ISBM
                statusCode   = (HttpStatusCode)500;
                reasonPhrase = "Internal Server Error";

                return;
            }
        }