Esempio n. 1
0
        public static Type GetValueType(this DataRoute route)
        {
            switch (route.Attribute.GetDeclarationType())
            {
            case DeclarationType.Single:
                return(route.RootType);

            case DeclarationType.List:
                return(route.Attribute.GetIsSingleFile()
                        ? ListType.MakeGenericType(route.RootType)
                        : route.RootType);

            case DeclarationType.Dictionary:
                if (!route.Attribute.GetIsSingleFile())
                {
                    return(route.RootType);
                }

                var dictionaryAttribute = (AssetDictionaryAttribute)route.Attribute;
                var keyType             = dictionaryAttribute.KeyType;
                if (keyType == null)
                {
                    goto default;
                }

                var valueType = route.RootType;
                return(DictionaryType.MakeGenericType(keyType, valueType));

            default:
                return(null);
            }
        }
        public static void AddTestDataRoute <T>(this RouteCollection routes, string name, string template, object defaults) where T : class, new()
        {
            IOptions <RouteOptions> routeOpts = Options.Create <RouteOptions>(new RouteOptions());
            var constraintResolver            = new DefaultInlineConstraintResolver(routeOpts);
            var dataFetchingRouter            = new DataFetchingRouter <T>(new MockRouter());
            var dataRoute = new DataRoute(
                dataFetchingRouter,
                name,
                template,
                new RouteValueDictionary(defaults),
                new RouteValueDictionary(null),
                new RouteValueDictionary(null),
                constraintResolver);

            routes.Add(dataRoute);
        }
Esempio n. 3
0
        public static void AddTestDataRoute <T>(this RouteCollection routes, string name, string template, object defaults,
                                                ContentPermission writePermission = null, DiversionStrategy divertOverride = null) where T : class, new()
        {
            IOptions <RouteOptions> routeOpts = Options.Create <RouteOptions>(new RouteOptions());
            var constraintResolver            = new DefaultInlineConstraintResolver(routeOpts);
            var dataFetchingRouter            = new DataFetchingRouter <T>(new MockRouter(), false, writePermission, divertOverride);
            var dataRoute = new DataRoute(
                dataFetchingRouter,
                name,
                template,
                new RouteValueDictionary(defaults),
                new RouteValueDictionary(null),
                new RouteValueDictionary(null),
                constraintResolver);

            routes.Add(dataRoute);
        }
Esempio n. 4
0
 public static bool IsSingle(this DataRoute route)
 {
     return(route.Attribute.IsSingle());
 }
Esempio n. 5
0
        /// <summary>
        /// Get the possible urls that could have selected a given container or content item via a given route
        /// </summary>
        /// <param name="route">The route</param>
        /// <param name="address">The content address</param>
        /// <returns>The urls that could have accessed the object via the route</returns>
        public List <string> GetUrls(DataRoute route, Address address)
        {
            // Start list of urls which could map to this item.  Note, catchall items (starting *) are treated as normal ones
            List <string> urls = new List <string> {
                route.RouteTemplate.Replace("{*", "{")
            };

            var keyOutputs = route.KeyOutputs();

            List <string> replaceWiths = null;

            // Action & controller

            if (keyOutputs.ContainsKey("controller") && keyOutputs.ContainsKey("action"))
            {
                if (keyOutputs["controller"].StartsWith("?"))
                {
                    throw new Exception("Route " + route.RouteTemplate + " is a data route which lacks but must have a specified controller");
                }

                if (!ContentTypeHierarchy.ControllerActions.ContainsKey(keyOutputs["controller"]))
                {
                    return(new List <string>());  // Controller doesn't exist therefore no urls
                }
                // copy list so we can change 'actions'
                var actions = ContentTypeHierarchy.ControllerActions[keyOutputs["controller"]].ToList();
                if (keyOutputs["action"].StartsWith("?"))
                {
                    if (keyOutputs["action"].Length > 1 && actions.Contains(keyOutputs["action"].Substring(1).ToLower()))
                    {
                        actions.Add("??");
                    }
                    UrlX.PermuteReplace(urls, "{action}", actions);
                }
                else if (!actions.Contains(keyOutputs["action"]))
                {
                    return(new List <string>()); // Fixed action doesn't exist therefore no urls
                }
            }

            // Route vars addressing the content item

            foreach (var kvp in keyOutputs)
            {
                if (kvp.Key == "controller" || kvp.Key == "action") // already dealt with
                {
                    continue;
                }

                if (kvp.Value.StartsWith("?") && kvp.Value != "??") // variable takes any value
                {
                    if (address.ContainsKey(kvp.Key))               // its part of the content item address
                    {
                        // path element is the default value, so this url element is optional
                        replaceWiths = new List <string> {
                            address.GetAsString(kvp.Key)
                        };
                        if (kvp.Value.Length > 1 && address.GetAsString(kvp.Key) == kvp.Value.Substring(1))
                        {
                            replaceWiths.Add("??");
                        }
                        // matches a path element
                        address.SetMatched(kvp.Key);   // this address element is matched
                    }
                    else
                    {
                        replaceWiths = new List <string> {
                            "?"
                        };                        // no match, so many urls mapped to this item
                        if (kvp.Value.Length > 1) // there's a default value so the element is optional
                        {
                            replaceWiths.Add("??");
                        }
                    }
                    UrlX.PermuteReplace(urls, "{" + kvp.Key + "}", replaceWiths);
                }
                else if (kvp.Value == "??")
                {
                    UrlX.PermuteReplace(urls, "{" + kvp.Key + "}", new List <string> {
                        "??"
                    });                                                                          // optional variable
                }
                else // fixed value, has to be matched by item
                {
                    bool matched = false;
                    if (address.ContainsKey(kvp.Key))
                    {
                        if (address.GetAsString(kvp.Key) == kvp.Value)
                        {
                            matched = true;
                            address.SetMatched(kvp.Key);
                        }
                    }
                    if (!matched)
                    {
                        return(new List <string>());
                    }
                }
            }

            if (!address.FullyMatched) // Fails because one or more address elements could not have come from this route
            {
                return(new List <string>());
            }

            // Deal with possible url variations with omitted path elements

            for (int i = 0; i < urls.Count; i++)
            {
                while (urls[i].EndsWith("/??"))
                {
                    urls[i] = urls[i].Substring(0, urls[i].Length - 3);
                }
                if (urls[i].Contains("??"))
                {
                    urls[i] = null;
                }
            }

            return(urls.Where(u => u != null).OrderBy(u => u.Split('/').Length).Select(u => "/" + u).ToList());
        }