-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathSparqlEndpoint.cs
More file actions
359 lines (304 loc) · 10.2 KB
/
SparqlEndpoint.cs
File metadata and controls
359 lines (304 loc) · 10.2 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Waher.Content;
using Waher.Content.Semantic;
using Waher.Events;
using Waher.Networking.HTTP;
using Waher.Script;
using Waher.Script.Persistence.SPARQL;
using Waher.Security.LoginMonitor;
namespace Waher.WebService.Sparql
{
/// <summary>
/// Web service that can be used to execute SPARQL queries on the server.
///
/// References:
/// https://www.w3.org/TR/sparql12-protocol/
/// https://www.w3.org/TR/sparql12-query/
/// https://www.w3.org/TR/sparql12-federated-query/
/// </summary>
public class SparqlEndpoint : HttpAsynchronousResource, IHttpGetMethod, IHttpPostMethod
{
private readonly HttpAuthenticationScheme[] authenticationSchemes;
/// <summary>
/// Web service that can be used to execute SPARQL queries on the server.
/// </summary>
/// <param name="ResourceName">Name of resource.</param>
/// <param name="AuthenticationSchemes">Authentication schemes.</param>
public SparqlEndpoint(string ResourceName, params HttpAuthenticationScheme[] AuthenticationSchemes)
: base(ResourceName)
{
this.authenticationSchemes = AuthenticationSchemes;
}
/// <summary>
/// If the resource handles sub-paths.
/// </summary>
public override bool HandlesSubPaths => false;
/// <summary>
/// If the resource uses user sessions.
/// </summary>
public override bool UserSessions => true;
/// <summary>
/// If the GET method is allowed.
/// </summary>
public bool AllowsGET => true;
/// <summary>
/// If the POST method is allowed.
/// </summary>
public bool AllowsPOST => true;
/// <summary>
/// Executes the GET method on the resource.
/// </summary>
/// <param name="Request">HTTP Request</param>
/// <param name="Response">HTTP Response</param>
/// <exception cref="HttpException">If an error occurred when processing the method.</exception>
public async Task GET(HttpRequest Request, HttpResponse Response)
{
CheckAuthorization(Request);
if (!Request.Header.TryGetQueryParameter("query", out _))
{
await Response.SendResponse(new SeeOtherException("/Sparql.md"));
return;
}
// TODO: Return page if empty GET.
State State = new State(Request);
State.Start();
(SparqlQuery Query, ISemanticCube[] DefaultGraphs, string[] NamedGraphs, bool Pretty) =
await this.GetQueryGraphs(Request.Header.QueryParameters, State);
Task T = Task.Run(() => this.Process(Request, Response, Query, DefaultGraphs, NamedGraphs, State, Pretty));
}
private static void CheckAuthorization(HttpRequest Request)
{
if (Request.User is null || !Request.User.HasPrivilege(SparqlServiceModule.QueryPrivileges))
throw new ForbiddenException(Request, "Access to endpoint denied.");
}
private async Task<(SparqlQuery, ISemanticCube[], string[], bool)> GetQueryGraphs(
IEnumerable<KeyValuePair<string, string>> QueryParameters, State State)
{
SparqlQuery Query = null;
foreach (KeyValuePair<string, string> P in QueryParameters)
{
switch (P.Key)
{
case "query":
Expression Exp = new Expression(P.Value);
if (!(Exp.Root is SparqlQuery Query2))
throw new BadRequestException("Invalid SPARQL Query.");
Query = Query2;
break;
case "default-graph-uri":
case "named-graph-uri":
case "pretty":
break;
default:
throw new BadRequestException("Unrecognized query parameter: " + P.Key);
}
}
if (Query is null)
throw new BadRequestException("Query missing.");
State.Parsed();
(ISemanticCube[] DefaultGraphs, string[] NamedGraphs, bool Pretty) =
await this.GetQueryGraphs(Query, QueryParameters, State);
return (Query, DefaultGraphs, NamedGraphs, Pretty);
}
private async Task<(ISemanticCube[], string[], bool)> GetQueryGraphs(SparqlQuery Query,
IEnumerable<KeyValuePair<string, string>> QueryParameters, State State)
{
List<ISemanticCube> DefaultGraphs = null;
List<string> NamedGraphs = null;
bool Pretty = false;
foreach (KeyValuePair<string, string> P in QueryParameters)
{
switch (P.Key)
{
case "default-graph-uri":
if (!string.IsNullOrEmpty(P.Value))
{
Uri SourceUri = new Uri(P.Value);
IGraphSource Source = await SparqlQuery.GetSourceHandler(SourceUri, false);
ISemanticCube Cube = await Source.LoadGraph(SourceUri, Query, false, await State.GetOrigin());
DefaultGraphs ??= new List<ISemanticCube>();
DefaultGraphs.Add(Cube);
}
break;
case "named-graph-uri":
if (!string.IsNullOrEmpty(P.Value))
{
NamedGraphs ??= new List<string>();
NamedGraphs.Add(P.Value);
}
break;
case "pretty":
if (!CommonTypes.TryParse(P.Value, out bool b))
throw new BadRequestException("Invalid boolean value.");
Pretty = b;
break;
}
}
State.DefaultLoaded();
return (DefaultGraphs?.ToArray(), NamedGraphs?.ToArray(), Pretty);
}
/// <summary>
/// Executes the POST method on the resource.
/// </summary>
/// <param name="Request">HTTP Request</param>
/// <param name="Response">HTTP Response</param>
/// <exception cref="HttpException">If an error occurred when processing the method.</exception>
public async Task POST(HttpRequest Request, HttpResponse Response)
{
CheckAuthorization(Request);
if (!Request.HasData)
{
await Response.SendResponse(new BadRequestException());
return;
}
State State = new State(Request);
State.Start();
ContentResponse Content = await Request.DecodeDataAsync();
if (Content.HasError)
{
await Response.SendResponse(Content.Error);
return;
}
object Obj = Content.Decoded;
ISemanticCube[] DefaultGraphs;
string[] NamedGraphs;
bool Pretty;
State.Parsed();
if (Obj is SparqlQuery Query)
{
(DefaultGraphs, NamedGraphs, Pretty) = await this.GetQueryGraphs(Query,
Request.Header.QueryParameters, State);
}
else if (Obj is Dictionary<string, string> Form)
{
(Query, DefaultGraphs, NamedGraphs, Pretty) = await this.GetQueryGraphs(Form, State);
}
else if (Obj is Dictionary<string, string[]> Form2)
{
LinkedList<KeyValuePair<string, string>> Parameters = new LinkedList<KeyValuePair<string, string>>();
foreach (KeyValuePair<string, string[]> P in Form2)
{
foreach (string s2 in P.Value)
Parameters.AddLast(new KeyValuePair<string, string>(P.Key, s2));
}
(Query, DefaultGraphs, NamedGraphs, Pretty) = await this.GetQueryGraphs(Parameters, State);
}
else if (Obj is Dictionary<string, object> Form3)
{
LinkedList<KeyValuePair<string, string>> Parameters = new LinkedList<KeyValuePair<string, string>>();
foreach (KeyValuePair<string, object> P in Form3)
{
if (P.Value is string s)
Parameters.AddLast(new KeyValuePair<string, string>(P.Key, s));
else if (P.Value is Array A)
{
foreach (object Item in A)
{
if (Item is string s2)
Parameters.AddLast(new KeyValuePair<string, string>(P.Key, s2));
else
{
await Response.SendResponse(new BadRequestException("Invalid form."));
return;
}
}
}
else
{
await Response.SendResponse(new BadRequestException("Invalid form."));
return;
}
}
(Query, DefaultGraphs, NamedGraphs, Pretty) = await this.GetQueryGraphs(Parameters, State);
}
else
{
await Response.SendResponse(new UnsupportedMediaTypeException("Content must be a SPARQL query or a web form containing a SPARQL query."));
return;
}
Task _ = Task.Run(() => this.Process(Request, Response, Query, DefaultGraphs, NamedGraphs, State, Pretty));
}
private async void Process(HttpRequest Request, HttpResponse Response,
SparqlQuery Query, ISemanticCube[] DefaultGraphs, string[] NamedGraphs,
State State, bool Pretty)
{
bool Error = false;
try
{
Variables v = Request.Session ?? new Variables();
if (!(DefaultGraphs is null) && DefaultGraphs.Length > 0)
{
if (DefaultGraphs.Length == 1)
v[" Default Graph "] = DefaultGraphs[0];
else
{
SemanticDataSet DataSet = new SemanticDataSet();
foreach (ISemanticCube Cube in DefaultGraphs)
DataSet.Add(Cube);
v[" Default Graph "] = DataSet;
}
}
else
v[" Default Graph "] = await GraphStore.GetDefaultSource(await GraphStore.GetOrigin(Request));
if (!(NamedGraphs is null))
Query.RegisterNamedGraph(NamedGraphs);
object Result = await Query.Expression.EvaluateAsync(v);
State.Evaluated();
if (Result is SparqlResultSet ResultSet)
ResultSet.Pretty = Pretty;
await Response.Return(Result);
State.Returned();
}
catch (Exception ex)
{
Error = true;
Log.Exception(ex);
if (!Response.ResponseSent)
{
await Response.SendResponse(ex);
State.Returned();
}
}
finally
{
State.Stop();
try
{
KeyValuePair<string, object>[] Tags = await LoginAuditor.Annotate(Request.RemoteEndPoint,
new KeyValuePair<string, object>("RemoteEndPoint", Request.RemoteEndPoint),
new KeyValuePair<string, object>("SPARQL", Query.SubExpression),
new KeyValuePair<string, object>("Total ms", State.TotalMilliseconds),
new KeyValuePair<string, object>("Parsing ms", State.ParsingTimeMs),
new KeyValuePair<string, object>("Loading ms", State.LoadDefaultMs),
new KeyValuePair<string, object>("Evaluating ms", State.EvaluatingMs),
new KeyValuePair<string, object>("Returning ms", State.ReturningMs),
new KeyValuePair<string, object>("Error", Error));
if (Error)
{
Log.Warning("SPARQL evaluated with errors.", Request.Resource.ResourceName,
Request.User.UserName, "SparqlEval", Tags);
}
else
{
Log.Notice("SPARQL evaluated.", Request.Resource.ResourceName,
Request.User.UserName, "SparqlEval", Tags);
}
}
catch (Exception ex)
{
Log.Exception(ex);
}
}
}
/// <summary>
/// Any authentication schemes used to authenticate users before access is granted to the corresponding resource.
/// </summary>
/// <param name="Request">Current request</param>
public override HttpAuthenticationScheme[] GetAuthenticationSchemes(HttpRequest Request)
{
return this.authenticationSchemes;
}
}
}