/// <summary>
 /// Queries a given commit point of a store using a SPARQL query
 /// </summary>
 /// <param name="commitPoint">The store commit point to be queried</param>
 /// <param name="queryExpression">The SPARQL query string</param>
 /// <param name="resultsMediaType">The media type for the serialization of the SPARQL results</param>
 /// <returns>A stream containing the serialized query results</returns>
 public Stream ExecuteQueryOnCommitPoint(CommitPointInfo commitPoint, string queryExpression, string resultsMediaType = null)
 {
     try
     {
         var resultsFormat = resultsMediaType == null ? SparqlResultsFormat.Xml : SparqlResultsFormat.GetResultsFormat(resultsMediaType);
         var pstream       = new MemoryStream();
         var t             = new Task(() => ServerCore.Query(commitPoint.StoreName, commitPoint.Id, queryExpression, resultsFormat, pstream));
         t.Start();
         t.Wait();
         if (t.IsFaulted && t.Exception != null)
         {
             throw t.Exception;
         }
         var cStream = new MemoryStream(pstream.GetBuffer(), 0, (int)pstream.Length);
         return(cStream);
     }
     catch (Exception ex)
     {
         Logging.LogError(BrightstarDB.BrightstarEventId.ServerCoreException, "Error executing query {0}@{1} {2}", commitPoint.StoreName, commitPoint.Id, queryExpression);
         throw new BrightstarClientException(
                   String.Format("Error executing query {0}@{1} {2}. {3}", commitPoint.StoreName, commitPoint.Id,
                                 queryExpression, ex.Message), ex);
     }
 }
Beispiel #2
0
 public Stream ExecuteQuery(string storeName, string queryExpression, DateTime?ifNotModifiedSince, string resultsMediaType = null)
 {
     try {
         var resultsFormat = resultsMediaType == null ? SparqlResultsFormat.Xml : SparqlResultsFormat.GetResultsFormat(resultsMediaType);
         var pStream       = new MemoryStream();
         NodeCore.ProcessQuery(storeName, queryExpression, ifNotModifiedSince, resultsFormat, pStream);
         var cStream = new MemoryStream(pStream.GetBuffer(), 0, (int)pStream.Length);
         return(cStream);
     }
     catch (AggregateException aggregateException)
     {
         if (aggregateException.InnerException is BrightstarStoreNotModifiedException)
         {
             throw new BrightstarClientException("Store not modified", aggregateException.InnerException);
         }
         throw new BrightstarClientException("Error executing query", aggregateException.InnerException);
     }
     catch (BrightstarStoreNotModifiedException ex)
     {
         throw new BrightstarClientException("Store not modified", new ExceptionDetail(ex));
     }
     catch (Exception ex)
     {
         Logging.LogError(BrightstarDB.BrightstarEventId.ServerCoreException, "Error Executing Query {0} {1}", storeName,
                          queryExpression);
         throw new BrightstarClientException(
                   "Error querying store " + storeName + " with expression " + queryExpression + ". " + ex.Message, ex);
     }
 }
        public Stream ExecuteQuery(string storeName, string queryExpression, DateTime?ifNotModifiedSince, string resultsMediaType = null)
        {
            try
            {
                var resultsFormat = resultsMediaType == null ? SparqlResultsFormat.Xml : SparqlResultsFormat.GetResultsFormat(resultsMediaType);

                /*
                 * var connStream = new ConnectionStream();
                 * var cStream = new ConsumerStream(connStream);
                 * var pStream = new ProducerStream(connStream);
                 */
                var pStream = new MemoryStream();
                ServerCore.Query(storeName, queryExpression, ifNotModifiedSince, resultsFormat, pStream);
                //var t = new Task(() => ServerCore.Query(storeName, queryExpression, ifNotModifiedSince, pStream));
                //t.Start();
                //t.Wait();
                //if (t.IsFaulted && t.Exception != null) throw t.Exception;
                var cStream = new MemoryStream(pStream.GetBuffer(), 0, (int)pStream.Length);

                return(cStream);
            }
            catch (AggregateException aggregateException)
            {
                if (aggregateException.InnerException is BrightstarStoreNotModifiedException)
                {
                    throw new BrightstarClientException("Store not modified", aggregateException.InnerException);
                }
                throw new BrightstarClientException("Error executing query", aggregateException.InnerException);
            }
            catch (BrightstarStoreNotModifiedException ex)
            {
                throw new BrightstarClientException("Store not modified", new ExceptionDetail(ex));
            }
            catch (Exception ex)
            {
                Logging.LogError(BrightstarDB.BrightstarEventId.ServerCoreException, "Error Executing Query {0} {1}", storeName,
                                 queryExpression);
                throw new BrightstarClientException(
                          "Error querying store " + storeName + " with expression " + queryExpression + ". " + ex.Message, ex);
            }
        }
        public ActionResult Query(string storename, string query)
        {
            // Determine what results format to return to the client
            string bestMimeType = MimeParse.MimeParse.BestMatch(SparqlResultsFormat.AllMediaTypes.ToList(),
                                                                Request.Headers["Accept"]);
            var callback = Request["callback"];

            if (!String.IsNullOrEmpty(callback) && Request.Headers["Accept"].Contains("*/*"))
            {
                // Workaround for jQuery clients that will send Accept */* for a JSONP request
                bestMimeType = SparqlResultsFormat.Json.MediaTypes[0];
            }
            SparqlResultsFormat resultsFormat = null;

            if (!String.IsNullOrEmpty(bestMimeType))
            {
                resultsFormat = SparqlResultsFormat.GetResultsFormat(bestMimeType);
            }
            if (resultsFormat == null)
            {
                throw new HttpException(406, "Not Acceptable");
            }

            if (query == null && Request.HttpMethod.ToLower().Equals("post") && Request.ContentType.Equals("application/sparql-query"))
            {
                try
                {
                    string q;
                    using (var streamReader = new StreamReader(Request.InputStream))
                    {
                        q = streamReader.ReadToEnd();
                        if (string.IsNullOrEmpty(q))
                        {
                            throw new HttpException(400, "No query in request body");
                        }
                    }

                    // Execute the query
                    var client  = BrightstarService.GetClient();
                    var results = client.ExecuteQuery(storename, q, DateTime.MinValue, resultsFormat);
                    return(new FileStreamResult(results, resultsFormat.ToString()));
                }
                catch (Exception ex)
                {
                    throw new HttpException(500, "Unable to execute query " + ex.Message, ex);
                }
            }

            if (query != null)
            {
                try
                {
                    var client = BrightstarService.GetClient();
                    if (resultsFormat.DefaultExtension.Equals(SparqlResultsFormat.Json.DefaultExtension) && !String.IsNullOrEmpty(callback))
                    {
                        // Handle JSONP request
                        var results = client.ExecuteQuery(storename, query, DateTime.MinValue, resultsFormat);
                        using (var stringReader = new StreamReader(results))
                        {
                            var rawJson = stringReader.ReadToEnd();
                            return(new ContentResult
                            {
                                ContentType = bestMimeType,
                                Content = callback + "(" + rawJson + ");",
                                ContentEncoding = Encoding.UTF8
                            });
                        }
                    }
                    else
                    {
                        var results = client.ExecuteQuery(storename, query, DateTime.MinValue, resultsFormat);
                        return(new FileStreamResult(results, resultsFormat.ToString()));
                    }
                }
                catch (Exception ex)
                {
                    throw new HttpException(500, "Unable to execute query " + ex.Message, ex);
                }
            }


            MimeParse.MimeParse.BestMatch(new []
            {
                "application/xbel+xml",
                "text/xml"
            }, "text/*;q=0.5,*; q=0.1");                        // 'text/xml'
            throw new HttpException(400, "Missing parameters or incorrect request format");
        }