Ejemplo n.º 1
0
        public GraphsModule(IBrightstarService brightstarService,
            AbstractStorePermissionsProvider storePermissionsProvider)
        {
            this.RequiresBrightstarStorePermission(storePermissionsProvider, get:StorePermissions.Read, put:StorePermissions.SparqlUpdate, post:StorePermissions.SparqlUpdate, delete:StorePermissions.SparqlUpdate);


            Get["/{storeName}/graphs"] = parameters =>
            {
                if (!brightstarService.DoesStoreExist(parameters["storeName"])) return HttpStatusCode.NotFound;
                Uri graphUri;
                if (Request.Query["default"] == null && Request.Query["graph"] == null)
                {
                    // Get with no default or graph parameter returns a list of graphs
                    // This is not part of the SPARQL 1.1 Graph Store protocol, but a useful extension
                    // The returned value is a SPARQL results set with a single variable named "graphUri"
                    // Acceptable media types are the supported SPARQL results set formats
                    return
                        Negotiate.WithModel(
                            new GraphListModel(brightstarService.ListNamedGraphs(parameters["storeName"])));
                }
                if (!TryGetGraphUri(out graphUri))
                {
                    // Will get here if the value of the graph parameter is parsed as a relative URI
                    return HttpStatusCode.BadRequest;
                }
                try
                {
                    // Return the serialized content of the graph
                    return SerializeGraph(brightstarService, parameters["storeName"], graphUri.ToString());
                }
                catch (BrightstarStoreNotModifiedException)
                {
                    return HttpStatusCode.NotModified;
                }
            };

            Put["/{storeName}/graphs"] = parameters =>
            {
                var storeParam = parameters["storeName"];
                var store = storeParam.Value as string;
                Uri graphUri;
                if (!brightstarService.DoesStoreExist(store)) return HttpStatusCode.NotFound;
                if (!TryGetGraphUri(out graphUri))
                {
                    return HttpStatusCode.BadRequest;
                }
                var graphUriStr = graphUri.ToString();
                bool isNewGraph = !graphUriStr.Equals(Constants.DefaultGraphUri) &&
                                  !(brightstarService.ListNamedGraphs(store).Any(x => x.Equals(graphUriStr)));

                try
                {
                    var rdfFormat = GetRequestBodyFormat();
                    if (rdfFormat == null) return HttpStatusCode.NotAcceptable;
                    var rdfPayload = ParseBody(rdfFormat);
                    var sparqlUpdate = graphUri.ToString().Equals(Constants.DefaultGraphUri) ?
                        String.Format(UpdateDefaultGraph, rdfPayload) :
                        String.Format(UpdateNamedGraph, graphUri, rdfPayload);

                    var job = brightstarService.ExecuteUpdate(store, sparqlUpdate, true, "Update Graph " + graphUri);
                    return job.JobCompletedOk
                        ? (isNewGraph ? HttpStatusCode.Created : HttpStatusCode.NoContent)
                        : HttpStatusCode.InternalServerError;
                }
                catch (RdfException)
                {
                    return HttpStatusCode.BadRequest;
                }
            };

            Post["/{storeName}/graphs"] = parameters =>
            {
                var storeParam = parameters["storeName"];
                var store = storeParam.Value as string;
                Uri graphUri;
                if (!brightstarService.DoesStoreExist(store)) return HttpStatusCode.NotFound;
                if (!TryGetGraphUri(out graphUri))
                {
                    return HttpStatusCode.BadRequest;
                }
                var graphUriStr = graphUri.ToString();
                var isNewGraph = !graphUriStr.Equals(Constants.DefaultGraphUri) &&
                                 !(brightstarService.ListNamedGraphs(store).Any(x => x.Equals(graphUriStr)));
                try
                {
                    var rdfFormat = GetRequestBodyFormat();
                    if (rdfFormat == null) return HttpStatusCode.NotAcceptable;
                    var rdfPayload = ParseBody(rdfFormat);
                    var sparqlUpdate = graphUri.ToString().Equals(Constants.DefaultGraphUri) ?
                        String.Format(MergeDefaultGraph, rdfPayload) :
                        String.Format(MergeNamedGraph, graphUri, rdfPayload);

                    var job = brightstarService.ExecuteUpdate(store, sparqlUpdate, true, "Update Graph " + graphUri);
                    return job.JobCompletedOk
                        ? (isNewGraph ? HttpStatusCode.Created : HttpStatusCode.NoContent)
                        : HttpStatusCode.InternalServerError;
                }
                catch (RdfException)
                {
                    return HttpStatusCode.BadRequest;
                }
            };

            Delete["{storeName}/graphs"] = parameters =>
            {
                Uri graphUri;
                string sparqlUpdate, jobName;
                var store = parameters["storeName"].Value as string;
                if (!brightstarService.DoesStoreExist(store)) return HttpStatusCode.NotFound;
                if (!TryGetGraphUri(out graphUri)) return HttpStatusCode.BadRequest;
                if (graphUri.ToString().Equals(Constants.DefaultGraphUri))
                {
                    // Clear the default graph
                    sparqlUpdate = DropDefaultGraph;
                    jobName = "Drop Default Graph";
                }
                else
                {
                    var graphId = graphUri.ToString();
                    if (!brightstarService.ListNamedGraphs(store).Contains(graphId))
                    {
                        return HttpStatusCode.NotFound;
                    }
                    // Clear the named graph
                    sparqlUpdate = String.Format(DropNamedGraph, graphId);
                    jobName = "Drop Graph " + graphId;
                }

                var job = brightstarService.ExecuteUpdate(store, sparqlUpdate, true, jobName);
                return job.JobCompletedOk
                    ? HttpStatusCode.NoContent
                    : HttpStatusCode.InternalServerError;
            };

        }
Ejemplo n.º 2
0
        public GraphsModule(IBrightstarService brightstarService,
                            AbstractStorePermissionsProvider storePermissionsProvider)
        {
            this.RequiresBrightstarStorePermission(storePermissionsProvider, get: StorePermissions.Read, put: StorePermissions.SparqlUpdate, post: StorePermissions.SparqlUpdate, delete: StorePermissions.SparqlUpdate);


            Get["/{storeName}/graphs"] = parameters =>
            {
                if (!brightstarService.DoesStoreExist(parameters["storeName"]))
                {
                    return(HttpStatusCode.NotFound);
                }
                Uri graphUri;
                if (Request.Query["default"] == null && Request.Query["graph"] == null)
                {
                    // Get with no default or graph parameter returns a list of graphs
                    // This is not part of the SPARQL 1.1 Graph Store protocol, but a useful extension
                    // The returned value is a SPARQL results set with a single variable named "graphUri"
                    // Acceptable media types are the supported SPARQL results set formats
                    return
                        (Negotiate.WithModel(
                             new GraphListModel(brightstarService.ListNamedGraphs(parameters["storeName"]))));
                }
                if (!TryGetGraphUri(out graphUri))
                {
                    // Will get here if the value of the graph parameter is parsed as a relative URI
                    return(HttpStatusCode.BadRequest);
                }
                try
                {
                    // Return the serialized content of the graph
                    return(SerializeGraph(brightstarService, parameters["storeName"], graphUri.ToString()));
                }
                catch (BrightstarStoreNotModifiedException)
                {
                    return(HttpStatusCode.NotModified);
                }
            };

            Put["/{storeName}/graphs"] = parameters =>
            {
                var storeParam = parameters["storeName"];
                var store      = storeParam.Value as string;
                Uri graphUri;
                if (!brightstarService.DoesStoreExist(store))
                {
                    return(HttpStatusCode.NotFound);
                }
                if (!TryGetGraphUri(out graphUri))
                {
                    return(HttpStatusCode.BadRequest);
                }
                var  graphUriStr = graphUri.ToString();
                bool isNewGraph  = !graphUriStr.Equals(Constants.DefaultGraphUri) &&
                                   !(brightstarService.ListNamedGraphs(store).Any(x => x.Equals(graphUriStr)));

                try
                {
                    var rdfFormat = GetRequestBodyFormat();
                    if (rdfFormat == null)
                    {
                        return(HttpStatusCode.NotAcceptable);
                    }
                    var rdfPayload   = ParseBody(rdfFormat);
                    var sparqlUpdate = graphUri.ToString().Equals(Constants.DefaultGraphUri) ?
                                       String.Format(UpdateDefaultGraph, rdfPayload) :
                                       String.Format(UpdateNamedGraph, graphUri, rdfPayload);

                    var job = brightstarService.ExecuteUpdate(store, sparqlUpdate, true, "Update Graph " + graphUri);
                    return(job.JobCompletedOk
                        ? (isNewGraph ? HttpStatusCode.Created : HttpStatusCode.NoContent)
                        : HttpStatusCode.InternalServerError);
                }
                catch (RdfException)
                {
                    return(HttpStatusCode.BadRequest);
                }
            };

            Post["/{storeName}/graphs"] = parameters =>
            {
                var storeParam = parameters["storeName"];
                var store      = storeParam.Value as string;
                Uri graphUri;
                if (!brightstarService.DoesStoreExist(store))
                {
                    return(HttpStatusCode.NotFound);
                }
                if (!TryGetGraphUri(out graphUri))
                {
                    return(HttpStatusCode.BadRequest);
                }
                var graphUriStr = graphUri.ToString();
                var isNewGraph  = !graphUriStr.Equals(Constants.DefaultGraphUri) &&
                                  !(brightstarService.ListNamedGraphs(store).Any(x => x.Equals(graphUriStr)));
                try
                {
                    var rdfFormat = GetRequestBodyFormat();
                    if (rdfFormat == null)
                    {
                        return(HttpStatusCode.NotAcceptable);
                    }
                    var rdfPayload   = ParseBody(rdfFormat);
                    var sparqlUpdate = graphUri.ToString().Equals(Constants.DefaultGraphUri) ?
                                       String.Format(MergeDefaultGraph, rdfPayload) :
                                       String.Format(MergeNamedGraph, graphUri, rdfPayload);

                    var job = brightstarService.ExecuteUpdate(store, sparqlUpdate, true, "Update Graph " + graphUri);
                    return(job.JobCompletedOk
                        ? (isNewGraph ? HttpStatusCode.Created : HttpStatusCode.NoContent)
                        : HttpStatusCode.InternalServerError);
                }
                catch (RdfException)
                {
                    return(HttpStatusCode.BadRequest);
                }
            };

            Delete["{storeName}/graphs"] = parameters =>
            {
                Uri    graphUri;
                string sparqlUpdate, jobName;
                var    store = parameters["storeName"].Value as string;
                if (!brightstarService.DoesStoreExist(store))
                {
                    return(HttpStatusCode.NotFound);
                }
                if (!TryGetGraphUri(out graphUri))
                {
                    return(HttpStatusCode.BadRequest);
                }
                if (graphUri.ToString().Equals(Constants.DefaultGraphUri))
                {
                    // Clear the default graph
                    sparqlUpdate = DropDefaultGraph;
                    jobName      = "Drop Default Graph";
                }
                else
                {
                    var graphId = graphUri.ToString();
                    if (!brightstarService.ListNamedGraphs(store).Contains(graphId))
                    {
                        return(HttpStatusCode.NotFound);
                    }
                    // Clear the named graph
                    sparqlUpdate = String.Format(DropNamedGraph, graphId);
                    jobName      = "Drop Graph " + graphId;
                }

                var job = brightstarService.ExecuteUpdate(store, sparqlUpdate, true, jobName);
                return(job.JobCompletedOk
                    ? HttpStatusCode.NoContent
                    : HttpStatusCode.InternalServerError);
            };
        }