Ejemplo n.º 1
0
        /*
         * existsInlist checks if a given service exists in an a given list of services
         *
         * @param List<ServiceDescriptionIntermed> serviceList
         * @param ServiceDescriptionIntermed service
         * @returns StatusDetailsIntermed object
         *
         */

        public static bool existsInlist(List <ServiceDescriptionIntermed> serviceList, ServiceDescriptionIntermed service)
        {
            //TempObject that fetches if a object if exists in list
            Models.ServiceDescriptionIntermed matches = new Models.ServiceDescriptionIntermed();

            //Check all the names of services in the list for a service.name
            matches = serviceList.Find(x => x.name == service.name);

            if (matches != null)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 2
0
        public Models.StatusDetailsIntermed publish(String jsonServiceDescriptionString, int token)
        {
            //Object for returning status
            Models.StatusDetailsIntermed statusDetails = new Models.StatusDetailsIntermed();

            //The new service to be added
            Models.ServiceDescriptionIntermed newServiceDescription = new Models.ServiceDescriptionIntermed();

            //Deserializing the inputed json string to add() to serviceList
            newServiceDescription = JsonConvert.DeserializeObject <Models.ServiceDescriptionIntermed>(jsonServiceDescriptionString);

            //Has to be decoded as it is a url
            newServiceDescription.serviceEndPoint = Uri.UnescapeDataString(newServiceDescription.serviceEndPoint);

            //Creating connection via channelfactory in authenticatorAccessInterface
            authenticateInterface = Models.AuthenticatorAccessInterface.remoteConnection();

            string tokenAuthenticationStatus = authenticateInterface.validate(token);

            String jsonString = JsonConvert.SerializeObject(newServiceDescription, Formatting.Indented);

            //If token is valid can proceed with filehandling
            if (tokenAuthenticationStatus.Equals("validated"))
            {
                //Checking if file exists in path and if not have to create a new text file
                if (!File.Exists(SOA_Utilities.Constants.pathForServiceReg))
                {
                    try
                    {
                        //Manually adding the [ and ] as it not will cause an error when deserializing again
                        string newJsonString = $"[{jsonString}]";
                        System.IO.File.WriteAllText(SOA_Utilities.Constants.pathForServiceReg, newJsonString);
                    }
                    catch (NotSupportedException e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    catch (DirectoryNotFoundException e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }

                else
                {
                    /*
                     * If file already exists
                     * Assuming Services are already published
                     * Cannot directly append
                     *
                     */

                    try
                    {
                        // Reading all the data in the json file
                        var allServiceJsonData = System.IO.File.ReadAllText(SOA_Utilities.Constants.pathForServiceReg);

                        // De-serialize all the objects in the json file
                        var serviceList = JsonConvert.DeserializeObject <List <Models.ServiceDescriptionIntermed> >(allServiceJsonData);

                        // Add the service to the list of services
                        serviceList.Add(newServiceDescription);

                        // Re-serialize to save in json file
                        String JsonFile = JsonConvert.SerializeObject(serviceList, Formatting.Indented);
                        File.WriteAllText(SOA_Utilities.Constants.pathForServiceReg, JsonFile);
                    }
                    catch (ArgumentNullException e)
                    {
                        Console.WriteLine(e);
                    }
                    catch (DirectoryNotFoundException e)
                    {
                        Console.WriteLine(e);
                    }
                }


                /*
                 *
                 * Re-checking if the published service is in the textfile
                 *
                 */

                statusDetails = Models.Filehandling.jsonFileHandlingStatusCheck(newServiceDescription);

                return(statusDetails);
            }
            else if (tokenAuthenticationStatus.Equals("not validated"))
            {
                statusDetails.Reason = "Authentication Error";
                statusDetails.Status = "Denied";
            }

            return(statusDetails);
        }
        /*
         *
         * This function searches for services in the registry
         * @param query which is the query
         * @returns Models.ServiceDescriptionNStatusInterMed which contains a list of services and the status of the function invocation
         *
         * References: Lab 3
         *             ReadAllLines and WriteAllText : https://docs.microsoft.com/en-us/dotnet/api/system.io.file.readalllines?view=net-5.0
         *             Uri.UnescapeDataString and EscapeDataString: https://docs.microsoft.com/en-us/dotnet/api/system.uri.unescapedatastring?view=net-5.0
         *                                                          https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.unescape?view=net-5.0
         *             ASP.NET Web API Tutorial for Beginners _ ASP.NET Web API Crash Course 2020 (720p) : https://www.youtube.com/watch?v=iaeHaydhatE
         *             Creating A REST Webservice With C# And Visual Studio (360p) : https://www.youtube.com/watch?v=LXZnuJY0dGI
         *             Intro to WebAPI - One of the most powerful project types in C# (720p) : https://www.youtube.com/watch?v=vN9NRqv7xmY&t=1186s
         *             C# Tutorial - How to create and Parse JSON Data : https://www.youtube.com/watch?v=NX3Um9E-AY0
         *             Stackover flow : https://stackoverflow.com/questions/9854917/how-can-i-find-a-specific-element-in-a-listt
         *             Search query : https://docs.microsoft.com/en-us/powerapps/developer/data-platform/org-service/linq-query-examples
         */
        public Models.ServiceDescriptionNStatusInterMed search(String query, int token)
        {
            string tokenStatus;

            //create connection with the authenticator
            authenticateInterface = Models.AuthenticatorAccessInterface.remoteConnection();

            //fetch token status
            tokenStatus = authenticateInterface.validate(token);

            Models.ServiceDescriptionNStatusInterMed descriptionNStatusInterMed = new Models.ServiceDescriptionNStatusInterMed();

            List <Models.ServiceDescriptionIntermed> serviceDescriptionsList = new List <Models.ServiceDescriptionIntermed>();

            Models.ServiceDescriptionIntermed service = new Models.ServiceDescriptionIntermed();


            //checking if token is "validated"
            if (tokenStatus.Equals("validated"))
            {
                //Checking if file exists
                if (File.Exists(SOA_Utilities.Constants.pathForServiceReg))
                {
                    //Read all text and deserialise
                    string allServicesJSONFormat = File.ReadAllText(SOA_Utilities.Constants.pathForServiceReg);
                    serviceDescriptionsList = JsonConvert.DeserializeObject <List <Models.ServiceDescriptionIntermed> >(allServicesJSONFormat);

                    //Search query in the deserialized list and fetch it in an IEnumerable list
                    var fetch =
                        from serviceDescription in serviceDescriptionsList
                        //where servicedescrition name = query
                        where serviceDescription.name.Contains(query)
                        select serviceDescription;

                    //To list converts to a list format, assign to the model class list(this can only be done as the list is IEnumerable)
                    serviceDescriptionsList = fetch.ToList();

                    //has to be assigned to ServiceDescriptionNStatusInterMed as the return type needs both the status and the list
                    descriptionNStatusInterMed.Services = serviceDescriptionsList;

                    return(descriptionNStatusInterMed);
                }
                else //There are no services as no json file so return no services status
                {
                    descriptionNStatusInterMed.Status = "No services!";
                    descriptionNStatusInterMed.Reason = "No JSON file!";

                    return(descriptionNStatusInterMed);
                }
            }

            //if token not validated
            else if (tokenStatus.Equals("not validated"))

            {
                descriptionNStatusInterMed.Reason = "Authentication Error";
                descriptionNStatusInterMed.Status = "Denied";

                return(descriptionNStatusInterMed);
            }

            //login error in the authenticator class
            else

            {
                descriptionNStatusInterMed.Reason = "Logic Error";
                descriptionNStatusInterMed.Status = "Terminated";

                return(descriptionNStatusInterMed);
            }
        }
        public Models.StatusDetailsIntermed unpublish(String serviceEndPointString, int token)
        {
            string status;
            List <Models.ServiceDescriptionIntermed> allServicesList = new List <Models.ServiceDescriptionIntermed>();

            Models.ServiceDescriptionIntermed serviceToRemove = new Models.ServiceDescriptionIntermed();

            //creating a remote connection
            authenticateInterface = Models.AuthenticatorAccessInterface.remoteConnection();

            //getting return if token is validated
            status = authenticateInterface.validate(token);

            if (status.Equals("validated"))
            {
                //Fetch all json services to jsonString
                String getAllServices = File.ReadAllText(SOA_Utilities.Constants.pathForServiceReg);

                //De-serialize all services to List<Models.ServiceDescriptionIntermed>
                allServicesList = JsonConvert.DeserializeObject <List <Models.ServiceDescriptionIntermed> >(getAllServices);

                //De-serialize to object form
                var serviceEndpoint = JsonConvert.DeserializeObject <Models.ServiceDescriptionIntermed>(serviceEndPointString);

                //Finding the service with the respective endpoint in the list
                serviceToRemove = allServicesList.Find(x => x.serviceEndPoint == serviceEndpoint.serviceEndPoint);

                //service not found
                if (serviceToRemove == null)
                {
                    statusIntermed.Status = "Unsuccessful!";
                    statusIntermed.Reason = "Service not published to unpublish or error";
                }//service exists in list
                else
                {
                    //Unpublishing service
                    allServicesList.Remove(serviceToRemove);

                    //De-serialize and have in serviceDetails.txt

                    String serviceListJsonFile = JsonConvert.SerializeObject(allServicesList, Formatting.Indented);

                    File.WriteAllText(SOA_Utilities.Constants.pathForServiceReg, serviceListJsonFile);

                    statusIntermed.Status = "Successful!";
                    statusIntermed.Reason = $"Unpublished from file path {SOA_Utilities.Constants.pathForServiceReg}";
                }

                return(statusIntermed);
            }

            else if (status.Equals("not validated"))
            {
                statusIntermed.Reason = "Authentication Error";
                statusIntermed.Status = "Denied";
            }
            else
            {
                statusIntermed.Reason = "Logic Error";
                statusIntermed.Status = "Terminated";
            }

            return(statusIntermed);
        }