Exemplo n.º 1
0
        public StoreModule(IBrightstarService brightstarService, AbstractStorePermissionsProvider storePermissionsProvider)
        {
            this.RequiresBrightstarStorePermission(storePermissionsProvider, get:StorePermissions.Read, delete:StorePermissions.Admin);

            Get["/{storeName}"] = parameters =>
            {
                var storeName = parameters["storeName"];
                ViewBag.Title = storeName;

                if (!brightstarService.DoesStoreExist(storeName)) return HttpStatusCode.NotFound;
                if (Request.Method.ToUpperInvariant() == "HEAD")
                {
                    IEnumerable < ICommitPointInfo > commitPoints = brightstarService.GetCommitPoints(storeName, 0, 1);
                    var commit = commitPoints.First();
                    return
                        Negotiate.WithHeader("Last-Modified", commit.CommitTime.ToUniversalTime().ToString("r"))
                            .WithStatusCode(HttpStatusCode.OK)
                            .WithModel(new StoreResponseModel(parameters["storeName"]));
                }
                return new StoreResponseModel(parameters["storeName"]);
            };

            Delete["/{storeName}"] = parameters =>
                {
                    var storeName = parameters["storeName"];
                    ViewBag.Title = "Deleted - " + storeName;
                    if (brightstarService.DoesStoreExist(storeName))
                    {
                        brightstarService.DeleteStore(storeName);
                    }
                    return Negotiate.WithMediaRangeModel(new MediaRange("text/html"), 
                                                         new StoreDeletedModel {StoreName = storeName});
                };
        }
Exemplo n.º 2
0
        public void TestDoesStoreExist()
        {
            var storeName = Guid.NewGuid().ToString();

            _client.CreateStore(storeName);
            Assert.IsTrue(_client.DoesStoreExist(storeName), "Expected DoesStoreExist to return true after store created");
            Assert.IsFalse(_client.DoesStoreExist("foo" + storeName), "Expected DoesStoreExist to return false for invalid name");
        }
Exemplo n.º 3
0
        public StoresModule(IBrightstarService brightstarService, AbstractSystemPermissionsProvider systemPermissionsProvider)
        {
            this.RequiresBrightstarSystemPermission(systemPermissionsProvider, get:SystemPermissions.ListStores, post:SystemPermissions.CreateStore);

            Get["/"] = parameters =>
            {
                ViewBag.Title = "Stores";
                var stores = brightstarService.ListStores();
                return
                    Negotiate.WithModel(new StoresResponseModel
                        {
                            Stores = stores.ToList()
                        });
                };

            Post["/"] = parameters =>
                {
                    ViewBag.Title = "Stores";
                    var request = this.Bind<CreateStoreRequestObject>();
                    if (request == null || String.IsNullOrEmpty(request.StoreName))
                    {
                        return HttpStatusCode.BadRequest;
                    }

                    // Return 409 Conflict if attempt to create a store with a name that is currently in use
                    if (brightstarService.DoesStoreExist(request.StoreName))
                    {
                        return HttpStatusCode.Conflict;
                    }

                    // Attempt to create the store
                    try
                    {
                        PersistenceType? storePersistenceType = request.GetBrightstarPersistenceType();
                        if (storePersistenceType.HasValue)
                        {
                            brightstarService.CreateStore(request.StoreName, storePersistenceType.Value);
                        }
                        else
                        {
                            brightstarService.CreateStore(request.StoreName);
                        }
                    }
                    catch (ArgumentException)
                    {
                        return HttpStatusCode.BadRequest;   
                    }
                    return
                        Negotiate.WithModel(new StoreResponseModel(request.StoreName))
                                 .WithStatusCode(HttpStatusCode.Created);
                };
        }
Exemplo n.º 4
0
        public StoresModule(IBrightstarService brightstarService, AbstractSystemPermissionsProvider systemPermissionsProvider)
        {
            this.RequiresBrightstarSystemPermission(systemPermissionsProvider, get: SystemPermissions.ListStores, post: SystemPermissions.CreateStore);

            Get["/"] = parameters =>
            {
                ViewBag.Title = "Stores";
                var stores = brightstarService.ListStores();
                return
                    (Negotiate.WithModel(new StoresResponseModel
                {
                    Stores = stores.ToList()
                }));
            };

            Post["/"] = parameters =>
            {
                ViewBag.Title = "Stores";
                var request = this.Bind <CreateStoreRequestObject>();
                if (request == null || String.IsNullOrEmpty(request.StoreName))
                {
                    return(HttpStatusCode.BadRequest);
                }

                // Return 409 Conflict if attempt to create a store with a name that is currently in use
                if (brightstarService.DoesStoreExist(request.StoreName))
                {
                    return(HttpStatusCode.Conflict);
                }

                // Attempt to create the store
                try
                {
                    PersistenceType?storePersistenceType = request.GetBrightstarPersistenceType();
                    if (storePersistenceType.HasValue)
                    {
                        brightstarService.CreateStore(request.StoreName, storePersistenceType.Value);
                    }
                    else
                    {
                        brightstarService.CreateStore(request.StoreName);
                    }
                }
                catch (ArgumentException)
                {
                    return(HttpStatusCode.BadRequest);
                }
                return
                    (Negotiate.WithModel(new StoreResponseModel(request.StoreName))
                     .WithStatusCode(HttpStatusCode.Created));
            };
        }
Exemplo n.º 5
0
        public StoreModule(IBrightstarService brightstarService, AbstractStorePermissionsProvider storePermissionsProvider)
        {
            this.RequiresBrightstarStorePermission(storePermissionsProvider, get: StorePermissions.Read, delete: StorePermissions.Admin);

            Get["/{storeName}"] = parameters =>
            {
                var storeName = parameters["storeName"];
                ViewBag.Title = storeName;

                if (!brightstarService.DoesStoreExist(storeName))
                {
                    return(HttpStatusCode.NotFound);
                }
                if (Request.Method.ToUpperInvariant() == "HEAD")
                {
                    IEnumerable <ICommitPointInfo> commitPoints = brightstarService.GetCommitPoints(storeName, 0, 1);
                    var commit = commitPoints.First();
                    return
                        (Negotiate.WithHeader("Last-Modified", commit.CommitTime.ToUniversalTime().ToString("r"))
                         .WithStatusCode(HttpStatusCode.OK)
                         .WithModel(new StoreResponseModel(parameters["storeName"])));
                }
                return(new StoreResponseModel(parameters["storeName"]));
            };

            Delete["/{storeName}"] = parameters =>
            {
                var storeName = parameters["storeName"];
                ViewBag.Title = "Deleted - " + storeName;
                if (brightstarService.DoesStoreExist(storeName))
                {
                    brightstarService.DeleteStore(storeName);
                }
                return(Negotiate.WithMediaRangeModel(new MediaRange("text/html"),
                                                     new StoreDeletedModel {
                    StoreName = storeName
                }));
            };
        }
Exemplo n.º 6
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;
            };

        }
Exemplo n.º 7
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);
            };
        }
Exemplo n.º 8
0
 /// <summary>
 /// Checks for the existence of a store with the given name
 /// </summary>
 /// <param name="storeName">Name of the store to check for</param>
 /// <returns>True if the store exists, false otherwise</returns>
 public bool DoesStoreExist(string storeName)
 {
     return(_client.DoesStoreExist(storeName));
 }