/// <summary> /// Parses a raw SPARQL Results String using the given Parser /// </summary> /// <param name="results">SPARQL Result Set to fill</param> /// <param name="data">Raw SPARQL Results String</param> /// <param name="reader">Parser to use</param> public static void ParseResultSet(SparqlResultSet results, String data, ISparqlResultsReader reader) { if (results == null) { throw new RdfParseException("Cannot read SPARQL Results into a null Result Set"); } if (data == null) { return; } if (reader == null) { //If no parser specified then auto-detect syntax ParseResultSet(results, data); } else { try { MemoryStream mem = new MemoryStream(); StreamWriter writer = new StreamWriter(mem); writer.Write(data); writer.Flush(); mem.Seek(0, SeekOrigin.Begin); reader.Load(results, new StreamReader(mem)); } catch { throw; } } }
/// <summary> /// Makes a Query where the expected Result is a <see cref="SparqlResultSet">SparqlResultSet</see> i.e. SELECT and ASK Queries. /// </summary> /// <param name="handler">Results Handler.</param> /// <param name="sparqlQuery">SPARQL Query String.</param> public virtual void QueryWithResultSet(ISparqlResultsHandler handler, String sparqlQuery) { try { // Make the Query HttpWebResponse httpResponse = QueryInternal(sparqlQuery, ResultsAcceptHeader); // Parse into a ResultSet based on Content Type String ctype = httpResponse.ContentType; if (ctype.Contains(";")) { ctype = ctype.Substring(0, ctype.IndexOf(";")); } ISparqlResultsReader resultsParser = MimeTypesHelper.GetSparqlParser(ctype); resultsParser.Load(handler, new StreamReader(httpResponse.GetResponseStream())); httpResponse.Close(); } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } // Some sort of HTTP Error occurred throw new RdfQueryException("A HTTP Error occurred while trying to make the SPARQL Query, see inner exception for details", webEx); } catch (RdfException) { // Some problem with the RDF or Parsing thereof throw; } }
/// <summary> /// Makes a Query against the SPARQL Endpoint processing the results with an appropriate handler from those provided /// </summary> /// <param name="rdfHandler">RDF Handler</param> /// <param name="resultsHandler">Results Handler</param> /// <param name="sparqlQuery">SPARQL Query</param> /// <returns></returns> public void Query(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, String sparqlQuery) { if (!this._skipLocalParsing) { //Parse the query locally to validate it and so we can decide what to do //when we receive the Response more easily as we'll know the query type //This also saves us wasting a HttpWebRequest on a malformed query SparqlQueryParser qparser = new SparqlQueryParser(); SparqlQuery q = qparser.ParseFromString(sparqlQuery); switch (q.QueryType) { case SparqlQueryType.Ask: case SparqlQueryType.Select: case SparqlQueryType.SelectAll: case SparqlQueryType.SelectAllDistinct: case SparqlQueryType.SelectAllReduced: case SparqlQueryType.SelectDistinct: case SparqlQueryType.SelectReduced: //Some kind of Sparql Result Set this._endpoint.QueryWithResultSet(resultsHandler, sparqlQuery); break; case SparqlQueryType.Construct: case SparqlQueryType.Describe: case SparqlQueryType.DescribeAll: //Some kind of Graph this._endpoint.QueryWithResultGraph(rdfHandler, sparqlQuery); break; case SparqlQueryType.Unknown: default: //Error throw new RdfQueryException("Unknown Query Type was used, unable to determine how to process the response from Talis"); } } else { //If we're skipping local parsing then we'll need to just make a raw query and process the response using (HttpWebResponse response = this._endpoint.QueryRaw(sparqlQuery)) { try { //Is the Content Type referring to a Sparql Result Set format? ISparqlResultsReader sparqlParser = MimeTypesHelper.GetSparqlParser(response.ContentType); sparqlParser.Load(resultsHandler, new StreamReader(response.GetResponseStream())); response.Close(); } catch (RdfParserSelectionException) { //If we get a Parser Selection exception then the Content Type isn't valid for a Sparql Result Set //Is the Content Type referring to a RDF format? IRdfReader rdfParser = MimeTypesHelper.GetParser(response.ContentType); rdfParser.Load(rdfHandler, new StreamReader(response.GetResponseStream())); response.Close(); } } } }
/// <summary> /// Determines whether the Knowledge Base is consistent /// </summary> /// <param name="callback">Callback to invoke when the operation completes</param> /// <param name="state">State to be passed to the callback</param> public void IsConsistent(PelletConsistencyCallback callback, Object state) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.Endpoint.Uri); request.Method = this.Endpoint.HttpMethods.First(); request.Accept = MimeTypesHelper.HttpSparqlAcceptHeader; #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugRequest(request); } #endif request.BeginGetResponse(result => { using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result)) { #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugResponse(response); } #endif ISparqlResultsReader parser = MimeTypesHelper.GetSparqlParser(response.ContentType); SparqlResultSet results = new SparqlResultSet(); parser.Load(results, new StreamReader(response.GetResponseStream())); //Expect a boolean result set callback(results.Result, state); } }, null); }
public static void Load(this ISparqlResultsReader reader, SparqlResultSet resultSet, string filename) { using (var input = new StreamReader(filename)) { reader.Load(resultSet, input); } }
public static void Load(this ISparqlResultsReader reader, ISparqlResultsHandler handler, string filename) { using (var input = new StreamReader(filename)) { reader.Load(handler, input); } }
/// <summary> /// Makes a SPARQL Query against the Knowledge Base /// </summary> /// <param name="sparqlQuery">SPARQL Query</param> /// <returns></returns> public Object Query(String sparqlQuery) { SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(UriFactory.Create(this._sparqlUri)); using (HttpWebResponse response = endpoint.QueryRaw(sparqlQuery)) { try { ISparqlResultsReader sparqlParser = MimeTypesHelper.GetSparqlParser(response.ContentType); SparqlResultSet results = new SparqlResultSet(); sparqlParser.Load(results, new StreamReader(response.GetResponseStream())); response.Close(); return(results); } catch (RdfParserSelectionException) { IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType); Graph g = new Graph(); parser.Load(g, new StreamReader(response.GetResponseStream())); response.Close(); return(g); } } }
/// <summary> /// Returns whether the Knowledge Base is consistent. /// </summary> public bool IsConsistent() { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Endpoint.Uri); request.Method = Endpoint.HttpMethods.First(); request.Accept = MimeTypesHelper.HttpSparqlAcceptHeader; Tools.HttpDebugRequest(request); try { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { Tools.HttpDebugResponse(response); ISparqlResultsReader parser = MimeTypesHelper.GetSparqlParser(response.ContentType); SparqlResultSet results = new SparqlResultSet(); parser.Load(results, new StreamReader(response.GetResponseStream())); // Expect a boolean result set return(results.Result); } } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } throw new RdfReasoningException("A HTTP error occurred while communicating with the Pellet Server", webEx); } }
/// <summary> /// Makes a SPARQL Query against the underlying 4store Instance processing the results with the appropriate handler from those provided /// </summary> /// <param name="rdfHandler">RDF Handler</param> /// <param name="resultsHandler">Results Handler</param> /// <param name="sparqlQuery">SPARQL Query</param> public void Query(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, String sparqlQuery) { try { // Ensure Proxy Settings have been taken from the class _endpoint.Proxy = Proxy; _endpoint.UseCredentialsForProxy = false; HttpWebResponse response = _endpoint.QueryRaw(sparqlQuery); StreamReader data = new StreamReader(response.GetResponseStream()); try { // Is the Content Type referring to a Sparql Result Set format? ISparqlResultsReader resreader = MimeTypesHelper.GetSparqlParser(response.ContentType); resreader.Load(resultsHandler, data); response.Close(); } catch (RdfParserSelectionException) { // If we get a Parser Selection exception then the Content Type isn't valid for a Sparql Result Set // Is the Content Type referring to a RDF format? IRdfReader rdfreader = MimeTypesHelper.GetParser(response.ContentType); rdfreader.Load(rdfHandler, data); response.Close(); } } catch (WebException webEx) { throw StorageHelper.HandleHttpQueryError(webEx); } }
/// <summary> /// Makes a Query asynchronously where the expected Result is a <see cref="SparqlResultSet">SparqlResultSet</see> i.e. SELECT and ASK Queries /// </summary> /// <param name="query">SPARQL Query String</param> /// <param name="callback">Callback to invoke when the query completes</param> /// <param name="state">State to pass to the callback</param> public void QueryWithResultSet(String query, SparqlResultsCallback callback, Object state) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.Uri); request.Method = "POST"; request.ContentType = MimeTypesHelper.WWWFormURLEncoded; request.Accept = MimeTypesHelper.HttpSparqlAcceptHeader; #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugRequest(request); } #endif request.BeginGetRequestStream(result => { Stream stream = request.EndGetRequestStream(result); using (StreamWriter writer = new StreamWriter(stream)) { writer.Write("query="); writer.Write(HttpUtility.UrlEncode(query)); foreach (String u in this.DefaultGraphs) { writer.Write("&default-graph-uri="); writer.Write(Uri.EscapeDataString(u)); } foreach (String u in this.NamedGraphs) { writer.Write("&named-graph-uri="); writer.Write(Uri.EscapeDataString(u)); } writer.Close(); } request.BeginGetResponse(innerResult => { using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(innerResult)) { #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugResponse(response); } #endif ISparqlResultsReader parser = MimeTypesHelper.GetSparqlParser(response.ContentType, false); SparqlResultSet rset = new SparqlResultSet(); parser.Load(rset, new StreamReader(response.GetResponseStream())); response.Close(); callback(rset, state); } }, null); }, null); }
public void ParsingGZipResultsByFilenameAuto2() { foreach (String filename in this._autoResultsTestFiles) { SparqlResultSet results = new SparqlResultSet(); ISparqlResultsReader reader = MimeTypesHelper.GetSparqlParserByFileExtension(MimeTypesHelper.GetTrueFileExtension(filename)); reader.Load(results, filename); Assert.IsTrue(this._results.Equals(results), "Result Sets for file " + filename + " were not equal"); } }
/// <summary> /// Determines whether the Knowledge Base is consistent. /// </summary> /// <param name="callback">Callback to invoke when the operation completes.</param> /// <param name="state">State to be passed to the callback.</param> /// <remarks> /// If the operation succeeds the callback will be invoked normally, if there is an error the callback will be invoked with a instance of <see cref="AsyncError"/> passed as the state which provides access to the error message and the original state passed in. /// </remarks> public void IsConsistent(PelletConsistencyCallback callback, Object state) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Endpoint.Uri); request.Method = Endpoint.HttpMethods.First(); request.Accept = MimeTypesHelper.HttpSparqlAcceptHeader; Tools.HttpDebugRequest(request); try { request.BeginGetResponse(result => { try { using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result)) { Tools.HttpDebugResponse(response); ISparqlResultsReader parser = MimeTypesHelper.GetSparqlParser(response.ContentType); SparqlResultSet results = new SparqlResultSet(); parser.Load(results, new StreamReader(response.GetResponseStream())); // Expect a boolean result set callback(results.Result, state); } } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } callback(false, new AsyncError(new RdfReasoningException("A HTTP error occurred while communicating with the Pellet Server, see inner exception for details", webEx), state)); } catch (Exception ex) { callback(false, new AsyncError(new RdfReasoningException("An unexpected error occurred while communicating with the Pellet Server, see inner exception for details", ex), state)); } }, null); } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } callback(false, new AsyncError(new RdfReasoningException("A HTTP error occurred while communicating with the Pellet Server, see inner exception for details", webEx), state)); } catch (Exception ex) { callback(false, new AsyncError(new RdfReasoningException("An unexpected error occurred while communicating with the Pellet Server, see inner exception for details", ex), state)); } }
/// <summary> /// Loads a Result Set from GZipped input. /// </summary> /// <param name="handler">Results Handler to use.</param> /// <param name="input">Input to load from.</param> public void Load(ISparqlResultsHandler handler, StreamReader input) { if (handler == null) { throw new RdfParseException("Cannot parse SPARQL Results using a null Handler"); } if (input == null) { throw new RdfParseException("Cannot parse SPARQL Results from a null input"); } if (input.BaseStream is GZipStream) { _parser.Load(handler, input); } else { // Force the inner stream to be GZipped input = new StreamReader(new GZipStream(input.BaseStream, CompressionMode.Decompress)); _parser.Load(handler, input); } }
/// <summary> /// Makes a Query where the expected Result is a <see cref="SparqlResultSet">SparqlResultSet</see> i.e. SELECT and ASK Queries /// </summary> /// <param name="handler">Results Handler</param> /// <param name="sparqlQuery">SPARQL Query String</param> public virtual void QueryWithResultSet(ISparqlResultsHandler handler, String sparqlQuery) { try { //Make the Query HttpWebResponse httpResponse = this.QueryInternal(sparqlQuery, MimeTypesHelper.HttpSparqlAcceptHeader); //Parse into a ResultSet based on Content Type String ctype = httpResponse.ContentType; if (ctype.Contains(";")) { ctype = ctype.Substring(0, ctype.IndexOf(";")); } if (MimeTypesHelper.SparqlResults.Contains(ctype)) { ISparqlResultsReader resultsParser = MimeTypesHelper.GetSparqlParser(ctype); resultsParser.Load(handler, new StreamReader(httpResponse.GetResponseStream())); httpResponse.Close(); } else { httpResponse.Close(); throw new RdfParseException("The SPARQL Endpoint returned unexpected Content Type '" + ctype + "', this error may be due to the given URI not returning a SPARQL Result Set"); } } catch (WebException webEx) { #if DEBUG if (Options.HttpDebugging) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } } #endif //Some sort of HTTP Error occurred throw new RdfQueryException("A HTTP Error occurred while trying to make the SPARQL Query, see inner exception for details", webEx); } catch (RdfException) { //Some problem with the RDF or Parsing thereof throw; } }
/// <summary> /// Makes a Query asynchronously where the expected Result is a <see cref="SparqlResultSet">SparqlResultSet</see> i.e. SELECT and ASK Queries /// </summary> /// <param name="query">SPARQL Query String</param> /// <param name="handler">Results Handler</param> /// <param name="callback">Callback to invoke when the query completes</param> /// <param name="state">State to pass to the callback</param> public void QueryWithResultSet(ISparqlResultsHandler handler, String query, QueryCallback callback, Object state) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.Uri); request.Method = "POST"; request.ContentType = MimeTypesHelper.WWWFormURLEncoded; request.Accept = this.RdfAcceptHeader; Tools.HttpDebugRequest(request); request.BeginGetRequestStream(result => { Stream stream = request.EndGetRequestStream(result); using (StreamWriter writer = new StreamWriter(stream)) { writer.Write("query="); writer.Write(HttpUtility.UrlEncode(query)); foreach (String u in this.DefaultGraphs) { writer.Write("&default-graph-uri="); writer.Write(HttpUtility.UrlEncode(u)); } foreach (String u in this.NamedGraphs) { writer.Write("&named-graph-uri="); writer.Write(HttpUtility.UrlEncode(u)); } writer.Close(); } request.BeginGetResponse(innerResult => { using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(innerResult)) { Tools.HttpDebugResponse(response); ISparqlResultsReader parser = MimeTypesHelper.GetSparqlParser(response.ContentType, false); parser.Load(handler, new StreamReader(response.GetResponseStream())); response.Close(); callback(null, handler, state); } }, null); }, null); }
public void ParsingGZipResultsByStreamManual() { foreach (String filename in this._manualResultsTestFiles) { SparqlResultSet results = new SparqlResultSet(); String ext = MimeTypesHelper.GetTrueFileExtension(filename); ext = ext.Substring(1); MimeTypeDefinition def = MimeTypesHelper.Definitions.Where(d => d.CanParseSparqlResults && d.SupportsFileExtension(ext)).FirstOrDefault(); Assert.NotNull(def); ISparqlResultsReader reader = def.GetSparqlResultsParser(); reader.Load(results, File.OpenText(filename)); Assert.True(this._results.Equals(results), "Result Sets for file " + filename + " were not equal"); } }
public void ParsingGZipResultsByGZipStreamAuto() { foreach (String filename in this._autoResultsTestFiles) { SparqlResultSet results = new SparqlResultSet(); String ext = MimeTypesHelper.GetTrueFileExtension(filename); ext = ext.Substring(1); MimeTypeDefinition def = MimeTypesHelper.Definitions.Where(d => d.CanParseSparqlResults && d.SupportsFileExtension(ext)).FirstOrDefault(); Assert.NotNull(def); ISparqlResultsReader reader = def.GetSparqlResultsParser(); reader.Load(results, new StreamReader(new GZipStream(new FileStream(filename, FileMode.Open, FileAccess.Read), CompressionMode.Decompress))); Assert.True(this._results.Equals(results), "Result Sets for file " + filename + " were not equal"); } }
/// <summary> /// Processes a SPARQL Query against the Knowledge Base passing the results to the RDF or Results handler as appropriate /// </summary> /// <param name="rdfHandler">RDF Handler</param> /// <param name="resultsHandler">Results Handler</param> /// <param name="sparqlQuery">SPARQL Query</param> public void Query(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, String sparqlQuery) { SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(UriFactory.Create(this._sparqlUri)); using (HttpWebResponse response = endpoint.QueryRaw(sparqlQuery)) { try { ISparqlResultsReader sparqlParser = MimeTypesHelper.GetSparqlParser(response.ContentType); sparqlParser.Load(resultsHandler, new StreamReader(response.GetResponseStream())); } catch (RdfParserSelectionException) { IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType); parser.Load(rdfHandler, new StreamReader(response.GetResponseStream())); } response.Close(); } }
public void ParsingGZipResultsByGZipStreamManual() { foreach (String filename in this._manualResultsTestFiles) { SparqlResultSet results = new SparqlResultSet(); String ext = MimeTypesHelper.GetTrueFileExtension(filename); ext = ext.Substring(1); MimeTypeDefinition def = MimeTypesHelper.Definitions.Where(d => d.CanParseSparqlResults && d.SupportsFileExtension(ext)).FirstOrDefault(); if (def == null) { Assert.Fail("Failed to find MIME Type Definition for File Extension ." + ext); } ISparqlResultsReader reader = def.GetSparqlResultsParser(); reader.Load(results, new StreamReader(new GZipStream(new FileStream(filename, FileMode.Open, FileAccess.Read), CompressionMode.Decompress))); Assert.AreEqual(this._results, results, "Result Sets for file " + filename + " were not equal"); } }
public void ParsingGZipResultsByStreamAuto() { foreach (String filename in this._autoResultsTestFiles) { SparqlResultSet results = new SparqlResultSet(); String ext = MimeTypesHelper.GetTrueFileExtension(filename); ext = ext.Substring(1); MimeTypeDefinition def = MimeTypesHelper.Definitions.Where(d => d.CanParseSparqlResults && d.SupportsFileExtension(ext)).FirstOrDefault(); if (def == null) { Assert.Fail("Failed to find MIME Type Definition for File Extension ." + ext); } ISparqlResultsReader reader = def.GetSparqlResultsParser(); reader.Load(results, new StreamReader(filename)); Assert.IsTrue(this._results.Equals(results), "Result Sets for file " + filename + " were not equal"); } }
public void WritingFormattingResultSets() { Graph g = new Graph(); g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl"); SparqlResultSet expected = g.ExecuteQuery("SELECT * WHERE { ?s a ?type }") as SparqlResultSet; List <IResultSetFormatter> formatters = new List <IResultSetFormatter>() { new SparqlXmlFormatter() }; List <ISparqlResultsReader> parsers = new List <ISparqlResultsReader>() { new SparqlXmlParser() }; Console.WriteLine("Using Formatter " + formatters.GetType().ToString()); for (int i = 0; i < formatters.Count; i++) { IResultSetFormatter formatter = formatters[i]; StringBuilder output = new StringBuilder(); output.AppendLine(formatter.FormatResultSetHeader(expected.Variables)); foreach (SparqlResult r in expected) { output.AppendLine(formatter.Format(r)); } output.AppendLine(formatter.FormatResultSetFooter()); Console.WriteLine(output.ToString()); //Try parsing to check it round trips SparqlResultSet actual = new SparqlResultSet(); ISparqlResultsReader parser = parsers[i]; parser.Load(actual, new StringReader(output.ToString())); Assert.AreEqual(expected, actual, "Result Sets should be equal after round tripping"); } Console.WriteLine(); }
/// <summary> /// Makes a SPARQL Query against the underlying 4store Instance processing the results with the appropriate handler from those provided /// </summary> /// <param name="rdfHandler">RDF Handler</param> /// <param name="resultsHandler">Results Handler</param> /// <param name="sparqlQuery">SPARQL Query</param> public void Query(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, String sparqlQuery) { HttpWebResponse response = this._endpoint.QueryRaw(sparqlQuery); StreamReader data = new StreamReader(response.GetResponseStream()); try { //Is the Content Type referring to a Sparql Result Set format? ISparqlResultsReader resreader = MimeTypesHelper.GetSparqlParser(response.ContentType); resreader.Load(resultsHandler, data); response.Close(); } catch (RdfParserSelectionException) { //If we get a Parser Selection exception then the Content Type isn't valid for a Sparql Result Set //Is the Content Type referring to a RDF format? IRdfReader rdfreader = MimeTypesHelper.GetParser(response.ContentType); rdfreader.Load(rdfHandler, data); response.Close(); } }
/// <summary> /// Lists the Graphs from the Repository /// </summary> /// <returns></returns> public IEnumerable <Uri> ListGraphs() { try { //Use the /contexts method to get the Graph URIs //HACK: Have to use SPARQL JSON as currently Dydra's SPARQL XML Results are malformed HttpWebRequest request = this.CreateRequest("/contexts", MimeTypesHelper.CustomHttpAcceptHeader(MimeTypesHelper.SparqlJson), "GET", new Dictionary <string, string>()); SparqlResultSet results = new SparqlResultSet(); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { ISparqlResultsReader parser = MimeTypesHelper.GetSparqlParser(response.ContentType); parser.Load(results, new StreamReader(response.GetResponseStream())); response.Close(); } List <Uri> graphUris = new List <Uri>(); foreach (SparqlResult r in results) { if (r.HasValue("contextID")) { INode value = r["contextID"]; if (value.NodeType == NodeType.Uri) { graphUris.Add(((IUriNode)value).Uri); } else if (value.NodeType == NodeType.Blank) { //Dydra allows BNode Graph URIs graphUris.Add(new Uri("dydra:bnode:" + ((IBlankNode)value).InternalID)); } } } return(graphUris); } catch (Exception ex) { throw new RdfStorageException("An error occurred while attempting to retrieve the Graph List from the Store, see inner exception for details", ex); } }
/// <summary> /// Lists the Graphs from the Repository /// </summary> /// <param name="callback">Callback</param> /// <param name="state">State to pass to the callback</param> /// <returns></returns> public override void ListGraphs(AsyncStorageCallback callback, Object state) { try { //Use the /contexts method to get the Graph URIs //HACK: Have to use SPARQL JSON as currently Dydra's SPARQL XML Results are malformed HttpWebRequest request = this.CreateRequest("/contexts", MimeTypesHelper.CustomHttpAcceptHeader(MimeTypesHelper.SparqlResultsJson), "GET", new Dictionary <string, string>()); request.BeginGetResponse(r => { try { HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(r); ISparqlResultsReader parser = MimeTypesHelper.GetSparqlParser(response.ContentType); ListUrisHandler handler = new ListUrisHandler("contextID"); parser.Load(handler, new StreamReader(response.GetResponseStream())); response.Close(); callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.ListGraphs, handler.Uris), state); } catch (WebException webEx) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.ListGraphs, StorageHelper.HandleHttpError(webEx, "list Graphs asynchronously from")), state); } catch (Exception ex) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.ListGraphs, StorageHelper.HandleError(ex, "list Graphs asynchronously from")), state); } }, state); } catch (WebException webEx) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.ListGraphs, StorageHelper.HandleHttpError(webEx, "list Graphs asynchronously from")), state); } catch (Exception ex) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.ListGraphs, StorageHelper.HandleError(ex, "list Graphs asynchronously from")), state); } }
public static async Task <SparqlResultSet> SelectQuery(this string endpointUri, string sparqlQuery) { WriteLine($"Querying : {endpointUri} whith \"{sparqlQuery}\""); SparqlResultSet results = new SparqlResultSet(); using (var client = new System.Net.Http.HttpClient()) { try { client.Timeout = new TimeSpan(23, 23, 59, 59, 59); var httpQuery = Tools.UrlEncode(sparqlQuery); var response = await client.GetAsync(endpointUri + "?query=" + httpQuery); response.EnsureSuccessStatusCode(); ISparqlResultsHandler handler = new VDS.RDF.Parsing.Handlers.ResultSetHandler(results); String ctype = response.Content.Headers.ContentType.ToString(); if (ctype.Contains(";")) { ctype = ctype.Substring(0, ctype.IndexOf(";")); } ISparqlResultsReader resultsParser = MimeTypesHelper.GetSparqlParser(ctype); resultsParser.Load(handler, new System.IO.StreamReader(response.Content.ReadAsStreamAsync().Result)); } catch (System.Net.Http.HttpRequestException e) { Console.WriteLine("\nException Caught !"); WriteLine($"endpoint : {endpointUri}"); WriteLine($"sparqlQuery : {sparqlQuery}"); Console.WriteLine("Message :{0} ", e.Message); } return(results); } }
/// <summary> /// Makes a Query asynchronously where the expected Result is a <see cref="SparqlResultSet">SparqlResultSet</see> i.e. SELECT and ASK Queries. /// </summary> /// <param name="query">SPARQL Query String.</param> /// <param name="handler">Results Handler.</param> /// <param name="callback">Callback to invoke when the query completes.</param> /// <param name="state">State to pass to the callback.</param> public void QueryWithResultSet(ISparqlResultsHandler handler, String query, QueryCallback callback, Object state) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Uri); request.Method = "POST"; request.ContentType = MimeTypesHelper.Utf8WWWFormURLEncoded; request.Accept = RdfAcceptHeader; ApplyRequestOptions(request); Tools.HttpDebugRequest(request); try { request.BeginGetRequestStream(result => { try { Stream stream = request.EndGetRequestStream(result); using (StreamWriter writer = new StreamWriter(stream, new UTF8Encoding(Options.UseBomForUtf8))) { writer.Write("query="); writer.Write(HttpUtility.UrlEncode(query)); foreach (String u in DefaultGraphs) { writer.Write("&default-graph-uri="); writer.Write(HttpUtility.UrlEncode(u)); } foreach (String u in NamedGraphs) { writer.Write("&named-graph-uri="); writer.Write(HttpUtility.UrlEncode(u)); } writer.Close(); } request.BeginGetResponse(innerResult => { try { using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(innerResult)) { Tools.HttpDebugResponse(response); ISparqlResultsReader parser = MimeTypesHelper.GetSparqlParser(response.ContentType, false); parser.Load(handler, new StreamReader(response.GetResponseStream())); response.Close(); callback(null, handler, state); } } catch (SecurityException secEx) { callback(null, handler, new AsyncError(new RdfQueryException("Calling code does not have permission to access the specified remote endpoint, see inner exception for details", secEx), state)); } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } callback(null, handler, new AsyncError(new RdfQueryException("A HTTP error occurred while making an asynchronous query, see inner exception for details", webEx), state)); } catch (Exception ex) { callback(null, handler, new AsyncError(new RdfQueryException("Unexpected error while making an asynchronous query, see inner exception for details", ex), state)); } }, null); } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } callback(null, handler, new AsyncError(new RdfQueryException("A HTTP error occurred while making an asynchronous query, see inner exception for details", webEx), state)); } catch (Exception ex) { callback(null, handler, new AsyncError(new RdfQueryException("Unexpected error while making an asynchronous query, see inner exception for details", ex), state)); } }, null); } catch (Exception ex) { callback(null, handler, new AsyncError(new RdfQueryException("Unexpected error while making an asynchronous query, see inner exception for details", ex), state)); } }
/// <summary> /// Executes a SPARQL Query on the Fuseki store processing the results using an appropriate handler from those provided /// </summary> /// <param name="rdfHandler">RDF Handler</param> /// <param name="resultsHandler">Results Handler</param> /// <param name="sparqlQuery">SPARQL Query</param> /// <param name="callback">Callback</param> /// <param name="state">State to pass to the callback</param> /// <returns></returns> public void Query(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, String sparqlQuery, AsyncStorageCallback callback, Object state) { try { HttpWebRequest request; // Create the Request, always use POST for async for simplicity String queryUri = this._queryUri; request = (HttpWebRequest)WebRequest.Create(queryUri); request.Method = "POST"; request.Accept = MimeTypesHelper.HttpRdfOrSparqlAcceptHeader; request = base.ApplyRequestOptions(request); // Build the Post Data and add to the Request Body request.ContentType = MimeTypesHelper.Utf8WWWFormURLEncoded; StringBuilder postData = new StringBuilder(); postData.Append("query="); postData.Append(HttpUtility.UrlEncode(sparqlQuery)); request.BeginGetRequestStream(r => { try { Stream stream = request.EndGetRequestStream(r); using (StreamWriter writer = new StreamWriter(stream, new UTF8Encoding(Options.UseBomForUtf8))) { writer.Write(postData); writer.Close(); } Tools.HttpDebugRequest(request); // Get the Response and process based on the Content Type request.BeginGetResponse(r2 => { try { HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(r2); Tools.HttpDebugResponse(response); StreamReader data = new StreamReader(response.GetResponseStream()); String ctype = response.ContentType; try { // Is the Content Type referring to a Sparql Result Set format? ISparqlResultsReader resreader = MimeTypesHelper.GetSparqlParser(ctype, true); resreader.Load(resultsHandler, data); response.Close(); } catch (RdfParserSelectionException) { // If we get a Parse exception then the Content Type isn't valid for a Sparql Result Set // Is the Content Type referring to a RDF format? IRdfReader rdfreader = MimeTypesHelper.GetParser(ctype); rdfreader.Load(rdfHandler, data); response.Close(); } callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, sparqlQuery, rdfHandler, resultsHandler), state); } catch (WebException webEx) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, StorageHelper.HandleHttpQueryError(webEx)), state); } catch (Exception ex) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, StorageHelper.HandleQueryError(ex)), state); } }, state); } catch (WebException webEx) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, StorageHelper.HandleHttpQueryError(webEx)), state); } catch (Exception ex) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, StorageHelper.HandleQueryError(ex)), state); } }, state); } catch (WebException webEx) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, StorageHelper.HandleHttpQueryError(webEx)), state); } catch (Exception ex) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, StorageHelper.HandleQueryError(ex)), state); } }
/// <summary> /// Makes a SPARQL Query against the underlying Store using whatever reasoning mode is currently in-use processing the results using an appropriate handler from those provided /// </summary> /// <param name="rdfHandler">RDF Handler</param> /// <param name="resultsHandler">Results Handler</param> /// <param name="sparqlQuery">SPARQL Query</param> /// <returns></returns> public void Query(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, String sparqlQuery) { try { HttpWebRequest request; String tID = (this._activeTrans == null) ? String.Empty : "/" + this._activeTrans; //String accept = MimeTypesHelper.HttpRdfOrSparqlAcceptHeader; String accept = MimeTypesHelper.CustomHttpAcceptHeader(MimeTypesHelper.SparqlResultsXml.Concat(MimeTypesHelper.Definitions.Where(d => d.CanParseRdf).SelectMany(d => d.MimeTypes))); //Create the Request Dictionary <String, String> queryParams = new Dictionary <string, string>(); if (sparqlQuery.Length < 2048) { queryParams.Add("query", sparqlQuery); request = this.CreateRequest(this._kb + tID + "/query", accept, "GET", queryParams); } else { request = this.CreateRequest(this._kb + tID + "/query", accept, "POST", queryParams); //Build the Post Data and add to the Request Body request.ContentType = MimeTypesHelper.WWWFormURLEncoded; StringBuilder postData = new StringBuilder(); postData.Append("query="); postData.Append(Uri.EscapeDataString(sparqlQuery)); StreamWriter writer = new StreamWriter(request.GetRequestStream()); writer.Write(postData); writer.Close(); } #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugRequest(request); } #endif //Get the Response and process based on the Content Type using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugResponse(response); } #endif StreamReader data = new StreamReader(response.GetResponseStream()); String ctype = response.ContentType; try { //Is the Content Type referring to a Sparql Result Set format? ISparqlResultsReader resreader = MimeTypesHelper.GetSparqlParser(ctype, Regex.IsMatch(sparqlQuery, "ASK", RegexOptions.IgnoreCase)); resreader.Load(resultsHandler, data); response.Close(); } catch (RdfParserSelectionException) { //If we get a Parser Selection exception then the Content Type isn't valid for a Sparql Result Set //Is the Content Type referring to a RDF format? IRdfReader rdfreader = MimeTypesHelper.GetParser(ctype); rdfreader.Load(rdfHandler, data); response.Close(); } } } catch (WebException webEx) { if (webEx.Response != null) { #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } #endif if (webEx.Response.ContentLength > 0) { try { String responseText = new StreamReader(webEx.Response.GetResponseStream()).ReadToEnd(); throw new RdfQueryException("A HTTP error occured while querying the Store. Store returned the following error message: " + responseText, webEx); } catch { throw new RdfQueryException("A HTTP error occurred while querying the Store", webEx); } } else { throw new RdfQueryException("A HTTP error occurred while querying the Store", webEx); } } else { throw new RdfQueryException("A HTTP error occurred while querying the Store", webEx); } } }
/// <summary> /// Parses a raw SPARQL Results String using the given Parser /// </summary> /// <param name="results">SPARQL Result Set to fill</param> /// <param name="data">Raw SPARQL Results String</param> /// <param name="reader">Parser to use</param> public static void ParseResultSet(SparqlResultSet results, String data, ISparqlResultsReader reader) { if (results == null) throw new RdfParseException("Cannot read SPARQL Results into a null Result Set"); if (data == null) return; if (reader == null) { //If no parser specified then auto-detect syntax ParseResultSet(results, data); } else { try { MemoryStream mem = new MemoryStream(); StreamWriter writer = new StreamWriter(mem); writer.Write(data); writer.Flush(); mem.Seek(0, SeekOrigin.Begin); reader.Load(results, new StreamReader(mem)); } catch { throw; } } }
/// <summary> /// Performs a SPARQL Query against the underlying Store /// </summary> /// <param name="rdfHandler">RDF Handler</param> /// <param name="resultsHandler">SPARQL Results Handler</param> /// <param name="sparqlQuery">SPARQL Query</param> /// <returns></returns> public void Query(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, string sparqlQuery) { try { //First off parse the Query to see what kind of query it is SparqlQuery q; try { q = this._parser.ParseFromString(sparqlQuery); } catch (RdfParseException parseEx) { throw; } catch (Exception ex) { throw new RdfStorageException("An unexpected error occurred while trying to parse the SPARQL Query prior to sending it to the Store, see inner exception for details", ex); } //Now select the Accept Header based on the query type String accept = (SparqlSpecsHelper.IsSelectQuery(q.QueryType) || q.QueryType == SparqlQueryType.Ask) ? MimeTypesHelper.HttpSparqlAcceptHeader : MimeTypesHelper.HttpAcceptHeader; //Create the Request HttpWebRequest request; Dictionary <String, String> queryParams = new Dictionary <string, string>(); if (sparqlQuery.Length < 2048) { queryParams.Add("query", sparqlQuery); request = this.CreateRequest("/sparql", accept, "GET", queryParams); } else { request = this.CreateRequest("/sparql", accept, "POST", queryParams); //Build the Post Data and add to the Request Body request.ContentType = MimeTypesHelper.WWWFormURLEncoded; StringBuilder postData = new StringBuilder(); postData.Append("query="); postData.Append(HttpUtility.UrlEncode(sparqlQuery)); StreamWriter writer = new StreamWriter(request.GetRequestStream()); writer.Write(postData); writer.Close(); } #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugRequest(request); } #endif //Get the Response and process based on the Content Type using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugResponse(response); } #endif StreamReader data = new StreamReader(response.GetResponseStream()); String ctype = response.ContentType; if (SparqlSpecsHelper.IsSelectQuery(q.QueryType) || q.QueryType == SparqlQueryType.Ask) { //ASK/SELECT should return SPARQL Results ISparqlResultsReader resreader = MimeTypesHelper.GetSparqlParser(ctype, q.QueryType == SparqlQueryType.Ask); resreader.Load(resultsHandler, data); response.Close(); } else { //CONSTRUCT/DESCRIBE should return a Graph IRdfReader rdfreader = MimeTypesHelper.GetParser(ctype); rdfreader.Load(rdfHandler, data); response.Close(); } } } catch (WebException webEx) { if (webEx.Response != null) { #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } #endif if (webEx.Response.ContentLength > 0) { try { String responseText = new StreamReader(webEx.Response.GetResponseStream()).ReadToEnd(); throw new RdfQueryException("A HTTP error occured while querying the Store. Store returned the following error message: " + responseText, webEx); } catch { throw new RdfQueryException("A HTTP error occurred while querying the Store", webEx); } } else { throw new RdfQueryException("A HTTP error occurred while querying the Store", webEx); } } else { throw new RdfQueryException("A HTTP error occurred while querying the Store", webEx); } } }
/// <summary> /// Queries the store asynchronously /// </summary> /// <param name="sparqlQuery">SPARQL Query</param> /// <param name="rdfHandler">RDF Handler</param> /// <param name="resultsHandler">Results Handler</param> /// <param name="callback">Callback</param> /// <param name="state">State to pass to the callback</param> public void Query(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, string sparqlQuery, AsyncStorageCallback callback, object state) { try { // First off parse the Query to see what kind of query it is SparqlQuery q; try { q = _parser.ParseFromString(sparqlQuery); } catch (RdfParseException parseEx) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, parseEx), state); return; } catch (Exception ex) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, new RdfStorageException("An unexpected error occurred while trying to parse the SPARQL Query prior to sending it to the Store, see inner exception for details", ex)), state); return; } // Now select the Accept Header based on the query type String accept = (SparqlSpecsHelper.IsSelectQuery(q.QueryType) || q.QueryType == SparqlQueryType.Ask) ? MimeTypesHelper.HttpSparqlAcceptHeader : MimeTypesHelper.HttpAcceptHeader; // Create the Request, for simplicity async requests are always POST HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_endpoint.Uri); request.Accept = accept; request.Method = "POST"; request.ContentType = MimeTypesHelper.Utf8WWWFormURLEncoded; request = ApplyRequestOptions(request); Tools.HttpDebugRequest(request); request.BeginGetRequestStream(r => { try { Stream stream = request.EndGetRequestStream(r); using (StreamWriter writer = new StreamWriter(stream, new UTF8Encoding(Options.UseBomForUtf8))) { writer.Write("query="); writer.Write(HttpUtility.UrlEncode(sparqlQuery)); writer.Close(); } request.BeginGetResponse(r2 => { // Get the Response and process based on the Content Type try { HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(r2); Tools.HttpDebugResponse(response); StreamReader data = new StreamReader(response.GetResponseStream()); String ctype = response.ContentType; if (SparqlSpecsHelper.IsSelectQuery(q.QueryType) || q.QueryType == SparqlQueryType.Ask) { // ASK/SELECT should return SPARQL Results ISparqlResultsReader resreader = MimeTypesHelper.GetSparqlParser(ctype, q.QueryType == SparqlQueryType.Ask); resreader.Load(resultsHandler, data); response.Close(); } else { // CONSTRUCT/DESCRIBE should return a Graph IRdfReader rdfreader = MimeTypesHelper.GetParser(ctype); rdfreader.Load(rdfHandler, data); response.Close(); } callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, sparqlQuery, rdfHandler, resultsHandler), state); } catch (WebException webEx) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, StorageHelper.HandleHttpQueryError(webEx)), state); } catch (Exception ex) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, StorageHelper.HandleQueryError(ex)), state); } }, state); } catch (WebException webEx) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, StorageHelper.HandleHttpQueryError(webEx)), state); } catch (Exception ex) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, StorageHelper.HandleQueryError(ex)), state); } }, state); } catch (WebException webEx) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, StorageHelper.HandleHttpQueryError(webEx)), state); } catch (Exception ex) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, StorageHelper.HandleQueryError(ex)), state); } }