-
Notifications
You must be signed in to change notification settings - Fork 87
/
UriLoaderAsync.cs
510 lines (478 loc) · 27 KB
/
UriLoaderAsync.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
// <copyright>
// dotNetRDF is free and open source software licensed under the MIT License
// -------------------------------------------------------------------------
//
// Copyright (c) 2009-2021 dotNetRDF Project (http://dotnetrdf.org/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is furnished
// to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
*/
using System;
using System.IO;
using System.Net;
using VDS.RDF.Parsing.Handlers;
namespace VDS.RDF.Parsing
{
public static partial class UriLoader
{
/// <summary>
/// Attempts to load a RDF Graph from a URI asynchronously.
/// </summary>
/// <param name="g">Graph to assert triple in.</param>
/// <param name="u">URI to load from.</param>
/// <param name="parser">Parser to use.</param>
/// <param name="callback">Callback to invoke when the operation completes.</param>
/// <param name="state">State to pass to the callback.</param>
/// <remarks>
/// <para>
/// Uses the supplied parser to attempt parsing regardless of the actual Content Type returned.
/// </para>
/// <para>
/// In the event that the URI is a File URI the <see cref="FileLoader">FileLoader</see> will be used instead. If the URI is a Data URI then the <see cref="DataUriLoader">DataUriLoader</see> will be used instead.
/// </para>
/// <para>
/// <strong>Note:</strong> UriLoader will assign the Graph the source URI as it's Base URI unless the Graph already has a Base URI or is non-empty prior to attempting parsing. Note that any Base URI specified in the RDF contained in the file will override this initial Base URI. In some cases this may lead to invalid RDF being accepted and generating strange relative URIs, if you encounter this either set a Base URI prior to calling this method or create an instance of the relevant parser and invoke it directly.
/// </para>
/// <para>
/// If the loading completes normally the callback will be invoked normally, if an error occurs it will be invoked and passed an instance of <see cref="AsyncError"/> as the state which contains details of the error and the original state.
/// </para>
/// </remarks>
public static void Load(IGraph g, Uri u, IRdfReader parser, GraphCallback callback, Object state)
{
if (g == null) throw new RdfParseException("Cannot read RDF into a null Graph");
if (u == null) throw new RdfParseException("Cannot read RDF from a null URI");
if (u.IsFile)
{
// Invoke FileLoader instead
RaiseWarning("This is a file: URI so invoking the FileLoader instead");
if (Path.DirectorySeparatorChar == '/')
{
FileLoader.Load(g, u.AbsoluteUri.Substring(7), parser);
}
else
{
FileLoader.Load(g, u.AbsoluteUri.Substring(8), parser);
}
// FileLoader.Load() will run synchronously so once this completes we can invoke the callback
callback(g, state);
return;
}
if (u.Scheme.Equals("data"))
{
// Invoke DataUriLoader instead
RaiseWarning("This is a data: URI so invoking the DataUriLoader instead");
DataUriLoader.Load(g, u);
// After DataUriLoader.Load() has run (which happens synchronously) can invoke the callback
callback(g, state);
return;
}
// Set Base URI if necessary
if (g.BaseUri == null && g.IsEmpty) g.BaseUri = u;
Load(new GraphHandler(g), u, parser, (_, s) => callback(g, s), state);
}
/// <summary>
/// Attempts to load a RDF Graph from a URI asynchronously.
/// </summary>
/// <param name="g">Graph to assert triple in.</param>
/// <param name="u">URI to load from.</param>
/// <param name="callback">Callback to invoke when the operation completes.</param>
/// <param name="state">State to pass to the callback.</param>
/// <remarks>
/// <para>
/// Will attempt to autodetect the format of the RDF based on the Content-Type header of the HTTP response.
/// </para>
/// <para>
/// In the event that the URI is a File URI the <see cref="FileLoader">FileLoader</see> will be used instead. If the URI is a Data URI then the <see cref="DataUriLoader">DataUriLoader</see> will be used instead.
/// </para>
/// <para>
/// <strong>Note:</strong> UriLoader will assign the Graph the source URI as it's Base URI unless the Graph already has a Base URI or is non-empty prior to attempting parsing. Note that any Base URI specified in the RDF contained in the file will override this initial Base URI. In some cases this may lead to invalid RDF being accepted and generating strange relative URIs, if you encounter this either set a Base URI prior to calling this method or create an instance of the relevant parser and invoke it directly.
/// </para>
/// <para>
/// If the loading completes normally the callback will be invoked normally, if an error occurs it will be invoked and passed an instance of <see cref="AsyncError"/> as the state which contains details of the error and the original state.
/// </para>
/// </remarks>
public static void Load(IGraph g, Uri u, GraphCallback callback, Object state)
{
Load(g, u, null, callback, state);
}
/// <summary>
/// Attempts to load a RDF Graph from a URI asynchronously using an RDF Handler.
/// </summary>
/// <param name="handler">RDF Handler to use.</param>
/// <param name="u">URI to load from.</param>
/// <param name="parser">Parser to use.</param>
/// <param name="callback">Callback to invoke when the operation completes.</param>
/// <param name="state">State to pass to the callback.</param>
/// <remarks>
/// <para>
/// Uses the supplied parser to attempt parsing regardless of the actual Content Type returned.
/// </para>
/// <para>
/// In the event that the URI is a File URI the <see cref="FileLoader">FileLoader</see> will be used instead.
/// </para>
/// <para>
/// If the URI is a Data URI then the <see cref="DataUriLoader">DataUriLoader</see> will be used instead.
/// </para>
/// <para>
/// If the loading completes normally the callback will be invoked normally, if an error occurs it will be invoked and passed an instance of <see cref="AsyncError"/> as the state which contains details of the error and the original state.
/// </para>
/// </remarks>
public static void Load(IRdfHandler handler, Uri u, IRdfReader parser, RdfHandlerCallback callback, Object state)
{
if (handler == null) throw new RdfParseException("Cannot read RDF using a null RDF Handler");
if (u == null) throw new RdfParseException("Cannot load RDF from a null URI");
try
{
if (u.IsFile)
{
// Invoke FileLoader instead
RaiseWarning("This is a file: URI so invoking the FileLoader instead");
if (Path.DirectorySeparatorChar == '/')
{
FileLoader.Load(handler, u.AbsoluteUri.Substring(7), parser);
}
else
{
FileLoader.Load(handler, u.AbsoluteUri.Substring(8), parser);
}
// FileLoader.Load() will run synchronously so once this completes we can invoke the callback
callback(handler, state);
return;
}
if (u.Scheme.Equals("data"))
{
// Invoke DataUriLoader instead
RaiseWarning("This is a data: URI so invoking the DataUriLoader instead");
DataUriLoader.Load(handler, u);
// After DataUriLoader.Load() has run (which happens synchronously) can invoke the callback
callback(handler, state);
return;
}
// Sanitise the URI to remove any Fragment ID
u = Tools.StripUriFragment(u);
// TODO: Add use of Cache into here, this is tricky because this code is primarily intended for Silverlight where we disable the cache purposefully
// Setup the Request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(u);
// Want to ask for RDF formats
if (parser != null)
{
// If a non-null parser set up a HTTP Header that is just for the given parser
request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(parser);
}
else
{
request.Accept = MimeTypesHelper.HttpAcceptHeader;
}
// Use HTTP GET
request.Method = "GET";
#if !NETCORE
request.Timeout = Options.UriLoaderTimeout;
#endif
if (_userAgent != null && !_userAgent.Equals(String.Empty))
{
#if NETCORE
request.Headers[HttpRequestHeader.UserAgent] = _userAgent;
#else
request.UserAgent = _userAgent;
#endif
}
Tools.HttpDebugRequest(request);
try
{
request.BeginGetResponse(result =>
{
try
{
using (HttpWebResponse response = (HttpWebResponse) request.EndGetResponse(result))
{
Tools.HttpDebugResponse(response);
// Get a Parser and load the RDF
if (parser == null)
{
// Only need to auto-detect the parser if a specific one wasn't specified
parser = MimeTypesHelper.GetParser(response.ContentType);
}
parser.Warning += RaiseWarning;
parser.Load(handler, new StreamReader(response.GetResponseStream()));
// Finally can invoke the callback
callback(handler, state);
}
}
catch (WebException webEx)
{
if (webEx.Response != null) Tools.HttpDebugResponse((HttpWebResponse)webEx.Response);
callback(handler, new AsyncError(new RdfParseException("A HTTP Error occurred loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exeption for details", webEx), state));
}
catch (Exception ex)
{
callback(handler, new AsyncError(new RdfParseException("Unexpected error while loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exception for details", ex), state));
}
}, null);
}
catch (WebException webEx)
{
if (webEx.Response != null) Tools.HttpDebugResponse((HttpWebResponse) webEx.Response);
callback(handler, new AsyncError(new RdfParseException("A HTTP Error occurred loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exeption for details", webEx), state));
}
catch (Exception ex)
{
callback(handler, new AsyncError(new RdfParseException("Unexpected error while loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exception for details", ex), state));
}
}
catch (UriFormatException uriEx)
{
// URI Format Invalid
throw new RdfParseException("Unable to load from the given URI '" + u.AbsoluteUri + "' since it's format was invalid, see inner exception for details", uriEx);
}
}
/// <summary>
/// Attempts to load a RDF Graph from a URI asynchronously using an RDF Handler.
/// </summary>
/// <param name="handler">RDF Handler to use.</param>
/// <param name="u">URI to load from.</param>
/// <param name="callback">Callback to invoke when the operation completes.</param>
/// <param name="state">State to pass to the callback.</param>
/// <remarks>
/// <para>
/// Attempts to autodetect the RDF format based on the Content-Type header of the HTTP response.
/// </para>
/// <para>
/// If the loading completes normally the callback will be invoked normally, if an error occurs it will be invoked and passed an instance of <see cref="AsyncError"/> as the state which contains details of the error and the original state.
/// </para>
/// </remarks>
public static void Load(IRdfHandler handler, Uri u, RdfHandlerCallback callback, Object state)
{
Load(handler, u, (IRdfReader)null, callback, state);
}
/// <summary>
/// Attempts to load a RDF dataset asynchronously from the given URI into the given Triple Store.
/// </summary>
/// <param name="store">Triple Store to load into.</param>
/// <param name="u">URI to attempt to get a RDF dataset from.</param>
/// <param name="parser">Parser to use to parse the RDF dataset.</param>
/// <param name="callback">Callback to invoke when the operation completes.</param>
/// <param name="state">State to pass to the callback.</param>
/// <remarks>
/// <para>
/// If the <paramref name="parser"/> parameter is set to null then this method attempts to select the relevant Store Parser based on the Content Type header returned in the HTTP Response.
/// </para>
/// <para>
/// If you know ahead of time the Content Type you can explicitly pass in the parser to use.
/// </para>
/// <para>
/// If the loading completes normally the callback will be invoked normally, if an error occurs it will be invoked and passed an instance of <see cref="AsyncError"/> as the state which contains details of the error and the original state.
/// </para>
/// </remarks>
public static void Load(ITripleStore store, Uri u, IStoreReader parser, TripleStoreCallback callback, Object state)
{
if (store == null) throw new RdfParseException("Cannot read a RDF dataset into a null Triple Store");
if (u == null) throw new RdfParseException("Cannot read a RDF dataset from a null URI");
Load(new StoreHandler(store), u, parser, (_, s) => callback(store, s), state);
}
/// <summary>
/// Attempts to load a RDF dataset asynchronously from the given URI into the given Triple Store.
/// </summary>
/// <param name="store">Triple Store to load into.</param>
/// <param name="u">URI to attempt to get a RDF dataset from.</param>
/// <param name="callback">Callback to invoke when the operation completes.</param>
/// <param name="state">State to pass to the callback.</param>
/// <remarks>
/// <para>
/// Attempts to select the relevant Store Parser based on the Content Type header returned in the HTTP Response.
/// </para>
/// <para>
/// If the loading completes normally the callback will be invoked normally, if an error occurs it will be invoked and passed an instance of <see cref="AsyncError"/> as the state which contains details of the error and the original state.
/// </para>
/// </remarks>
public static void Load(ITripleStore store, Uri u, TripleStoreCallback callback, Object state)
{
Load(store, u, null, callback, state);
}
/// <summary>
/// Attempts to load a RDF dataset asynchronously from the given URI using a RDF Handler.
/// </summary>
/// <param name="handler">RDF Handler to use.</param>
/// <param name="u">URI to attempt to get a RDF dataset from.</param>
/// <param name="parser">Parser to use to parse the RDF dataset.</param>
/// <param name="callback">Callback to invoke when the operation completes.</param>
/// <param name="state">State to pass to the callback.</param>
/// <remarks>
/// <para>
/// If the <paramref name="parser"/> parameter is set to null then this method attempts to select the relevant Store Parser based on the Content Type header returned in the HTTP Response.
/// </para>
/// <para>
/// If you know ahead of time the Content Type you can explicitly pass in the parser to use.
/// </para>
/// <para>
/// If the loading completes normally the callback will be invoked normally, if an error occurs it will be invoked and passed an instance of <see cref="AsyncError"/> as the state which contains details of the error and the original state.
/// </para>
/// </remarks>
public static void Load(IRdfHandler handler, Uri u, IStoreReader parser, RdfHandlerCallback callback, Object state)
{
if (u == null) throw new RdfParseException("Cannot read a RDF dataset from a null URI");
if (handler == null) throw new RdfParseException("Cannot read a RDF dataset using a null RDF handler");
try
{
if (u.IsFile)
{
// Invoke FileLoader instead
RaiseWarning("This is a file: URI so invoking the FileLoader instead");
if (Path.DirectorySeparatorChar == '/')
{
FileLoader.Load(handler, u.AbsoluteUri.Substring(7), parser);
}
else
{
FileLoader.Load(handler, u.AbsoluteUri.Substring(8), parser);
}
// FileLoader.Load() will run synchronously so once this completes we can invoke the callback
callback(handler, state);
return;
}
if (u.Scheme.Equals("data"))
{
// Invoke DataUriLoader instead
RaiseWarning("This is a data: URI so invoking the DataUriLoader instead");
DataUriLoader.Load(handler, u);
// After DataUriLoader.Load() has run (which happens synchronously) can invoke the callback
callback(handler, state);
return;
}
// Sanitise the URI to remove any Fragment ID
u = Tools.StripUriFragment(u);
// Setup the Request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(u);
// Want to ask for RDF dataset formats
if (parser != null)
{
// If a non-null parser set up a HTTP Header that is just for the given parser
request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(parser);
}
else
{
request.Accept = MimeTypesHelper.HttpAcceptHeader;
}
// Use HTTP GET
request.Method = "GET";
#if !NETCORE
request.Timeout = Options.UriLoaderTimeout;
#endif
if (_userAgent != null && !_userAgent.Equals(String.Empty))
{
#if NETCORE
request.Headers[HttpRequestHeader.UserAgent] = _userAgent;
#else
request.UserAgent = _userAgent;
#endif
}
Tools.HttpDebugRequest(request);
try
{
request.BeginGetResponse(result =>
{
try
{
using (HttpWebResponse response = (HttpWebResponse) request.EndGetResponse(result))
{
Tools.HttpDebugResponse(response);
// Get a Parser and load the RDF
if (parser == null)
{
try
{
// Only need to auto-detect the parser if a specific one wasn't specified
parser = MimeTypesHelper.GetStoreParser(response.ContentType);
parser.Warning += RaiseWarning;
parser.Load(handler, new StreamReader(response.GetResponseStream()));
}
catch (RdfParserSelectionException)
{
RaiseStoreWarning("Unable to select a RDF Dataset parser based on Content-Type: " + response.ContentType + " - seeing if the content is an RDF Graph instead");
try
{
// If not a RDF Dataset format see if it is a Graph
IRdfReader rdfParser = MimeTypesHelper.GetParser(response.ContentType);
rdfParser.Load(handler, new StreamReader(response.GetResponseStream()));
}
catch (RdfParserSelectionException)
{
String data = new StreamReader(response.GetResponseStream()).ReadToEnd();
parser = StringParser.GetDatasetParser(data);
parser.Warning += RaiseStoreWarning;
parser.Load(handler, new StringReader(data));
}
}
}
else
{
parser.Warning += RaiseStoreWarning;
parser.Load(handler, new StreamReader(response.GetResponseStream()));
}
// Finally can invoke the callback
callback(handler, state);
}
}
catch (WebException webEx)
{
if (webEx.Response != null) Tools.HttpDebugResponse((HttpWebResponse)webEx.Response);
callback(handler, new AsyncError(new RdfParseException("A HTTP Error occurred loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exeption for details", webEx), state));
}
catch (Exception ex)
{
callback(handler, new AsyncError(new RdfParseException("Unexpected error while loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exception for details", ex), state));
}
}, null);
}
catch (WebException webEx)
{
if (webEx.Response != null) Tools.HttpDebugResponse((HttpWebResponse)webEx.Response);
callback(handler, new AsyncError(new RdfParseException("A HTTP Error occurred loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exeption for details", webEx), state));
}
catch (Exception ex)
{
callback(handler, new AsyncError(new RdfParseException("Unexpected error while loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exception for details", ex), state));
}
}
catch (UriFormatException uriEx)
{
// Uri Format Invalid
throw new RdfException("Unable to load from the given URI '" + u.AbsoluteUri + "' since it's format was invalid, see inner exception for details", uriEx);
}
}
/// <summary>
/// Attempts to load a RDF dataset asynchronously from the given URI using a RDF Handler.
/// </summary>
/// <param name="handler">RDF Handler to use.</param>
/// <param name="u">URI to attempt to get a RDF dataset from.</param>
/// <param name="callback">Callback to invoke when the operation completes.</param>
/// <param name="state">State to pass to the callback.</param>
/// <remarks>
/// <para>
/// Attempts to select the relevant Store Parser based on the Content Type header returned in the HTTP Response.
/// </para>
/// <para>
/// If the loading completes normally the callback will be invoked normally, if an error occurs it will be invoked and passed an instance of <see cref="AsyncError"/> as the state which contains details of the error and the original state.
/// </para>
/// </remarks>
public static void LoadDataset(IRdfHandler handler, Uri u, RdfHandlerCallback callback, Object state)
{
Load(handler, u, (IStoreReader)null, callback, state);
}
}
}