Skip to content

Commit

Permalink
sparql functionalities improved
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaCimminoArriaga committed Jan 20, 2022
1 parent 28cb06d commit 6008d36
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>es.upm.fi.oeg</groupId>
<artifactId>wothive</artifactId>
<version>0.2.0</version>
<version>0.2.1</version>
<name>Directory for the Web of Things</name>

<properties>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/directory/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class Utils {
public static final String METHOD_PUT = "PUT";

public static final String MIME_DIRECTORY_ERROR = "application/problem+json";
protected static final String DIRECTORY_VERSION = "WoTHive/0.2.0";
protected static final String DIRECTORY_VERSION = "WoTHive/0.2.1";
protected static final String WOT_DIRECTORY_LOGO = "\n"+
"██╗ ██╗ ██████╗ ████████╗\n" +
"██║ ██║██╔═══██╗╚══██╔══╝\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ public void setValidation(ValidationConfiguration validation) {

public DirectoryConfiguration createDefault() {
try {
this.triplestore = new TriplestoreConfiguration(DEFAULT_TRIPLESTORE_ENDPOINT, DEFAULT_TRIPLESTORE_ENDPOINT,
true);
this.triplestore = new TriplestoreConfiguration(DEFAULT_TRIPLESTORE_ENDPOINT, DEFAULT_TRIPLESTORE_ENDPOINT);
this.service = new ServiceConfiguration("https://oeg.fi.upm.es/wothive/", 9000, 200, 2, 30000, 100, "./events.json");
this.validation = new ValidationConfiguration(true, true, "./schema.json", "./shape.ttl");
this.validation = new ValidationConfiguration(false, true, "./schema.json", "./shape.ttl");
} catch (Exception e) {
Directory.LOGGER.error(e.toString());
}
Expand Down
39 changes: 27 additions & 12 deletions src/main/java/directory/configuration/TriplestoreConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,37 @@
import java.net.URISyntaxException;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

import directory.Directory;
import directory.exceptions.ConfigurationException;

public class TriplestoreConfiguration extends AbstractConfiguration{

// -- Attributes
private URI updateEnpoint = null;
private URI queryEnpoint = null;
private Boolean queryUsingGET = true;
private String username = null;
private String password = null;

// -- Constructors

public TriplestoreConfiguration() {
super();
}

public TriplestoreConfiguration( String queryEnpoint, String updateEnpoint, Boolean queryUsingGET) throws URISyntaxException {
public TriplestoreConfiguration( String queryEnpoint, String updateEnpoint) throws URISyntaxException {
super();
this.updateEnpoint = new URI(updateEnpoint);
this.queryEnpoint = new URI(queryEnpoint);
this.queryUsingGET = queryUsingGET;

}

public TriplestoreConfiguration( URI queryEnpoint, URI updateEnpoint, Boolean queryUsingGET) {
public TriplestoreConfiguration( URI queryEnpoint, URI updateEnpoint) {
super();
this.updateEnpoint = updateEnpoint;
this.queryEnpoint = queryEnpoint;
this.queryUsingGET = queryUsingGET;
}

// -- Getters & Setters
Expand All @@ -52,17 +54,25 @@ public URI getQueryEnpoint() {
public void setQueryEnpoint(URI queryEnpoint) {
this.queryEnpoint = queryEnpoint;
}

public String getUsername() {
return username;
}

public Boolean getQueryUsingGET() {
return queryUsingGET;
public void setUsername(String username) {
this.username = username;
}

public void setQueryUsingGET(Boolean queryUsingGET) {
this.queryUsingGET = queryUsingGET;
public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

// Serialization methods

public static TriplestoreConfiguration serialiseFromJson(String rawJson) {
JsonObject body = null;
try {
Expand All @@ -75,9 +85,14 @@ public static TriplestoreConfiguration serialiseFromJson(String rawJson) {

validatePayload( body, "updateEnpoint", ConfigurationException.EXCEPTION_CODE_2, "Provided JSON lacks of mandatory key \"updateEnpoint\" with the triplestore endpoint for updating data");
validatePayload( body, "queryEnpoint", ConfigurationException.EXCEPTION_CODE_2, "Provided JSON lacks of mandatory key \"queryEnpoint\" with the triplestore endpoint for querying data");
validatePayload( body, "queryUsingGET", ConfigurationException.EXCEPTION_CODE_2, "Provided JSON lacks of mandatory key \"queryUsingGET\" with a boolean value indicating if the communication with the triplestore shuld be done using the GET method. If false the directory will use POST.");
if((!body.has("username") && body.has("password")) ||(body.has("username") && !body.has("password")))
throw new ConfigurationException(ConfigurationException.EXCEPTION_CODE_2, "Provided JSON must have both optional keywords \"username\" and \"passwords\" in order to connect to a remote triple store");
if(!body.has("username"))
Directory.LOGGER.info("No 'username' has been provided for the remote triple store");
if(!body.has("password"))
Directory.LOGGER.info("No 'password' has been provided for the remote triple store");

return (new Gson()).fromJson(body, TriplestoreConfiguration.class);
return (new GsonBuilder().serializeNulls().create()).fromJson(body, TriplestoreConfiguration.class);
}


Expand Down
5 changes: 3 additions & 2 deletions src/main/java/directory/things/Things.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ public class Things {

public static String TDD_RAW_CONTEXT = "https://raw.githubusercontent.com/oeg-upm/wot-hive/AndreaCimminoArriaga-tdd-context/tdd.jsonld";

protected static String inject(JsonElement value, String injection) {
protected static String inject(JsonObject json, String key, String injection) {
JsonElement value = json.deepCopy().get(key);
JsonArray values = new JsonArray();
if(value instanceof JsonArray) {
values = value.getAsJsonArray();
}else if(value instanceof JsonPrimitive) {
values.add(value.toString());
values.add(value);
}
values.add(injection);
return values.toString();
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/directory/things/ThingsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import java.util.List;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.sparql.resultset.ResultsFormat;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import directory.Utils;
import directory.events.DirectoryEvent;
Expand Down Expand Up @@ -93,20 +96,20 @@ private static String prepareManagementInformation(JsonObject td, String graphId

String rawContext = td.get("@context").toString();
if(Utils.InjectRegistrationInfo)
rawContext = Things.inject(td.get("@context"), Things.TDD_RAW_CONTEXT);
rawContext = Things.inject(td, "@context", Things.TDD_RAW_CONTEXT).toString();

String frame = Utils.buildEncoded(" { \"@context\" : ",rawContext , ", \"@type\" : \"Thing\" }");
if(td.has("@type") && hasTypeThing) {
frame = Utils.buildEncoded(" { \"@context\" : ",rawContext, ", \"@type\" : ",td.get("@type").toString()," }");
}else if(td.has("@type") && !hasTypeThing) {
frame = Utils.buildEncoded(" { \"@context\" : ",rawContext , ", \"@type\" : ",Things.inject(td.get("@type"), "Thing")," }");
frame = Utils.buildEncoded(" { \"@context\" : ",rawContext , ", \"@type\" : ",Things.inject(td, "@type", "Thing").toString()," }");
}
return Utils.buildMessage("GRAPH <",MANAGEMENT_GRAPH,"> { <",graphId,"> <hive:b64:security> \"",security,"\" . <",graphId,"> <hive:b64:frame> \"",frame,"\" . <",graphId,"> <hive:b64:type> \"",hasTypeThing.toString(),"\"}");
}

private static void enrichTd(JsonObject td) {
if(!Things.hasThingType(td))
Things.inject(td.get("@type"), "Thing");
td.add("@type", (new Gson()).fromJson( Things.inject(td, "@type", "Thing"), JsonElement.class));
if(Utils.InjectRegistrationInfo) {
//TODO:
}
Expand All @@ -118,14 +121,18 @@ private static void enrichTd(JsonObject td) {
* @return returns a JSON-LD 1.1 representation of the Thing
*/
protected static final JsonObject retrieveThing(String id) {
System.out.println(id);
String graphId = createGraphId(id);
if(!exists(graphId))
throw new ThingException(Utils.buildMessage("Requested Thing not found"));

// Retrieve meta information of Thing
String query = Utils.buildMessage("SELECT ?security ?frame ?type WHERE { <",graphId,"> <hive:b64:security> ?security ; <hive:b64:frame> ?frame; <hive:b64:type> ?type . } ");
String query = Utils.buildMessage("SELECT ?security ?frame ?type WHERE { GRAPH <",MANAGEMENT_GRAPH, "> { <",graphId,"> <hive:b64:security> ?security ; <hive:b64:frame> ?frame; <hive:b64:type> ?type . } }");
ByteArrayOutputStream baos = Sparql.query(query, ResultsFormat.FMT_RS_CSV);
String[] rawResponse = baos.toString().replace("security,frame,type", "").trim().split(",");
String baosRaw = baos.toString().replace("security,frame,type", "").trim();
if(baosRaw.isEmpty())
throw new ThingException(Utils.buildMessage("Requested Thing not found"));
String[] rawResponse = baosRaw.split(",");
String security = new String(Base64.getDecoder().decode(rawResponse[0].getBytes()));
String frame = new String(Base64.getDecoder().decode(rawResponse[1].getBytes()));
Boolean type = Boolean.valueOf(new String(rawResponse[2]));
Expand All @@ -140,7 +147,7 @@ protected static final JsonObject retrieveThing(String id) {

if(!type)
Things.cleanThingType(response);

System.out.println(response);
return response;
}

Expand Down
37 changes: 34 additions & 3 deletions src/main/java/directory/triplestore/Sparql.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

import java.io.ByteArrayOutputStream;

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.jena.query.Dataset;
import org.apache.jena.query.DatasetFactory;
import org.apache.jena.query.Query;
Expand All @@ -20,6 +26,7 @@
import org.apache.jena.update.UpdateFactory;
import org.apache.jena.update.UpdateProcessor;
import org.apache.jena.update.UpdateRequest;
import org.eclipse.jetty.client.HttpClient;

import directory.Directory;
import directory.Utils;
Expand All @@ -43,11 +50,20 @@ public static ResultsFormat guess(String str) {
// query methods

public static ByteArrayOutputStream query(String sparql, ResultsFormat format) {
return query(sparql, format, Directory.getConfiguration().getTriplestore().getQueryEnpoint().toString(), Directory.getConfiguration().getTriplestore().getUsername(), Directory.getConfiguration().getTriplestore().getPassword());
}

public static ByteArrayOutputStream query(String sparql, ResultsFormat format, String endpoint, String username, String password) {

ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
Query query = QueryFactory.create(sparql) ;
QueryExecution qexec = QueryExecutionFactory.sparqlService(Directory.getConfiguration().getTriplestore().getQueryEnpoint().toString(), query);
QueryExecution qexec = QueryExecutionFactory.sparqlService(endpoint, query);
if(username!= null && password!=null) {
CloseableHttpClient client = connectPW(endpoint, username,password);
qexec = QueryExecutionFactory.sparqlService(Directory.getConfiguration().getTriplestore().getQueryEnpoint().toString(), query, client);
}

if(query.isSelectType()) {
ResultSetFormatter.output(stream, qexec.execSelect(), format);
}else if(query.isAskType()) {
Expand Down Expand Up @@ -81,12 +97,27 @@ public static ByteArrayOutputStream query(String sparql, ResultsFormat format) {
return stream;
}

public static CloseableHttpClient connectPW(String URL, String user, String password) {
BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
Credentials credentials = new UsernamePasswordCredentials(user, password);
credsProvider.setCredentials(AuthScope.ANY, credentials);
return HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();

}

public static void update(String sparql ) {
update( sparql, Directory.getConfiguration().getTriplestore().getUpdateEnpoint().toString(), Directory.getConfiguration().getTriplestore().getUsername(), Directory.getConfiguration().getTriplestore().getPassword());
}

public static void update(String sparql) {
public static void update(String sparql, String endpoint, String username, String password) {
try {
UpdateRequest updateRequest = UpdateFactory.create(sparql);
UpdateProcessor updateProcessor = UpdateExecutionFactory.createRemote(updateRequest, Directory.getConfiguration().getTriplestore().getUpdateEnpoint().toString());
UpdateProcessor updateProcessor = UpdateExecutionFactory.createRemote(updateRequest, endpoint);

if(username!=null && password!=null) {
CloseableHttpClient client = connectPW(endpoint,username, password);
updateProcessor = UpdateExecutionFactory.createRemote(updateRequest, endpoint, client);
}
updateProcessor.execute();
}catch(QueryException e){
throw new RemoteSparqlEndpointException(e.toString()); // syntax error
Expand Down

0 comments on commit 6008d36

Please sign in to comment.