/// <summary> /// Extracts the Graph which comprises the class hierarchy /// </summary> public IGraph Classify() { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.Endpoint.Uri); request.Method = this.Endpoint.HttpMethods.First(); request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(this.MimeTypes, MimeTypesHelper.SupportedRdfMimeTypes); Tools.HttpDebugRequest(request); try { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { Tools.HttpDebugResponse(response); IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType); Graph g = new Graph(); parser.Load(g, new StreamReader(response.GetResponseStream())); response.Close(); return(g); } } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } throw new RdfReasoningException("A HTTP error occurred while communicating with the Pellet Server", webEx); } }
/// <summary> /// Gets the Graph which comprises the class hierarchy and individuals of those classes /// </summary> /// <param name="callback">Callback to invoke when the operation completes</param> /// <param name="state">State to pass to the callback</param> public void Realize(GraphCallback callback, Object state) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.Endpoint.Uri); request.Method = this.Endpoint.HttpMethods.First(); request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(this.MimeTypes, MimeTypesHelper.SupportedRdfMimeTypes); #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugRequest(request); } #endif request.BeginGetResponse(result => { using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result)) { #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugResponse(response); } #endif IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType); Graph g = new Graph(); parser.Load(g, new StreamReader(response.GetResponseStream())); response.Close(); callback(g, state); } }, null); }
/// <summary> /// Gets the raw Cluster Graph for the Knowledge Base /// </summary> /// <param name="number">Number of Clusters</param> /// <returns></returns> public IGraph ClusterRaw(int number) { if (number < 2) { throw new RdfReasoningException("Pellet Server requires the number of Clusters to be at least 2"); } String requestUri = this._clusterUri + number + "/"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri); request.Method = this.Endpoint.HttpMethods.First(); request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(this.MimeTypes.Where(type => !type.Equals("text/json")), MimeTypesHelper.SupportedRdfMimeTypes); Tools.HttpDebugRequest(request); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { Tools.HttpDebugResponse(response); IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType); Graph g = new Graph(); parser.Load(g, new StreamReader(response.GetResponseStream())); response.Close(); return(g); } }
/// <summary> /// Extracts an RDF Dataset which details the Constraints violated (if any) and whether Constraints are satisified /// </summary> /// <param name="callback">Callback to invoke when the operation completes</param> /// <param name="state">State to pass to the callback</param> public void Validate(TripleStoreCallback callback, Object state) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.Endpoint.Uri); request.Method = this.Endpoint.HttpMethods.First(); request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(this.MimeTypes, MimeTypesHelper.SupportedRdfDatasetMimeTypes); #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugRequest(request); } #endif request.BeginGetResponse(result => { using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result)) { #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugResponse(response); } #endif IStoreReader parser = MimeTypesHelper.GetStoreParser(response.ContentType); TripleStore store = new TripleStore(); StreamParams parameters = new StreamParams(response.GetResponseStream()); parser.Load(store, parameters); response.Close(); callback(store, state); } }, null); }
/// <summary> /// Gets the raw Similarity Graph for the Knowledge Base /// </summary> /// <param name="number">Number of Similar Individuals</param> /// <param name="individual">QName of a Individual to find Similar Individuals to</param> /// <param name="callback">Callback to invoke when the operation completes</param> /// <param name="state">State to pass to the callback</param> /// <remarks> /// If the operation succeeds the callback will be invoked normally, if there is an error the callback will be invoked with a instance of <see cref="AsyncError"/> passed as the state which provides access to the error message and the original state passed in. /// </remarks> public void SimilarityRaw(int number, String individual, GraphCallback callback, Object state) { if (number < 1) { throw new RdfReasoningException("Pellet Server requires the number of Similar Individuals to be at least 1"); } String requestUri = _similarityUri + number + "/" + individual; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri); request.Method = Endpoint.HttpMethods.First(); request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(MimeTypes.Where(t => !t.Equals("text/json")), MimeTypesHelper.SupportedRdfMimeTypes); Tools.HttpDebugRequest(request); try { request.BeginGetResponse(result => { try { using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result)) { Tools.HttpDebugResponse(response); IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType); Graph g = new Graph(); parser.Load(g, new StreamReader(response.GetResponseStream())); response.Close(); callback(g, state); } } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } callback(null, new AsyncError(new RdfReasoningException("A HTTP error occurred while communicating with the Pellet Server, see inner exception for details", webEx), state)); } catch (Exception ex) { callback(null, new AsyncError(new RdfReasoningException("An unexpected error occurred while communicating with the Pellet Server, see inner exception for details", ex), state)); } }, null); } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } callback(null, new AsyncError(new RdfReasoningException("A HTTP error occurred while communicating with the Pellet Server, see inner exception for details", webEx), state)); } catch (Exception ex) { callback(null, new AsyncError(new RdfReasoningException("An unexpected error occurred while communicating with the Pellet Server, see inner exception for details", ex), state)); } }
/// <summary> /// Gets the Raw Predictions Graph from the Knowledge Base /// </summary> /// <param name="individual">QName of an Individual</param> /// <param name="property">QName of a Property</param> /// <param name="callback">Callback to invoke when the operation completes</param> /// <param name="state">State to pass to the callback</param> public void PredictRaw(String individual, String property, GraphCallback callback, Object state) { String requestUri = this._predictUri + individual + "/" + property; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri); request.Method = this.Endpoint.HttpMethods.First(); request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(this.MimeTypes.Where(t => !t.Equals("text/json")), MimeTypesHelper.SupportedRdfMimeTypes); #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugRequest(request); } #endif request.BeginGetResponse(result => { using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result)) { #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugResponse(response); } #endif IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType); Graph g = new Graph(); parser.Load(g, new StreamReader(response.GetResponseStream())); response.Close(); callback(g, state); } }, null); }
/// <summary> /// Gets the raw Cluster Graph for the Knowledge Base /// </summary> /// <param name="number">Number of Clusters</param> /// <param name="type">QName of a Type to Cluster around</param> /// <param name="callback">Callback to be invoked when the operation completes</param> /// <param name="state">State to be passed to the callback</param> public void ClusterRaw(int number, String type, GraphCallback callback, Object state) { if (number < 2) { throw new RdfReasoningException("Pellet Server requires the number of Clusters to be at least 2"); } String requestUri = this._clusterUri + number + "/" + type; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri); request.Method = this.Endpoint.HttpMethods.First(); request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(this.MimeTypes.Where(t => !t.Equals("text/json")), MimeTypesHelper.SupportedRdfMimeTypes); Tools.HttpDebugRequest(request); request.BeginGetResponse(result => { using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result)) { Tools.HttpDebugResponse(response); IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType); Graph g = new Graph(); parser.Load(g, new StreamReader(response.GetResponseStream())); response.Close(); callback(g, state); } }, null); }
/// <summary> /// Extracts an RDF Dataset which details the Constraints violated (if any) and whether Constraints are satisified. /// </summary> /// <returns></returns> public ITripleStore Validate() { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Endpoint.Uri); request.Method = Endpoint.HttpMethods.First(); request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(MimeTypes, MimeTypesHelper.SupportedRdfDatasetMimeTypes); Tools.HttpDebugRequest(request); try { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { Tools.HttpDebugResponse(response); IStoreReader parser = MimeTypesHelper.GetStoreParser(response.ContentType); TripleStore store = new TripleStore(); parser.Load(store, new StreamReader(response.GetResponseStream())); response.Close(); return(store); } } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } throw new RdfReasoningException("A HTTP error occurred while communicating with the Pellet Server", webEx); } }
/// <summary> /// Gets the raw Similarity Graph for the Knowledge Base /// </summary> /// <param name="number">Number of Similar Individuals</param> /// <param name="individual">QName of a Individual to find Similar Individuals to</param> /// <returns></returns> public IGraph SimilarityRaw(int number, String individual) { if (number < 1) { throw new RdfReasoningException("Pellet Server requires the number of Similar Individuals to be at least 1"); } String requestUri = _similarityUri + number + "/" + individual; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri); request.Method = Endpoint.HttpMethods.First(); request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(MimeTypes.Where(t => !t.Equals("text/json")), MimeTypesHelper.SupportedRdfMimeTypes); Tools.HttpDebugRequest(request); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { Tools.HttpDebugResponse(response); IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType); Graph g = new Graph(); parser.Load(g, new StreamReader(response.GetResponseStream())); response.Close(); return(g); } }
/// <summary> /// Gets the Raw Predictions Graph from the Knowledge Base. /// </summary> /// <param name="individual">QName of an Individual.</param> /// <param name="property">QName of a Property.</param> /// <returns></returns> public IGraph PredictRaw(String individual, String property) { String requestUri = _predictUri + individual + "/" + property; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri); request.Method = Endpoint.HttpMethods.First(); request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(MimeTypes.Where(t => !t.Equals("text/json")), MimeTypesHelper.SupportedRdfMimeTypes); Tools.HttpDebugRequest(request); try { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { Tools.HttpDebugResponse(response); IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType); Graph g = new Graph(); parser.Load(g, new StreamReader(response.GetResponseStream())); response.Close(); return(g); } } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } throw new RdfReasoningException("A HTTP error occurred while communicating with the Pellet Server", webEx); } }
/// <summary> /// Extracts the Graph which comprises the class hierarchy. /// </summary> /// <param name="callback">Callback for when the operation completes.</param> /// <param name="state">State to be passed to the callback.</param> /// <remarks> /// If the operation succeeds the callback will be invoked normally, if there is an error the callback will be invoked with a instance of <see cref="AsyncError"/> passed as the state which provides access to the error message and the original state passed in. /// </remarks> public void Classify(GraphCallback callback, Object state) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Endpoint.Uri); request.Method = Endpoint.HttpMethods.First(); request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(MimeTypes, MimeTypesHelper.SupportedRdfMimeTypes); Tools.HttpDebugRequest(request); try { request.BeginGetResponse(result => { try { using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result)) { Tools.HttpDebugResponse(response); IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType); Graph g = new Graph(); parser.Load(g, new StreamReader(response.GetResponseStream())); response.Close(); callback(g, state); } } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } callback(null, new AsyncError(new RdfReasoningException("A HTTP error occurred while communicating with the Pellet Server, see inner exception for details", webEx), state)); } catch (Exception ex) { callback(null, new AsyncError(new RdfReasoningException("An unexpected error occurred while communicating with the Pellet Server, see inner exception for details", ex), state)); } }, null); } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } callback(null, new AsyncError(new RdfReasoningException("A HTTP error occurred while communicating with the Pellet Server, see inner exception for details", webEx), state)); } catch (Exception ex) { callback(null, new AsyncError(new RdfReasoningException("An unexpected error occurred while communicating with the Pellet Server, see inner exception for details", ex), state)); } }
/// <summary> /// Gets the raw Cluster Graph for the Knowledge Base /// </summary> /// <param name="number">Number of Clusters</param> /// <param name="type">QName of a Type to Cluster around</param> /// <returns></returns> public IGraph ClusterRaw(int number, String type) { if (number < 2) { throw new RdfReasoningException("Pellet Server requires the number of Clusters to be at least 2"); } String requestUri = this._clusterUri + number + "/" + type; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri); request.Method = this.Endpoint.HttpMethods.First(); request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(this.MimeTypes.Where(t => !t.Equals("text/json")), MimeTypesHelper.SupportedRdfMimeTypes); #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugRequest(request); } #endif try { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugResponse(response); } #endif IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType); Graph g = new Graph(); parser.Load(g, new StreamReader(response.GetResponseStream())); response.Close(); return(g); } } catch (WebException webEx) { #if DEBUG if (Options.HttpDebugging) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } } #endif throw new RdfReasoningException("A HTTP error occurred while communicating with the Pellet Server", webEx); } }
/// <summary> /// Gets a Graph explaining the result of the SPARQL Query /// </summary> /// <param name="sparqlQuery">SPARQL Query</param> /// <returns></returns> public IGraph Explain(String sparqlQuery) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this._explainUri + "?query=" + HttpUtility.UrlEncode(sparqlQuery)); request.Method = this.Endpoint.HttpMethods.First(); request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(this.MimeTypes, MimeTypesHelper.SupportedRdfMimeTypes); #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugRequest(request); } #endif try { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugResponse(response); } #endif IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType); Graph g = new Graph(); parser.Load(g, new StreamReader(response.GetResponseStream())); response.Close(); return(g); } } catch (WebException webEx) { #if DEBUG if (Options.HttpDebugging) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } } #endif throw new RdfReasoningException("A HTTP error occurred while communicating with the Pellet Server", webEx); } }
/// <summary> /// Makes a Query to a SPARQL Endpoint and returns the raw Response. /// </summary> /// <param name="sparqlQuery">SPARQL Query String.</param> /// <param name="mimeTypes">MIME Types to use for the Accept Header.</param> /// <returns></returns> public virtual HttpWebResponse QueryRaw(String sparqlQuery, String[] mimeTypes) { try { // Make the Query return(QueryInternal(sparqlQuery, MimeTypesHelper.CustomHttpAcceptHeader(mimeTypes))); } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } // Some sort of HTTP Error occurred throw new RdfQueryException("A HTTP Error occurred while trying to make the SPARQL Query", webEx); } }
/// <summary> /// Lists the Graphs from the Repository /// </summary> /// <returns></returns> public IEnumerable <Uri> ListGraphs() { 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.SparqlJson), "GET", new Dictionary <string, string>()); SparqlResultSet results = new SparqlResultSet(); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { ISparqlResultsReader parser = MimeTypesHelper.GetSparqlParser(response.ContentType); parser.Load(results, new StreamReader(response.GetResponseStream())); response.Close(); } List <Uri> graphUris = new List <Uri>(); foreach (SparqlResult r in results) { if (r.HasValue("contextID")) { INode value = r["contextID"]; if (value.NodeType == NodeType.Uri) { graphUris.Add(((IUriNode)value).Uri); } else if (value.NodeType == NodeType.Blank) { //Dydra allows BNode Graph URIs graphUris.Add(new Uri("dydra:bnode:" + ((IBlankNode)value).InternalID)); } } } return(graphUris); } catch (Exception ex) { throw new RdfStorageException("An error occurred while attempting to retrieve the Graph List from the Store, see inner exception for details", ex); } }
/// <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); } }
/// <summary> /// Gets a Graph explaining the result of the SPARQL Query /// </summary> /// <param name="sparqlQuery">SPARQL Query</param> /// <param name="callback">Callback to invoke when the operation completes</param> /// <param name="state">State to pass to the callback</param> /// <returns></returns> public void Explain(String sparqlQuery, GraphCallback callback, Object state) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this._explainUri + "?query=" + HttpUtility.UrlEncode(sparqlQuery)); request.Method = this.Endpoint.HttpMethods.First(); request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(this.MimeTypes, MimeTypesHelper.SupportedRdfMimeTypes); Tools.HttpDebugRequest(request); request.BeginGetResponse(result => { using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result)) { Tools.HttpDebugResponse(response); IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType); Graph g = new Graph(); parser.Load(g, new StreamReader(response.GetResponseStream())); response.Close(); callback(g, state); } }, null); }
/// <summary> /// Attempts to load a RDF dataset from the given URI using a RDF Handler /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="u">URI to attempt to get a RDF dataset from</param> /// <param name="parser">Parser to use to parse the RDF dataset</param> /// <remarks> /// <para> /// If the <paramref name="parser"/> parameter is set to null then this method attempts to select the relevant Store Parser based on the Content Type header returned in the HTTP Response. /// </para> /// <para> /// If you know ahead of time the Content Type you can explicitly pass in the parser to use. /// </para> /// </remarks> public static void Load(IRdfHandler handler, Uri u, IStoreReader parser) { if (u == null) { throw new RdfParseException("Cannot read a RDF dataset from a null URI"); } if (handler == null) { throw new RdfParseException("Cannot read a RDF dataset using a null RDF handler"); } try { #if SILVERLIGHT if (u.IsFile()) #else if (u.IsFile) #endif { //Invoke FileLoader instead RaiseWarning("This is a file: URI so invoking the FileLoader instead"); if (Path.DirectorySeparatorChar == '/') { FileLoader.Load(handler, u.ToString().Substring(7), parser); } else { FileLoader.Load(handler, u.ToString().Substring(8), parser); } return; } //Sanitise the URI to remove any Fragment ID u = Tools.StripUriFragment(u); //Set-up the Request HttpWebRequest httpRequest; httpRequest = (HttpWebRequest)WebRequest.Create(u); //Want to ask for TriG, NQuads or TriX if (parser != null) { //If a non-null parser set up a HTTP Header that is just for the given parser httpRequest.Accept = MimeTypesHelper.CustomHttpAcceptHeader(parser); } else { httpRequest.Accept = MimeTypesHelper.HttpRdfDatasetAcceptHeader; } //Use HTTP GET httpRequest.Method = "GET"; #if !SILVERLIGHT httpRequest.Timeout = Options.UriLoaderTimeout; #endif if (_userAgent != null && !_userAgent.Equals(String.Empty)) { httpRequest.UserAgent = _userAgent; } #if DEBUG //HTTP Debugging if (Options.HttpDebugging) { Tools.HttpDebugRequest(httpRequest); } #endif using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse()) { #if DEBUG //HTTP Debugging if (Options.HttpDebugging) { Tools.HttpDebugResponse(httpResponse); } #endif //Get a Parser and Load the RDF if (parser == null) { try { parser = MimeTypesHelper.GetStoreParser(httpResponse.ContentType); parser.Warning += RaiseStoreWarning; parser.Load(handler, new StreamParams(httpResponse.GetResponseStream())); } catch (RdfParserSelectionException selEx) { String data = new StreamReader(httpResponse.GetResponseStream()).ReadToEnd(); parser = StringParser.GetDatasetParser(data); parser.Warning += RaiseStoreWarning; parser.Load(handler, new TextReaderParams(new StringReader(data))); } } else { parser.Warning += RaiseStoreWarning; parser.Load(handler, new StreamParams(httpResponse.GetResponseStream())); } } } catch (UriFormatException uriEx) { //Uri Format Invalid throw new RdfException("Unable to load from the given URI '" + u.ToString() + "' since it's format was invalid, see inner exception for details", uriEx); } catch (WebException webEx) { #if DEBUG if (Options.HttpDebugging) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } } #endif //Some sort of HTTP Error occurred throw new WebException("A HTTP Error occurred resolving the URI '" + u.ToString() + "', see innner exception for details", webEx); } }
/// <summary> /// Attempts to load a RDF Graph from the given URI using a RDF Handler /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="u">URI to attempt to get RDF from</param> /// <param name="parser">Parser to use</param> /// <remarks> /// <para> /// Uses the supplied parser to attempt parsing regardless of the actual Content Type returned /// </para> /// <para> /// In the event that the URI is a File URI the <see cref="FileLoader">FileLoader</see> will be used instead /// </para> /// <para> /// If the URI is a Data URI then the <see cref="DataUriLoader">DataUriLoader</see> will be used instead. /// </para> /// </remarks> public static void Load(IRdfHandler handler, Uri u, IRdfReader parser) { if (handler == null) { throw new RdfParseException("Cannot read RDF using a null RDF Handler"); } if (u == null) { throw new RdfParseException("Cannot load RDF from a null URI"); } try { #if SILVERLIGHT if (u.IsFile()) #else if (u.IsFile) #endif { //Invoke FileLoader instead RaiseWarning("This is a file: URI so invoking the FileLoader instead"); if (Path.DirectorySeparatorChar == '/') { FileLoader.Load(handler, u.ToString().Substring(7), parser); } else { FileLoader.Load(handler, u.ToString().Substring(8), parser); } return; } if (u.Scheme.Equals("data")) { //Invoke DataUriLoader instead RaiseWarning("This is a data: URI so invoking the DataUriLoader instead"); DataUriLoader.Load(handler, u); return; } //Sanitise the URI to remove any Fragment ID u = Tools.StripUriFragment(u); #if !NO_URICACHE //Use Cache if possible String etag = String.Empty; String local = null; if (Options.UriLoaderCaching) { if (_cache.HasETag(u)) { //Get the ETag and then we'll include an If-None-Match header in our request etag = _cache.GetETag(u); } else if (_cache.HasLocalCopy(u, true)) { //Just try loading from the local copy local = _cache.GetLocalCopy(u); if (local != null) { try { FileLoader.Load(handler, local, new TurtleParser()); } catch { //If we get an Exception we failed to access the file successfully _cache.RemoveETag(u); _cache.RemoveLocalCopy(u); UriLoader.Load(handler, u, parser); } return; } } } #endif //Set-up the Request HttpWebRequest httpRequest; httpRequest = (HttpWebRequest)WebRequest.Create(u); //Want to ask for RDF formats if (parser != null) { //If a non-null parser set up a HTTP Header that is just for the given parser httpRequest.Accept = MimeTypesHelper.CustomHttpAcceptHeader(parser); } else { httpRequest.Accept = MimeTypesHelper.HttpAcceptHeader; } #if !NO_URICACHE if (Options.UriLoaderCaching) { if (!etag.Equals(String.Empty)) { httpRequest.Headers.Add(HttpRequestHeader.IfNoneMatch, etag); } } #endif //Use HTTP GET httpRequest.Method = "GET"; #if !SILVERLIGHT httpRequest.Timeout = Options.UriLoaderTimeout; #endif if (_userAgent != null && !_userAgent.Equals(String.Empty)) { httpRequest.UserAgent = _userAgent; } #if DEBUG //HTTP Debugging if (Options.HttpDebugging) { Tools.HttpDebugRequest(httpRequest); } #endif using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse()) { #if DEBUG //HTTP Debugging if (Options.HttpDebugging) { Tools.HttpDebugResponse(httpResponse); } #endif #if !NO_URICACHE if (Options.UriLoaderCaching) { //Are we using ETag based caching? if (!etag.Equals(String.Empty)) { //Did we get a Not-Modified response? if (httpResponse.StatusCode == HttpStatusCode.NotModified) { //If so then we need to load the Local Copy assuming it exists? if (_cache.HasLocalCopy(u, false)) { local = _cache.GetLocalCopy(u); try { FileLoader.Load(handler, local, new TurtleParser()); } catch { //If we get an Exception we failed to access the file successfully _cache.RemoveETag(u); _cache.RemoveLocalCopy(u); UriLoader.Load(handler, u, parser); } return; } else { //If the local copy didn't exist then we need to redo the response without //the ETag as we've lost the cached copy somehow _cache.RemoveETag(u); UriLoader.Load(handler, u, parser); return; } } //If we didn't get a Not-Modified response then we'll continue and parse the new response } } #endif //Get a Parser and Load the RDF if (parser == null) { //Only need to auto-detect the parser if a specific one wasn't specified parser = MimeTypesHelper.GetParser(httpResponse.ContentType); } parser.Warning += RaiseWarning; #if !NO_URICACHE //To do caching we ask the cache to give us a handler and then we tie it to IRdfHandler cacheHandler = _cache.ToCache(u, Tools.StripUriFragment(httpResponse.ResponseUri), httpResponse.Headers["ETag"]); if (cacheHandler != null) { //Note: We can ONLY use caching when we know that the Handler will accept all the data returned //i.e. if the Handler may trim the data in some way then we shouldn't cache the data returned if (handler.AcceptsAll) { handler = new MultiHandler(new IRdfHandler[] { handler, cacheHandler }); } else { cacheHandler = null; } } try { #endif parser.Load(handler, new StreamReader(httpResponse.GetResponseStream())); #if !NO_URICACHE } catch { //If we were trying to cache the response and something went wrong discard the cached copy _cache.RemoveETag(u); _cache.RemoveETag(Tools.StripUriFragment(httpResponse.ResponseUri)); _cache.RemoveLocalCopy(u); _cache.RemoveLocalCopy(Tools.StripUriFragment(httpResponse.ResponseUri)); } #endif } } catch (UriFormatException uriEx) { //Uri Format Invalid throw new RdfParseException("Unable to load from the given URI '" + u.ToString() + "' since it's format was invalid", uriEx); } catch (WebException webEx) { #if DEBUG if (webEx.Response != null && Options.HttpDebugging) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } #endif #if !NO_URICACHE if (webEx.Response != null) { if (((HttpWebResponse)webEx.Response).StatusCode == HttpStatusCode.NotModified) { //If so then we need to load the Local Copy assuming it exists? if (_cache.HasLocalCopy(u, false)) { String local = _cache.GetLocalCopy(u); try { FileLoader.Load(handler, local, new TurtleParser()); } catch { //If we get an Exception we failed to access the file successfully _cache.RemoveETag(u); _cache.RemoveLocalCopy(u); UriLoader.Load(handler, u, parser); } return; } else { //If the local copy didn't exist then we need to redo the response without //the ETag as we've lost the cached copy somehow _cache.RemoveETag(u); UriLoader.Load(handler, u, parser); return; } } } #endif //Some sort of HTTP Error occurred throw new WebException("A HTTP Error occurred resolving the URI '" + u.ToString() + "'", webEx); } }
/// <summary> /// Attempts to load a RDF dataset asynchronously from the given URI using a RDF Handler. /// </summary> /// <param name="handler">RDF Handler to use.</param> /// <param name="u">URI to attempt to get a RDF dataset from.</param> /// <param name="parser">Parser to use to parse the RDF dataset.</param> /// <param name="callback">Callback to invoke when the operation completes.</param> /// <param name="state">State to pass to the callback.</param> /// <remarks> /// <para> /// If the <paramref name="parser"/> parameter is set to null then this method attempts to select the relevant Store Parser based on the Content Type header returned in the HTTP Response. /// </para> /// <para> /// If you know ahead of time the Content Type you can explicitly pass in the parser to use. /// </para> /// <para> /// If the loading completes normally the callback will be invoked normally, if an error occurs it will be invoked and passed an instance of <see cref="AsyncError"/> as the state which contains details of the error and the original state. /// </para> /// </remarks> public static void Load(IRdfHandler handler, Uri u, IStoreReader parser, RdfHandlerCallback callback, Object state) { if (u == null) { throw new RdfParseException("Cannot read a RDF dataset from a null URI"); } if (handler == null) { throw new RdfParseException("Cannot read a RDF dataset using a null RDF handler"); } try { if (u.IsFile) { // Invoke FileLoader instead RaiseWarning("This is a file: URI so invoking the FileLoader instead"); if (Path.DirectorySeparatorChar == '/') { FileLoader.Load(handler, u.AbsoluteUri.Substring(7), parser); } else { FileLoader.Load(handler, u.AbsoluteUri.Substring(8), parser); } // FileLoader.Load() will run synchronously so once this completes we can invoke the callback callback(handler, state); return; } if (u.Scheme.Equals("data")) { // Invoke DataUriLoader instead RaiseWarning("This is a data: URI so invoking the DataUriLoader instead"); DataUriLoader.Load(handler, u); // After DataUriLoader.Load() has run (which happens synchronously) can invoke the callback callback(handler, state); return; } // Sanitise the URI to remove any Fragment ID u = Tools.StripUriFragment(u); // Setup the Request HttpWebRequest request = (HttpWebRequest)WebRequest.Create(u); // Want to ask for RDF dataset formats if (parser != null) { // If a non-null parser set up a HTTP Header that is just for the given parser request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(parser); } else { request.Accept = MimeTypesHelper.HttpAcceptHeader; } // Use HTTP GET request.Method = "GET"; #if !NETCORE request.Timeout = Options.UriLoaderTimeout; #endif if (_userAgent != null && !_userAgent.Equals(String.Empty)) { #if NETCORE request.Headers[HttpRequestHeader.UserAgent] = _userAgent; #else request.UserAgent = _userAgent; #endif } Tools.HttpDebugRequest(request); try { request.BeginGetResponse(result => { try { using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result)) { Tools.HttpDebugResponse(response); // Get a Parser and load the RDF if (parser == null) { try { // Only need to auto-detect the parser if a specific one wasn't specified parser = MimeTypesHelper.GetStoreParser(response.ContentType); parser.Warning += RaiseWarning; parser.Load(handler, new StreamReader(response.GetResponseStream())); } catch (RdfParserSelectionException) { RaiseStoreWarning("Unable to select a RDF Dataset parser based on Content-Type: " + response.ContentType + " - seeing if the content is an RDF Graph instead"); try { // If not a RDF Dataset format see if it is a Graph IRdfReader rdfParser = MimeTypesHelper.GetParser(response.ContentType); rdfParser.Load(handler, new StreamReader(response.GetResponseStream())); } catch (RdfParserSelectionException) { String data = new StreamReader(response.GetResponseStream()).ReadToEnd(); parser = StringParser.GetDatasetParser(data); parser.Warning += RaiseStoreWarning; parser.Load(handler, new StringReader(data)); } } } else { parser.Warning += RaiseStoreWarning; parser.Load(handler, new StreamReader(response.GetResponseStream())); } // Finally can invoke the callback callback(handler, state); } } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } callback(handler, new AsyncError(new RdfParseException("A HTTP Error occurred loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exeption for details", webEx), state)); } catch (Exception ex) { callback(handler, new AsyncError(new RdfParseException("Unexpected error while loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exception for details", ex), state)); } }, null); } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } callback(handler, new AsyncError(new RdfParseException("A HTTP Error occurred loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exeption for details", webEx), state)); } catch (Exception ex) { callback(handler, new AsyncError(new RdfParseException("Unexpected error while loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exception for details", ex), state)); } } catch (UriFormatException uriEx) { // Uri Format Invalid throw new RdfException("Unable to load from the given URI '" + u.AbsoluteUri + "' since it's format was invalid, see inner exception for details", uriEx); } }
/// <summary> /// Makes a SPARQL Query against the underlying Store using whatever reasoning mode is currently in-use processing the results using an appropriate handler from those provided /// </summary> /// <param name="rdfHandler">RDF Handler</param> /// <param name="resultsHandler">Results Handler</param> /// <param name="sparqlQuery">SPARQL Query</param> /// <returns></returns> public void Query(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, String sparqlQuery) { try { HttpWebRequest request; String tID = (this._activeTrans == null) ? String.Empty : "/" + this._activeTrans; //String accept = MimeTypesHelper.HttpRdfOrSparqlAcceptHeader; String accept = MimeTypesHelper.CustomHttpAcceptHeader(MimeTypesHelper.SparqlResultsXml.Concat(MimeTypesHelper.Definitions.Where(d => d.CanParseRdf).SelectMany(d => d.MimeTypes))); //Create the Request Dictionary <String, String> queryParams = new Dictionary <string, string>(); if (sparqlQuery.Length < 2048) { queryParams.Add("query", sparqlQuery); request = this.CreateRequest(this._kb + tID + "/query", accept, "GET", queryParams); } else { request = this.CreateRequest(this._kb + tID + "/query", accept, "POST", queryParams); //Build the Post Data and add to the Request Body request.ContentType = MimeTypesHelper.WWWFormURLEncoded; StringBuilder postData = new StringBuilder(); postData.Append("query="); postData.Append(Uri.EscapeDataString(sparqlQuery)); StreamWriter writer = new StreamWriter(request.GetRequestStream()); writer.Write(postData); writer.Close(); } #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugRequest(request); } #endif //Get the Response and process based on the Content Type using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugResponse(response); } #endif StreamReader data = new StreamReader(response.GetResponseStream()); String ctype = response.ContentType; try { //Is the Content Type referring to a Sparql Result Set format? ISparqlResultsReader resreader = MimeTypesHelper.GetSparqlParser(ctype, Regex.IsMatch(sparqlQuery, "ASK", RegexOptions.IgnoreCase)); resreader.Load(resultsHandler, data); response.Close(); } catch (RdfParserSelectionException) { //If we get a Parser Selection exception then the Content Type isn't valid for a Sparql Result Set //Is the Content Type referring to a RDF format? IRdfReader rdfreader = MimeTypesHelper.GetParser(ctype); rdfreader.Load(rdfHandler, data); response.Close(); } } } catch (WebException webEx) { if (webEx.Response != null) { #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } #endif if (webEx.Response.ContentLength > 0) { try { String responseText = new StreamReader(webEx.Response.GetResponseStream()).ReadToEnd(); throw new RdfQueryException("A HTTP error occured while querying the Store. Store returned the following error message: " + responseText, webEx); } catch { throw new RdfQueryException("A HTTP error occurred while querying the Store", webEx); } } else { throw new RdfQueryException("A HTTP error occurred while querying the Store", webEx); } } else { throw new RdfQueryException("A HTTP error occurred while querying the Store", webEx); } } }
/// <summary> /// Attempts to load a RDF Graph from a URI asynchronously using an RDF Handler /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="u">URI to load from</param> /// <param name="parser">Parser to use</param> /// <param name="callback">Callback to invoke when the operation completes</param> /// <param name="state">State to pass to the callback</param> /// <remarks> /// <para> /// Uses the supplied parser to attempt parsing regardless of the actual Content Type returned /// </para> /// <para> /// In the event that the URI is a File URI the <see cref="FileLoader">FileLoader</see> will be used instead /// </para> /// <para> /// If the URI is a Data URI then the <see cref="DataUriLoader">DataUriLoader</see> will be used instead. /// </para> /// <para> /// If the loading completes normally the callback will be invoked normally, if an error occurs it will be invoked and passed an instance of <see cref="AsyncError"/> as the state which contains details of the error and the original state. /// </para> /// </remarks> public static void Load(IRdfHandler handler, Uri u, IRdfReader parser, RdfHandlerCallback callback, Object state) { if (handler == null) { throw new RdfParseException("Cannot read RDF using a null RDF Handler"); } if (u == null) { throw new RdfParseException("Cannot load RDF from a null URI"); } try { #if SILVERLIGHT if (u.IsFile()) #else if (u.IsFile) #endif { #if PORTABLE throw new PlatformNotSupportedException("FileLoader is not supported by the Portable Class Library build"); #else //Invoke FileLoader instead RaiseWarning("This is a file: URI so invoking the FileLoader instead"); if (Path.DirectorySeparatorChar == '/') { FileLoader.Load(handler, u.AbsoluteUri.Substring(7), parser); } else { FileLoader.Load(handler, u.AbsoluteUri.Substring(8), parser); } //FileLoader.Load() will run synchronously so once this completes we can invoke the callback callback(handler, state); return; #endif } if (u.Scheme.Equals("data")) { //Invoke DataUriLoader instead RaiseWarning("This is a data: URI so invoking the DataUriLoader instead"); DataUriLoader.Load(handler, u); //After DataUriLoader.Load() has run (which happens synchronously) can invoke the callback callback(handler, state); return; } //Sanitise the URI to remove any Fragment ID u = Tools.StripUriFragment(u); //TODO: Add use of Cache into here, this is tricky because this code is primarily intended for Silverlight where we disable the cache purposefully //Setup the Request HttpWebRequest request = (HttpWebRequest)WebRequest.Create(u); //Want to ask for RDF formats if (parser != null) { //If a non-null parser set up a HTTP Header that is just for the given parser request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(parser); } else { request.Accept = MimeTypesHelper.HttpAcceptHeader; } //Use HTTP GET request.Method = "GET"; #if !SILVERLIGHT request.Timeout = Options.UriLoaderTimeout; #endif if (_userAgent != null && !_userAgent.Equals(String.Empty)) { #if PORTABLE request.Headers[HttpRequestHeader.UserAgent] = _userAgent; #else request.UserAgent = _userAgent; #endif } Tools.HttpDebugRequest(request); try { request.BeginGetResponse(result => { try { using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result)) { Tools.HttpDebugResponse(response); //Get a Parser and load the RDF if (parser == null) { //Only need to auto-detect the parser if a specific one wasn't specified parser = MimeTypesHelper.GetParser(response.ContentType); } parser.Warning += RaiseWarning; parser.Load(handler, new StreamReader(response.GetResponseStream())); //Finally can invoke the callback callback(handler, state); } } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } callback(handler, new AsyncError(new RdfParseException("A HTTP Error occurred loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exeption for details", webEx), state)); } catch (Exception ex) { callback(handler, new AsyncError(new RdfParseException("Unexpected error while loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exception for details", ex), state)); } }, null); } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } callback(handler, new AsyncError(new RdfParseException("A HTTP Error occurred loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exeption for details", webEx), state)); } catch (Exception ex) { callback(handler, new AsyncError(new RdfParseException("Unexpected error while loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exception for details", ex), state)); } } #if PORTABLE catch (FormatException uriEx) #else catch (UriFormatException uriEx) #endif { //URI Format Invalid throw new RdfParseException("Unable to load from the given URI '" + u.AbsoluteUri + "' since it's format was invalid, see inner exception for details", uriEx); } }