-
Notifications
You must be signed in to change notification settings - Fork 475
/
Copy pathWebApiRequestMessage.cs
237 lines (211 loc) · 8.06 KB
/
WebApiRequestMessage.cs
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using Microsoft.AspNet.OData.Batch;
using Microsoft.AspNet.OData.Common;
using Microsoft.AspNet.OData.Extensions;
using Microsoft.AspNet.OData.Formatter;
using Microsoft.AspNet.OData.Formatter.Deserialization;
using Microsoft.AspNet.OData.Interfaces;
using Microsoft.AspNet.OData.Routing;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OData;
using Microsoft.OData.UriParser;
using ODataPath = Microsoft.AspNet.OData.Routing.ODataPath;
namespace Microsoft.AspNet.OData.Adapters
{
/// <summary>
/// Adapter class to convert Asp.Net WebApi request message to OData WebApi.
/// </summary>
internal class WebApiRequestMessage : IWebApiRequestMessage
{
/// <summary>
/// The inner request wrapped by this instance.
/// </summary>
private HttpRequest innerRequest;
/// <summary>
/// Initializes a new instance of the WebApiRequestMessage class.
/// </summary>
/// <param name="request">The inner request.</param>
public WebApiRequestMessage(HttpRequest request)
{
if (request == null)
{
throw Error.ArgumentNull("request");
}
this.innerRequest = request;
this.Headers = new WebApiRequestHeaders(request.Headers);
IODataFeature feature = request.ODataFeature();
if (feature != null)
{
this.Context = new WebApiContext(feature);
}
// Get the ODataOptions from the global service provider.
ODataOptions options = request.HttpContext.RequestServices.GetRequiredService<ODataOptions>();
if (options != null)
{
this.Options = new WebApiOptions(options);
}
}
/// <summary>
/// Gets the contents of the HTTP message.
/// </summary>
public IWebApiContext Context { get; private set; }
/// <summary>
/// WebAPI headers associated with the request
/// </summary>
public IWebApiHeaders Headers { get; private set;}
/// <summary>
/// Gets a value indicating if this is a count request.
/// </summary>
/// <returns></returns>
public bool IsCountRequest()
{
ODataPath path = this.innerRequest.ODataFeature().Path;
return path != null && path.Segments.LastOrDefault() is CountSegment;
}
/// <summary>
/// Gets the HTTP method used by the HTTP request message.
/// </summary>
public ODataRequestMethod Method
{
get
{
string method = this.innerRequest.Method.ToUpperInvariant();
bool ignoreCase = true;
ODataRequestMethod methodEnum = ODataRequestMethod.Unknown;
if (Enum.TryParse<ODataRequestMethod>(method, ignoreCase, out methodEnum))
{
return methodEnum;
}
return ODataRequestMethod.Unknown;
}
}
/// <summary>
/// Get the options associated with the request.
/// </summary>
public IWebApiOptions Options { get; private set; }
/// <summary>
/// The request container associated with the request.
/// </summary>
public IServiceProvider RequestContainer
{
get { return this.innerRequest.GetRequestContainer(); }
}
/// <summary>
/// Gets the Uri used for the HTTP request.
/// </summary>
public Uri RequestUri
{
get { return new Uri(this.innerRequest.GetEncodedUrl()); }
}
/// <summary>
/// get the deserializer provider associated with the request.
/// </summary>
/// <returns></returns>
public ODataDeserializerProvider DeserializerProvider
{
get
{
return this.innerRequest.GetRequestContainer().GetRequiredService<ODataDeserializerProvider>();
}
}
/// <summary>
/// Get the next page link for a given page size.
/// </summary>
/// <param name="pageSize">The page size.</param>
/// <param name="instance">Object which will be used to generate the skiptoken value.</param>
/// <param name="objToSkipTokenValue">Function that takes in an instance and returns the skiptoken value string.</param>
/// <returns></returns>
public Uri GetNextPageLink(int pageSize, object instance = null, Func<object, string> objToSkipTokenValue = null)
{
return this.innerRequest.GetNextPageLink(pageSize, instance, objToSkipTokenValue);
}
/// <summary>
/// Creates an ETag from concurrency property names and values.
/// </summary>
/// <param name="properties">The input property names and values.</param>
/// <returns>The generated ETag string.</returns>
public string CreateETag(IDictionary<string, object> properties)
{
IODataFeature feature = this.innerRequest.ODataFeature();
if (feature == null)
{
throw Error.InvalidOperation(SRResources.RequestMustContainConfiguration);
}
return this.innerRequest.GetETagHandler().CreateETag(properties)?.ToString();
}
/// <summary>
/// Gets the EntityTagHeaderValue ETag>.
/// </summary>
/// <remarks>This function uses types that are AspNet-specific.</remarks>
public ETag GetETag(EntityTagHeaderValue etagHeaderValue)
{
return this.innerRequest.GetETag(etagHeaderValue);
}
/// <summary>
/// Gets the EntityTagHeaderValue ETag>.
/// </summary>
/// <remarks>This function uses types that are AspNet-specific.</remarks>
public ETag GetETag<TEntity>(EntityTagHeaderValue etagHeaderValue)
{
return this.innerRequest.GetETag<TEntity>(etagHeaderValue);
}
/// <summary>
/// Gets a list of content Id mappings associated with the request.
/// </summary>
/// <returns></returns>
public IDictionary<string, string> ODataContentIdMapping
{
get { return innerRequest.GetODataContentIdMapping(); }
}
/// <summary>
/// Gets the path handler associated with the request.
/// </summary>
/// <returns></returns>
public IODataPathHandler PathHandler
{
get { return this.innerRequest.GetPathHandler(); }
}
/// <summary>
/// Gets the query parameters from the query with duplicated key ignored.
/// </summary>
/// <returns></returns>
public IDictionary<string, string> QueryParameters
{
get
{
IDictionary<string, string> result = new Dictionary<string, string>();
foreach (var pair in this.innerRequest.Query)
{
if (!result.ContainsKey(pair.Key))
{
result.Add(pair.Key, pair.Value);
}
}
return result;
}
}
/// <summary>
/// Gets the reader settings associated with the request.
/// </summary>
/// <returns></returns>
public ODataMessageReaderSettings ReaderSettings
{
get { return this.innerRequest.GetReaderSettings(); }
}
/// <summary>
/// Gets the writer settings associated with the request.
/// </summary>
/// <returns></returns>
public ODataMessageWriterSettings WriterSettings
{
get { return this.innerRequest.GetWriterSettings(); }
}
}
}