/// <summary>
        /// Determines whether this instance equals a specified route.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="route">The route to compare.</param>
        /// <param name="parameterName">The name of the parameter.</param>
        /// <param name="values">A list of parameter values.</param>
        /// <param name="routeDirection">The route direction.</param>
        /// <returns>
        /// True if this instance equals a specified route; otherwise, false.
        /// </returns>
        public virtual bool Match(HttpRequestMessage request, IHttpRoute route, string parameterName, IDictionary <string, object> values, HttpRouteDirection routeDirection)
        {
            if (request == null)
            {
                throw Error.ArgumentNull("request");
            }

            if (values == null)
            {
                throw Error.ArgumentNull("values");
            }

            if (routeDirection == HttpRouteDirection.UriResolution)
            {
                object odataPathRouteValue;
                if (values.TryGetValue(ODataRouteConstants.ODataPath, out odataPathRouteValue))
                {
                    string odataPath = odataPathRouteValue as string;
                    if (odataPath == null)
                    {
                        // No odataPath means the path is empty; this is necessary for service documents
                        odataPath = String.Empty;
                    }

                    ODataPath path = PathHandler.Parse(EdmModel, odataPath);
                    if (path != null)
                    {
                        // Set all the properties we need for routing, querying, formatting
                        request.SetEdmModel(EdmModel);
                        request.SetODataPathHandler(PathHandler);
                        request.SetODataPath(path);
                        request.SetODataRouteName(RouteName);
                        request.SetODataRoutingConventions(RoutingConventions);

                        if (!values.ContainsKey(ODataRouteConstants.Controller))
                        {
                            // Select controller name using the routing conventions
                            string controllerName = SelectControllerName(path, request);
                            if (controllerName != null)
                            {
                                values[ODataRouteConstants.Controller] = controllerName;
                            }
                        }

                        return(true);
                    }
                }
                return(false);
            }
            else
            {
                // This constraint only applies to URI resolution
                return(true);
            }
        }
        /// <summary>
        /// Determines whether this instance equals a specified route.
        /// </summary>
        ///
        /// <returns>
        /// True if this instance equals a specified route; otherwise, false.
        /// </returns>
        /// <param name="request">The request.</param><param name="route">The route to compare.</param><param name="parameterName">The name of the parameter.</param><param name="values">A list of parameter values.</param><param name="routeDirection">The route direction.</param>
        public override bool Match(HttpRequestMessage request, IHttpRoute route, string parameterName, IDictionary <string, object> values, HttpRouteDirection routeDirection)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            if (values == null)
            {
                throw new ArgumentNullException("values");
            }
            if (routeDirection != HttpRouteDirection.UriResolution)
            {
                return(true);
            }
            object obj;

            if (values.TryGetValue(ODataRouteConstants.ODataPath, out obj))
            {
                var entitylistODataFeedDataAdapter = new EntityListODataFeedDataAdapter(new PortalConfigurationDataAdapterDependencies());
                EdmModel = entitylistODataFeedDataAdapter.GetEdmModel();

                string    odataPath1 = obj as string ?? string.Empty;
                ODataPath odataPath2;
                try
                {
                    odataPath2 = PathHandler.Parse(EdmModel, odataPath1);
                }
                catch (ODataException ex)
                {
                    throw new HttpResponseException(request.CreateErrorResponse(HttpStatusCode.NotFound, "The OData path is invalid.", ex));
                }
                if (odataPath2 != null)
                {
                    var odataProperties = request.ODataProperties();
                    odataProperties.Model              = EdmModel;
                    odataProperties.PathHandler        = PathHandler;
                    odataProperties.Path               = odataPath2;
                    odataProperties.RouteName          = RouteName;
                    odataProperties.RoutingConventions = RoutingConventions;
                    if (!values.ContainsKey(ODataRouteConstants.Controller))
                    {
                        string str = SelectControllerName(odataPath2, request);
                        if (str != null)
                        {
                            values[ODataRouteConstants.Controller] = str;
                        }
                    }
                    return(true);
                }
            }
            return(false);
        }
        public virtual bool Match(HttpRequestMessage request, IHttpRoute route, string parameterName, IDictionary <string, object> values, HttpRouteDirection routeDirection)
        {
            if (request == null)
            {
                throw Error.ArgumentNull("request");
            }

            if (values == null)
            {
                throw Error.ArgumentNull("values");
            }

            if (routeDirection == HttpRouteDirection.UriResolution)
            {
                object odataPathRouteValue;
                if (values.TryGetValue(ODataRouteConstants.ODataPath, out odataPathRouteValue))
                {
                    string odataPath = odataPathRouteValue as string;
                    if (odataPath == null)
                    {
                        // No odataPath means the path is empty; this is necessary for service documents
                        odataPath = String.Empty;
                    }

                    ODataPath path;
                    try
                    {
                        path = PathHandler.Parse(EdmModel, odataPath);
                    }
                    catch (ODataException e)
                    {
                        throw new HttpResponseException(
                                  request.CreateErrorResponse(HttpStatusCode.NotFound, SRResources.ODataPathInvalid, e));
                    }

                    if (path != null)
                    {
                        // Set all the properties we need for routing, querying, formatting
                        request.ODataProperties().Model              = EdmModel;
                        request.ODataProperties().PathHandler        = PathHandler;
                        request.ODataProperties().Path               = path;
                        request.ODataProperties().RouteName          = RouteName;
                        request.ODataProperties().RoutingConventions = RoutingConventions;

                        if (!values.ContainsKey(ODataRouteConstants.Controller))
                        {
                            // Select controller name using the routing conventions
                            string controllerName = SelectControllerName(path, request);
                            if (controllerName != null)
                            {
                                values[ODataRouteConstants.Controller] = controllerName;
                            }
                        }

                        return(true);
                    }
                }
                return(false);
            }
            else
            {
                // This constraint only applies to URI resolution
                return(true);
            }
        }
Beispiel #4
0
        public override bool Match(HttpRequestMessage request, IHttpRoute route, string parameterName, IDictionary <string, object> values, HttpRouteDirection routeDirection)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            if (routeDirection != HttpRouteDirection.UriResolution)
            {
                return(true);
            }

            object oDataPathValue;

            if (!values.TryGetValue(ODataRouteConstants.ODataPath, out oDataPathValue))
            {
                return(false);
            }

            string oDataPathString = oDataPathValue as string;

            ODataPath path;
            IEdmModel model;

            try
            {
                model = EdmModelProvider(request);

                string requestLeftPart = request.RequestUri.GetLeftPart(UriPartial.Path);
                string serviceRoot     = requestLeftPart;

                if (!string.IsNullOrEmpty(oDataPathString))
                {
                    serviceRoot = RemoveODataPath(serviceRoot, oDataPathString);
                }

                string oDataPathAndQuery = requestLeftPart.Substring(serviceRoot.Length);
                oDataPathAndQuery = WebUtility.UrlDecode(oDataPathAndQuery);
                path = PathHandler.Parse(model, oDataPathAndQuery);
            }
            catch (Exception)
            {
                throw;
                //TODO: add logging
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            if (path == null)
            {
                return(false);
            }

            HttpRequestMessageProperties odataProperties = request.ODataProperties();

            odataProperties.Model              = model;
            odataProperties.PathHandler        = PathHandler;
            odataProperties.Path               = path;
            odataProperties.RouteName          = RouteName;
            odataProperties.RoutingConventions = RoutingConventions;
            values.Add(typeof(IDataService).FullName, _dataService);

            if (values.ContainsKey(ODataRouteConstants.Controller))
            {
                return(true);
            }

            string controllerName = SelectControllerName(path, request);

            if (controllerName != null)
            {
                values[ODataRouteConstants.Controller] = controllerName;
            }

            return(true);
        }
        public virtual bool Match(HttpRequestMessage request, IHttpRoute route, string parameterName, IDictionary <string, object> values, HttpRouteDirection routeDirection)
        {
            if (request == null)
            {
                throw Error.ArgumentNull("request");
            }

            if (values == null)
            {
                throw Error.ArgumentNull("values");
            }

            if (routeDirection == HttpRouteDirection.UriResolution)
            {
                object oDataPathValue;
                if (values.TryGetValue(ODataRouteConstants.ODataPath, out oDataPathValue))
                {
                    string    oDataPathString = oDataPathValue as string;
                    ODataPath path;

                    try
                    {
                        // Service root is the current RequestUri, less the query string and the ODataPath (always the
                        // last portion of the absolute path).  ODL expects an escaped service root and other service
                        // root calculations are calculated using AbsoluteUri (also escaped).  But routing exclusively
                        // uses unescaped strings, determined using
                        //    address.GetComponents(UriComponents.Path, UriFormat.Unescaped)
                        //
                        // For example if the AbsoluteUri is
                        // <http://localhost/odata/FunctionCall(p0='Chinese%E8%A5%BF%E9%9B%85%E5%9B%BEChars')>, the
                        // oDataPathString will contain "FunctionCall(p0='Chinese西雅图Chars')".
                        //
                        // Due to this decoding and the possibility of unecessarily-escaped characters, there's no
                        // reliable way to determine the original string from which oDataPathString was derived.
                        // Therefore a straightforward string comparison won't always work.  See RemoveODataPath() for
                        // details of chosen approach.
                        string requestLeftPart = request.RequestUri.GetLeftPart(UriPartial.Path);
                        string serviceRoot     = requestLeftPart;
                        if (!String.IsNullOrEmpty(oDataPathString))
                        {
                            serviceRoot = RemoveODataPath(serviceRoot, oDataPathString);
                        }

                        // As mentioned above, we also need escaped ODataPath.
                        // The requestLeftPart and request.RequestUri.Query are both escaped.
                        // The ODataPath for service documents is empty.
                        string oDataPathAndQuery = requestLeftPart.Substring(serviceRoot.Length);
                        if (!String.IsNullOrEmpty(request.RequestUri.Query))
                        {
                            // Ensure path handler receives the query string as well as the path.
                            oDataPathAndQuery += request.RequestUri.Query;
                        }

                        // Leave an escaped '/' out of the service route because DefaultODataPathHandler will add a
                        // literal '/' to the end of this string if not already present. That would double the slash
                        // in response links and potentially lead to later 404s.
                        if (serviceRoot.EndsWith(_escapedSlash, StringComparison.OrdinalIgnoreCase))
                        {
                            serviceRoot = serviceRoot.Substring(0, serviceRoot.Length - 3);
                        }

                        path = PathHandler.Parse(EdmModel, serviceRoot, oDataPathAndQuery);
                    }
                    catch (ODataException)
                    {
                        path = null;
                    }

                    if (path != null)
                    {
                        // Set all the properties we need for routing, querying, formatting
                        request.ODataProperties().Model              = EdmModel;
                        request.ODataProperties().PathHandler        = PathHandler;
                        request.ODataProperties().Path               = path;
                        request.ODataProperties().RouteName          = RouteName;
                        request.ODataProperties().RoutingConventions = RoutingConventions;

                        if (!values.ContainsKey(ODataRouteConstants.Controller))
                        {
                            // Select controller name using the routing conventions
                            string controllerName = SelectControllerName(path, request);
                            if (controllerName != null)
                            {
                                values[ODataRouteConstants.Controller] = controllerName;
                            }
                        }

                        return(true);
                    }
                }

                return(false);
            }
            else
            {
                // This constraint only applies to URI resolution
                return(true);
            }
        }
Beispiel #6
0
        public override bool Match(
            HttpRequestMessage request,
            IHttpRoute route,
            string parameterName,
            IDictionary <string, object> values,
            HttpRouteDirection routeDirection)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            if (values == null)
            {
                throw new ArgumentNullException("values");
            }

            if (routeDirection != HttpRouteDirection.UriResolution)
            {
                return(true);
            }

            object odataPathRouteValue;

            if (!values.TryGetValue(ODataRouteConstants.ODataPath, out odataPathRouteValue))
            {
                return(false);
            }

            string odataPath = odataPathRouteValue as string ?? string.Empty;

            ODataPath path;
            IEdmModel model;

            try
            {
                request.Properties[Constants.CustomODataPath] = odataPath;
                model     = EdmModelProvider(request);
                odataPath = (string)request.Properties[Constants.CustomODataPath];
                path      = PathHandler.Parse(model, odataPath);
            }
            catch (ODataException)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            if (path == null)
            {
                return(false);
            }

            HttpRequestMessageProperties odataProperties = request.ODataProperties();

            odataProperties.Model              = model;
            odataProperties.PathHandler        = PathHandler;
            odataProperties.Path               = path;
            odataProperties.RouteName          = RouteName;
            odataProperties.RoutingConventions = RoutingConventions;

            if (values.ContainsKey(ODataRouteConstants.Controller))
            {
                return(true);
            }

            string controllerName = SelectControllerName(path, request);

            if (controllerName != null)
            {
                values[ODataRouteConstants.Controller] = controllerName;
            }

            return(true);
        }
Beispiel #7
0
        public override bool Match(
            HttpRequestMessage request,
            IHttpRoute route,
            string parameterName,
            IDictionary <string, object> values,
            HttpRouteDirection routeDirection)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            if (values == null)
            {
                throw new ArgumentNullException("values");
            }

            if (routeDirection != HttpRouteDirection.UriResolution)
            {
                return(true);
            }

            object oDataPathValue;

            if (!values.TryGetValue(ODataRouteConstants.ODataPath, out oDataPathValue))
            {
                return(false);
            }

            string oDataPathString = oDataPathValue as string;

            ODataPath path;
            IEdmModel model;

            try
            {
                request.Properties[Constants.CustomODataPath] = oDataPathString;

                model           = EdmModelProvider(request);
                oDataPathString = (string)request.Properties[Constants.CustomODataPath];

                string requestLeftPart = request.RequestUri.GetLeftPart(UriPartial.Path);
                string serviceRoot     = requestLeftPart;

                if (!String.IsNullOrEmpty(oDataPathString))
                {
                    serviceRoot = RemoveODataPath(serviceRoot, oDataPathString);
                }

                string oDataPathAndQuery = requestLeftPart.Substring(serviceRoot.Length);
                if (!String.IsNullOrEmpty(request.RequestUri.Query))
                {
                    oDataPathAndQuery += request.RequestUri.Query;
                }

                if (serviceRoot.EndsWith(_escapedSlash, StringComparison.OrdinalIgnoreCase))
                {
                    serviceRoot = serviceRoot.Substring(0, serviceRoot.Length - 3);
                }

                path = PathHandler.Parse(model, serviceRoot, oDataPathAndQuery);
            }
            catch (ODataException)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            if (path == null)
            {
                return(false);
            }

            HttpRequestMessageProperties odataProperties = request.ODataProperties();

            odataProperties.Model              = model;
            odataProperties.PathHandler        = PathHandler;
            odataProperties.Path               = path;
            odataProperties.RouteName          = RouteName;
            odataProperties.RoutingConventions = RoutingConventions;

            if (values.ContainsKey(ODataRouteConstants.Controller))
            {
                return(true);
            }

            string controllerName = SelectControllerName(path, request);

            if (controllerName != null)
            {
                values[ODataRouteConstants.Controller] = controllerName;
            }

            return(true);
        }