/// <summary> /// Processes a POST operation which adds triples to a new Graph in the Store and returns the URI of the newly created Graph /// </summary> /// <param name="context">HTTP Context</param> /// <remarks> /// <para> /// This operation allows clients to POST data to an endpoint and have it create a Graph and assign a URI for them. /// </para> /// </remarks> public override void ProcessPostCreate(HttpContext context) { //Get the payload assuming there is one IGraph g = this.ParsePayload(context); //Mint a URI for the Graph Uri graphUri = this.MintGraphUri(context, g); //First generate a CREATE to ensure that the Graph exists //We don't do a CREATE SILENT as this operation is supposed to generate a new Graph URI //so if MintGraphUri() fails to deliver a unused Graph URI then the operation should fail StringBuilder insert = new StringBuilder(); insert.AppendLine("CREATE GRAPH @graph ;"); //Then Generate an INSERT DATA command for the actual POST //Note that if the payload is empty this still has the effect of creating a Graph if (g != null) { insert.AppendLine("INSERT DATA { GRAPH @graph {"); TurtleFormatter formatter = new TurtleFormatter(g.NamespaceMap); foreach (Triple t in g.Triples) { insert.AppendLine(t.ToString(formatter)); } insert.AppendLine("} }"); } //Parse and evaluate the command SparqlParameterizedString insertCmd = new SparqlParameterizedString(insert.ToString()); insertCmd.Namespaces = g.NamespaceMap; insertCmd.SetUri("graph", graphUri); SparqlUpdateCommandSet cmds = this._parser.ParseFromString(insertCmd); this._updateProcessor.ProcessCommandSet(cmds); this._updateProcessor.Flush(); //Finally return a 201 Created and a Location header with the new Graph URI context.Response.StatusCode = (int)HttpStatusCode.Created; try { context.Response.Headers.Add("Location", graphUri.ToString()); } catch (PlatformNotSupportedException) { context.Response.AddHeader("Location", graphUri.ToString()); } }
/// <summary> /// Processes a PUT operation /// </summary> /// <param name="context">HTTP Context</param> public override void ProcessPut(HttpContext context) { //Get the payload assuming there is one IGraph g = this.ParsePayload(context); //Get the Graph URI of the Graph to be added Uri graphUri = this.ResolveGraphUri(context, g); //Determine whether the Graph already exists or not, if it doesn't then we have to send a 201 Response bool created = false; try { SparqlQueryParser parser = new SparqlQueryParser(); SparqlParameterizedString graphExistsQuery = new SparqlParameterizedString(); graphExistsQuery.CommandText = "ASK WHERE { GRAPH @graph { } }"; graphExistsQuery.SetUri("graph", graphUri); Object temp = this._queryProcessor.ProcessQuery(parser.ParseFromString(graphExistsQuery)); if (temp is SparqlResultSet) { created = !((SparqlResultSet)temp).Result; } } catch { //If any error occurs assume the Graph doesn't exist and so we'll return a 201 created created = true; } //Generate a set of commands based upon this StringBuilder cmdSequence = new StringBuilder(); if (graphUri != null) { cmdSequence.AppendLine("DROP SILENT GRAPH @graph ;"); cmdSequence.Append("CREATE SILENT GRAPH @graph"); } else { cmdSequence.Append("DROP SILENT DEFAULT"); } if (g != null) { cmdSequence.AppendLine(" ;"); if (graphUri != null) { cmdSequence.AppendLine("INSERT DATA { GRAPH @graph {"); } else { cmdSequence.AppendLine("INSERT DATA { "); } TurtleFormatter formatter = new TurtleFormatter(g.NamespaceMap); foreach (Triple t in g.Triples) { cmdSequence.AppendLine(t.ToString(formatter)); } if (graphUri != null) { cmdSequence.AppendLine("} }"); } else { cmdSequence.AppendLine("}"); } } SparqlParameterizedString put = new SparqlParameterizedString(cmdSequence.ToString()); put.Namespaces = g.NamespaceMap; if (graphUri != null) put.SetUri("graph", graphUri); SparqlUpdateCommandSet putCmds = this._parser.ParseFromString(put); this._updateProcessor.ProcessCommandSet(putCmds); this._updateProcessor.Flush(); //Return a 201 if required, otherwise the default behaviour of returning a 200 will occur automatically if (created) { context.Response.StatusCode = (int)HttpStatusCode.Created; } }
/// <summary> /// Processes a POST operation /// </summary> /// <param name="context">HTTP Context</param> public override void ProcessPost(HttpContext context) { //Get the payload assuming there is one IGraph g = this.ParsePayload(context); if (g == null) { //Q: What should the behaviour be when the payload is null for a POST? Assuming a 200 OK response return; } //Get the Graph URI of the Graph to be added Uri graphUri = this.ResolveGraphUri(context, g); //First we need a //Generate an INSERT DATA command for the POST StringBuilder insert = new StringBuilder(); if (graphUri != null) { insert.AppendLine("INSERT DATA { GRAPH @graph {"); } else { insert.AppendLine("INSERT DATA {"); } TurtleFormatter formatter = new TurtleFormatter(g.NamespaceMap); foreach (Triple t in g.Triples) { insert.AppendLine(t.ToString(formatter)); } if (graphUri != null) { insert.AppendLine("} }"); } else { insert.AppendLine("}"); } //Parse and evaluate the command SparqlParameterizedString insertCmd = new SparqlParameterizedString(insert.ToString()); insertCmd.Namespaces = g.NamespaceMap; if (graphUri != null) insertCmd.SetUri("graph", graphUri); SparqlUpdateCommandSet cmds = this._parser.ParseFromString(insertCmd); this._updateProcessor.ProcessCommandSet(cmds); this._updateProcessor.Flush(); }