Example #1
0
        public async Task <SparqlQueryResult> ExecuteQueryAsync(string expr, CancellationToken ct)
        {
            if (expr == null)
            {
                throw new ArgumentNullException(nameof(expr));
            }
            ct.ThrowIfCancellationRequested();
            var loadedGraph = await EnsureGraphLoadedAsync();

            if (expr == null)
            {
                throw new ArgumentNullException(nameof(expr));
            }
            ct.ThrowIfCancellationRequested();
            var sw = Stopwatch.StartNew();

            using (logger.BeginScope("ExprHash={ExprHash}", expr.GetHashCode()))
            {
                try
                {
                    logger.LogInformation("Start execute query; ExprLength={ExprLength}.", expr.Length);
                    var queryStr = new SparqlParameterizedString(expr);
                    foreach (var prefix in loadedGraph.Graph.NamespaceMap.Prefixes)
                    {
                        if (!queryStr.Namespaces.HasNamespace(prefix))
                        {
                            queryStr.Namespaces.AddNamespace(prefix, loadedGraph.Graph.NamespaceMap.GetNamespaceUri(prefix));
                        }
                    }
                    var query = queryParser.ParseFromString(queryStr);
                    logger.LogDebug("Parsed query. Elapsed time: {Elapsed}.", sw.Elapsed);
                    query.Timeout = (int)options.QueryTimeout.TotalMilliseconds;
                    query.Limit   = options.ResultLimit;
                    ct.ThrowIfCancellationRequested();

                    var weakResult = loadedGraph.QueryProcessor.ProcessQuery(query);
                    // returns either a SparqlResultSet or an IGraph instance
                    if (weakResult is IGraph)
                    {
                        logger.LogWarning("Query result is IGraph.");
                    }
                    var result = new SparqlQueryResult(weakResult);
                    logger.LogInformation("Executed query with {Results} {ResultType} results. Elapsed time: {Elapsed}.",
                                          result.RecordsCount, result.ResultType, sw.Elapsed);
                    return(result);
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, "Error during executing query. Elapsed time: {Elapsed}.", sw.Elapsed);
                    throw;
                }
            }
        }
Example #2
0
        // See https://github.com/dotnetrdf/dotnetrdf/blob/5b7fc480346c90eb9b164fb4f8ee09f378442d52/Libraries/dotNetRDF.Web/HandlerHelper.cs#L157
        /// <summary>
        /// Helper function which returns the Results (Graph/Triple Store/SPARQL Results) back to the Client in one of their accepted formats
        /// </summary>
        /// <param name="context">Context of the HTTP Request</param>
        /// <param name="result">Results of the Sparql Query</param>
        /// <param name="config">Handler Configuration</param>
        public static void SendToClient(HttpContext context, SparqlQueryResult result)
        {
            MimeTypeDefinition definition = null;
            const string       TEXT_PLAIN = "text/plain";
            var acceptTypes = context.Request.Headers[HeaderNames.Accept];

            // Return the Results
            if (result.SparqlResultSet != null)
            {
                ISparqlResultsWriter sparqlWriter = null;

                // Try and get a MIME Type Definition using the HTTP Requests Accept Header
                if (acceptTypes.Count > 0)
                {
                    definition = MimeTypesHelper.GetDefinitions((IEnumerable <string>)acceptTypes).FirstOrDefault(d => d.CanWriteSparqlResults);
                }
                // Try and get the registered Definition for SPARQL Results XML
                if (definition == null)
                {
                    definition = MimeTypesHelper.GetDefinitions(MimeTypesHelper.SparqlResultsXml[0]).FirstOrDefault();
                }
                // If Definition is still null create a temporary definition
                if (definition == null)
                {
                    definition = new MimeTypeDefinition("SPARQL Results XML", MimeTypesHelper.SparqlResultsXml, Enumerable.Empty <String>());
                    definition.SparqlResultsWriterType = typeof(VDS.RDF.Writing.SparqlXmlWriter);
                }

                // Set up the Writer appropriately
                sparqlWriter = definition.GetSparqlResultsWriter();
                context.Response.ContentType = definition.CanonicalMimeType;
                // HandlerHelper.ApplyWriterOptions(sparqlWriter, config);

                // Send Result Set to Client
                context.Response.Headers[HeaderNames.ContentEncoding] = definition.Encoding.WebName;
                sparqlWriter.Save(result.SparqlResultSet, new StreamWriter(context.Response.Body, definition.Encoding));
            }
            else if (result.Graph != null)
            {
                IRdfWriter rdfWriter = null;
                var        ctype     = TEXT_PLAIN;
                // Try and get a MIME Type Definition using the HTTP Requests Accept Header
                if (acceptTypes.Count > 0)
                {
                    definition = MimeTypesHelper.GetDefinitions((IEnumerable <string>)acceptTypes).FirstOrDefault(d => d.CanWriteRdf);
                }
                if (definition == null)
                {
                    // If no appropriate definition then use the GetWriter method instead
                    rdfWriter = MimeTypesHelper.GetWriter((IEnumerable <string>)acceptTypes, out ctype);
                }
                else
                {
                    rdfWriter = definition.GetRdfWriter();
                }

                // Setup the writer
                if (definition != null)
                {
                    ctype = definition.CanonicalMimeType;
                }
                context.Response.ContentType = ctype;
                //HandlerHelper.ApplyWriterOptions(rdfWriter, config);

                // Clear any existing Response
                //context.Response.Clear();

                // Send Graph to Client
                if (definition != null)
                {
                    context.Response.Headers[HeaderNames.ContentEncoding] = definition.Encoding.WebName;
                    rdfWriter.Save(result.Graph, new StreamWriter(context.Response.Body, definition.Encoding));
                }
                else
                {
                    rdfWriter.Save(result.Graph, new StreamWriter(context.Response.Body));
                }
            }
            else
            {
                Debug.Assert(result == SparqlQueryResult.Null);
            }
        }