Skip to content
This repository has been archived by the owner on Jan 28, 2025. It is now read-only.

Commit

Permalink
Bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
MaillPierre committed Aug 28, 2024
1 parent 0bb37b6 commit f04dca1
Show file tree
Hide file tree
Showing 6 changed files with 393 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
@Path("sparql")
public class SPARQLRestAPI implements ResultFormatDef, URLParam {
private static final String ERROR_ENDPOINT = "Error while querying Corese SPARQL endpoint";
private static final String headerAccept = "Access-Control-Allow-Origin";
private static final String headerContent = "Content-type";
private static final String HEADER_ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin";
private static final String HEADER_CONTENT_TYPE = "Content-type";
private static final String TEXT_PLAIN = "text/plain";

static final String SPARQL_RESULTS_XML = ResultFormat.SPARQL_RESULTS_XML;
Expand All @@ -57,6 +57,7 @@ public class SPARQLRestAPI implements ResultFormatDef, URLParam {
static final String SPARQL_RESULTS_TSV = ResultFormat.SPARQL_RESULTS_TSV;
static final String SPARQL_RESULTS_MD = ResultFormat.SPARQL_RESULTS_MD;
static final String SPARQL_QUERY = ResultFormat.SPARQL_QUERY;
static final String SPARQL_UPDATE_QUERY = "application/sparql-update";

static final String XML = ResultFormat.XML;
static final String RDF_XML = ResultFormat.RDF_XML;
Expand Down Expand Up @@ -99,7 +100,7 @@ public class SPARQLRestAPI implements ResultFormatDef, URLParam {

private static Profile mprofile;

static private final Logger logger = LogManager.getLogger(SPARQLRestAPI.class);
private static final Logger logger = LogManager.getLogger(SPARQLRestAPI.class);
private static String key;

static {
Expand Down Expand Up @@ -187,7 +188,7 @@ public Response initRDF(
setVisitor(QuerySolverVisitorServer.create(createEval()));
getVisitor().initServer(EmbeddedJettyServer.BASE_URI);
init();
return Response.status(200).header(headerAccept, "*").entity("Endpoint reset").build();
return Response.status(200).header(HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, "*").entity("Endpoint reset").build();
}

void init() {
Expand Down Expand Up @@ -245,7 +246,7 @@ public Response loadRDF(
if (remotePath == null) {
String error = "Null remote path";
logger.error(error);
return Response.status(404).header(headerAccept, "*").entity(error).build();
return Response.status(404).header(HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, "*").entity(error).build();
}

logger.debug(remotePath);
Expand All @@ -259,10 +260,10 @@ public Response loadRDF(
}
} catch (LoadException ex) {
logger.error(ex);
return Response.status(404).header(headerAccept, "*").entity(output).build();
return Response.status(404).header(HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, "*").entity(output).build();
}

return Response.status(200).header(headerAccept, "*").entity(output).build();
return Response.status(200).header(HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, "*").entity(output).build();
}

@GET
Expand All @@ -274,7 +275,7 @@ public Response setDebug(@QueryParam("value") String debug, @QueryParam("detail"
if (detail != null) {
isDetail = detail.equals("true");
}
return Response.status(200).header(headerAccept, "*")
return Response.status(200).header(HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, "*")
.entity("debug: " + isDebug + " ; " + "detail: " + isDetail).build();
}

Expand Down Expand Up @@ -642,6 +643,7 @@ public Response getXMLForPost(@jakarta.ws.rs.core.Context HttpServletRequest req
@PathParam("name") String name,
@PathParam("oper") String oper,
@DefaultValue("") @QueryParam("query") String query,
@DefaultValue("") @FormParam("update") String update,
@QueryParam("access") String access,
@QueryParam("default-graph-uri") List<String> defaut,
@QueryParam("named-graph-uri") List<String> named,
Expand All @@ -650,7 +652,7 @@ public Response getXMLForPost(@jakarta.ws.rs.core.Context HttpServletRequest req
@QueryParam("mode") List<String> mode,
@QueryParam("uri") List<String> uri) {

query = getQuery(query, message);
query = getQuery(query, update, message);

return getResultFormat(request, name, oper, uri, param, mode, query, access, defaut, named, XML_FORMAT);
}
Expand All @@ -662,6 +664,7 @@ public Response getXMLForPostText(@jakarta.ws.rs.core.Context HttpServletRequest
@PathParam("name") String name,
@PathParam("oper") String oper,
@DefaultValue("") @QueryParam("query") String query,
@DefaultValue("") @FormParam("update") String update,
@QueryParam("access") String access,
@QueryParam("default-graph-uri") List<String> defaut,
@QueryParam("named-graph-uri") List<String> named,
Expand All @@ -670,7 +673,7 @@ public Response getXMLForPostText(@jakarta.ws.rs.core.Context HttpServletRequest
@QueryParam("mode") List<String> mode,
@QueryParam("uri") List<String> uri) {

query = getQuery(query, message);
query = getQuery(query, update, message);

return getResultFormat(request, name, oper, uri, param, mode, query, access, defaut, named, TEXT_FORMAT);
}
Expand Down Expand Up @@ -987,31 +990,112 @@ void afterRequest(HttpServletRequest request, Response resp, String query, Mappi
}

@POST
@Consumes("application/sparql-update")
public Response updateTriplesDirect(@jakarta.ws.rs.core.Context HttpServletRequest request,
@Consumes(SPARQL_UPDATE_QUERY)
@Produces(SPARQL_RESULTS_XML)
public Response updateTriplesDirectXML(@jakarta.ws.rs.core.Context HttpServletRequest request,
String message, // standard parameter, do not add @QueryParam()
@PathParam("name") String name,
@PathParam("oper") String oper,
@QueryParam("access") String access,
@QueryParam("using-graph-uri") List<String> defaut,
@QueryParam("using-named-graph-uri") List<String> named) {
try {
Mappings map = null;
if (message != null) {
beforeRequest(request, message);
map = getTripleStore(name).query(request, message, createDataset(request, defaut, named, access));
} else {
logger.warn("Null update query !");
}
@QueryParam("using-named-graph-uri") List<String> named,
@QueryParam("param") List<String> param,
@QueryParam("mode") List<String> mode,
@QueryParam("uri") List<String> uri) {
if (message != null) {
return getResultFormat(request, name, oper, uri, param, mode, message, access, defaut, named, XML_FORMAT);
} else {
logger.warn("Null update query !");
return Response.status(ERROR).header(HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, "*").entity(ERROR_ENDPOINT)
.build();
}
}

Response resp = Response.status(200)
.header(headerAccept, "*")
.header(headerContent, TEXT_PLAIN)
.entity("").build();
afterRequest(request, resp, message, map, resp.getEntity().toString());
return resp;
} catch (Exception ex) {
logger.error(ERROR_ENDPOINT, ex);
return Response.status(ERROR).header(headerAccept, "*").entity(ERROR_ENDPOINT).build();
@POST
@Consumes(SPARQL_UPDATE_QUERY)
@Produces(SPARQL_RESULTS_CSV)
public Response updateTriplesDirectCSV(@jakarta.ws.rs.core.Context HttpServletRequest request,
String message, // standard parameter, do not add @QueryParam()
@PathParam("name") String name,
@PathParam("oper") String oper,
@QueryParam("access") String access,
@QueryParam("using-graph-uri") List<String> defaut,
@QueryParam("using-named-graph-uri") List<String> named,
@QueryParam("param") List<String> param,
@QueryParam("mode") List<String> mode,
@QueryParam("uri") List<String> uri) {
if (message != null) {
return getResultFormat(request, name, oper, uri, param, mode, message, access, defaut, named, CSV_FORMAT);
} else {
logger.warn("Null update query !");
return Response.status(ERROR).header(HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, "*").entity(ERROR_ENDPOINT)
.build();
}
}

@POST
@Consumes(SPARQL_UPDATE_QUERY)
@Produces(SPARQL_RESULTS_TSV)
public Response updateTriplesDirectTSV(@jakarta.ws.rs.core.Context HttpServletRequest request,
String message, // standard parameter, do not add @QueryParam()
@PathParam("name") String name,
@PathParam("oper") String oper,
@QueryParam("access") String access,
@QueryParam("using-graph-uri") List<String> defaut,
@QueryParam("using-named-graph-uri") List<String> named,
@QueryParam("param") List<String> param,
@QueryParam("mode") List<String> mode,
@QueryParam("uri") List<String> uri) {
if (message != null) {
return getResultFormat(request, name, oper, uri, param, mode, message, access, defaut, named, TSV_FORMAT);
} else {
logger.warn("Null update query !");
return Response.status(ERROR).header(HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, "*").entity(ERROR_ENDPOINT)
.build();
}
}

@POST
@Consumes(SPARQL_UPDATE_QUERY)
@Produces(SPARQL_RESULTS_JSON)
public Response updateTriplesDirectJSON(@jakarta.ws.rs.core.Context HttpServletRequest request,
String message, // standard parameter, do not add @QueryParam()
@PathParam("name") String name,
@PathParam("oper") String oper,
@QueryParam("access") String access,
@QueryParam("using-graph-uri") List<String> defaut,
@QueryParam("using-named-graph-uri") List<String> named,
@QueryParam("param") List<String> param,
@QueryParam("mode") List<String> mode,
@QueryParam("uri") List<String> uri) {
if (message != null) {
return getResultFormat(request, name, oper, uri, param, mode, message, access, defaut, named, JSON_FORMAT);
} else {
logger.warn("Null update query !");
return Response.status(ERROR).header(HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, "*").entity(ERROR_ENDPOINT)
.build();
}
}

@POST
@Consumes(SPARQL_UPDATE_QUERY)
@Produces(SPARQL_RESULTS_MD)
public Response updateTriplesDirectMD(@jakarta.ws.rs.core.Context HttpServletRequest request,
String message, // standard parameter, do not add @QueryParam()
@PathParam("name") String name,
@PathParam("oper") String oper,
@QueryParam("access") String access,
@QueryParam("using-graph-uri") List<String> defaut,
@QueryParam("using-named-graph-uri") List<String> named,
@QueryParam("param") List<String> param,
@QueryParam("mode") List<String> mode,
@QueryParam("uri") List<String> uri) {
if (message != null) {
return getResultFormat(request, name, oper, uri, param, mode, message, access, defaut, named, MARKDOWN_FORMAT);
} else {
logger.warn("Null update query !");
return Response.status(ERROR).header(HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, "*").entity(ERROR_ENDPOINT)
.build();
}
}

Expand All @@ -1024,11 +1108,13 @@ public Response getTriplesForHead(@jakarta.ws.rs.core.Context HttpServletRequest
@QueryParam("named-graph-uri") List<String> named) {
try {
Mappings mp = getTripleStore(name).query(request, query, createDataset(request, defaut, named, access));
return Response.status(mp.size() > 0 ? 200 : 400).header(headerAccept, "*").entity("Query has no response")
return Response.status(mp.size() > 0 ? 200 : 400).header(HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, "*")
.entity("Query has no response")
.build();
} catch (Exception ex) {
logger.error(ERROR_ENDPOINT, ex);
return Response.status(ERROR).header(headerAccept, "*").entity(ERROR_ENDPOINT).build();
return Response.status(ERROR).header(HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, "*").entity(ERROR_ENDPOINT)
.build();
}
}

Expand Down Expand Up @@ -1076,7 +1162,8 @@ public Response getTriplesJSONForGetWithGraph(@jakarta.ws.rs.core.Context HttpSe

} catch (Exception ex) {
logger.error(ERROR_ENDPOINT, ex);
return Response.status(ERROR).header(headerAccept, "*").entity(ERROR_ENDPOINT).build();
return Response.status(ERROR).header(HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, "*").entity(ERROR_ENDPOINT)
.build();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void getGraphStoreProtocolWithGraph() throws Exception {
headers.add(acceptHeader);

String urlQuery = GRAPH_STORE_ENDPOINT + "?" + SPARQLTestUtils.generateGraphStoreParameters("http://example.com/A");
HttpURLConnection con = SPARQLTestUtils.getConnection(urlQuery, headers);
HttpURLConnection con = HTTPConnectionUtils.getConnection(urlQuery, headers);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
Expand Down Expand Up @@ -112,7 +112,7 @@ public void getGraphStoreProtocolWithDefault() throws Exception{
headers.add(acceptHeader);

String urlQuery = GRAPH_STORE_ENDPOINT + "?" + SPARQLTestUtils.generateGraphStoreParameters("default");
HttpURLConnection con = SPARQLTestUtils.getConnection(urlQuery, headers);
HttpURLConnection con = HTTPConnectionUtils.getConnection(urlQuery, headers);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
Expand Down Expand Up @@ -147,7 +147,7 @@ public void getGraphStoreProtocolWithUnknownGraph() throws Exception{
headers.add(acceptHeader);

String urlQuery = GRAPH_STORE_ENDPOINT + "?" + SPARQLTestUtils.generateGraphStoreParameters("http://example.com/Z");
HttpURLConnection con = SPARQLTestUtils.getConnection(urlQuery, headers);
HttpURLConnection con = HTTPConnectionUtils.getConnection(urlQuery, headers);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
Expand Down Expand Up @@ -186,7 +186,7 @@ public void putGraphStoreProtocolNewGraph() throws Exception {

String rdfPayload = "@prefix ex: <http://example.com/> . ex:C a ex:Thing .";

HttpURLConnection con = SPARQLTestUtils.putConnection(urlQuery, headers, rdfPayload);
HttpURLConnection con = HTTPConnectionUtils.putConnection(urlQuery, headers, rdfPayload);

int status = con.getResponseCode();

Expand Down Expand Up @@ -214,7 +214,7 @@ public void putGraphStoreProtocolExistingGraph() throws Exception {

String rdfPayload = "@prefix ex: <http://example.com/> . ex:C a ex:Thing .";

HttpURLConnection con = SPARQLTestUtils.putConnection(urlQuery, headers, rdfPayload);
HttpURLConnection con = HTTPConnectionUtils.putConnection(urlQuery, headers, rdfPayload);

int status = con.getResponseCode();

Expand All @@ -233,7 +233,7 @@ public void deleteGraphStoreProtocol() throws Exception {
boolean presenceTest = SPARQLTestUtils.sendSPARQLAsk("ASK { GRAPH <http://example.com/B> { ?s ?p ?o } }");

String urlQuery = GRAPH_STORE_ENDPOINT + "?" + SPARQLTestUtils.generateGraphStoreParameters("http://example.com/B");
HttpURLConnection deleteCon = SPARQLTestUtils.deleteConnection(urlQuery);
HttpURLConnection deleteCon = HTTPConnectionUtils.deleteConnection(urlQuery);

int status = deleteCon.getResponseCode();

Expand All @@ -252,7 +252,7 @@ public void deleteGraphStoreProtocolWithUnknownGraph() throws Exception {
boolean presenceTest = ! SPARQLTestUtils.sendSPARQLAsk("ASK { GRAPH <http://example.com/Z> { ?s ?p ?o } }");

String urlQuery = GRAPH_STORE_ENDPOINT + "?" + SPARQLTestUtils.generateGraphStoreParameters("http://example.com/Z");
HttpURLConnection deleteCon = SPARQLTestUtils.deleteConnection(urlQuery);
HttpURLConnection deleteCon = HTTPConnectionUtils.deleteConnection(urlQuery);

int status = deleteCon.getResponseCode();

Expand Down Expand Up @@ -280,7 +280,7 @@ public void postGraphStoreProtocolNewGraph() throws Exception {

String rdfPayload = "@prefix ex: <http://example.com/> . ex:C a ex:Thing .";

HttpURLConnection con = SPARQLTestUtils.postConnection(urlQuery, headers, rdfPayload);
HttpURLConnection con = HTTPConnectionUtils.postConnection(urlQuery, headers, rdfPayload);

int status = con.getResponseCode();

Expand Down Expand Up @@ -308,7 +308,7 @@ public void postGraphStoreProtocolExistingGraph() throws Exception {

String rdfPayload = "@prefix ex: <http://example.com/> . ex:C a ex:Thing .";

HttpURLConnection con = SPARQLTestUtils.postConnection(urlQuery, headers, rdfPayload);
HttpURLConnection con = HTTPConnectionUtils.postConnection(urlQuery, headers, rdfPayload);

int status = con.getResponseCode();

Expand All @@ -331,7 +331,7 @@ public void headGraphStoreProtocolWithDefault() throws Exception{
headers.add(acceptHeader);

String urlQuery = GRAPH_STORE_ENDPOINT + "?" + SPARQLTestUtils.generateGraphStoreParameters("default");
HttpURLConnection con = SPARQLTestUtils.headConnection(urlQuery);
HttpURLConnection con = HTTPConnectionUtils.headConnection(urlQuery);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
Expand All @@ -356,7 +356,7 @@ public void headGraphStoreProtocolWithGraph() throws Exception{
boolean presenceTest = SPARQLTestUtils.sendSPARQLAsk("ASK { GRAPH <http://example.com/A> { ?x ?y ?z } }");

String urlQuery = GRAPH_STORE_ENDPOINT + "?" + SPARQLTestUtils.generateGraphStoreParameters("http://example.com/A");
HttpURLConnection con = SPARQLTestUtils.headConnection(urlQuery);
HttpURLConnection con = HTTPConnectionUtils.headConnection(urlQuery);

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
Expand All @@ -381,7 +381,7 @@ public void headGraphStoreProtocolWithUnknownGraph() throws Exception{
boolean absenceTest = ! SPARQLTestUtils.sendSPARQLAsk("ASK { GRAPH <http://example.com/Z> { ?x ?y ?z } }");

String urlQuery = GRAPH_STORE_ENDPOINT + "?" + SPARQLTestUtils.generateGraphStoreParameters("http://example.com/Z");
HttpURLConnection con = SPARQLTestUtils.headConnection(urlQuery);
HttpURLConnection con = HTTPConnectionUtils.headConnection(urlQuery);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package fr.inria.corese.server.webservice;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
Expand Down
Loading

0 comments on commit f04dca1

Please sign in to comment.