/// <summary> /// Processes a SPARQL Query Request /// </summary> /// <param name="context">HTTP Context</param> public void ProcessRequest(HttpContext context) { this._config = this.LoadConfig(context); //Add our Standard Headers HandlerHelper.AddStandardHeaders(context, this._config); if (context.Request.HttpMethod.Equals("OPTIONS")) { //OPTIONS requests always result in the Service Description document IGraph svcDescrip = SparqlServiceDescriber.GetServiceDescription(context, this._config, new Uri(context.Request.Url.AbsoluteUri)); HandlerHelper.SendToClient(context, svcDescrip, this._config); return; } //See if there has been an query submitted String queryText = context.Request.QueryString["query"]; if (queryText == null || queryText.Equals(String.Empty)) { queryText = context.Request.Form["query"]; } //If no Query sent either show Query Form or give a HTTP 400 response if (queryText == null || queryText.Equals(String.Empty)) { //If there is no Query we may return the SPARQL Service Description where appropriate try { //If we might show the Query Form only show the Description if the selected writer is //not a HTML writer MimeTypeDefinition definition = MimeTypesHelper.GetDefinitions(HandlerHelper.GetAcceptTypes(context)).FirstOrDefault(d => d.CanWriteRdf); if (definition != null) { IRdfWriter writer = definition.GetRdfWriter(); if (!this._config.ShowQueryForm || !(writer is IHtmlWriter)) { //If not a HTML Writer selected OR not showing Query Form then show the Service Description Graph //unless an error occurs creating it IGraph serviceDescrip = SparqlServiceDescriber.GetServiceDescription(context, this._config, new Uri(context.Request.Url.AbsoluteUri)); context.Response.ContentType = definition.CanonicalMimeType; context.Response.ContentEncoding = definition.Encoding; writer.Save(serviceDescrip, new StreamWriter(context.Response.OutputStream, definition.Encoding)); return; } } } catch { //Ignore Exceptions - we'll just show the Query Form or return a 400 Bad Request instead } //If a Writer can't be selected then we'll either show the Query Form or return a 400 Bad Request if (this._config.ShowQueryForm) { this.ShowQueryForm(context); } else { context.Response.StatusCode = (int)HttpStatusCode.BadRequest; } return; } //Get Other options associated with this query List<String> userDefaultGraphs = new List<String>(); List<String> userNamedGraphs = new List<String>(); long timeout = 0; bool partialResults = this._config.DefaultPartialResults; //Get the Default Graph URIs (if any) if (context.Request.QueryString["default-graph-uri"] != null) { userDefaultGraphs.AddRange(context.Request.QueryString.GetValues("default-graph-uri")); } else if (context.Request.Form["default-graph-uri"] != null) { userDefaultGraphs.AddRange(context.Request.Form.GetValues("default-graph-uri")); } //Get the Named Graph URIs (if any) if (context.Request.QueryString["named-graph-uri"] != null) { userNamedGraphs.AddRange(context.Request.QueryString.GetValues("named-graph-uri")); } else if (context.Request.Form["named-graph-uri"] != null) { userNamedGraphs.AddRange(context.Request.Form.GetValues("named-graph-uri")); } //Get Timeout setting (if any) if (context.Request.QueryString["timeout"] != null) { if (!Int64.TryParse(context.Request.QueryString["timeout"], out timeout)) { timeout = this._config.DefaultTimeout; } } else if (context.Request.Form["timeout"] != null) { if (!Int64.TryParse(context.Request.Form["timeout"], out timeout)) { timeout = this._config.DefaultTimeout; } } //Get Partial Results Setting (if any); if (context.Request.QueryString["partialResults"] != null) { if (!Boolean.TryParse(context.Request.QueryString["partialResults"], out partialResults)) { partialResults = this._config.DefaultPartialResults; } } else if (context.Request.Form["partialResults"] != null) { if (!Boolean.TryParse(context.Request.Form["partialResults"], out partialResults)) { partialResults = this._config.DefaultPartialResults; } } try { //Now we're going to parse the Query SparqlQueryParser parser = new SparqlQueryParser(this._config.Syntax); parser.ExpressionFactories = this._config.ExpressionFactories; parser.QueryOptimiser = this._config.QueryOptimiser; SparqlQuery query = parser.ParseFromString(queryText); query.AlgebraOptimisers = this._config.AlgebraOptimisers; //Check whether we need to use authentication //If there are no user groups then no authentication is in use so we default to authenticated with no per-action authentication needed bool isAuth = true, requireActionAuth = false; if (this._config.UserGroups.Any()) { //If we have user isAuth = HandlerHelper.IsAuthenticated(context, this._config.UserGroups); requireActionAuth = true; } if (!isAuth) return; //Is this user allowed to make this kind of query? if (requireActionAuth) HandlerHelper.IsAuthenticated(context, this._config.UserGroups, this.GetPermissionAction(query)); //Set the Default Graph URIs (if any) if (userDefaultGraphs.Count > 0) { //Default Graph Uri specified by default-graph-uri parameter or Web.config settings foreach (String userDefaultGraph in userDefaultGraphs) { if (!userDefaultGraph.Equals(String.Empty)) { query.AddDefaultGraph(new Uri(userDefaultGraph)); } } } else if (!this._config.DefaultGraphURI.Equals(String.Empty)) { //Only applies if the Query doesn't specify any Default Graph if (!query.DefaultGraphs.Any()) { query.AddDefaultGraph(new Uri(this._config.DefaultGraphURI)); } } //Set the Named Graph URIs (if any) if (userNamedGraphs.Count > 0) { foreach (String userNamedGraph in userNamedGraphs) { if (!userNamedGraph.Equals(String.Empty)) { query.AddNamedGraph(new Uri(userNamedGraph)); } } } //Set Timeout setting if (timeout > 0) { query.Timeout = timeout; } else { query.Timeout = this._config.DefaultTimeout; } //Set Partial Results Setting query.PartialResultsOnTimeout = partialResults; //Set Describe Algorithm query.Describer = this._config.DescribeAlgorithm; //Now we can finally make the query and return the results Object result = this.ProcessQuery(query); this.ProcessResults(context, result); //Update the Cache as the request may have changed the endpoint this.UpdateConfig(context); } catch (RdfParseException parseEx) { HandleErrors(context, "Parsing Error", queryText, parseEx, (int)HttpStatusCode.BadRequest); } catch (RdfQueryTimeoutException timeoutEx) { HandleErrors(context, "Query Timeout Error", queryText, timeoutEx); } catch (RdfQueryException queryEx) { HandleErrors(context, "Update Error", queryText, queryEx); } catch (RdfWriterSelectionException writerSelEx) { HandleErrors(context, "Output Selection Error", queryText, writerSelEx, (int)HttpStatusCode.NotAcceptable); } catch (RdfException rdfEx) { HandleErrors(context, "RDF Error", queryText, rdfEx); } catch (Exception ex) { HandleErrors(context, "Error", queryText, ex); } }
/// <summary> /// Generates a SPARQL Service Description Graph for the given Query Handler Configuration or uses the configuration supplied Description Graph /// </summary> /// <param name="context">HTTP Context</param> /// <param name="config">Query Handler Configuration</param> /// <param name="descripUri">Base URI of the Description</param> /// <returns></returns> public static IGraph GetServiceDescription(HttpContext context, BaseQueryHandlerConfiguration config, Uri descripUri) { //Use user specified Service Description if present if (config.ServiceDescription != null) return config.ServiceDescription; IGraph g = SparqlServiceDescriber.GetNewGraph(); //Add the Top Level Node representing the Service IUriNode descrip = g.CreateUriNode(descripUri); IUriNode rdfType = g.CreateUriNode(new Uri(RdfSpecsHelper.RdfType)); IUriNode service = g.CreateUriNode("sd:" + ClassService); g.Assert(descrip, rdfType, service); //Add its sd:url IUriNode url = g.CreateUriNode("sd:" + PropertyUrl); g.Assert(descrip, url, descrip); //Add the sd:supportedLanguage - Requires Query Language to be configurable through the Configuration API IUriNode supportedLang = g.CreateUriNode("sd:" + PropertySupportedLanguage); IUriNode lang; switch (config.Syntax) { case SparqlQuerySyntax.Extended: case SparqlQuerySyntax.Sparql_1_1: lang = g.CreateUriNode("sd:" + InstanceSparql11Query); break; default: lang = g.CreateUriNode("sd:" + InstanceSparql10Query); break; } g.Assert(descrip, supportedLang, lang); //Add the Result Formats IUriNode resultFormat = g.CreateUriNode("sd:" + PropertyResultFormat); foreach (MimeTypeDefinition definition in MimeTypesHelper.Definitions) { if (definition.CanWriteRdf || definition.CanWriteSparqlResults) { if (definition.FormatUri != null) { g.Assert(descrip, resultFormat, g.CreateUriNode(new Uri(definition.FormatUri))); } } } //Add Features and Dataset Description //First add descriptions for Global Expression Factories IUriNode extensionFunction = g.CreateUriNode("sd:" + PropertyExtensionFunction); IUriNode extensionAggregate = g.CreateUriNode("sd:" + PropertyExtensionAggregate); foreach (ISparqlCustomExpressionFactory factory in SparqlExpressionFactory.Factories) { foreach (Uri u in factory.AvailableExtensionFunctions) { g.Assert(descrip, extensionFunction, g.CreateUriNode(u)); } foreach (Uri u in factory.AvailableExtensionAggregates) { g.Assert(descrip, extensionAggregate, g.CreateUriNode(u)); } } //Then get the Configuration Object to add any other Feature Descriptions it wishes to config.AddFeatureDescription(g, descrip); return g; }