Skip to content

Commit

Permalink
Merge branch 'GraphProtocol' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
MaillPierre committed Aug 28, 2024
2 parents 5779810 + cffc7c2 commit bca607c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,25 @@
/**
* SPARQL 1.1 Graph Store HTTP Protocol
*
* Olivier Corby
* @author Olivier Corby
* @author Pierre Maillot, P16 Wimmics INRIA I3S, 2024
*/
@Path("rdf-graph-store")
public class GraphProtocol {
static private final Logger logger = LogManager.getLogger(GraphProtocol.class);

static final String NAMED_GRAPH_QUERY = "construct {?s ?p ?o} where {graph <%s> {?s ?p ?o}}";
static final String DEFAULT_GRAPH_QUERY = "construct where {?s ?p ?o}";
static final String NAMED_GRAPH_QUERY = "CONSTRUCT {?s ?p ?o} WHERE { GRAPH <%s> {?s ?p ?o}}";
static final String DEFAULT_GRAPH_QUERY = "CONSTRUCT {?s ?p ?o}";

static final String NAMED_GRAPH_INSERT = "insert data {graph <%s> {%s}}";
static final String DEFAULT_GRAPH_INSERT = "insert data {%s}";
static final String NAMED_GRAPH_INSERT = "INSERT DATA { GRAPH <%s> {%s}}";
static final String DEFAULT_GRAPH_INSERT = "INSERT DATA {%s}";

Response get(HttpServletRequest request, String name, String graph, String pattern, String access, int format) {
String query = pattern;
if (name != null) {
query = String.format(pattern, NSManager.nsm().toNamespace(graph));
}
return new SPARQLRestAPI().myGetResult(request, name, null, null, null, null, query, access, null, null,
return new SPARQLRestAPI().getResultFormat(request, name, null, null, null, null, query, access, null, null,
format);
}

Expand All @@ -51,7 +52,7 @@ Response post(HttpServletRequest request, String name, String graph, String patt
} else {
query = String.format(NAMED_GRAPH_INSERT, NSManager.nsm().toNamespace(graph), pattern);
}
return new SPARQLRestAPI().myGetResult(request, name, null, null, null, null, query, access, null, null,
return new SPARQLRestAPI().getResultFormat(request, name, null, null, null, null, query, access, null, null,
format);
}

Expand Down Expand Up @@ -80,7 +81,7 @@ public Response getTurtle(@jakarta.ws.rs.core.Context HttpServletRequest request
@QueryParam("mode") List<String> mode) {

logger.info("getTurtle");
return get(request, name, graph, getQuery(name), access, ResultFormat.TURTLE_FORMAT);
return get(request, name, graph, getQuery(graph), access, ResultFormat.TURTLE_FORMAT);
}

@GET
Expand All @@ -92,7 +93,7 @@ public Response getXML(@jakarta.ws.rs.core.Context HttpServletRequest request,
@QueryParam("mode") List<String> mode) {

logger.info("getXML");
return get(request, name, graph, getQuery(name), access, ResultFormat.RDF_XML_FORMAT);
return get(request, name, graph, getQuery(graph), access, ResultFormat.RDF_XML_FORMAT);
}

@GET
Expand All @@ -104,58 +105,30 @@ public Response getJSON(@jakarta.ws.rs.core.Context HttpServletRequest request,
@QueryParam("mode") List<String> mode) {

logger.info("getJSON");
return get(request, name, graph, getQuery(name), access, ResultFormat.JSONLD_FORMAT);
return get(request, name, graph, getQuery(graph), access, ResultFormat.JSONLD_FORMAT);
}

@PUT
@Produces({ ResultFormat.SPARQL_RESULTS_XML, ResultFormat.XML})
public Response put(@jakarta.ws.rs.core.Context HttpServletRequest request,
@PathParam("name") String name,
@QueryParam("graph") String graph,
// @QueryParam("query")
String query,
@QueryParam("access") String access,
@QueryParam("mode") List<String> mode) {

logger.info("put");
return post(request, name, graph, query, access, ResultFormat.XML_FORMAT);
}

@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response put2(@jakarta.ws.rs.core.Context HttpServletRequest request,
@PathParam("name") String name,
@FormParam("graph") String graph,
@FormParam("query") String query,
@FormParam("access") String access,
@FormParam("mode") List<String> mode) {

logger.info(String.format("put2: graph %s", graph));
return post(request, name, graph, query, access, ResultFormat.XML_FORMAT);
return post(request, name, graph, getQuery(graph), access, ResultFormat.XML_FORMAT);
}

@POST
public Response post(@jakarta.ws.rs.core.Context HttpServletRequest request,
@PathParam("name") String name,
@QueryParam("graph") String graph,
@QueryParam("query") String query,
@QueryParam("access") String access,
@QueryParam("mode") List<String> mode) {

logger.info("post");
return post(request, name, graph, query, access, ResultFormat.XML_FORMAT);
}

@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response post2(@jakarta.ws.rs.core.Context HttpServletRequest request,
@PathParam("name") String name,
@FormParam("graph") String graph,
@FormParam("query") String query,
@FormParam("access") String access,
@FormParam("mode") List<String> mode) {

logger.info(String.format("post2: graph %s", graph));
return post(request, name, graph, query, access, ResultFormat.XML_FORMAT);
return post(request, name, graph, getQuery(graph), access, ResultFormat.XML_FORMAT);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static fr.inria.corese.core.print.ResultFormat.TURTLE_TEXT;
import static fr.inria.corese.core.api.Loader.TURTLE_FORMAT;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.BufferedReader;
Expand Down Expand Up @@ -174,7 +175,7 @@ public void getGraphStoreProtocolWithUnknownGraph() throws Exception{
@Test
public void putGraphStoreProtocolNewGraph() throws Exception {

boolean absenceTest = ! SPARQLTestUtils.sendSPARQLAsk("ASK { GRAPH <http://example.com/C> { <http://example.com/C> a <http://example.com/Thing> } }");
boolean absenceTest = SPARQLTestUtils.sendSPARQLAsk("ASK { GRAPH <http://example.com/C> { <http://example.com/C> a <http://example.com/Thing> } }");

List<List<String>> headers = new LinkedList<>();
List<String> acceptHeader = new LinkedList<>();
Expand All @@ -194,9 +195,9 @@ public void putGraphStoreProtocolNewGraph() throws Exception {

boolean presenceTest = SPARQLTestUtils.sendSPARQLAsk("ASK { GRAPH <http://example.com/C> { <http://example.com/C> a <http://example.com/Thing> } }");

assertFalse(absenceTest);
assertEquals(201, status);
assertTrue(presenceTest);
assertTrue(absenceTest);
}

@Test
Expand Down

0 comments on commit bca607c

Please sign in to comment.