예제 #1
0
        public void AddRoot(string ChildSegment, Engine_Path_Endpoint Child)
        {
            // Ensure the dictionary is built correctly
            ensure_dictionary_built();

            // Does an endpoint already exist here?
            if (rootPathsDictionary.ContainsKey(ChildSegment))
            {
                Engine_Path_Endpoint matchingEndpoint = rootPathsDictionary[ChildSegment];
                RootPaths.Remove(matchingEndpoint);
            }

            rootPathsDictionary[ChildSegment] = Child;
            RootPaths.Add(Child);
        }
        public void AddRoot(string ChildSegment, Engine_Path_Endpoint Child)
        {
            // Ensure the dictionary is built correctly
            ensure_dictionary_built();

            // Does an endpoint already exist here?
            if (rootPathsDictionary.ContainsKey(ChildSegment))
            {
                Engine_Path_Endpoint matchingEndpoint = rootPathsDictionary[ChildSegment];
                RootPaths.Remove(matchingEndpoint);
            }

            rootPathsDictionary[ChildSegment] = Child;
            RootPaths.Add(Child);
        }
예제 #3
0
        public void AddChild(string ChildSegment, Engine_Path_Endpoint Child)
        {
            // Ensure the collection exists
            if (Children == null)
            {
                Children = new List <Engine_Path_Endpoint>();
            }

            // Ensure the dictionary is built correctly
            ensure_dictionary_built();

            // Does an endpoint already exist here?
            if (childDictionary.ContainsKey(ChildSegment))
            {
                Engine_Path_Endpoint matchingEndpoint = childDictionary[ChildSegment];
                Children.Remove(matchingEndpoint);
            }

            childDictionary[ChildSegment] = Child;
            Children.Add(Child);
        }
예제 #4
0
        /// <summary> Get the endpoint configuration, based on the requested path </summary>
        /// <param name="Paths"> Requested URL paths </param>
        /// <returns> Matched endpoint configuration, otherwise NULL </returns>
        public Engine_Path_Endpoint Get_Endpoint(List <string> Paths)
        {
            // Ensure the dictionary is built
            ensure_dictionary_built();

            // Find a match by path
            if (rootPathsDictionary.ContainsKey(Paths[0]))
            {
                Engine_Path_Endpoint path = rootPathsDictionary[Paths[0]];
                Paths.RemoveAt(0);

                do
                {
                    // Did we find an endpoint?
                    if (path.IsEndpoint)
                    {
                        return(path);
                    }

                    // Look to the next part of the path
                    if (Paths.Count > 0)
                    {
                        if (!path.ContainsChildKey(Paths[0]))
                        {
                            return(null);
                        }

                        path = path.GetChild(Paths[0]);
                        Paths.RemoveAt(0);
                    }
                    else
                    {
                        return(null);
                    }
                } while (true);
            }

            return(null);
        }
        private void recursively_write_all_endpoints( Engine_Path_Endpoint RootEndpoint , TextWriter Output , string Url )
        {
            if (RootEndpoint.IsEndpoint)
            {
                // How many verbs are defined for this endpoint?
                List<Engine_VerbMapping> mappings = RootEndpoint.AllVerbMappings;

                if (mappings.Count == 1)
                {
                    Output.WriteLine("    <tr>");
                    Output.WriteLine("      <td>" + Url + "</td>");
                    add_single_verb_mapping_in_table(mappings[0], Output);
                    Output.WriteLine("    </tr>");
                }
                else if (mappings.Count > 1)
                {
                    Output.WriteLine("    <tr>");
                    Output.WriteLine("      <td rowspan=\"" + mappings.Count + "\">" + Url + "</td>");
                    add_single_verb_mapping_in_table(mappings[0], Output);
                    Output.WriteLine("    </tr>");

                    for (int i = 1; i < mappings.Count; i++)
                    {
                        Output.WriteLine("    <tr>");
                        add_single_verb_mapping_in_table(mappings[i], Output);
                        Output.WriteLine("    </tr>");
                    }
                }
            }
            else
            {
                foreach (Engine_Path_Endpoint childEndpoint in RootEndpoint.Children)
                {
                    recursively_write_all_endpoints(childEndpoint, Output, Url + "/" + childEndpoint.Segment);
                }
            }
        }
        public void AddChild(string ChildSegment, Engine_Path_Endpoint Child)
        {
            // Ensure the collection exists
            if (Children == null)
                Children = new List<Engine_Path_Endpoint>();

            // Ensure the dictionary is built correctly
            ensure_dictionary_built();

            // Does an endpoint already exist here?
            if (childDictionary.ContainsKey(ChildSegment))
            {
                Engine_Path_Endpoint matchingEndpoint = childDictionary[ChildSegment];
                Children.Remove(matchingEndpoint);
            }

            childDictionary[ChildSegment] = Child;
            Children.Add(Child);
        }
        private static void engine_config_finalize_recurse_through_endpoints(Engine_Path_Endpoint pathEndpoint, Dictionary<string, Engine_Component> components, Dictionary<string, Engine_RestrictionRange> restrictionRanges)
        {
            // Handle this one first
            if (pathEndpoint.HasVerbMapping)
            {
                // Do the individual mappings
                engine_config_finalize_handle_single_mapping(pathEndpoint.GetMapping, components, restrictionRanges);
                engine_config_finalize_handle_single_mapping(pathEndpoint.PostMapping, components, restrictionRanges);
                engine_config_finalize_handle_single_mapping(pathEndpoint.PutMapping, components, restrictionRanges);
                engine_config_finalize_handle_single_mapping(pathEndpoint.DeleteMapping, components, restrictionRanges);
            }

            // Now, step through any children as well
            if ((pathEndpoint.Children != null) && (pathEndpoint.Children.Count > 0))
            {
                foreach (Engine_Path_Endpoint childNode in pathEndpoint.Children)
                {
                    engine_config_finalize_recurse_through_endpoints(childNode, components, restrictionRanges);
                }
            }
        }
        private static void read_microservices_simple_endpoint_details(XmlReader ReaderXml, Engine_Path_Endpoint ParentSegment)
        {
            Engine_Path_Endpoint endpoint = new Engine_Path_Endpoint {IsEndpoint = true};

            string componentid = String.Empty;
            string restrictionid = String.Empty;
            string method = String.Empty;
            bool enabled = true;
            Microservice_Endpoint_Protocol_Enum protocol = Microservice_Endpoint_Protocol_Enum.JSON;

            if (ReaderXml.MoveToAttribute("Segment"))
                endpoint.Segment = ReaderXml.Value.Trim();
            if (ReaderXml.MoveToAttribute("ComponentID"))
                componentid = ReaderXml.Value.Trim();
            if (ReaderXml.MoveToAttribute("Method"))
                method = ReaderXml.Value.Trim();
            if (ReaderXml.MoveToAttribute("Enabled"))
            {
                if (String.Compare(ReaderXml.Value.Trim(), "false", StringComparison.OrdinalIgnoreCase) == 0)
                    enabled = false;
            }
            if (ReaderXml.MoveToAttribute("RestrictionRangeID"))
                restrictionid = ReaderXml.Value.Trim();
            if (ReaderXml.MoveToAttribute("Protocol"))
            {
                switch (ReaderXml.Value.Trim().ToUpper())
                {
                    case "JSON":
                        protocol = Microservice_Endpoint_Protocol_Enum.JSON;
                        break;

                    case "JSON-P":
                        protocol = Microservice_Endpoint_Protocol_Enum.JSON_P;
                        break;

                    case "PROTOBUF":
                        protocol = Microservice_Endpoint_Protocol_Enum.PROTOBUF;
                        break;

                    case "SOAP":
                        protocol = Microservice_Endpoint_Protocol_Enum.SOAP;
                        break;

                    case "XML":
                        protocol = Microservice_Endpoint_Protocol_Enum.XML;
                        break;

                    case "TEXT":
                        protocol = Microservice_Endpoint_Protocol_Enum.TEXT;
                        break;

                    default:
                        protocol = Microservice_Endpoint_Protocol_Enum.JSON;
                        break;
                }
            }

            ReaderXml.MoveToElement();

            if ((componentid.Length > 0) && (endpoint.Segment.Length > 0) && (method.Length > 0))
            {
                if (ParentSegment != null)
                {
                    // Add this endpoint
                    ParentSegment.AddChild(endpoint.Segment, endpoint);

                    // Add the verb mapping defaulted to GET
                    endpoint.GetMapping = new Engine_VerbMapping(method, enabled, protocol, Microservice_Endpoint_RequestType_Enum.GET, componentid, restrictionid);
                }
            }
        }
        private static void read_microservices_details_mapping(XmlReader ReaderXml, Engine_Server_Configuration Config, Engine_Path_Endpoint ParentSegment )
        {
            while (ReaderXml.Read())
            {
                if (ReaderXml.NodeType == XmlNodeType.Element)
                {
                    switch (ReaderXml.Name.ToLower())
                    {
                        case "removeall":
                            if (ParentSegment != null)
                            {
                                if (ParentSegment.Children != null)
                                    ParentSegment.Children.Clear();
                            }
                            else
                            {
                                Config.RootPaths.Clear();
                            }
                            break;

                        case "path":
                            if (ReaderXml.MoveToAttribute("Segment"))
                            {
                                Engine_Path_Endpoint path;
                                string segment = ReaderXml.Value.Trim();

                                if (ParentSegment == null)
                                {
                                    if (Config.ContainsRootKey(segment.ToLower()))
                                        path = Config.GetRoot(segment.ToLower());
                                    else
                                    {
                                        path = new Engine_Path_Endpoint {Segment = segment};
                                        Config.AddRoot(segment.ToLower(), path);
                                    }
                                }
                                else
                                {
                                    if (ParentSegment.ContainsChildKey(segment.ToLower()))
                                    {
                                        path = ParentSegment.GetChild(segment.ToLower());
                                    }
                                    else
                                    {
                                        path = new Engine_Path_Endpoint { Segment = segment };
                                        ParentSegment.AddChild(path.Segment, path );
                                    }

                                }

                                ReaderXml.MoveToElement();
                                XmlReader subTreeReader = ReaderXml.ReadSubtree();
                                subTreeReader.Read();
                                read_microservices_details_mapping(subTreeReader, Config, path);
                            }
                            break;

                        case "complexendpoint":
                            // Read the top-endpoint information, before getting to each verb mapping
                            bool disabled_at_top = false;
                            Engine_Path_Endpoint endpoint = new Engine_Path_Endpoint { IsEndpoint = true };
                            if (ReaderXml.MoveToAttribute("Segment"))
                                endpoint.Segment = ReaderXml.Value.Trim();
                            if ((ReaderXml.MoveToAttribute("Enabled")) && (String.Compare(ReaderXml.Value.Trim(), "false", StringComparison.OrdinalIgnoreCase) == 0))
                                disabled_at_top = true;

                            // Now, read what remains
                            ReaderXml.MoveToElement();
                            XmlReader complexReader = ReaderXml.ReadSubtree();
                            complexReader.Read();
                            read_microservices_complex_endpoint_details(complexReader, endpoint, disabled_at_top);

                            // If a verb was mapped and there was a valid segment, add this
                            if ((!String.IsNullOrEmpty(endpoint.Segment)) && (endpoint.HasVerbMapping))
                            {
                                if (ParentSegment != null)
                                {
                                    // Add this endpoint
                                    ParentSegment.AddChild(endpoint.Segment, endpoint);
                                }
                            }
                            break;

                        case "endpoint":
                            read_microservices_simple_endpoint_details(ReaderXml, ParentSegment);
                            break;
                    }
                }
            }
        }
        private static void read_microservices_complex_endpoint_details(XmlReader ReaderXml, Engine_Path_Endpoint Endpoint, bool DisabledAtTop)
        {
            while (ReaderXml.Read())
            {
                if (ReaderXml.NodeType == XmlNodeType.Element)
                {
                    if (String.Compare(ReaderXml.Name, "verbmapping", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        // Ensure verb is indicated first
                        if (ReaderXml.MoveToAttribute("Verb"))
                        {
                            Microservice_Endpoint_RequestType_Enum verb = Microservice_Endpoint_RequestType_Enum.ERROR;
                            switch (ReaderXml.Value.Trim().ToUpper())
                            {
                                case "DELETE":
                                    verb = Microservice_Endpoint_RequestType_Enum.DELETE;
                                    break;

                                case "GET":
                                    verb = Microservice_Endpoint_RequestType_Enum.GET;
                                    break;

                                case "POST":
                                    verb = Microservice_Endpoint_RequestType_Enum.POST;
                                    break;

                                case "PUT":
                                    verb = Microservice_Endpoint_RequestType_Enum.PUT;
                                    break;
                            }

                            // If a valid verb found, continue
                            if (verb != Microservice_Endpoint_RequestType_Enum.ERROR)
                            {
                                // Build the verb mapping
                                Engine_VerbMapping verbMapping = new Engine_VerbMapping(null, !DisabledAtTop, Microservice_Endpoint_Protocol_Enum.JSON, verb);
                                if (ReaderXml.MoveToAttribute("Method"))
                                    verbMapping.Method = ReaderXml.Value.Trim();
                                if ((!DisabledAtTop) && (ReaderXml.MoveToAttribute("Enabled")) && (String.Compare(ReaderXml.Value.Trim(), "false", StringComparison.OrdinalIgnoreCase) == 0))
                                    verbMapping.Enabled = false;
                                if (ReaderXml.MoveToAttribute("Protocol"))
                                {
                                    switch (ReaderXml.Value.Trim().ToUpper())
                                    {
                                        case "JSON":
                                            verbMapping.Protocol = Microservice_Endpoint_Protocol_Enum.JSON;
                                            break;

                                        case "JSON-P":
                                            verbMapping.Protocol = Microservice_Endpoint_Protocol_Enum.JSON_P;
                                            break;

                                        case "PROTOBUF":
                                            verbMapping.Protocol = Microservice_Endpoint_Protocol_Enum.PROTOBUF;
                                            break;

                                        case "SOAP":
                                            verbMapping.Protocol = Microservice_Endpoint_Protocol_Enum.SOAP;
                                            break;

                                        case "TEXT":
                                            verbMapping.Protocol = Microservice_Endpoint_Protocol_Enum.TEXT;
                                            break;

                                        case "XML":
                                            verbMapping.Protocol = Microservice_Endpoint_Protocol_Enum.XML;
                                            break;

                                        default:
                                            verbMapping.Protocol = Microservice_Endpoint_Protocol_Enum.JSON;
                                            break;
                                    }
                                }

                                // Get the mapping to componentid and restriction id
                                if (ReaderXml.MoveToAttribute("ComponentID"))
                                    verbMapping.ComponentId = ReaderXml.Value.Trim();
                                if (ReaderXml.MoveToAttribute("RestrictionRangeID"))
                                {
                                    verbMapping.RestrictionRangeSetId = ReaderXml.Value.Trim();
                                }

                                // If valid, add to this endpoint
                                if ((!String.IsNullOrEmpty(verbMapping.ComponentId)) && (!String.IsNullOrEmpty(verbMapping.Method)))
                                {
                                    // Add the verb mapping to the right spot
                                    switch (verbMapping.RequestType)
                                    {
                                        case Microservice_Endpoint_RequestType_Enum.DELETE:
                                            Endpoint.DeleteMapping = verbMapping;
                                            break;

                                        case Microservice_Endpoint_RequestType_Enum.GET:
                                            Endpoint.GetMapping = verbMapping;
                                            break;

                                        case Microservice_Endpoint_RequestType_Enum.POST:
                                            Endpoint.PostMapping = verbMapping;
                                            break;

                                        case Microservice_Endpoint_RequestType_Enum.PUT:
                                            Endpoint.PutMapping = verbMapping;
                                            break;

                                    }
                                }
                            }
                        }
                    }
                }
            }
        }