/// <summary>
        /// Saves a Graph to the Protocol Server
        /// </summary>
        /// <param name="g">Graph to save</param>
        public virtual void SaveGraph(IGraph g)
        {
            String saveUri = _serviceUri;

            if (g.BaseUri != null)
            {
                saveUri += "?graph=" + Uri.EscapeDataString(g.BaseUri.AbsoluteUri);
            }
            else
            {
                saveUri += "?default";
            }
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(UriFactory.Create(saveUri));
                request.Method = "PUT";
                //request.ContentType = MimeTypesHelper.RdfXml[0];
                request.ContentType = _writerMimeTypeDefinition.CanonicalMimeType;
                request             = ApplyRequestOptions(request);

                //RdfXmlWriter writer = new RdfXmlWriter();
                IRdfWriter writer = _writerMimeTypeDefinition.GetRdfWriter();
                writer.Save(g, new StreamWriter(request.GetRequestStream()));

                Tools.HttpDebugRequest(request);

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    Tools.HttpDebugResponse(response);
                    // If we get here then it was OK
                    response.Close();
                }
            }
            catch (WebException webEx)
            {
                throw StorageHelper.HandleHttpError(webEx, "saving a Graph to");
            }
        }
예제 #2
0
        /// <summary>
        /// Saves a Graph to the Store
        /// </summary>
        /// <param name="g">Graph to save</param>
        public void SaveGraph(IGraph g)
        {
            try
            {
                HttpWebRequest request;
                Dictionary <String, String> requestParams = new Dictionary <string, string>();
                if (g.BaseUri != null)
                {
                    requestParams.Add("context", g.BaseUri.AbsoluteUri);
                    request = this.CreateRequest("/statements", MimeTypesHelper.Any, "PUT", requestParams);
                }
                else
                {
                    request = this.CreateRequest("/statements", MimeTypesHelper.Any, "POST", requestParams);
                }

                IRdfWriter rdfWriter = new RdfXmlWriter();
                request.ContentType = MimeTypesHelper.RdfXml[0];
                using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
                {
                    rdfWriter.Save(g, writer);
                    writer.Close();
                }

                Tools.HttpDebugRequest(request);

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    Tools.HttpDebugResponse(response);
                    //If we get here then operation completed OK
                    response.Close();
                }
            }
            catch (WebException webEx)
            {
                throw StorageHelper.HandleHttpError(webEx, "save a Graph to");
            }
        }
예제 #3
0
        /// <summary>
        /// Deletes a Graph from the store
        /// </summary>
        /// <param name="graphUri">URI of the Graph to delete</param>
        public virtual void DeleteGraph(String graphUri)
        {
            String deleteUri = this._serviceUri;

            if (graphUri != null && !graphUri.Equals(String.Empty))
            {
                deleteUri += "?graph=" + Uri.EscapeDataString(graphUri);
            }
            else
            {
                deleteUri += "?default";
            }

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(UriFactory.Create(deleteUri));
                request.Method = "DELETE";
                request        = base.ApplyRequestOptions(request);

                Tools.HttpDebugRequest(request);

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    Tools.HttpDebugResponse(response);
                    // If we get here then it was OK
                    response.Close();
                }
            }
            catch (WebException webEx)
            {
                // Don't throw the error if we get a 404 - this means we couldn't do a delete as the graph didn't exist to start with
                if (webEx.Response == null || (webEx.Response != null && ((HttpWebResponse)webEx.Response).StatusCode != HttpStatusCode.NotFound))
                {
                    throw StorageHelper.HandleHttpError(webEx, "deleting a Graph from");
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Saves a Graph to a 4store instance (Warning: Completely replaces any existing Graph with the same URI)
        /// </summary>
        /// <param name="g">Graph to save</param>
        /// <remarks>
        /// <para>
        /// Completely replaces any existing Graph with the same Uri in the store
        /// </para>
        /// <para>
        /// Attempting to save a Graph which doesn't have a Base Uri will result in an error
        /// </para>
        /// </remarks>
        /// <exception cref="RdfStorageException">Thrown if you try and save a Graph without a Base Uri or if there is an error communicating with the 4store instance</exception>
        public void SaveGraph(IGraph g)
        {
            try
            {
                // Set up the Request
                HttpWebRequest request;
                if (g.BaseUri != null)
                {
                    request = (HttpWebRequest)WebRequest.Create(_baseUri + "data/" + Uri.EscapeUriString(g.BaseUri.AbsoluteUri));
                }
                else
                {
                    throw new RdfStorageException("Cannot save a Graph without a Base URI to a 4store Server");
                }
                request.Method      = "PUT";
                request.ContentType = MimeTypesHelper.Turtle[0];
                request             = ApplyRequestOptions(request);

                // Write the Graph as Turtle to the Request Stream
                CompressingTurtleWriter writer = new CompressingTurtleWriter(WriterCompressionLevel.High);
                writer.Save(g, new StreamWriter(request.GetRequestStream()));

                Tools.HttpDebugRequest(request);

                // Make the Request
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    Tools.HttpDebugResponse(response);
                    // If we get here then it was OK
                    response.Close();
                }
            }
            catch (WebException webEx)
            {
                throw StorageHelper.HandleHttpError(webEx, "saving a Graph to");
            }
        }
예제 #5
0
        /// <summary>
        /// Helper method for doing async update operations, callers just need to provide an appropriately prepared HTTP request and a RDF writer which will be used to write the data to the request body
        /// </summary>
        /// <param name="request">HTTP Request</param>
        /// <param name="writer">RDF writer</param>
        /// <param name="graphUri">URI of the Graph to update</param>
        /// <param name="ts">Triples</param>
        /// <param name="callback">Callback</param>
        /// <param name="state">State to pass to the callback</param>
        protected internal void UpdateGraphAsync(HttpWebRequest request, IRdfWriter writer, Uri graphUri, IEnumerable <Triple> ts, AsyncStorageCallback callback, Object state)
        {
            Graph g = new Graph();

            g.Assert(ts);

            request.BeginGetRequestStream(r =>
            {
                try
                {
                    Stream reqStream = request.EndGetRequestStream(r);
                    writer.Save(g, new StreamWriter(reqStream));

                    Tools.HttpDebugRequest(request);

                    request.BeginGetResponse(r2 =>
                    {
                        try
                        {
                            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(r2);
                            Tools.HttpDebugResponse(response);
                            //If we get here then it was OK
                            response.Close();
                            callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.UpdateGraph, graphUri), state);
                        }
                        catch (WebException webEx)
                        {
                            callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.UpdateGraph, graphUri, StorageHelper.HandleHttpError(webEx, "updating a Graph asynchronously in")), state);
                        }
                        catch (Exception ex)
                        {
                            callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.UpdateGraph, graphUri, StorageHelper.HandleError(ex, "updating a Graph asynchronously in")), state);
                        }
                    }, state);
                }
                catch (WebException webEx)
                {
                    callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.UpdateGraph, graphUri, StorageHelper.HandleHttpError(webEx, "updating a Graph asynchronously in")), state);
                }
                catch (Exception ex)
                {
                    callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.UpdateGraph, graphUri, StorageHelper.HandleError(ex, "updating a Graph asynchronously in")), state);
                }
            }, state);
        }
예제 #6
0
        /// <summary>
        /// Helper method for doing async save operations, callers just need to provide an appropriately perpared HTTP requests and a RDF writer which will be used to write the data to the request body
        /// </summary>
        /// <param name="request">HTTP request</param>
        /// <param name="writer">RDF Writer</param>
        /// <param name="g">Graph to save</param>
        /// <param name="callback">Callback</param>
        /// <param name="state">State to pass to the callback</param>
        protected internal void SaveGraphAsync(HttpWebRequest request, IRdfWriter writer, IGraph g, AsyncStorageCallback callback, Object state)
        {
            request.BeginGetRequestStream(r =>
            {
                try
                {
                    Stream reqStream = request.EndGetRequestStream(r);
                    writer.Save(g, new StreamWriter(reqStream));

                    Tools.HttpDebugRequest(request);

                    request.BeginGetResponse(r2 =>
                    {
                        try
                        {
                            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(r2);
                            Tools.HttpDebugResponse(response);
                            //If we get here then it was OK
                            response.Close();
                            callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SaveGraph, g), state);
                        }
                        catch (WebException webEx)
                        {
                            callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SaveGraph, g, StorageHelper.HandleHttpError(webEx, "saving a Graph asynchronously to")), state);
                        }
                        catch (Exception ex)
                        {
                            callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SaveGraph, g, StorageHelper.HandleError(ex, "saving a Graph asynchronously to")), state);
                        }
                    }, state);
                }
                catch (WebException webEx)
                {
                    callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SaveGraph, g, StorageHelper.HandleHttpError(webEx, "saving a Graph asynchronously to")), state);
                }
                catch (Exception ex)
                {
                    callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SaveGraph, g, StorageHelper.HandleError(ex, "saving a Graph asynchronously to")), state);
                }
            }, state);
        }
예제 #7
0
        /// <summary>
        /// Deletes a Graph from the store asynchronously
        /// </summary>
        /// <param name="graphUri">URI of the graph to delete</param>
        /// <param name="callback">Callback</param>
        /// <param name="state">State to pass to the callback</param>
        public override void DeleteGraph(String graphUri, AsyncStorageCallback callback, Object state)
        {
            String deleteUri = this._serviceUri;

            if (graphUri != null && !graphUri.Equals(String.Empty))
            {
                deleteUri += "?graph=" + Uri.EscapeDataString(graphUri);
            }
            else
            {
                deleteUri += "?default";
            }

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(UriFactory.Create(deleteUri));
                request.Method = "DELETE";
                request        = base.ApplyRequestOptions(request);

                this.DeleteGraphAsync(request, true, graphUri, callback, state);
            }
            catch (WebException webEx)
            {
                // Don't throw the error if we get a 404 - this means we couldn't do a delete as the graph didn't exist to start with
                if (webEx.Response == null || (webEx.Response != null && ((HttpWebResponse)webEx.Response).StatusCode != HttpStatusCode.NotFound))
                {
                    callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.DeleteGraph, graphUri.ToSafeUri(), StorageHelper.HandleHttpError(webEx, "deleting a Graph from")), state);
                }
                else
                {
                    callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.DeleteGraph, graphUri.ToSafeUri()), state);
                }
            }
        }
예제 #8
0
        /// <summary>
        /// Updates a Graph in the Store asychronously
        /// </summary>
        /// <param name="graphUri">URI of the Graph to update</param>
        /// <param name="additions">Triples to be added</param>
        /// <param name="removals">Triples to be removed</param>
        /// <param name="callback">Callback</param>
        /// <param name="state">State to pass to the callback</param>
        public override void UpdateGraph(string graphUri, IEnumerable <Triple> additions, IEnumerable <Triple> removals, AsyncStorageCallback callback, object state)
        {
            if (!_updatesEnabled)
            {
                throw new RdfStorageException("4store does not support Triple level updates");
            }
            else if (graphUri.Equals(String.Empty))
            {
                throw new RdfStorageException("Cannot update a Graph without a Graph URI on a 4store Server");
            }
            else
            {
                try
                {
                    StringBuilder delete = new StringBuilder();
                    if (removals != null)
                    {
                        if (removals.Any())
                        {
                            // Build up the DELETE command and execute
                            delete.AppendLine("DELETE DATA");
                            delete.AppendLine("{ GRAPH <" + graphUri.Replace(">", "\\>") + "> {");
                            foreach (Triple t in removals)
                            {
                                delete.AppendLine(t.ToString(_formatter));
                            }
                            delete.AppendLine("}}");
                        }
                    }

                    StringBuilder insert = new StringBuilder();
                    if (additions != null)
                    {
                        if (additions.Any())
                        {
                            // Build up the INSERT command and execute
                            insert.AppendLine("INSERT DATA");
                            insert.AppendLine("{ GRAPH <" + graphUri.Replace(">", "\\>") + "> {");
                            foreach (Triple t in additions)
                            {
                                insert.AppendLine(t.ToString(_formatter));
                            }
                            insert.AppendLine("}}");
                        }
                    }

                    // Use Update() method to send the updates
                    if (delete.Length > 0)
                    {
                        if (insert.Length > 0)
                        {
                            Update(delete.ToString() + "\n;\n" + insert.ToString(), (sender, args, st) =>
                            {
                                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.UpdateGraph, graphUri.ToSafeUri(), args.Error), state);
                            }, state);
                        }
                        else
                        {
                            Update(delete.ToString(), (sender, args, st) =>
                            {
                                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.UpdateGraph, graphUri.ToSafeUri(), args.Error), state);
                            }, state);
                        }
                    }
                    else if (insert.Length > 0)
                    {
                        Update(insert.ToString(), (sender, args, st) =>
                        {
                            callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.UpdateGraph, graphUri.ToSafeUri(), args.Error), state);
                        }, state);
                    }
                    else
                    {
                        // Nothing to do
                        callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.UpdateGraph, graphUri.ToSafeUri()), state);
                    }
                }
                catch (WebException webEx)
                {
                    callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.UpdateGraph, graphUri.ToSafeUri(), StorageHelper.HandleHttpError(webEx, "updating a Graph asynchronously in")), state);
                }
            }
        }
예제 #9
0
        /// <summary>
        /// Updates a Graph in the store
        /// </summary>
        /// <param name="graphUri">Uri of the Graph to Update</param>
        /// <param name="additions">Triples to be added</param>
        /// <param name="removals">Triples to be removed</param>
        /// <remarks>
        /// May throw an error since the default builds of 4store don't support Triple level updates.  There are builds that do support this and the user can instantiate the connector with support for this enabled if they wish, if they do so and the underlying 4store doesn't support updates errors will occur when updates are attempted.
        /// </remarks>
        public void UpdateGraph(String graphUri, IEnumerable <Triple> additions, IEnumerable <Triple> removals)
        {
            if (!_updatesEnabled)
            {
                throw new RdfStorageException("4store does not support Triple level updates");
            }
            else if (graphUri.Equals(String.Empty))
            {
                throw new RdfStorageException("Cannot update a Graph without a Graph URI on a 4store Server");
            }
            else
            {
                try
                {
                    StringBuilder delete = new StringBuilder();
                    if (removals != null)
                    {
                        if (removals.Any())
                        {
                            // Build up the DELETE command and execute
                            delete.AppendLine("DELETE DATA");
                            delete.AppendLine("{ GRAPH <" + graphUri.Replace(">", "\\>") + "> {");
                            foreach (Triple t in removals)
                            {
                                delete.AppendLine(t.ToString(_formatter));
                            }
                            delete.AppendLine("}}");
                        }
                    }

                    StringBuilder insert = new StringBuilder();
                    if (additions != null)
                    {
                        if (additions.Any())
                        {
                            // Build up the INSERT command and execute
                            insert.AppendLine("INSERT DATA");
                            insert.AppendLine("{ GRAPH <" + graphUri.Replace(">", "\\>") + "> {");
                            foreach (Triple t in additions)
                            {
                                insert.AppendLine(t.ToString(_formatter));
                            }
                            insert.AppendLine("}}");
                        }
                    }

                    // Use Update() method to send the updates
                    if (delete.Length > 0)
                    {
                        if (insert.Length > 0)
                        {
                            Update(delete.ToString() + "\n;\n" + insert.ToString());
                        }
                        else
                        {
                            Update(delete.ToString());
                        }
                    }
                    else if (insert.Length > 0)
                    {
                        Update(insert.ToString());
                    }
                }
                catch (WebException webEx)
                {
                    throw StorageHelper.HandleHttpError(webEx, "updating a Graph in");
                }
            }
        }
        /// <summary>
        /// Makes a SPARQL Update request to the Allegro Graph server
        /// </summary>
        /// <param name="sparqlUpdate">SPARQL Update</param>
        /// <param name="callback">Callback</param>
        /// <param name="state">State to pass to the callback</param>
        public virtual void Update(string sparqlUpdate, AsyncStorageCallback callback, Object state)
        {
            try
            {
                HttpWebRequest request;

                //Create the Request
                request = this.CreateRequest(this._repositoriesPrefix + this._store + this._updatePath, MimeTypesHelper.Any, "POST", new Dictionary <String, String>());

                //Build the Post Data and add to the Request Body
                request.ContentType = MimeTypesHelper.WWWFormURLEncoded;
                StringBuilder postData = new StringBuilder();
                postData.Append("query=");
                postData.Append(HttpUtility.UrlEncode(EscapeQuery(sparqlUpdate)));

                request.BeginGetRequestStream(r =>
                {
                    try
                    {
                        Stream stream = request.EndGetRequestStream(r);
                        using (StreamWriter writer = new StreamWriter(stream))
                        {
                            writer.Write(postData);
                            writer.Close();
                        }

                        Tools.HttpDebugRequest(request);

                        //Get the Response and process based on the Content Type
                        request.BeginGetResponse(r2 =>
                        {
                            try
                            {
                                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(r2);
                                Tools.HttpDebugResponse(response);
                                //If we get here it completed OK
                                response.Close();
                                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlUpdate, sparqlUpdate), state);
                            }
                            catch (WebException webEx)
                            {
                                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlUpdate, sparqlUpdate, StorageHelper.HandleHttpError(webEx, "updating")), state);
                            }
                            catch (Exception ex)
                            {
                                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlUpdate, sparqlUpdate, StorageHelper.HandleError(ex, "updating")), state);
                            }
                        }, state);
                    }
                    catch (WebException webEx)
                    {
                        callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlUpdate, sparqlUpdate, StorageHelper.HandleHttpError(webEx, "updating")), state);
                    }
                    catch (Exception ex)
                    {
                        callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlUpdate, sparqlUpdate, StorageHelper.HandleError(ex, "updating")), state);
                    }
                }, state);
            }
            catch (WebException webEx)
            {
                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlUpdate, sparqlUpdate, StorageHelper.HandleHttpError(webEx, "updating")), state);
            }
            catch (Exception ex)
            {
                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlUpdate, sparqlUpdate, StorageHelper.HandleError(ex, "updating")), state);
            }
        }
예제 #11
0
        /// <summary>
        /// Updates a Graph on the Fuseki Server
        /// </summary>
        /// <param name="graphUri">URI of the Graph to update</param>
        /// <param name="additions">Triples to be added</param>
        /// <param name="removals">Triples to be removed</param>
        /// <param name="callback">Callback</param>
        /// <param name="state">State to pass to the callback</param>
        public override void UpdateGraph(string graphUri, IEnumerable <Triple> additions, IEnumerable <Triple> removals, AsyncStorageCallback callback, object state)
        {
            try
            {
                String        graph  = (graphUri != null && !graphUri.Equals(String.Empty)) ? "GRAPH <" + this._formatter.FormatUri(graphUri) + "> {" : String.Empty;
                StringBuilder update = new StringBuilder();

                if (additions != null)
                {
                    if (additions.Any())
                    {
                        update.AppendLine("INSERT DATA {");
                        if (!graph.Equals(String.Empty))
                        {
                            update.AppendLine(graph);
                        }

                        foreach (Triple t in additions)
                        {
                            update.AppendLine(this._formatter.Format(t));
                        }

                        if (!graph.Equals(String.Empty))
                        {
                            update.AppendLine("}");
                        }
                        update.AppendLine("}");
                    }
                }

                if (removals != null)
                {
                    if (removals.Any())
                    {
                        if (update.Length > 0)
                        {
                            update.AppendLine(";");
                        }

                        update.AppendLine("DELETE DATA {");
                        if (!graph.Equals(String.Empty))
                        {
                            update.AppendLine(graph);
                        }

                        foreach (Triple t in removals)
                        {
                            update.AppendLine(this._formatter.Format(t));
                        }

                        if (!graph.Equals(String.Empty))
                        {
                            update.AppendLine("}");
                        }
                        update.AppendLine("}");
                    }
                }

                if (update.Length > 0)
                {
                    this.Update(update.ToString(), (sender, args, st) =>
                    {
                        if (args.WasSuccessful)
                        {
                            callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.UpdateGraph, graphUri.ToSafeUri()), state);
                        }
                        else
                        {
                            callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.UpdateGraph, graphUri.ToSafeUri(), args.Error), state);
                        }
                    }, state);
                }
                else
                {
                    callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.UpdateGraph, graphUri.ToSafeUri()), state);
                }
            }
            catch (WebException webEx)
            {
                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.UpdateGraph, graphUri.ToSafeUri(), StorageHelper.HandleHttpError(webEx, "updating a Graph asynchronously")), state);
            }
        }
예제 #12
0
        /// <summary>
        /// Executes SPARQL Updates against the Fuseki store
        /// </summary>
        /// <param name="sparqlUpdate">SPARQL Update</param>
        /// <param name="callback">Callback</param>
        /// <param name="state">State to pass to the callback</param>
        public void Update(String sparqlUpdate, AsyncStorageCallback callback, Object state)
        {
            try
            {
                // Make the SPARQL Update Request
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this._updateUri);
                request.Method      = "POST";
                request.ContentType = "application/sparql-update";
                request             = base.ApplyRequestOptions(request);

                request.BeginGetRequestStream(r =>
                {
                    try
                    {
                        Stream stream       = request.EndGetRequestStream(r);
                        StreamWriter writer = new StreamWriter(stream);
                        writer.Write(sparqlUpdate);
                        writer.Close();

                        Tools.HttpDebugRequest(request);

                        request.BeginGetResponse(r2 =>
                        {
                            try
                            {
                                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(r2);
                                Tools.HttpDebugResponse(response);
                                // If we get here without erroring then the request was OK
                                response.Close();
                                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlUpdate, sparqlUpdate), state);
                            }
                            catch (WebException webEx)
                            {
                                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlUpdate, sparqlUpdate, StorageHelper.HandleHttpError(webEx, "updating")), state);
                            }
                            catch (Exception ex)
                            {
                                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlUpdate, sparqlUpdate, StorageHelper.HandleError(ex, "updating")), state);
                            }
                        }, state);
                    }
                    catch (WebException webEx)
                    {
                        callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlUpdate, sparqlUpdate, StorageHelper.HandleHttpError(webEx, "updating")), state);
                    }
                    catch (Exception ex)
                    {
                        callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlUpdate, sparqlUpdate, StorageHelper.HandleError(ex, "updating")), state);
                    }
                }, state);
            }
            catch (WebException webEx)
            {
                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlUpdate, sparqlUpdate, StorageHelper.HandleHttpError(webEx, "updating")), state);
            }
            catch (Exception ex)
            {
                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlUpdate, sparqlUpdate, StorageHelper.HandleError(ex, "updating")), state);
            }
        }
예제 #13
0
        /// <summary>
        /// Updates a Graph in the Fuseki store
        /// </summary>
        /// <param name="graphUri">URI of the Graph to update</param>
        /// <param name="additions">Triples to be added</param>
        /// <param name="removals">Triples to be removed</param>
        public override void UpdateGraph(string graphUri, IEnumerable <Triple> additions, IEnumerable <Triple> removals)
        {
            try
            {
                String        graph  = (graphUri != null && !graphUri.Equals(String.Empty)) ? "GRAPH <" + this._formatter.FormatUri(graphUri) + "> {" : String.Empty;
                StringBuilder update = new StringBuilder();

                if (additions != null)
                {
                    if (additions.Any())
                    {
                        update.AppendLine("INSERT DATA {");
                        if (!graph.Equals(String.Empty))
                        {
                            update.AppendLine(graph);
                        }

                        foreach (Triple t in additions)
                        {
                            update.AppendLine(this._formatter.Format(t));
                        }

                        if (!graph.Equals(String.Empty))
                        {
                            update.AppendLine("}");
                        }
                        update.AppendLine("}");
                    }
                }

                if (removals != null)
                {
                    if (removals.Any())
                    {
                        if (update.Length > 0)
                        {
                            update.AppendLine(";");
                        }

                        update.AppendLine("DELETE DATA {");
                        if (!graph.Equals(String.Empty))
                        {
                            update.AppendLine(graph);
                        }

                        foreach (Triple t in removals)
                        {
                            update.AppendLine(this._formatter.Format(t));
                        }

                        if (!graph.Equals(String.Empty))
                        {
                            update.AppendLine("}");
                        }
                        update.AppendLine("}");
                    }
                }

                if (update.Length > 0)
                {
                    // Make the SPARQL Update Request
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this._updateUri);
                    request.Method      = "POST";
                    request.ContentType = "application/sparql-update";
                    request             = base.ApplyRequestOptions(request);

                    Tools.HttpDebugRequest(request);

                    StreamWriter writer = new StreamWriter(request.GetRequestStream());
                    writer.Write(update.ToString());
                    writer.Close();

                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        Tools.HttpDebugResponse(response);

                        // If we get here without erroring then the request was OK
                        response.Close();
                    }
                }
            }
            catch (WebException webEx)
            {
                throw StorageHelper.HandleHttpError(webEx, "updating a Graph in");
            }
        }
예제 #14
0
        /// <summary>
        /// Updates the store asynchronously
        /// </summary>
        /// <param name="sparqlUpdates">SPARQL Update</param>
        /// <param name="callback">Callback</param>
        /// <param name="state">State to pass to the callback</param>
        public void Update(string sparqlUpdates, AsyncStorageCallback callback, object state)
        {
            try
            {
                HttpWebRequest request = this.CreateRequest("/sparql", MimeTypesHelper.HttpSparqlAcceptHeader, "POST", null);
                request.BeginGetRequestStream(r =>
                {
                    try
                    {
                        Stream stream = request.EndGetRequestStream(r);
                        using (StreamWriter writer = new StreamWriter(stream))
                        {
                            writer.Write("query=");
                            writer.Write(HttpUtility.UrlEncode(sparqlUpdates));
                            writer.Close();
                        }
                        Tools.HttpDebugRequest(request);

                        request.BeginGetResponse(r2 =>
                        {
                            try
                            {
                                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(r2);
                                Tools.HttpDebugResponse(response);
                                //If we get here then it completed OK
                                response.Close();
                            }
                            catch (WebException webEx)
                            {
                                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, StorageHelper.HandleHttpError(webEx, "updating")), state);
                            }
                            catch (Exception ex)
                            {
                                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, StorageHelper.HandleError(ex, "updating")), state);
                            }
                        }, state);
                    }
                    catch (WebException webEx)
                    {
                        callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, StorageHelper.HandleHttpError(webEx, "updating")), state);
                    }
                    catch (Exception ex)
                    {
                        callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, StorageHelper.HandleError(ex, "updating")), state);
                    }
                }, state);
            }
            catch (WebException webEx)
            {
                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, StorageHelper.HandleHttpError(webEx, "updating")), state);
            }
            catch (Exception ex)
            {
                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.SparqlQueryWithHandler, StorageHelper.HandleError(ex, "updating")), state);
            }
        }
예제 #15
0
        /// <summary>
        /// Lists the Graphs from the Repository
        /// </summary>
        /// <param name="callback">Callback</param>
        /// <param name="state">State to pass to the callback</param>
        /// <returns></returns>
        public override void ListGraphs(AsyncStorageCallback callback, Object state)
        {
            try
            {
                //Use the /contexts method to get the Graph URIs
                //HACK: Have to use SPARQL JSON as currently Dydra's SPARQL XML Results are malformed
                HttpWebRequest request = this.CreateRequest("/contexts", MimeTypesHelper.CustomHttpAcceptHeader(MimeTypesHelper.SparqlResultsJson), "GET", new Dictionary <string, string>());
                request.BeginGetResponse(r =>
                {
                    try
                    {
                        HttpWebResponse response    = (HttpWebResponse)request.EndGetResponse(r);
                        ISparqlResultsReader parser = MimeTypesHelper.GetSparqlParser(response.ContentType);
                        ListUrisHandler handler     = new ListUrisHandler("contextID");
                        parser.Load(handler, new StreamReader(response.GetResponseStream()));
                        response.Close();

                        callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.ListGraphs, handler.Uris), state);
                    }
                    catch (WebException webEx)
                    {
                        callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.ListGraphs, StorageHelper.HandleHttpError(webEx, "list Graphs asynchronously from")), state);
                    }
                    catch (Exception ex)
                    {
                        callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.ListGraphs, StorageHelper.HandleError(ex, "list Graphs asynchronously from")), state);
                    }
                }, state);
            }
            catch (WebException webEx)
            {
                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.ListGraphs, StorageHelper.HandleHttpError(webEx, "list Graphs asynchronously from")), state);
            }
            catch (Exception ex)
            {
                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.ListGraphs, StorageHelper.HandleError(ex, "list Graphs asynchronously from")), state);
            }
        }