/// <summary> /// Determines a routing target and returns the result as a routing entry. If null is returned, the target cannot be resolved. /// </summary> /// <param name="path">HTTP Method + URL</param> /// <returns>Routing entry or null</returns> public RoutingEntry GetEntry(string path) { //1. find target identifiers IList <RoutingEntry> routingEntries = _routingTree.FindRoutingEntries(path); if (routingEntries.Count == 0) { //no entries found return(null); } else { //priorize Routing Entries with no generic element foreach (RoutingEntry routingEntry in routingEntries) { //search for non generic first and return the first occurence: if (!routingEntry.Path.Contains("*")) { return(routingEntry); } } //if only generic Routing Entries have been found, return the generic Routing Entry with the highest metric RoutingEntry generic = null; foreach (RoutingEntry routingEntry in routingEntries) { if (generic == null || generic.Metrics < routingEntry.Metrics) { generic = routingEntry; } } return(generic); } }
/// <summary> /// Adds a path or a sub path to this tree (depending on whether this path or parts of this path have already been added) /// and registers the ID as a leaf /// </summary> /// <param name="path">path</param> /// <param name="routingEntry">the routing entry</param> public void AddPath(string path, RoutingEntry routingEntry) { //Path looks like GET/login/{id}/hello/ //1. Split the path into its elements string[] elements = path.Split('/'); /* * //1.a check for "*" which are not trailing * if (path.TrimEnd('*').Contains("*")) * { * throw new Exception("'*' is only allowed as a trailer, use '{...}' instead to get generic values"); * } */ //2. add path TreeNode <string> currentNode = _root; foreach (string element in elements) { string value; if (element.Contains("{")) { value = "{}"; } else { value = element; } TreeNode <string> child = currentNode.GetChildWithValue(value); if (child == null) { TreeNode <string> newChild = new TreeNode <string>(value); currentNode.Add(newChild); currentNode = newChild; } else { currentNode = child; } } //3. add identifier if (!currentNode.HasChildWithValue(routingEntry.Identifier)) { routingEntry.Metrics = elements.Length; currentNode.Add(routingEntry); } }
public void AddEntry(string destinationPath, string processingGroup, HttpController httpController, MethodInfo methodInfo) { //1. check whether path is valid if (!IsPathValid(destinationPath)) { throw new Exception("Destination Path is in an invalid format: " + destinationPath); } //2. check whether path is distinct if (!IsPathDisjunct(destinationPath)) { throw new Exception("Destination Path is not distinct: " + destinationPath); } //3. create and add route RoutingEntry routingEntry = new RoutingEntry(processingGroup, httpController, methodInfo, "Rx" + _counter++, destinationPath, destinationPath.Split('/').Length); _routingTree.AddPath(destinationPath, routingEntry); }