summaryrefslogtreecommitdiff
path: root/external/aspnetwebstack/src/System.Web.Http/QueryableAttribute.cs
blob: 0a7a16cd3755a1ef1d8b9d4a96ab77f42893c4dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using System.Web.Http.Properties;
using System.Web.Http.Query;

namespace System.Web.Http
{
    [SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes", Justification = "We want to be able to subclass this type")]
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
    public class QueryableAttribute : ActionFilterAttribute
    {
        private readonly QueryValidator _queryValidator;

        public QueryableAttribute()
        {
            _queryValidator = QueryValidator.Instance;
        }

        /// <summary>
        /// The maximum number of results that should be returned from this query regardless of query-specified limits. A value of <c>0</c>
        /// indicates no limit. Negative values are not supported and will cause a runtime exception.
        /// </summary>
        public int ResultLimit { get; set; }

        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            if (actionContext == null)
            {
                throw Error.ArgumentNull("actionContext");
            }

            if (ResultLimit < 0)
            {
                throw Error.InvalidOperation(SRResources.QueryableAttribute_InvalidResultLimit,
                    actionContext.ActionDescriptor.ActionName, actionContext.ControllerContext.ControllerDescriptor.ControllerName);
            }
        }

        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            if (actionExecutedContext == null)
            {
                throw Error.ArgumentNull("actionExecutedContext");
            }

            Contract.Assert(actionExecutedContext.Request != null);

            HttpRequestMessage request = actionExecutedContext.Request;
            HttpResponseMessage response = actionExecutedContext.Response;

            IQueryable query;
            if (response != null && response.TryGetContentValue(out query))
            {
                IQueryable deserializedQuery = null;
                if (request != null && request.RequestUri != null && !String.IsNullOrWhiteSpace(request.RequestUri.Query))
                {
                    Uri requestUri = request.RequestUri;
                    try
                    {
                        ServiceQuery serviceQuery = ODataQueryDeserializer.GetServiceQuery(requestUri);

                        if (serviceQuery.QueryParts.Count > 0)
                        {
                            IQueryable baseQuery = Array.CreateInstance(query.ElementType, 0).AsQueryable(); // T[]
                            deserializedQuery = ODataQueryDeserializer.Deserialize(baseQuery, serviceQuery.QueryParts);
                            if (_queryValidator != null)
                            {
                                _queryValidator.Validate(deserializedQuery);
                            }
                        }
                    }
                    catch (ParseException e)
                    {
                        actionExecutedContext.Response = request.CreateResponse(
                            HttpStatusCode.BadRequest,
                            Error.Format(SRResources.UriQueryStringInvalid, e.Message));
                        return;
                    }
                }

                if (deserializedQuery != null)
                {
                    query = QueryComposer.Compose(query, deserializedQuery);
                }

                query = ApplyResultLimit(actionExecutedContext, query);

                ((ObjectContent)response.Content).Value = query;
            }
        }

        protected virtual IQueryable ApplyResultLimit(HttpActionExecutedContext actionExecutedContext, IQueryable query)
        {
            if (actionExecutedContext == null)
            {
                throw Error.ArgumentNull("actionExecutedContext");
            }
            if (query == null)
            {
                throw Error.ArgumentNull("query");
            }

            if (ResultLimit > 0)
            {
                query = query.Take(ResultLimit);
            }
            return query;
        }
    }
}