Skip to content

Commit

Permalink
Added /downloadOwl to Query Service
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Cuddihy committed Aug 24, 2021
1 parent ba23a17 commit 2cd8017
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected void addParams(HttpPost httppost, String query, SparqlResultTypes resu
List<NameValuePair> params = new ArrayList<NameValuePair>(3);

// if (this.password == null) {
if (resultType == SparqlResultTypes.TABLE || resultType == SparqlResultTypes.GRAPH_JSONLD) {
if (resultType == SparqlResultTypes.TABLE || resultType == SparqlResultTypes.GRAPH_JSONLD || resultType == SparqlResultTypes.RDF) {
params.add(new BasicNameValuePair("query", query));
} else {
params.add(new BasicNameValuePair("update", query));
Expand Down Expand Up @@ -135,7 +135,7 @@ public boolean isExceptionRetryAble(Exception e) {
* Build a POST URL
*/
public String getPostURL(SparqlResultTypes resultType) {
if (resultType == SparqlResultTypes.TABLE || resultType == SparqlResultTypes.GRAPH_JSONLD) {
if (resultType == SparqlResultTypes.TABLE || resultType == SparqlResultTypes.GRAPH_JSONLD || resultType == SparqlResultTypes.RDF) {
return String.format("%s:%s/%s/sparql", this.server, this.port, this.endpoint);
} else{
// context-uri trick is from
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected void addParams(HttpPost httppost, String query, SparqlResultTypes resu
List<NameValuePair> params = new ArrayList<NameValuePair>(3);

// if (this.password == null) {
if (resultType == SparqlResultTypes.TABLE || resultType == SparqlResultTypes.GRAPH_JSONLD) {
if (resultType == SparqlResultTypes.TABLE || resultType == SparqlResultTypes.GRAPH_JSONLD || resultType == SparqlResultTypes.RDF) {
params.add(new BasicNameValuePair("query", query));
} else {
params.add(new BasicNameValuePair("update", query));
Expand All @@ -105,7 +105,7 @@ protected void addParams(HttpPost httppost, String query, SparqlResultTypes resu
}

/**
* Override identical function of parent because some of the CONTENTTYPE_ constants are overriden
* Override identical function of parent because some of the CONTENTTYPE_ constants are overridden
*/
@Override
protected String getContentType(SparqlResultTypes resultType) throws Exception{
Expand All @@ -119,10 +119,12 @@ protected String getContentType(SparqlResultTypes resultType) throws Exception{
return CONTENTTYPE_X_JSON_LD;
} else if (resultType == SparqlResultTypes.HTML) {
return CONTENTTYPE_HTML;
} else if (resultType == SparqlResultTypes.RDF) {
return CONTENTTYPE_RDF;
}

// fail and throw an exception if the value was not valid.
throw new Exception("Cannot get content type for query type " + resultType);
throw new Exception("Cannot get Fuseki content type for query type " + resultType);
}
/**
* Build a GET URL
Expand All @@ -136,9 +138,10 @@ public String getGetURL(){
* Build a POST URL
*/
public String getPostURL(SparqlResultTypes resultType) {
if (resultType == SparqlResultTypes.TABLE || resultType == SparqlResultTypes.GRAPH_JSONLD) {
return String.format("%s:%s/%s", this.server, this.port, this.endpoint);
} else{
if (resultType == SparqlResultTypes.TABLE || resultType == SparqlResultTypes.GRAPH_JSONLD || resultType == SparqlResultTypes.RDF ) {
return String.format("%s:%s/%s", this.server, this.port, this.endpoint);

}else {
return String.format("%s:%s/%s/update", this.server, this.port, this.endpoint);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public abstract class SparqlEndpointInterface {
protected static final String CONTENTTYPE_SPARQL_QUERY_RESULT_JSON = "application/sparql-results+json";
protected static final String CONTENTTYPE_X_JSON_LD = "application/x-json+ld";
protected static final String CONTENTTYPE_HTML = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
protected static final String CONTENTTYPE_RDF = "application/rdf+xml";

private static final int MAX_QUERY_TRIES = 4;

Expand Down Expand Up @@ -981,6 +982,20 @@ protected JSONObject parseResponse(SparqlResultTypes resultType, String response
return this.handleEmptyResponse(resultType);
}

// check for RDF first, return { "rdf": "<rdf .... >" }

if (resultType==SparqlResultTypes.RDF) {
String beginning = responseTxt.length() > 100 ? responseTxt.substring(0,100) : responseTxt;
beginning = beginning.trim();
if (!beginning.trim().contains("<rdf")) {
throw new Exception("non-rdf response, starting with: " + beginning);
}
JSONObject res = new JSONObject();
res.put(SparqlResultTypes.RDF.toString(), responseTxt);
return res;
}

// it now must be JSON. Start by parsing.
try {
responseObj = new JSONParser().parse(responseTxt);
} catch(Exception e) {
Expand Down Expand Up @@ -1100,6 +1115,14 @@ public void authorizeUpload() throws AuthorizationException {
}


public String downloadOwl() throws AuthorizationException, Exception {
String query = SparqlToXUtils.generateConstructSPOSparql(this, "");
AuthorizationManager.authorizeQuery(this, query);

JSONObject res = this.executeQueryPost(query, SparqlResultTypes.RDF);
return (String) res.get(SparqlResultTypes.RDF.toString());
}

/**
* Execute query using GET (use should be rare - in cases where POST is not supported)
* See "internal use" note
Expand Down Expand Up @@ -1228,6 +1251,8 @@ protected String getContentType(SparqlResultTypes resultType) throws Exception{
return CONTENTTYPE_X_JSON_LD;
} else if (resultType == SparqlResultTypes.HTML) {
return CONTENTTYPE_HTML;
} else if (resultType == SparqlResultTypes.RDF) {
return CONTENTTYPE_RDF;
}

// fail and throw an exception if the value was not valid.
Expand Down Expand Up @@ -1264,7 +1289,10 @@ protected JSONObject getResultsFromResponse(Object responseObj, SparqlResultType
} else if(resultType == SparqlResultTypes.GRAPH_JSONLD) {
retval = getJsonldResponse(responseObj);

} else{
} else if (resultType == SparqlResultTypes.RDF) {
retval = (JSONObject) responseObj;

} else {
throw new Exception("an unknown results type was passed to \"getResultsBasedOnExpectedType\". don't know how to handle type: " + resultType);
}
return retval;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public enum SparqlResultTypes {
GRAPH_JSONLD, // expect graph results (e.g. for construct queries)
// GRAPH_TURTLE // in the future may support multiple result types for a graph
CONFIRM, // expect a confirmation message (e.g. "inserted 5 tuples", "cleared graph")
RDF, //
HTML; // e.g. uploading owl to strange endpoint
/**
* Determines if a query is a DROP GRAPH query or not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.ge.research.semtk.belmont.ValueConstraint;
import com.ge.research.semtk.belmont.XSDSupportedType;
import com.ge.research.semtk.ontologyTools.OntologyInfo;
import com.ge.research.semtk.ontologyTools.OntologyPath;
import com.ge.research.semtk.ontologyTools.Triple;
import com.ge.research.semtk.utility.LocalLogger;

public class SparqlToXUtils {
Expand Down Expand Up @@ -238,7 +240,11 @@ public static String generateCreateGraphSparql(SparqlEndpointInterface sei) {
}

public static String generateSelectSPOSparql(SparqlEndpointInterface sei, String clause) {
return "SELECT ?s ?p ?o from <" + sei.getGraph() + "> WHERE { ?s ?p ?o. " + clause + "}";
return "SELECT ?s ?p ?o FROM <" + sei.getGraph() + "> WHERE { ?s ?p ?o. " + clause + "}";
}

public static String generateConstructSPOSparql(SparqlEndpointInterface sei, String clause) {
return "CONSTRUCT { ?s ?p ?o } FROM <" + sei.getGraph() + "> WHERE { ?s ?p ?o. " + clause + "}";
}

/**
Expand Down Expand Up @@ -417,6 +423,49 @@ public static String generatePredicateStatsQuery(SparqlConnection conn, Ontology
return sparql;
}

public static String generatePathInstanceCountQuery(OntologyPath path, SparqlConnection conn, OntologyInfo oInfo) throws Exception {

String sparql =
"select (COUNT(*) as ?count)\n"
+ generateSparqlFromOrUsing("", "from", conn, oInfo) + "\n"
+ "WHERE {\n"
;
OntologyPath tempPath = new OntologyPath(path.getStartClassName());
Triple t0 = path.getTriple(0);
sparql += String.format(
" ?s_0 a <%s>.\n" +
" ?s_0 <%s> ?o_0.\n " +
" ?o_0 a <%s>.\n",
t0.getSubject(),
t0.getPredicate(),
t0.getObject()
);
tempPath.addTriple(t0.getSubject(), t0.getPredicate(), t0.getObject());

for (int i=1; i < path.getLength(); i++) {
Triple t = path.getTriple(i);

// add next triple forward or backward
if (t.getSubject().equals(tempPath.getEndClassName())) {
sparql += String.format(
" ?o_%d <%s> ?o_%d.\n " +
" ?o_%d a <%s> .\n",
i-1, t.getPredicate(), i,
i, t.getObject());
} else {
sparql += String.format(
" ?o_%d <%s> ?o_%d.\n " +
" ?o_%d a <%s>.\n",
i, t.getPredicate(), i-1,
i, t.getSubject());
}

tempPath.addTriple(t.getSubject(), t.getPredicate(), t.getObject());
}
sparql += "}";
return sparql;
}

public static String tabIndent(String tab) {
return tab.concat("\t");
}
Expand Down Expand Up @@ -563,4 +612,6 @@ public static String generateSelectInstanceDataSubjects(SparqlConnection conn, O
System.out.println(sparql.toString());
return sparql.toString();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import com.ge.research.semtk.springutillib.properties.EnvironmentProperties;
import com.ge.research.semtk.utility.LocalLogger;
import com.ge.research.semtk.utility.Utility;
import com.google.common.net.MediaType;

import io.swagger.annotations.ApiOperation;

Expand Down Expand Up @@ -377,7 +378,7 @@ public JSONObject clearModel(SparqlPrefixesAuthRequestBody requestBody, String e
* Execute clear all query
*/
@CrossOrigin
@RequestMapping(value="/clearAll", method= RequestMethod.POST)
@RequestMapping(value={"/clearAll", "/clearGraph"}, method= RequestMethod.POST)
public JSONObject clearAll(@RequestBody SparqlAuthRequestBody requestBody, @RequestHeader HttpHeaders headers) {
HeadersManager.setHeaders(headers);
GeneralResultSet resultSet = null;
Expand All @@ -404,6 +405,31 @@ public JSONObject clearAll(@RequestBody SparqlAuthRequestBody requestBody, @Requ
return resultSet.toJson();
}

/**
* get <rdf>....</rdf> dump of a graph
* or <error>message</error>
*/
@CrossOrigin
@RequestMapping(value={"/downloadOwl"}, method= RequestMethod.POST, produces="rdf/xml")
public String downloadOwl(@RequestBody SparqlAuthRequestBody requestBody, @RequestHeader HttpHeaders headers) {
HeadersManager.setHeaders(headers);
SparqlEndpointInterface sei = null;
LocalLogger.logToStdOut("Sparql Query Service start downloadOwl");

try {
requestBody.validate(); // check inputs
sei = SparqlEndpointInterface.getInstance(requestBody.getServerType(), requestBody.getServerAndPort(), requestBody.getGraph(), requestBody.getUser(), requestBody.getPassword());
return sei.downloadOwl();

} catch (Exception e) {
LocalLogger.printStackTrace(e);
return "<error>\n\t" + e.toString() + "\n</error>";

} finally {
HeadersManager.setHeaders(new HttpHeaders());
}

}
//public JSONObject uploadOwl(@RequestBody SparqlAuthRequestBody requestBody, @RequestParam("owlFile") MultipartFile owlFile){
// We can't use a @RequestBody with a @RequestParam,
// So the SparqlAuthRequestBody is broken into individual string @RequestParams
Expand Down

0 comments on commit 2cd8017

Please sign in to comment.