Exemple #1
0
        public object Get(DynamicRouteRequest request)
        {
            // Match the route request to validate the route requested
            Match match = Regex.Match(Request.GetPhysicalPath(), @"\/api\/(?<indexName>\w*)\/(?<routeId>\w*)\/(?<route>.*)");
            DynamicOpenSearchRoute routeDefinition = null;

            // Load the dynamic route according to the index and the route id
            if (match.Success)
            {
                routeDefinition = LoadRoute(match.Groups["indexName"].Value, match.Groups["routeId"].Value);
            }
            else
            {
                throw new InvalidOperationException(string.Format("incorrect API route: {0}", Request.GetPhysicalPath()));
            }

            // Loads the type of the dynamic route
            IOpenSearchableElasticType type = ecf.GetOpenSearchableElasticTypeByName(match.Groups["indexName"].Value, routeDefinition.TypeName);

            if (type == null)
            {
                throw new InvalidTypeModelException(routeDefinition.TypeName, string.Format("Type '{0}' is not found in the type extensions. Check that plugins are loaded", routeDefinition.TypeName));
            }

            // split the route defintion
            string[] defintionRouteSplitted = routeDefinition.RouteFromIndex.Split('/');
            // check that requested route elements are respected
            string[] requestRouteSplitted = match.Groups["route"].Value.Split('/');
            if (requestRouteSplitted.Count() != defintionRouteSplitted.Count())
            {
                throw new InvalidOperationException(string.Format("Incorrect API request: {0}. Must match defintion: {1}", match.Groups["route"].Value, routeDefinition.RouteFromIndex));
            }

            // build a parameters table with params defintion
            RouteParameter[] parameters = new RouteParameter[defintionRouteSplitted.Count()];
            for (int i = 0; i < defintionRouteSplitted.Count(); i++)
            {
                string routeElement = defintionRouteSplitted[i];
                if (!routeElement.StartsWith("{"))
                {
                    parameters[i] = null;
                    continue;
                }
                string parameter = routeElement.Trim(new char[] { '{', '}' });
                if (string.IsNullOrEmpty(parameter))
                {
                    throw new InvalidOperationException(string.Format("API route defintion error: {0}. Empty parameter.", routeDefinition.RouteFromIndex));
                }
                if (!routeDefinition.RouteParameters.ContainsKey(parameter))
                {
                    throw new InvalidOperationException(string.Format("API route defintion error: {0}. Undefined parameter: {1}", routeDefinition.RouteFromIndex, routeElement));
                }

                parameters[i] = routeDefinition.RouteParameters[parameter];
            }



            // Now from requested route rebuild the opensearch request
            UriBuilder url = new UriBuilder(ecf.RootWebConfig.AppSettings.Settings["baseUrl"].Value);

            url.Path += string.Format("/catalogue/{0}/{1}/search", match.Groups["indexName"].Value, routeDefinition.TypeName);
            NameValueCollection osParameters = HttpUtility.ParseQueryString("");

            for (int i = 0; i < requestRouteSplitted.Count(); i++)
            {
                if (parameters[i] == null)
                {
                    continue;
                }
                osParameters.Add(parameters[i].OpenSearchParameterId, requestRouteSplitted[i]);
            }

            List <string> mimeTypes = Request.AcceptTypes.ToList();

            if (osParameters.AllKeys.Contains("enctype"))
            {
                mimeTypes.Add(osParameters["enctype"]);
            }

            OpenSearchDescription osd = type.GetProxyOpenSearchDescription();

            OpenSearchDescriptionUrl osdUrl = OpenSearchFactory.GetOpenSearchUrlByTypeAndMaxParam(osd, mimeTypes, osParameters);

            osParameters = OpenSearchFactory.ReplaceTemplateByIdentifier(osParameters, osdUrl);
            osParameters.Add(Request.QueryString);

            return(OpenSearchService.Query(type, osParameters));
        }