Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
InternetContentSource.cs
1using System;
3using System.Runtime.ExceptionServices;
4using System.Security.Cryptography.X509Certificates;
5using System.Text;
6using System.Threading.Tasks;
7using System.Xml;
8using Waher.Content;
15using Waher.Things;
16
18{
23 {
28 {
29 }
30
36 public Grade Supports(Uri Source)
37 {
38 if (!Source.IsAbsoluteUri ||
40 !InternetContent.CanGet(Source, out Grade _, out _))
41 {
42 return Grade.NotAtAll;
43 }
44 else
45 return Grade.Barely;
46 }
47
56 public async Task<ISemanticCube> LoadGraph(Uri Source, ScriptNode Node, bool NullIfNotFound,
57 RequestOrigin Caller)
58 {
59 object Result = null;
60 string XmlString = null;
61
62 try
63 {
64 // TODO: Include caller credentials in request, if available.
65
66 if (!Types.TryGetModuleParameter("X509", out X509Certificate Certificate))
67 Certificate = null;
68
69 ContentResponse Content = await InternetContent.GetAsync(Source, Certificate,
70 new KeyValuePair<string, string>("Accept", "text/turtle, application/x-turtle, application/rdf+xml;q=0.9, application/ld+json;q=0.8, text/xml;q=0.2, " + PlainTextCodec.DefaultContentType + ";q=0.1"));
71
72 if (NullIfNotFound && Content.HasError)
73 return null;
74 else
75 Content.AssertOk();
76
77 Result = Content.Decoded;
78 if (Result is ISemanticCube Cube)
79 return Cube;
80
81 if (Result is ISemanticModel Model)
82 return await InMemorySemanticCube.Create(Model);
83
84 if (Result is XmlDocument Xml)
85 return new RdfDocument(Xml);
86
87 if (Result is string s)
88 {
89 if (XML.IsValidXml(s))
90 {
91 Xml = XML.ParseXml(XmlString = s, true);
92 return new RdfDocument(Xml);
93 }
94 else
95 return new TurtleDocument(s);
96 }
97
98 }
99 catch (XmlException ex)
100 {
101 if (NullIfNotFound)
102 return null;
103 else
104 throw XML.AnnotateException(ex, XmlString);
105 }
106 catch (Exception ex)
107 {
108 if (NullIfNotFound)
109 return null;
110 else
111 ExceptionDispatchInfo.Capture(ex).Throw();
112 }
113
114 if (NullIfNotFound)
115 return null;
116 else
117 {
118 StringBuilder sb = new StringBuilder();
119
120 sb.Append("Graph not a semantic cube or semantic model: ");
121 sb.Append(Source.ToString());
122 sb.Append(" Type of content returned: ");
123 sb.Append(Result?.GetType().FullName);
124
125 throw new ScriptRuntimeException(sb.ToString(), Node);
126 }
127 }
128 }
129}
Contains information about a response to a content request.
bool HasError
If an error occurred.
object Decoded
Decoded object.
void AssertOk()
Asserts response is OK.
Static class managing encoding and decoding of internet content.
static bool CanGet(Uri Uri, out Grade Grade, out IContentGetter Getter)
If a resource can be gotten, given its URI.
static Task< ContentResponse > GetAsync(Uri Uri, params KeyValuePair< string, string >[] Headers)
Gets a resource, given its URI.
static async Task< InMemorySemanticCube > Create(ISemanticModel Model)
Creates an in-memory semantic cube from a semantic model.
Contains semantic information stored in an RDF document.
Definition: RdfDocument.cs:23
Contains semantic information stored in a turtle document. https://www.w3.org/TR/rdf12-turtle/ https:...
Plain text encoder/decoder.
const string DefaultContentType
text/plain
Helps with common XML-related tasks.
Definition: XML.cs:21
static XmlException AnnotateException(XmlException ex)
Creates a new XML Exception object, with reference to the source XML file, for information.
Definition: XML.cs:1762
static XmlDocument ParseXml(string Xml)
Parses an XML Document from its string representation.
Definition: XML.cs:713
static bool IsValidXml(string Xml)
Checks if a string is valid XML
Definition: XML.cs:1397
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:15
static bool TryGetModuleParameter(string Name, out object Value)
Tries to get a module parameter value.
Definition: Types.cs:607
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
async Task< ISemanticCube > LoadGraph(Uri Source, ScriptNode Node, bool NullIfNotFound, RequestOrigin Caller)
Loads the graph
Grade Supports(Uri Source)
How well a source with a given URI can be loaded by the class.
static bool IsLocal(Uri Source)
Checks if an URI corresponds to a local file name resource.
Tokens available in request.
Definition: RequestOrigin.cs:9
Interface for semantic cubes.
Interface for semantic models.
Definition: ImplTypes.g.cs:58
Grade
Grade enumeration
Definition: Grade.cs:7