Exemplo n.º 1
0
        public void SparqlInjection()
        {
            String baseQuery = @"PREFIX ex: <http://example.org/Vehicles/>
SELECT * WHERE {
    ?s a @type .
}";

            SparqlParameterizedString query  = new SparqlParameterizedString(baseQuery);
            SparqlQueryParser         parser = new SparqlQueryParser();

            List <String> injections = new List <string>()
            {
                "ex:Car ; ex:Speed ?speed",
                "ex:Plane ; ?prop ?value",
                "ex:Car . EXISTS {?s ex:Speed ?speed}",
                "ex:Car \u0022; ex:Speed ?speed"
            };

            foreach (String value in injections)
            {
                query.SetLiteral("type", value);
                query.SetLiteral("@type", value);
                Console.WriteLine(query.ToString());
                Console.WriteLine();

                SparqlQuery q = parser.ParseFromString(query.ToString());
                Assert.Equal(1, q.Variables.Count());
            }
        }
Exemplo n.º 2
0
        public void SparqlConflictingParamNames()
        {
            String baseQuery = @"SELECT * WHERE {
    ?s a @type ; a @type1 ; a @type2 .
}";
            SparqlParameterizedString query  = new SparqlParameterizedString(baseQuery);
            SparqlQueryParser         parser = new SparqlQueryParser();

            query.SetUri("type", new Uri("http://example.org/type"));

            Console.WriteLine("Only one of the type parameters should be replaced here");
            Console.WriteLine(query.ToString());
            Console.WriteLine();

            query.SetUri("type1", new Uri("http://example.org/anotherType"));
            query.SetUri("type2", new Uri("http://example.org/yetAnotherType"));

            Console.WriteLine("Now all the type parameters should have been replaced");
            Console.WriteLine(query.ToString());
            Console.WriteLine();

            Assert.Throws <FormatException>(() =>
            {
                query.SetUri("not valid", new Uri("http://example.org/invalidParam"));
            });

            query.SetUri("is_valid", new Uri("http://example.org/validParam"));
        }
Exemplo n.º 3
0
        public void SparqlParameterizedStringWithNulls()
        {
            SparqlParameterizedString query = new SparqlParameterizedString();
            query.CommandText = "SELECT * WHERE { @s ?p ?o }";

            //Set a URI to a valid value
            query.SetUri("s", new Uri("http://example.org"));
            Console.WriteLine(query.ToString());
            Console.WriteLine();

            //Set the parameter to a null
            query.SetParameter("s", null);
            Console.WriteLine(query.ToString());
        }
Exemplo n.º 4
0
        public void SparqlParameterizedStringWithNulls()
        {
            SparqlParameterizedString query = new SparqlParameterizedString();

            query.CommandText = "SELECT * WHERE { @s ?p ?o }";

            //Set a URI to a valid value
            query.SetUri("s", new Uri("http://example.org"));
            Console.WriteLine(query.ToString());
            Console.WriteLine();

            //Set the parameter to a null
            query.SetParameter("s", null);
            Console.WriteLine(query.ToString());
        }
Exemplo n.º 5
0
        public void SparqlParameterizedStringShouldNotEncodeUri()
        {
            SparqlParameterizedString query = new SparqlParameterizedString("DESCRIBE @uri");

            query.SetUri("uri", new Uri("http://example.com/some@encoded/uri"));
            Assert.AreEqual("DESCRIBE <http://example.com/some@encoded/uri>", query.ToString(), "The query should contain the encoded form of the given uri");
        }
Exemplo n.º 6
0
        public void SparqlParameterizedStringShouldNotEncodeUri()
        {
            SparqlParameterizedString query = new SparqlParameterizedString("DESCRIBE @uri");

            query.SetUri("uri", new Uri("http://example.com/some@encoded/uri"));
            Assert.Equal("DESCRIBE <http://example.com/some@encoded/uri>", query.ToString());
        }
        /// <summary>
        /// Creates a new Open Connection Form
        /// </summary>
        /// <param name="g">Graph contaning Connection Definitions</param>
        public OpenConnectionForm(IGraph g)
        {
            InitializeComponent();

            //Find connections defined in the Configuration Graph
            this._g = g;
            INode genericManager = ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.ClassGenericManager);
            INode rdfsLabel = g.CreateUriNode(new Uri(NamespaceMapper.RDFS + "label"));

            SparqlParameterizedString getConnections = new SparqlParameterizedString();
            getConnections.QueryText = "SELECT ?obj ?label WHERE { ?obj a @type . OPTIONAL { ?obj @label ?label } } ORDER BY ?label";
            getConnections.SetParameter("type", genericManager);
            getConnections.SetParameter("label", rdfsLabel);

            Object results = this._g.ExecuteQuery(getConnections.ToString());
            if (results is SparqlResultSet)
            {
                SparqlResultSet rset = (SparqlResultSet)results;
                foreach (SparqlResult r in rset)
                {
                    this._connectionNodes.Add(r["obj"]);
                    if (r.HasValue("label"))
                    {
                        this.lstConnections.Items.Add(r["label"]);
                    }
                    else
                    {
                        this.lstConnections.Items.Add(r["obj"]);
                    }
                }
            }
        }
Exemplo n.º 8
0
        public void SparqlRemoteEndpointMemoryLeak2()
        {
            //Do a GC before attempting the test
            GC.GetTotalMemory(true);

            //First off make sure to load some data into the some
            SparqlRemoteUpdateEndpoint updateEndpoint = RemoteEndpoints.GetUpdateEndpoint();

            updateEndpoint.Update("DROP ALL");

            int totalRuns  = 10000;
            int subjects   = 1000;
            int predicates = 10;

            //Loop over making queries to try and reproduce the memory leak
            for (int i = 1; i <= totalRuns; i++)
            {
                //Add new data each time around
                updateEndpoint.Update("INSERT DATA { <http://subject/" + (i % subjects) + "> <http://predicate/" + (i % predicates) + "> <http://object/" + i + "> . }");

                SparqlRemoteEndpoint      endpoint    = RemoteEndpoints.GetQueryEndpoint();
                SparqlParameterizedString queryString = new SparqlParameterizedString();
                queryString.CommandText = "SELECT * WHERE { <http://subject/" + (i % 1000) + "> ?p ?o }";

                ResultCountHandler handler = new ResultCountHandler();
                endpoint.QueryWithResultSet(handler, queryString.ToString());
                Assert.True(handler.Count >= 1 && handler.Count <= subjects, "Result Count " + handler.Count + " is not in expected range 1 <= x < " + (i % 1000));

                if (i % 500 == 0)
                {
                    Debug.WriteLine("Memory Usage after " + i + " Iterations: " + Process.GetCurrentProcess().PrivateMemorySize64);
                }
            }
            Debug.WriteLine("Memory Usage after " + totalRuns + " Iterations: " + Process.GetCurrentProcess().PrivateMemorySize64);
        }
Exemplo n.º 9
0
        public void ShouldReplaceVariableStartingWithUnderscore()
        {
            var sut = new SparqlParameterizedString("SELECT * WHERE { ?s a ?_o }");

            sut.SetVariable("_o", new NodeFactory().CreateLiteralNode("test"));

            Assert.Equal("SELECT * WHERE { ?s a \"test\" }", sut.ToString());
        }
Exemplo n.º 10
0
        public void SetParameterShouldIgnoreParameterPrefixChar()
        {
            var sut = new SparqlParameterizedString("SELECT * WHERE { ?s a @o }");

            sut.SetParameter("@o", new NodeFactory().CreateLiteralNode("test"));

            Assert.Equal("SELECT * WHERE { ?s a \"test\" }", sut.ToString());
        }
Exemplo n.º 11
0
        public void SetVariableShouldIgnoreVariablePrefixChar2()
        {
            var sut = new SparqlParameterizedString("SELECT * WHERE { ?s a ?o }");

            sut.SetVariable("$o", new NodeFactory().CreateLiteralNode("test"));

            Assert.Equal("SELECT * WHERE { ?s a \"test\" }", sut.ToString());
        }
Exemplo n.º 12
0
        public void SparqlConflictingParamNames()
        {
            String baseQuery = @"SELECT * WHERE {
    ?s a @type ; a @type1 ; a @type2 .
}";
            SparqlParameterizedString query  = new SparqlParameterizedString(baseQuery);
            SparqlQueryParser         parser = new SparqlQueryParser();

            query.SetUri("type", new Uri("http://example.org/type"));

            Console.WriteLine("Only one of the type parameters should be replaced here");
            Console.WriteLine(query.ToString());
            Console.WriteLine();

            query.SetUri("type1", new Uri("http://example.org/anotherType"));
            query.SetUri("type2", new Uri("http://example.org/yetAnotherType"));

            Console.WriteLine("Now all the type parameters should have been replaced");
            Console.WriteLine(query.ToString());
            Console.WriteLine();

            try
            {
                query.SetUri("not valid", new Uri("http://example.org/invalidParam"));
                Assert.Fail("Should reject bad parameter names");
            }
            catch (FormatException ex)
            {
                //This is ok
                Console.WriteLine("Bad Parameter Name was rejected - " + ex.Message);
            }

            try
            {
                query.SetUri("is_valid", new Uri("http://example.org/validParam"));
            }
            catch (FormatException)
            {
                Assert.Fail("Should have been a valid parameter name");
            }
        }
Exemplo n.º 13
0
        public void SparqlParameterizedStringWithNulls2()
        {
            SparqlParameterizedString query = new SparqlParameterizedString();

            query.CommandText = "SELECT * WHERE { @s ?p ?o }";

            //Set a URI to a valid value
            query.SetUri("s", new Uri("http://example.org"));
            Console.WriteLine(query.ToString());
            Console.WriteLine();

            //Set the URI to a null
            Assert.Throws <ArgumentNullException>(() => query.SetUri("s", null));
        }
        protected override async Task ProcessStore(TripleStore store)
        {
            ResolverCollectorEventSource.Log.ProcessingBatch(BatchCount);

            try
            {
                SparqlResultSet distinctIds = SparqlHelpers.Select(store, Utils.GetResource("sparql.SelectDistinctPackage.rq"));

                IDictionary<Uri, IGraph> resolverResources = new Dictionary<Uri, IGraph>();

                foreach (SparqlResult row in distinctIds)
                {
                    string id = row["id"].ToString();

                    SparqlParameterizedString sparql = new SparqlParameterizedString();
                    sparql.CommandText = Utils.GetResource("sparql.ConstructResolverGraph.rq");

                    string baseAddress = _storage.BaseAddress.ToString();

                    sparql.SetLiteral("id", id);
                    sparql.SetLiteral("base", baseAddress);
                    sparql.SetLiteral("extension", ".json");
                    sparql.SetLiteral("galleryBase", GalleryBaseAddress);
                    sparql.SetLiteral("contentBase", ContentBaseAddress);

                    IGraph packageRegistration = SparqlHelpers.Construct(store, sparql.ToString());

                    if (packageRegistration.Triples.Count == 0)
                    {
                        throw new Exception("packageRegistration.Triples.Count == 0");
                    }

                    Uri registrationUri = new Uri(baseAddress + id.ToLowerInvariant() + ".json");
                    resolverResources.Add(registrationUri, packageRegistration);
                }

                if (resolverResources.Count != distinctIds.Count)
                {
                    throw new Exception("resolverResources.Count != distinctIds.Count");
                }

                await MergeAll(resolverResources);
            }
            finally
            {
                ResolverCollectorEventSource.Log.ProcessedBatch(BatchCount);
                store.Dispose();
            }
        }
Exemplo n.º 15
0
        public void SparqlRemoteEndpointMemoryLeak1()
        {
            /*
             * Dim endpoint = New SparqlRemoteEndpoint(New Uri("http://localhost:8080/sesame/repositories/my_repo"))
             * Dim queryString As SparqlParameterizedString = New SparqlParameterizedString()
             * queryString.Namespaces.AddNamespace("annot", New Uri(oAppSettingsReader.GetValue("BaseUriSite", GetType(System.String)) & "/annotations.owl#"))
             * queryString.CommandText = "SELECT DISTINCT ?text WHERE {?annotation annot:onContent <" & _uriDocument & "> ; annot:onContentPart """ & ContentPart & """ ; annot:text ?text ; annot:isValid ""false""^^xsd:boolean . }"
             * Dim results As SparqlResultSet = endpoint.QueryWithResultSet(queryString.ToString)
             * For Each result As SparqlResult In results
             * Console.WriteLine(DirectCast(result.Value("text"), LiteralNode).Value)
             * Next
             * results.Dispose()
             */

            //Do a GC before attempting the test
            GC.GetTotalMemory(true);

            //First off make sure to load some data into the some
            SparqlRemoteUpdateEndpoint updateEndpoint = RemoteEndpoints.GetUpdateEndpoint();

            updateEndpoint.Update("DROP ALL; INSERT DATA { <http://subject> <http://predicate> <http://object> . }");

            int totalRuns = 10000;

            //Loop over making queries to try and reproduce the memory leak
            for (int i = 1; i <= totalRuns; i++)
            {
                SparqlRemoteEndpoint      endpoint    = RemoteEndpoints.GetQueryEndpoint();
                SparqlParameterizedString queryString = new SparqlParameterizedString();
                queryString.CommandText = "SELECT * WHERE { ?s ?p ?o }";

                SparqlResultSet results = endpoint.QueryWithResultSet(queryString.ToString());
                Assert.Equal(1, results.Count);
                foreach (SparqlResult result in results)
                {
                    //We're just iterating to make sure we touch the whole of the results
                }
                results.Dispose();

                if (i % 500 == 0)
                {
                    Debug.WriteLine("Memory Usage after " + i + " Iterations: " + Process.GetCurrentProcess().PrivateMemorySize64);
                }
            }
            Debug.WriteLine("Memory Usage after " + totalRuns + " Iterations: " + Process.GetCurrentProcess().PrivateMemorySize64);
        }
Exemplo n.º 16
0
        public void Core445_ParsingTest()
        {
            Random randomizer = new Random();
            SparqlParameterizedString command = new SparqlParameterizedString("SELECT * FROM <urn:with@char> FROM <urn:with?or$char> WHERE { ?subject rdfs:subClassOf ? $c . $c rdfs:label \"string literal with lang tag @inLiteralParamPattern \"@en }");

            command.SetVariable("subject", g.CreateUriNode(UriFactory.Create("urn:some-uri")));
            command.SetVariable("c", g.CreateUriNode(UriFactory.Create("urn:some-uri")));
            command.SetVariable("or", g.CreateUriNode(UriFactory.Create("urn:some-uri")));
            command.SetVariable("char", g.CreateUriNode(UriFactory.Create("urn:some-uri")));
            command.SetParameter("char", g.CreateUriNode(UriFactory.Create("urn:some-uri")));
            command.SetParameter("en", g.CreateUriNode(UriFactory.Create("urn:some-uri")));
            command.SetParameter("inLiteralParamPattern", g.CreateUriNode(UriFactory.Create("urn:some-uri")));
            String output = command.ToString();

            Assert.True(output.Contains("<urn:with@char>"), "In IRI @ characters should not start a parameter capture");
            Assert.True(output.Contains("<urn:with?or$char>"), "In IRI ? and $ characters should not start a variable capture");
            Assert.True(output.Contains("rdfs:subClassOf ? "), "Property path ? quantifier should not start a variable capture");
            Assert.True(output.Contains("@en"), "Language tags should not start a parameter capture");
            Assert.True(output.Contains("@inLiteralParamPattern"), "In string literal @ characters should not start a parameter capture");
        }
Exemplo n.º 17
0
        public void Core445_Benchmark()
        {
            Stopwatch timer       = new Stopwatch();
            String    randomQuery = BuildRandomSizeQuery();

            // Parsing performances
            timer.Start();
            SparqlParameterizedString command = new SparqlParameterizedString(baseQuery);

            command.Append(randomQuery);
            command.Append("}");
            timer.Stop();

            Console.WriteLine("Query Size: " + command.CommandText.Length.ToString());
            Console.WriteLine("Variables: " + _variables.Count.ToString());
            Console.WriteLine("Parsing: " + timer.ElapsedMilliseconds.ToString());

            for (int i = 0; i < runs; i++)
            {
                Console.WriteLine("Run #" + i.ToString());
                timer.Reset();
                timer.Start();
                int variablesToSet = randomizer.Next(_variables.Count);
                if (variablesToSet > _variables.Count / 2)
                {
                    variablesToSet = _variables.Count;
                }
                foreach (String variable in _variables.Take(variablesToSet))
                {
                    command.SetVariable(variable, g.CreateUriNode(UriFactory.Create("urn:test#" + randomizer.Next(randomBodyIterations).ToString())));
                }
                timer.Stop();
                Console.WriteLine(variablesToSet.ToString() + " Variables set: " + timer.ElapsedMilliseconds.ToString());
                timer.Reset();
                timer.Start();
                String commandString = command.ToString();
                timer.Stop();
                Console.WriteLine("ToString: " + timer.ElapsedMilliseconds.ToString());
            }
        }
        async Task ProcessStore(TripleStore store)
        {
            try
            {
                SparqlResultSet distinctIds = SparqlHelpers.Select(store, Utils.GetResource("sparql.SelectDistinctPackage.rq"));

                IDictionary<Uri, IGraph> resolverResources = new Dictionary<Uri, IGraph>();

                foreach (SparqlResult row in distinctIds)
                {
                    string id = row["id"].ToString();

                    SparqlParameterizedString sparql = new SparqlParameterizedString();
                    sparql.CommandText = Utils.GetResource("sparql.ConstructResolverGraph.rq");

                    string baseAddress = _storage.BaseAddress + _storage.Container + "/resolver/";

                    sparql.SetLiteral("id", id);
                    sparql.SetLiteral("base", baseAddress);
                    sparql.SetLiteral("extension", ".json");

                    IGraph packageRegistration = SparqlHelpers.Construct(store, sparql.ToString());

                    Uri registrationUri = new Uri(baseAddress + id.ToLowerInvariant() + ".json");
                    resolverResources.Add(registrationUri, packageRegistration);
                }

                if (resolverResources.Count != distinctIds.Count)
                {
                    throw new Exception("resolverResources.Count != distinctIds.Count");
                }

                await MergeAll(resolverResources);
            }
            finally
            {
                store.Dispose();
            }
        }
Exemplo n.º 19
0
        /// <summary>
        /// Loads a Graph from the Store
        /// </summary>
        /// <param name="g">Graph to load into</param>
        /// <param name="graphUri">Uri of the Graph to load</param>
        /// <remarks>
        /// If an empty/null Uri is specified then the Default Graph of the Store will be loaded
        /// </remarks>
        public void LoadGraph(IGraph g, string graphUri)
        {
            try
            {
                HttpWebRequest request;
                Dictionary<String, String> serviceParams = new Dictionary<string, string>();

                String tID = (this._activeTrans.Value == null) ? String.Empty : "/" + this._activeTrans.Value;
                String requestUri = this._kb + tID + "/query";
                SparqlParameterizedString construct = new SparqlParameterizedString();
                if (!graphUri.Equals(String.Empty))
                {
                    construct.CommandText = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH @graph { ?s ?p ?o } }";
                    construct.SetUri("graph", new Uri(graphUri));
                    if (g.IsEmpty) g.BaseUri = new Uri(graphUri);
                }
                else
                {
                    construct.CommandText = "CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }";
                }
                serviceParams.Add("query", construct.ToString());

                request = this.CreateRequest(requestUri, MimeTypesHelper.HttpAcceptHeader, "GET", serviceParams);

#if DEBUG
                if (Options.HttpDebugging)
                {
                    Tools.HttpDebugRequest(request);
                }
#endif

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
#if DEBUG
                    if (Options.HttpDebugging)
                    {
                        Tools.HttpDebugResponse(response);
                    }
#endif
                    IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType);
                    parser.Load(g, new StreamReader(response.GetResponseStream()));
                    response.Close();
                }
            }
            catch (WebException webEx)
            {
#if DEBUG
                if (Options.HttpDebugging)
                {
                    if (webEx.Response != null) Tools.HttpDebugResponse((HttpWebResponse)webEx.Response);
                }
#endif
                throw new RdfStorageException("A HTTP Error occurred while trying to load a Graph from the Store", webEx);
            }
        }
        /// <summary>
        /// Saves a Graph into the Store (see remarks for notes on merge/overwrite behaviour)
        /// </summary>
        /// <param name="g">Graph to save</param>
        /// <remarks>
        /// <para>
        /// If the Graph has no URI then the contents will be appended to the Store's Default Graph. If the Graph has a URI then existing Graph associated with that URI will be replaced. To append to a named Graph use the <see cref="BlazegraphConnector.UpdateGraph(Uri,IEnumerable{Triple},IEnumerable{Triple})">UpdateGraph()</see> method instead
        /// </para>
        /// </remarks>
        public virtual void SaveGraph(IGraph g)
        {
            string activeTID;
            string tID = null;
            lock (transLock)
            {
                activeTID = this._activeTrans;
            }

            try
            {
                //Get a Transaction ID, if there is no active Transaction then this operation will be auto-committed
                tID = activeTID ?? this.BeginTransaction();

                Dictionary<string, string> queryParams = new Dictionary<string, string>();
                if (activeTID == null)
                {
                    queryParams.Add("timestamp", tID);
                }

                HttpWebRequest request;
                string boundary = String.Format("----------{0:N}", Guid.NewGuid());
                StringBuilder sb = new StringBuilder();

                if (g.BaseUri != null)
                {
                    SparqlParameterizedString construct = new SparqlParameterizedString();
                    construct.CommandText = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH @graph { ?s ?p ?o } }";
                    construct.SetUri("graph", g.BaseUri);
                    queryParams.Add("query", construct.ToString());

                    request = this.CreateRequest("/sparql", MimeTypesHelper.Any, "PUT", queryParams);
                    request.ContentType = MimeTypesHelper.GetDefinitionsByFileExtension(MimeTypesHelper.DefaultTriGExtension).First().MimeTypes.First();
                }
                else
                {
                    request = this.CreateRequest("/sparql?updatePost", MimeTypesHelper.Any, "POST", queryParams);
                    request.ContentType = MimeTypesHelper.FormMultipart + "; boundary=" + boundary;

                    string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\nContent-Type: {2}\r\n\r\n{3}",
                        boundary,
                        "add",
                        MimeTypesHelper.GetDefinitionsByFileExtension(MimeTypesHelper.DefaultTriGExtension).First().MimeTypes.First(),
                        "");
                    sb.Append(postData);
                }

                //Save the Data as TriG to the Request Stream
                TripleStore store = new TripleStore();
                store.Add(g);
                this._writer.Save(store, new System.IO.StringWriter(sb));

                if (g.BaseUri == null)
                {
                    string footer = "\r\n--" + boundary + "--\r\n";
                    sb.Append(footer);
                }

                using (StreamWriter writer = new StreamWriter(request.GetRequestStream(), new UTF8Encoding()))
                {
                    writer.Write(sb.ToString());
                    writer.Close();
                }

                Tools.HttpDebugRequest(request);

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    Tools.HttpDebugResponse(response);
                    //If we get here then it was OK
                    response.Close();
                }

                //Commit Transaction only if in auto-commit mode (active transaction will be null)
                if (activeTID != null) return;
                try
                {
                    this.CommitTransaction(tID);
                }
                catch (Exception ex)
                {
                    throw new RdfStorageException("Blazegraph failed to commit a Transaction", ex);
                }
            }
            catch (WebException webEx)
            {
                //Rollback Transaction only if got as far as creating a Transaction
                //and in auto-commit mode
                if (tID != null)
                {
                    if (activeTID == null)
                    {
                        try
                        {
                            this.RollbackTransaction(tID);
                        }
                        catch (Exception ex)
                        {
                            StorageHelper.HandleHttpError(webEx, "");
                            throw new RdfStorageException("Blazegraph failed to rollback a Transaction", ex);
                        }
                    }
                }

                throw StorageHelper.HandleHttpError(webEx, "saving a Graph to");
            }
        }
        protected virtual void SaveGraphAsync(bool autoCommit, IGraph g, AsyncStorageCallback callback, object state)
        {
            try
            {
                Dictionary<string, string> queryParams = new Dictionary<string, string>();

                HttpWebRequest request;
                string boundary = String.Format("----------{0:N}", Guid.NewGuid());
                StringBuilder sb = new StringBuilder();

                if (g.BaseUri != null)
                {
                    SparqlParameterizedString construct = new SparqlParameterizedString();
                    construct.CommandText = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH @graph { ?s ?p ?o } }";
                    construct.SetUri("graph", g.BaseUri);
                    queryParams.Add("query", construct.ToString());

                    request = this.CreateRequest("/sparql", MimeTypesHelper.Any, "PUT", queryParams);
                    request.ContentType = MimeTypesHelper.GetDefinitionsByFileExtension(MimeTypesHelper.DefaultTriGExtension).First().MimeTypes.First();
                }
                else
                {
                    request = this.CreateRequest("/sparql?updatePost", MimeTypesHelper.Any, "POST", queryParams);
                    request.ContentType = MimeTypesHelper.FormMultipart + "; boundary=" + boundary;

                    string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\nContent-Type: {2}\r\n\r\n{3}",
                        boundary,
                        "add",
                        MimeTypesHelper.GetDefinitionsByFileExtension(MimeTypesHelper.DefaultTriGExtension).First().MimeTypes.First(),
                        "");
                    sb.Append(postData);
                }

                //Save the Data as TriG to the Request Stream
                TripleStore store = new TripleStore();
                store.Add(g);
                this._writer.Save(store, new System.IO.StringWriter(sb));

                if (g.BaseUri == null)
                {
                    string footer = "\r\n--" + boundary + "--\r\n";
                    sb.Append(footer);
                }

                request.BeginGetRequestStream(r =>
                {
                    try
                    {
                        //Save the Data as TriG to the Request Stream
                        using (StreamWriter writer = new StreamWriter(request.EndGetRequestStream(r), new UTF8Encoding()))
                        {
                            writer.Write(sb.ToString());
                            writer.Close();
                        }

                        Tools.HttpDebugRequest(request);
                        request.BeginGetResponse(r2 =>
                        {
                            try
                            {
                                using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(r2))
                                {
                                    Tools.HttpDebugResponse(response);

                                    //If we get here then it was OK
                                    response.Close();
                                }

                                //Commit Transaction only if in auto-commit mode (active transaction will be null)
                                if (autoCommit)
                                {
                                    this.Commit((sender, args, st) =>
                                    {
                                        if (args.WasSuccessful)
                                        {
                                            callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SaveGraph, g), state);
                                        }
                                        else
                                        {
                                            callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SaveGraph, args.Error), state);
                                        }
                                    }, state);
                                }
                                else
                                {
                                    callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SaveGraph, g), state);
                                }
                            }
                            catch (WebException webEx)
                            {
                                if (autoCommit)
                                {
                                    //If something went wrong try to rollback, don't care what the rollback response is
                                    this.Rollback((sender, args, st) => { }, state);
                                }
                                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SaveGraph, StorageHelper.HandleHttpError(webEx, "saving a Graph asynchronously to")), state);
                            }
                            catch (Exception ex)
                            {
                                if (autoCommit)
                                {
                                    //If something went wrong try to rollback, don't care what the rollback response is
                                    this.Rollback((sender, args, st) => { }, state);
                                }
                                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SaveGraph, StorageHelper.HandleError(ex, "saving a Graph asynchronously to")), state);
                            }
                        }, state);
                    }
                    catch (WebException webEx)
                    {
                        if (autoCommit)
                        {
                            //If something went wrong try to rollback, don't care what the rollback response is
                            this.Rollback((sender, args, st) => { }, state);
                        }
                        callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SaveGraph, StorageHelper.HandleHttpError(webEx, "saving a Graph asynchronously to")), state);
                    }
                    catch (Exception ex)
                    {
                        if (autoCommit)
                        {
                            //If something went wrong try to rollback, don't care what the rollback response is
                            this.Rollback((sender, args, st) => { }, state);
                        }
                        callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SaveGraph, StorageHelper.HandleError(ex, "saving a Graph asynchronously to")), state);
                    }
                }, state);
            }
            catch (WebException webEx)
            {
                if (autoCommit)
                {
                    //If something went wrong try to rollback, don't care what the rollback response is
                    this.Rollback((sender, args, st) => { }, state);
                }
                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SaveGraph, StorageHelper.HandleHttpError(webEx, "saving a Graph asynchronously to")), state);
            }
            catch (Exception ex)
            {
                if (autoCommit)
                {
                    //If something went wrong try to rollback, don't care what the rollback response is
                    this.Rollback((sender, args, st) => { }, state);
                }
                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SaveGraph, StorageHelper.HandleError(ex, "saving a Graph asynchronously to")), state);
            }
        }
Exemplo n.º 22
0
        public void SparqlConflictingParamNames()
        {
            String baseQuery = @"SELECT * WHERE {
    ?s a @type ; a @type1 ; a @type2 .
}";
            SparqlParameterizedString query = new SparqlParameterizedString(baseQuery);
            SparqlQueryParser parser = new SparqlQueryParser();

            query.SetUri("type", new Uri("http://example.org/type"));

            Console.WriteLine("Only one of the type parameters should be replaced here");
            Console.WriteLine(query.ToString());
            Console.WriteLine();

            query.SetUri("type1", new Uri("http://example.org/anotherType"));
            query.SetUri("type2", new Uri("http://example.org/yetAnotherType"));

            Console.WriteLine("Now all the type parameters should have been replaced");
            Console.WriteLine(query.ToString());
            Console.WriteLine();

            try
            {
                query.SetUri("not valid", new Uri("http://example.org/invalidParam"));
                Assert.Fail("Should reject bad parameter names");
            }
            catch (FormatException ex)
            {
                //This is ok
                Console.WriteLine("Bad Parameter Name was rejected - " + ex.Message);
            }

            try
            {
                query.SetUri("is_valid", new Uri("http://example.org/validParam"));
            }
            catch (FormatException)
            {
                Assert.Fail("Should have been a valid parameter name");
            }

        }
        /// <summary>
        /// Loads a Graph from the Store asynchronously
        /// </summary>
        /// <param name="handler">Handler to load with</param>
        /// <param name="graphUri">URI of the Graph to load</param>
        /// <param name="callback">Callback</param>
        /// <param name="state">State to pass to the callback</param>
        public override void LoadGraph(IRdfHandler handler, string graphUri, AsyncStorageCallback callback, object state)
        {
            try
            {
                HttpWebRequest request;
                Dictionary<String, String> serviceParams = new Dictionary<string, string>();
                Uri baseUri = null;

                SparqlParameterizedString construct = new SparqlParameterizedString();
                if (!graphUri.Equals(string.Empty))
                {
                    construct.CommandText = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH @graph { ?s ?p ?o } }";
                    baseUri = UriFactory.Create(graphUri);
                    construct.SetUri("graph", baseUri);
                }
                else
                {
                    construct.CommandText = "CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }";
                }
                serviceParams.Add("query", construct.ToString());

                request = this.CreateRequest("/sparql", MimeTypesHelper.HttpAcceptHeader, "GET", serviceParams);

                Tools.HttpDebugRequest(request);

                request.BeginGetResponse(r =>
                {
                    try
                    {
                        using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(r))
                        {
                            Tools.HttpDebugResponse(response);

                            IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType);
                            parser.Load(handler, new StreamReader(response.GetResponseStream()));
                            if (baseUri != null)
                            {
                                handler.StartRdf();
                                handler.HandleBaseUri(baseUri);
                                handler.EndRdf(true);
                            }
                            response.Close();
                        }

                        callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.LoadWithHandler, handler), state);
                    }
                    catch (WebException webEx)
                    {
                        callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.LoadWithHandler, StorageHelper.HandleHttpError(webEx, "loading a Graph from")), state);
                    }
                    catch (Exception ex)
                    {
                        callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.LoadWithHandler, StorageHelper.HandleError(ex, "loading a Graph from")), state);
                    }
                }, state);
            }
            catch (WebException webEx)
            {
                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.LoadWithHandler, StorageHelper.HandleHttpError(webEx, "loading a Graph from")), state);
            }
            catch (Exception ex)
            {
                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.LoadWithHandler, StorageHelper.HandleError(ex, "loading a Graph from")), state);
            }
        }
Exemplo n.º 24
0
        public void SparqlInjection()
        {
            String baseQuery = @"PREFIX ex: <http://example.org/Vehicles/>
SELECT * WHERE {
    ?s a @type .
}";

            SparqlParameterizedString query = new SparqlParameterizedString(baseQuery);
            SparqlQueryParser parser = new SparqlQueryParser();

            List<String> injections = new List<string>()
            {
                "ex:Car ; ex:Speed ?speed",
                "ex:Plane ; ?prop ?value",
                "ex:Car . EXISTS {?s ex:Speed ?speed}",
                "ex:Car \u0022; ex:Speed ?speed"
            };

            foreach (String value in injections)
            {
                query.SetLiteral("type", value);
                query.SetLiteral("@type", value);
                Console.WriteLine(query.ToString());
                Console.WriteLine();

                SparqlQuery q = parser.ParseFromString(query.ToString());
                Assert.AreEqual(1, q.Variables.Count(), "Should only be one variable in the query");
            }
        }
Exemplo n.º 25
0
 private void TestNegation(ISparqlDataset data, SparqlParameterizedString queryWithNegation, SparqlParameterizedString queryWithoutNegation)
 {
     this.TestNegation(data, queryWithNegation.ToString(), queryWithoutNegation.ToString());
 }
Exemplo n.º 26
0
 /// <summary>
 /// Creates a new SPARQL View
 /// </summary>
 /// <param name="sparqlQuery">SPARQL Query</param>
 /// <param name="store">Triple Store to query</param>
 public SparqlView(SparqlParameterizedString sparqlQuery, IInMemoryQueryableStore store)
     : this(sparqlQuery.ToString(), store)
 {
 }
Exemplo n.º 27
0
 private void TestNegation(ISparqlDataset data, SparqlParameterizedString queryWithNegation, SparqlParameterizedString queryWithoutNegation)
 {
     this.TestNegation(data, queryWithNegation.ToString(), queryWithoutNegation.ToString());
 }
Exemplo n.º 28
0
        public static IEnumerable<NamespaceTerm> LoadNamespaceTerms(String namespaceUri)
        {
            //Don't load if already loaded
            if (_loadedNamespaces.Contains(namespaceUri)) return GetNamespaceTerms(namespaceUri);

            try
            {
                Graph g = new Graph();
                try
                {
                    UriLoader.Load(g, new Uri(namespaceUri));
                }
                catch
                {
                    //Try and load from our local copy if there is one
                    String prefix = GetDefaultPrefix(namespaceUri);
                    if (!prefix.Equals(String.Empty))
                    {
                        Stream localCopy = Assembly.GetExecutingAssembly().GetManifestResourceStream("rdfEditor.AutoComplete.Vocabularies." + prefix + ".ttl");
                        if (localCopy != null)
                        {
                            TurtleParser ttlparser = new TurtleParser();
                            ttlparser.Load(g, new StreamReader(localCopy));
                        }
                    }
                }
                List<NamespaceTerm> terms = new List<NamespaceTerm>();
                String termUri;

                //UriNode rdfType = g.CreateUriNode(new Uri(RdfSpecsHelper.RdfType));
                IUriNode rdfsClass = g.CreateUriNode(new Uri(NamespaceMapper.RDFS + "Class"));
                IUriNode rdfsLabel = g.CreateUriNode(new Uri(NamespaceMapper.RDFS + "label"));
                IUriNode rdfsComment = g.CreateUriNode(new Uri(NamespaceMapper.RDFS + "comment"));
                IUriNode rdfProperty = g.CreateUriNode(new Uri(NamespaceMapper.RDF + "Property"));
                IUriNode rdfsDatatype = g.CreateUriNode(new Uri(NamespaceMapper.RDFS + "Datatype"));

                SparqlParameterizedString queryString = new SparqlParameterizedString();
                queryString.CommandText = "SELECT ?term STR(?label) AS ?RawLabel STR(?comment) AS ?RawComment WHERE { {{?term a @class} UNION {?term a @property} UNION {?term a @datatype}} OPTIONAL {?term @label ?label} OPTIONAL {?term @comment ?comment} }";
                queryString.SetParameter("class", rdfsClass);
                queryString.SetParameter("property", rdfProperty);
                queryString.SetParameter("datatype", rdfsDatatype);
                queryString.SetParameter("label", rdfsLabel);
                queryString.SetParameter("comment", rdfsComment);

                Object results = g.ExecuteQuery(queryString.ToString());
                if (results is SparqlResultSet)
                {
                    foreach (SparqlResult r in ((SparqlResultSet)results))
                    {
                        termUri = r["term"].ToString();
                        if (termUri.StartsWith(namespaceUri))
                        {
                            //Use the Comment as the label if available
                            if (r.HasValue("RawComment"))
                            {
                                if (r["RawComment"] != null)
                                {
                                    terms.Add(new NamespaceTerm(namespaceUri, termUri.Substring(namespaceUri.Length), r["RawComment"].ToString()));
                                    continue;
                                }
                            }

                            //Use the Label as the label if available
                            if (r.HasValue("RawLabel"))
                            {
                                if (r["RawLabel"] != null)
                                {
                                    terms.Add(new NamespaceTerm(namespaceUri, termUri.Substring(namespaceUri.Length), r["RawLabel"].ToString()));
                                    continue;
                                }
                            }

                            //Otherwise no label
                            terms.Add(new NamespaceTerm(namespaceUri, termUri.Substring(namespaceUri.Length)));
                        }
                    }
                }

                lock (_terms)
                {
                    terms.RemoveAll(t => _terms.Contains(t));
                    _terms.AddRange(terms.Distinct());
                }
            }
            catch
            {
                //Ignore Exceptions - just means we won't have those namespace terms available
            }
            try
            {
                _loadedNamespaces.Add(namespaceUri);
            }
            catch (NullReferenceException)
            {
                //For some reason .Net sometimes throws a NullReferenceException here which we shall ignore
            }
            return GetNamespaceTerms(namespaceUri);
        }
Exemplo n.º 29
0
        public org.openrdf.repository.RepositoryResult getStatements(org.openrdf.model.Resource r, org.openrdf.model.URI uri, org.openrdf.model.Value v, bool b, params org.openrdf.model.Resource[] rarr)
        {
            SparqlParameterizedString queryString = new SparqlParameterizedString();
            IEnumerable<Uri> contexts = rarr.ToContexts(this._mapping);
            if (contexts.Any())
            {
                queryString.CommandText = "SELECT (?s AS ?subj) (?p AS ?pred) (?o AS ?obj)\n";
                foreach (Uri u in contexts)
                {
                    queryString.CommandText += "FROM <" + this._formatter.FormatUri(u) + ">\n";
                }
                queryString.CommandText += "WHERE { ?s ?p ?o }";
            }
            else
            {
                queryString.CommandText = "SELECT (?s AS ?subj) (?p AS ?pred) (?o AS ?obj) WHERE { ?s ?p ?o }";
            }
            if (r != null) queryString.SetVariable("s", SesameConverter.FromSesameResource(r, this._mapping));
            if (uri != null) queryString.SetVariable("p", SesameConverter.FromSesameUri(uri, this._mapping));
            if (v != null) queryString.SetVariable("o", SesameConverter.FromSesameValue(v, this._mapping));

            return this.GetStatementsInternal(queryString.ToString(), this._mapping);
        }
        /// <summary>
        /// Determines whether a Graph with the given URI exists
        /// </summary>
        /// <param name="graphUri">Graph URI</param>
        /// <returns></returns>
        protected override bool HasGraph(Uri graphUri)
        {
            if (this._manager is IQueryableGenericIOManager)
            {
                //Generate an ASK query based on this
                SparqlParameterizedString ask = new SparqlParameterizedString();
                if (graphUri != null)
                {
                    ask.CommandText = "ASK WHERE { GRAPH @graph { ?s ?p ?o . } }";
                    ask.SetUri("graph", graphUri);
                }
                else
                {
                    ask.CommandText = "ASK WHERE { ?s ?p ?o }";
                }

                Object results = ((IQueryableGenericIOManager)this._manager).Query(ask.ToString());
                if (results is SparqlResultSet)
                {
                    return ((SparqlResultSet)results).Result;
                }
                else
                {
                    throw new SparqlHttpProtocolException("Failed to retrieve a Boolean Result since the query processor did not return a valid SPARQL Result Set as expected");
                }
            }
            else
            {
                IGraph g = this.GetGraph(graphUri);
                return !g.IsEmpty;
            }
        }
        public override IGraph CreatePageContent(CatalogContext context)
        {
            try
            {
                IGraph content;

                using (TripleStore store = new TripleStore())
                {
                    store.Add(_catalogItem, true);

                    SparqlParameterizedString sparql = new SparqlParameterizedString();
                    sparql.CommandText = Utils.GetResource("sparql.ConstructPackagePageContentGraph.rq");

                    sparql.SetUri("package", GetItemAddress());
                    sparql.SetUri("catalogPackage", _catalogUri);
                    sparql.SetUri("baseAddress", BaseAddress);
                    sparql.SetUri("packageContent", GetPackageContentAddress());
                    sparql.SetUri("registrationBaseAddress", _registrationBaseAddress);

                    content = SparqlHelpers.Construct(store, sparql.ToString());
                }

                return content;
            }
            catch (Exception e)
            {
                throw new Exception(string.Format("Exception processing catalog item {0}", _catalogUri), e);
            }
        }
Exemplo n.º 32
0
        public bool hasStatement(org.openrdf.model.Resource r, org.openrdf.model.URI uri, org.openrdf.model.Value v, bool b, params org.openrdf.model.Resource[] rarr)
        {
            SparqlParameterizedString queryString = new SparqlParameterizedString();
            queryString.CommandText = "ASK";
            foreach (Uri u in rarr.ToContexts(this._mapping))
            {
                queryString.CommandText += "\nFROM <" + this._formatter.FormatUri(u) + ">";
            }
            queryString.CommandText += "\nWHERE { ?subject ?predicate ?object}";
            if (r != null) queryString.SetVariable("subject", SesameConverter.FromSesameResource(r, this._mapping));
            if (uri != null) queryString.SetVariable("predicate", SesameConverter.FromSesameUri(uri, this._mapping));
            if (v != null) queryString.SetVariable("object", SesameConverter.FromSesameValue(v, this._mapping));

            return this.HasTriplesInternal(queryString.ToString());
        }
Exemplo n.º 33
0
        /// <summary>
        /// Queries the Store using the Graph Pattern specified by the set of Statement Patterns
        /// </summary>
        /// <param name="graph">Graph Pattern</param>
        /// <param name="options">Query Options</param>
        /// <param name="sink">Results Sink</param>
        /// <remarks>
        /// <para>
        /// Implemented by converting the Statement Patterns into a SPARQL SELECT query and executing that against the underlying Store's SPARQL engine
        /// </para>
        /// <para>
        /// The only Query Option that is supported is the Limit option
        /// </para>
        /// </remarks>
        public void Query(Statement[] graph, SW.Query.QueryOptions options, SW.Query.QueryResultSink sink)
        {
            //Implement as a SPARQL SELECT
            SparqlParameterizedString queryString = new SparqlParameterizedString();
            queryString.QueryText = "SELECT * WHERE {";

            int p = 0;
            foreach (Statement stmt in graph)
            {
                //Add Subject
                queryString.QueryText += "\n";
                if (stmt.Subject is Variable)
                {
                    queryString.QueryText += stmt.Subject.ToString();
                }
                else
                {
                    queryString.QueryText += "@param" + p;
                    queryString.SetParameter("param" + p, SemWebConverter.FromSemWeb(stmt.Subject, this._mapping));
                    p++;
                }
                queryString.QueryText += " ";

                //Add Predicate
                if (stmt.Predicate is Variable)
                {
                    queryString.QueryText += stmt.Predicate.ToString();
                }
                else
                {
                    queryString.QueryText += "@param" + p;
                    queryString.SetParameter("param" + p, SemWebConverter.FromSemWeb(stmt.Predicate, this._mapping));
                    p++;
                }
                queryString.QueryText += " ";

                //Add Object
                if (stmt.Object is Variable)
                {
                    queryString.QueryText += stmt.Object.ToString();
                }
                else
                {
                    queryString.QueryText += "@param" + p;
                    queryString.SetParameter("param" + p, SemWebConverter.FromSemWeb(stmt.Object, this._mapping));
                    p++;
                }
                queryString.QueryText += " .";
            }

            queryString.QueryText += "}";

            //Execute the Query and convert the Results
            Object results = this._store.ExecuteQuery(queryString.ToString());

            if (results is SparqlResultSet)
            {
                SparqlResultSet rset = (SparqlResultSet)results;
                sink.Init(rset.Variables.Select(v => new Variable(v)).ToArray());
                if (rset.Count > 0)
                {
                    int c = 0;
                    foreach (SparqlResult r in rset)
                    {
                        //Apply Limit if applicable
                        if (options.Limit > 0 && c >= options.Limit)
                        {
                            sink.Finished();
                            return;
                        }

                        //Convert the Set to VariableBindings for SemWeb
                        Variable[] vars = r.Variables.Select(v => new Variable(v)).ToArray();
                        Resource[] resources = r.Variables.Select(v => SemWebConverter.ToSemWeb(r[v], this._mapping)).ToArray();
                        SW.Query.VariableBindings bindings = new SW.Query.VariableBindings(vars, resources);

                        //Keep adding results until the sink tells us to stop
                        if (!sink.Add(bindings))
                        {
                            sink.Finished();
                            return;
                        }
                        c++;
                    }
                    sink.Finished();
                }
                else
                {
                    sink.Finished();
                }
            }
            else
            {
                throw new RdfQueryException("Query returned an unexpected result where a SPARQL Result Set was expected");
            }
        }
        async Task DeletePackage(Uri resourceUri, string version)
        {
            string existingJson = await _storage.Load(resourceUri);
            if (existingJson != null)
            {
                IGraph currentPackageRegistration = Utils.CreateGraph(existingJson);

                using (TripleStore store = new TripleStore())
                {
                    store.Add(currentPackageRegistration, true);

                    SparqlParameterizedString sparql = new SparqlParameterizedString();
                    sparql.CommandText = Utils.GetResource("sparql.DeletePackage.rq");

                    sparql.SetLiteral("version", version);

                    IGraph modifiedPackageRegistration = SparqlHelpers.Construct(store, sparql.ToString());

                    if (CountPackage(modifiedPackageRegistration) == 0)
                    {
                        await _storage.Delete(resourceUri);
                    }
                    else
                    {
                        string content = Utils.CreateJson(modifiedPackageRegistration, _resolverFrame);
                        await _storage.Save("application/json", resourceUri, content);
                    }
                }
            }
        }
Exemplo n.º 35
0
 /// <summary>
 /// Creates a new SPARQL View
 /// </summary>
 /// <param name="sparqlQuery">SPARQL Query</param>
 /// <param name="store">Triple Store to query</param>
 protected BaseSparqlView(SparqlParameterizedString sparqlQuery, ITripleStore store)
     : this(sparqlQuery.ToString(), store)
 {
 }
Exemplo n.º 36
0
        public void RunTest(String[] args)
        {
            if (args.Length < 2)
            {
                Console.Error.WriteLine("rdfWebDeploy: Error: 2 Arguments are required in order to use the -test mode");
                return;
            }

            if (!File.Exists(args[1]))
            {
                Console.Error.WriteLine("rdfWebDeploy: Error: Cannot test " + args[1] + " since the file does not exist");
                return;
            }

            Graph g = new Graph();
            try
            {
                FileLoader.Load(g, args[1]);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("rdfWebDeploy: Error: Cannot parse the configuration file");
                Console.Error.WriteLine("rdfWebDeploy: Error: " + ex.Message);
                return;
            }
            Console.WriteLine("rdfWebDeploy: Opened the configuration file successfully");

            Graph vocab = new Graph();
            try
            {
                TurtleParser ttlparser = new TurtleParser();
                ttlparser.Load(vocab, new StreamReader(Assembly.GetAssembly(typeof(IGraph)).GetManifestResourceStream("VDS.RDF.Configuration.configuration.ttl")));
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("rdfWebDeploy: Error: Cannot parse the configuration vocabulary");
                Console.Error.WriteLine("rdfWebDeploy: Error: " + ex.Message);
                return;
            }
            Console.WriteLine("rdfWebDeploy: Loaded the configuration vocabulary successfully");
            Console.WriteLine();
            Console.WriteLine("rdfWebDeploy: Tests Started...");
            Console.WriteLine();

            //Now make some tests against it
            int warnings = 0, errors = 0;
            IInMemoryQueryableStore store = new TripleStore();
            store.Add(vocab);
            if (g.BaseUri == null) g.BaseUri = new Uri("dotnetrdf:config");
            store.Add(g);
            Object results;
            SparqlResultSet rset;

            //Unknown vocabulary term test
            Console.WriteLine("rdfWebDeploy: Testing for URIs in the vocabulary namespace which are unknown");
            results = store.ExecuteQuery(RdfWebDeployHelper.NamespacePrefixes + UnknownVocabularyTermsTest);
            if (results is SparqlResultSet)
            {
                rset = (SparqlResultSet)results;
                foreach (SparqlResult r in rset)
                {
                    Console.Error.WriteLine("rdfWebDeploy: Error: The configuration file uses the URI '" + r["term"] + "' for a property/type and this does not appear to be a valid term in the Configuration vocabulary");
                    errors++;
                }
            }
            Console.WriteLine();

            #region General dnr:type Tests

            //Missing dnr:type test
            Console.WriteLine("rdfWebDeploy: Testing for missing dnr:type properties");
            results = g.ExecuteQuery(RdfWebDeployHelper.NamespacePrefixes + MissingConfigurationTypeTest);
            if (results is SparqlResultSet)
            {
                rset = (SparqlResultSet)results;
                foreach (SparqlResult r in rset)
                {
                    Console.Error.WriteLine("rdfWebDeploy: Warning: The Node '" + r["s"].ToString() + "' has an rdf:type but no dnr:type which may be needed by the Configuration Loader");
                    warnings++;
                }
            }
            Console.WriteLine();

            //Invalid dnr:type test
            Console.WriteLine("rdfWebDeploy: Testing that values given for dnr:type property are literals");
            results = g.ExecuteQuery(RdfWebDeployHelper.NamespacePrefixes + InvalidTypePropertyTest);
            if (results is SparqlResultSet)
            {
                rset = (SparqlResultSet)results;
                foreach (SparqlResult r in rset)
                {
                    Console.Error.WriteLine("rdfWebDeploy: Error: The node '" + r["s"].ToString() + "' has a dnr:type of '" + r["type"].ToString() + "' which is not given as a string literal");
                    errors++;
                }
            }
            Console.WriteLine();

            //Multiple dnr:type test
            Console.WriteLine("rdfWebDeploy: Testing that no object has multiple dnr:type values");
            results = g.ExecuteQuery(RdfWebDeployHelper.NamespacePrefixes + MultipleTypeTest);
            if (results is SparqlResultSet)
            {
                rset = (SparqlResultSet)results;
                foreach (SparqlResult r in rset)
                {
                    Console.Error.WriteLine("rdfWebDeploy: Error: The node '" + r["s"].ToString() + "' has multiple dnr:type values which is not valid");
                    errors++;
                }
            }
            Console.WriteLine();

            //Unknown Library Types test
            Console.WriteLine("rdfWebDeploy: Testing that values given for dnr:type property in the VDS.RDF namespace are valid");
            results = g.ExecuteQuery(RdfWebDeployHelper.NamespacePrefixes + LibraryTypesTest);
            if (results is SparqlResultSet)
            {
                rset = (SparqlResultSet)results;
                Assembly dotnetrdf = Assembly.GetAssembly(typeof(IGraph));
                foreach (SparqlResult r in rset)
                {
                    Type t = dotnetrdf.GetType(((ILiteralNode)r["type"]).Value);
                    if (t == null)
                    {
                        Console.Error.WriteLine("rdfWebDeploy: Error: The node '" + r["s"].ToString() + "' has a dnr:type of '" + r["type"].ToString() + "' which does not appear to be a valid type in dotNetRDF");
                        errors++;
                    }
                }
            }
            Console.WriteLine();

            #endregion

            #region dnr:HttpHandler Tests including specific dnr:type Tests

            //Bad Handler URI test
            Console.WriteLine("rdfWebDeploy: Testing for bad URIs given the rdf:type of dnr:HttpHandler");
            results = g.ExecuteQuery(RdfWebDeployHelper.NamespacePrefixes + BadHandlerUriTest);
            if (results is SparqlResultSet)
            {
                rset = (SparqlResultSet)results;
                foreach (SparqlResult r in rset)
                {
                    Console.Error.WriteLine("rdfWebDeploy: Error: The Handler node '" + r["s"].ToString() + "' is not a URI of the form <dotnetrdf:/path> which is required for correct detection of handler configuration");
                    errors++;
                }
            }
            Console.WriteLine();

            //Missing Handler type test
            Console.WriteLine("rdfWebDeploy: Testing for missing dnr:type for dnr:HttpHandler objects");
            results = g.ExecuteQuery(RdfWebDeployHelper.NamespacePrefixes + MissingHandlerTypeTest);
            if (results is SparqlResultSet)
            {
                rset = (SparqlResultSet)results;
                foreach (SparqlResult r in rset)
                {
                    Console.Error.WriteLine("rdfWebDeploy: Error: The Handler node '" + r["s"].ToString() + "' has an rdf:type but no dnr:type which is requiring for automated deployment via this tool");
                    errors++;
                }
            }
            Console.WriteLine();

            //Invalid Handler Type test
            Console.WriteLine("rdfWebDeploy: Testing that values given for dnr:type for dnr:HttpHandler objects in the VDS.RDF namespace are valid IHttpHandlers");
            results = g.ExecuteQuery(RdfWebDeployHelper.NamespacePrefixes + InvalidHandlerTypeTest);
            if (results is SparqlResultSet)
            {
                rset = (SparqlResultSet)results;
                Assembly dotnetrdf = Assembly.GetAssembly(typeof(IGraph));
                foreach (SparqlResult r in rset)
                {
                    Type t = dotnetrdf.GetType(((ILiteralNode)r["type"]).Value);
                    if (t != null)
                    {
                        if (!t.GetInterfaces().Any(i => i.Equals(typeof(System.Web.IHttpHandler))))
                        {
                            Console.Error.WriteLine("rdfWebDeploy: Error: The node '" + r["s"].ToString() + "' has a dnr:type of '" + r["type"].ToString() + "' which does not appear to be a valid IHttpHandler implementation in dotNetRDF");
                            errors++;
                        }
                    }
                }
            }
            Console.WriteLine();

            #endregion

            #region Property Tests

            //Range test
            Console.WriteLine("rdfWebDeploy: Testing for bad ranges for properties");
            results = store.ExecuteQuery(RdfWebDeployHelper.NamespacePrefixes + InvalidRangeTest);
            if (results is SparqlResultSet)
            {
                rset = (SparqlResultSet)results;
                foreach (SparqlResult r in rset)
                {
                    SparqlParameterizedString hasValidRange = new SparqlParameterizedString();
                    hasValidRange.QueryText = RdfWebDeployHelper.NamespacePrefixes + ValidRangeTest;
                    hasValidRange.SetParameter("property", r["property"]);
                    hasValidRange.SetParameter("s", r["s"]);
                    Object temp = store.ExecuteQuery(hasValidRange.ToString());
                    if (temp is SparqlResultSet)
                    {
                        if (!((SparqlResultSet)temp).Result)
                        {
                            Console.Error.WriteLine("rdfWebDeploy: Error: The Node '" + r["s"].ToString() + "' has a value for the property '" + r["property"].ToString() + "' which is '" + r["obj"].ToString() + "' which does not appear to be valid for the range of this property which is '" + r["range"].ToString() + "'");
                            errors++;
                        }
                    }
                }
            }
            Console.WriteLine();

            //Domain test
            Console.WriteLine("rdfWebDeploy: Testing for bad domains for properties");
            results = store.ExecuteQuery(RdfWebDeployHelper.NamespacePrefixes + InvalidDomainTest);
            if (results is SparqlResultSet)
            {
                rset = (SparqlResultSet)results;
                foreach (SparqlResult r in rset)
                {
                    SparqlParameterizedString hasValidDomain = new SparqlParameterizedString();
                    hasValidDomain.QueryText = RdfWebDeployHelper.NamespacePrefixes + ValidDomainTest;
                    hasValidDomain.SetParameter("property", r["property"]);
                    hasValidDomain.SetParameter("s", r["s"]);
                    Object temp = store.ExecuteQuery(hasValidDomain.ToString());
                    if (temp is SparqlResultSet)
                    {
                        if (!((SparqlResultSet)temp).Result)
                        {
                            Console.Error.WriteLine("rdfWebDeploy: Error: The Node '" + r["s"].ToString() + "' has a value for the property '" + r["property"].ToString() + "' and the type given is '" + r["type"].ToString() + "' which does not match the domain of this property which is '" + r["domain"].ToString() + "'");
                            errors++;
                        }
                    }
                }
            }
            Console.WriteLine();

            #endregion

            #region Clear Text Password Tests

            Console.WriteLine("rdfWebDeploy: Testing for clear text passwords used with dnr:password property");
            results = store.ExecuteQuery(RdfWebDeployHelper.NamespacePrefixes + ClearTextPasswordTest);
            if (results is SparqlResultSet)
            {
                foreach (SparqlResult r in (SparqlResultSet)results)
                {
                    Console.Error.WriteLine("rdfWebDeploy: Warning: The Node '" + r["s"].ToString() + "' has a value for the dnr:password property specified as a Literal in clear text.  It is recommended that you specify any passwords as AppSetting URIs e.g. <appsetting:MyPassword> and then create an AppSetting in the <appSettings> section of your Web.config file to store your password.  The Web.config file can then have the <appSettings> section encrypted to help protect your password");
                    warnings++;
                }
            }
            Console.WriteLine();

            #endregion

            //Show Test Results
            Console.WriteLine("rdfWedDeploy: Tests Completed - " + warnings + " Warning(s) - " + errors + " Error(s)");
        }
Exemplo n.º 37
0
 /// <summary>
 /// Creates a new SPARQL View
 /// </summary>
 /// <param name="sparqlQuery">SPARQL Query</param>
 /// <param name="store">Triple Store to query</param>
 public NativeSparqlView(SparqlParameterizedString sparqlQuery, INativelyQueryableStore store)
     : this(sparqlQuery.ToString(), store) { }
Exemplo n.º 38
0
 /// <summary>
 /// Parses a SPARQL Update Command Set from the given String
 /// </summary>
 /// <param name="updates">SPARQL Update Commands</param>
 /// <returns></returns>
 public SparqlUpdateCommandSet ParseFromString(SparqlParameterizedString updates)
 {
     if (updates == null) throw new RdfParseException("Cannot parse SPARQL Update Commands from a null String");
     return this.ParseFromString(updates.ToString());
 }
        static XElement CreateNuspecMetadata(TripleStore store, Uri packageUri)
        {
            SparqlParameterizedString sparql = new SparqlParameterizedString();
            sparql.CommandText = Utils.GetResource("sparql.SelectPackageDetails.rq");
            sparql.SetUri("package", packageUri);
            SparqlResult packageInfo = SparqlHelpers.Select(store, sparql.ToString()).FirstOrDefault();

            if (packageInfo == null)
            {
                throw new ArgumentException(string.Format("no package details for {0}", packageUri));
            }

            XElement metadata = new XElement(nuget.GetName("metadata"));

            metadata.Add(new XElement(nuget.GetName("id"), packageInfo["id"].ToString()));
            metadata.Add(new XElement(nuget.GetName("version"), packageInfo["version"].ToString()));

            //  in the nuspec the published and owners fields are not present and ignored respectfully

            //if (packageInfo.HasBoundValue("published"))
            //{
            //    DateTime val = DateTime.Parse(((ILiteralNode)packageInfo["published"]).Value);
            //    metadata.Add(new XElement(nuget.GetName("published"), val.ToUniversalTime().ToString("O")));
            //}

            if (packageInfo.HasBoundValue("authors"))
            {
                metadata.Add(new XElement(nuget.GetName("authors"), packageInfo["authors"].ToString()));
            }

            if (packageInfo.HasBoundValue("description"))
            {
                metadata.Add(new XElement(nuget.GetName("description"), packageInfo["description"].ToString()));
            }

            if (packageInfo.HasBoundValue("summary"))
            {
                metadata.Add(new XElement(nuget.GetName("summary"), packageInfo["summary"].ToString()));
            }

            if (packageInfo.HasBoundValue("language"))
            {
                metadata.Add(new XElement(nuget.GetName("language"), packageInfo["language"].ToString()));
            }

            if (packageInfo.HasBoundValue("title"))
            {
                metadata.Add(new XElement(nuget.GetName("title"), packageInfo["title"].ToString()));
            }

            if (packageInfo.HasBoundValue("targetFramework"))
            {
                metadata.Add(new XElement(nuget.GetName("targetFramework"), packageInfo["targetFramework"].ToString()));
            }

            if (packageInfo.HasBoundValue("requireLicenseAcceptance"))
            {
                bool val = bool.Parse(((ILiteralNode)packageInfo["requireLicenseAcceptance"]).Value);
                metadata.Add(new XElement(nuget.GetName("requireLicenseAcceptance"), val));
            }

            if (packageInfo.HasBoundValue("licenseUrl"))
            {
                metadata.Add(new XElement(nuget.GetName("licenseUrl"), packageInfo["licenseUrl"].ToString()));
            }

            if (packageInfo.HasBoundValue("iconUrl"))
            {
                metadata.Add(new XElement(nuget.GetName("iconUrl"), packageInfo["iconUrl"].ToString()));
            }

            if (packageInfo.HasBoundValue("projectUrl"))
            {
                metadata.Add(new XElement(nuget.GetName("projectUrl"), packageInfo["projectUrl"].ToString()));
            }

            if (packageInfo.HasBoundValue("releaseNotes"))
            {
                metadata.Add(new XElement(nuget.GetName("releaseNotes"), packageInfo["releaseNotes"].ToString()));
            }

            if (packageInfo.HasBoundValue("copyright"))
            {
                metadata.Add(new XElement(nuget.GetName("copyright"), packageInfo["copyright"].ToString()));
            }

            if (packageInfo.HasBoundValue("minClientVersion"))
            {
                metadata.Add(new XAttribute("minClientVersion", packageInfo["minClientVersion"].ToString()));
            }

            if (packageInfo.HasBoundValue("developmentDependency"))
            {
                bool val = bool.Parse(((ILiteralNode)packageInfo["developmentDependency"]).Value);
                metadata.Add(new XElement(nuget.GetName("developmentDependency"), val));
            }

            return metadata;
        }
Exemplo n.º 40
0
 private void TestBindings(ISparqlDataset data, SparqlParameterizedString queryWithBindings, SparqlParameterizedString queryWithoutBindings)
 {
     this.TestBindings(data, queryWithBindings.ToString(), queryWithoutBindings.ToString());
 }
Exemplo n.º 41
0
 private void TestBindings(ISparqlDataset data, SparqlParameterizedString queryWithBindings, SparqlParameterizedString queryWithoutBindings)
 {
     this.TestBindings(data, queryWithBindings.ToString(), queryWithoutBindings.ToString());
 }
Exemplo n.º 42
0
 /// <summary>
 /// Creates a new SPARQL View
 /// </summary>
 /// <param name="sparqlQuery">SPARQL Query</param>
 /// <param name="store">Triple Store to query</param>
 public NativeSparqlView(SparqlParameterizedString sparqlQuery, INativelyQueryableStore store)
     : this(sparqlQuery.ToString(), store)
 {
 }
Exemplo n.º 43
0
        private void ExpandByDataset(UriToExpand u, ExpansionContext context, ExpansionDataset dataset)
        {
            if (u.Depth == context.Profile.MaxExpansionDepth) return;
            if (dataset.Ignore) return;

            foreach (Uri endpoint in dataset.SparqlEndpoints)
            {
                Thread.Sleep(HttpRequestInterval);
                SparqlRemoteEndpoint sparqlEndpoint = new SparqlRemoteEndpoint(endpoint);
                try
                {
                    SparqlParameterizedString queryString = new SparqlParameterizedString("DESCRIBE @uri");
                    queryString.SetUri("uri", u.Uri);
                    Object temp = sparqlEndpoint.QueryWithResultGraph(queryString.ToString());
                    if (temp is Graph)
                    {
                        Graph g = (Graph)temp;
                        this.ExpandGraph(u, g, context);
                    }
                }
                catch (RdfException rdfEx)
                {
                    this.DebugErrors("Error: Tried to DESCRIBE <" + u.Uri.ToString() + "> against the SPARQL Endpoint <" + endpoint.ToString() + "> but an error occurred:", rdfEx);
                }
                catch (WebException webEx)
                {
                    this.DebugErrors("Error: Tried to DESCRIBE <" + u.Uri.ToString() + "> against the SPARQL Endpoint <" + endpoint.ToString() + "> but an error occurred:", webEx);
                }
            }

            foreach (Uri endpoint in dataset.UriLookupEndpoints)
            {
                this.ExpandByUriLookup(u, context, endpoint);
            }

            foreach (Uri endpoint in dataset.UriDiscoveryEndpoints)
            {
                this.ExpandByUriDiscovery(u, context, endpoint);
            }
        }
Exemplo n.º 44
0
 /// <summary>
 /// Creates a new SPARQL View
 /// </summary>
 /// <param name="sparqlQuery">SPARQL Query</param>
 /// <param name="store">Triple Store to query</param>
 public SparqlView(SparqlParameterizedString sparqlQuery, IInMemoryQueryableStore store)
     : this(sparqlQuery.ToString(), store) { }
        static void AddExistingItem(IDictionary<RegistrationEntryKey, RegistrationCatalogEntry> resources, TripleStore store, Uri catalogEntry)
        {
            Trace.TraceInformation("RegistrationPersistence.AddExistingItem: catalogEntry = {0}", catalogEntry);

            SparqlParameterizedString sparql = new SparqlParameterizedString();
            sparql.CommandText = Utils.GetResource("sparql.ConstructCatalogEntryGraph.rq");
            sparql.SetUri("catalogEntry", catalogEntry);

            IGraph graph = SparqlHelpers.Construct(store, sparql.ToString());

            resources.Add(RegistrationCatalogEntry.Promote(catalogEntry.AbsoluteUri, graph));
        }
Exemplo n.º 46
0
 /// <summary>
 /// Creates a new SPARQL View
 /// </summary>
 /// <param name="sparqlQuery">SPARQL Query</param>
 /// <param name="store">Triple Store to query</param>
 public BaseSparqlView(SparqlParameterizedString sparqlQuery, ITripleStore store)
     : this(sparqlQuery.ToString(), store) { }
        protected override bool HasTripleInternal(Triple t)
        {
            if (this._manager is IQueryableGenericIOManager)
            {
                SparqlParameterizedString queryString = new SparqlParameterizedString("ASK WHERE { @subject @predicate @object}");
                queryString.SetParameter("subject", t.Subject);
                queryString.SetParameter("predicate", t.Predicate);
                queryString.SetParameter("object", t.Object);

                Object results = ((IQueryableGenericIOManager)this._manager).Query(queryString.ToString());
                if (results is SparqlResultSet)
                {
                    SparqlResultSet rset = (SparqlResultSet)results;
                    return rset.ResultsType == SparqlResultsType.Boolean && rset.Result;
                } 
                else 
                {
                    return false;
                }
            }
            else
            {
                throw new dotSesameRepo.RepositoryException("This dotNetRDF Generic Repository does not support detecting whether a given Statement exists in the Store");
            }
        }
        static XElement CreateNuspecMetadataFrameworkAssembly(TripleStore store, Uri packageUri)
        {
            SparqlParameterizedString sparql = new SparqlParameterizedString();
            sparql.CommandText = Utils.GetResource("sparql.SelectPackageFrameworkAssembly.rq");
            sparql.SetUri("package", packageUri);
            SparqlResultSet packageFrameworkAssembly = SparqlHelpers.Select(store, sparql.ToString());

            if (packageFrameworkAssembly.Count > 0)
            {
                XElement frameworkAssemblies = new XElement(nuget.GetName("frameworkAssemblies"));

                foreach (SparqlResult row in packageFrameworkAssembly)
                {
                    string targetFramework = row["targetFramework"].ToString();
                    string assembly = row["assembly"].ToString();

                    XElement frameworkAssembly = new XElement(nuget.GetName("frameworkAssembly"));

                    frameworkAssembly.Add(new XAttribute("assemblyName", assembly));
                    frameworkAssembly.Add(new XAttribute("targetFramework", targetFramework));

                    frameworkAssemblies.Add(frameworkAssembly);
                }

                return frameworkAssemblies;
            }

            return null;
        }
Exemplo n.º 49
0
 /// <summary>
 /// Parses a SPARQL Query from a SPARQL Parameterized String
 /// </summary>
 /// <param name="queryString">A SPARQL Parameterized String</param>
 /// <returns></returns>
 /// <remarks>
 /// The <see cref="SparqlParameterizedString">SparqlParameterizedString</see> class allows you to use parameters in a String in a manner similar to SQL Commands in the ADO.Net model.  See the documentation for <see cref="SparqlParameterizedString">SparqlParameterizedString</see> for details of this.
 /// </remarks>
 public SparqlQuery ParseFromString(SparqlParameterizedString queryString)
 {
     if (queryString == null) throw new RdfParseException("Cannot parse a SPARQL Query from a null String");
     return this.ParseFromString(queryString.ToString());
 }