Skip to content

Commit 6008d36

Browse files
author
Andrea Cimmino Arriaga
committed
sparql functionalities improved
1 parent 28cb06d commit 6008d36

7 files changed

Lines changed: 81 additions & 28 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>es.upm.fi.oeg</groupId>
55
<artifactId>wothive</artifactId>
6-
<version>0.2.0</version>
6+
<version>0.2.1</version>
77
<name>Directory for the Web of Things</name>
88

99
<properties>

src/main/java/directory/Utils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class Utils {
4646
public static final String METHOD_PUT = "PUT";
4747

4848
public static final String MIME_DIRECTORY_ERROR = "application/problem+json";
49-
protected static final String DIRECTORY_VERSION = "WoTHive/0.2.0";
49+
protected static final String DIRECTORY_VERSION = "WoTHive/0.2.1";
5050
protected static final String WOT_DIRECTORY_LOGO = "\n"+
5151
"██╗ ██╗ ██████╗ ████████╗\n" +
5252
"██║ ██║██╔═══██╗╚══██╔══╝\n" +

src/main/java/directory/configuration/DirectoryConfiguration.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,9 @@ public void setValidation(ValidationConfiguration validation) {
6767

6868
public DirectoryConfiguration createDefault() {
6969
try {
70-
this.triplestore = new TriplestoreConfiguration(DEFAULT_TRIPLESTORE_ENDPOINT, DEFAULT_TRIPLESTORE_ENDPOINT,
71-
true);
70+
this.triplestore = new TriplestoreConfiguration(DEFAULT_TRIPLESTORE_ENDPOINT, DEFAULT_TRIPLESTORE_ENDPOINT);
7271
this.service = new ServiceConfiguration("https://oeg.fi.upm.es/wothive/", 9000, 200, 2, 30000, 100, "./events.json");
73-
this.validation = new ValidationConfiguration(true, true, "./schema.json", "./shape.ttl");
72+
this.validation = new ValidationConfiguration(false, true, "./schema.json", "./shape.ttl");
7473
} catch (Exception e) {
7574
Directory.LOGGER.error(e.toString());
7675
}

src/main/java/directory/configuration/TriplestoreConfiguration.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,37 @@
44
import java.net.URISyntaxException;
55

66
import com.google.gson.Gson;
7+
import com.google.gson.GsonBuilder;
78
import com.google.gson.JsonObject;
89

10+
import directory.Directory;
911
import directory.exceptions.ConfigurationException;
1012

1113
public class TriplestoreConfiguration extends AbstractConfiguration{
1214

1315
// -- Attributes
1416
private URI updateEnpoint = null;
1517
private URI queryEnpoint = null;
16-
private Boolean queryUsingGET = true;
18+
private String username = null;
19+
private String password = null;
1720

1821
// -- Constructors
1922

2023
public TriplestoreConfiguration() {
2124
super();
2225
}
2326

24-
public TriplestoreConfiguration( String queryEnpoint, String updateEnpoint, Boolean queryUsingGET) throws URISyntaxException {
27+
public TriplestoreConfiguration( String queryEnpoint, String updateEnpoint) throws URISyntaxException {
2528
super();
2629
this.updateEnpoint = new URI(updateEnpoint);
2730
this.queryEnpoint = new URI(queryEnpoint);
28-
this.queryUsingGET = queryUsingGET;
31+
2932
}
3033

31-
public TriplestoreConfiguration( URI queryEnpoint, URI updateEnpoint, Boolean queryUsingGET) {
34+
public TriplestoreConfiguration( URI queryEnpoint, URI updateEnpoint) {
3235
super();
3336
this.updateEnpoint = updateEnpoint;
3437
this.queryEnpoint = queryEnpoint;
35-
this.queryUsingGET = queryUsingGET;
3638
}
3739

3840
// -- Getters & Setters
@@ -52,17 +54,25 @@ public URI getQueryEnpoint() {
5254
public void setQueryEnpoint(URI queryEnpoint) {
5355
this.queryEnpoint = queryEnpoint;
5456
}
57+
58+
public String getUsername() {
59+
return username;
60+
}
5561

56-
public Boolean getQueryUsingGET() {
57-
return queryUsingGET;
62+
public void setUsername(String username) {
63+
this.username = username;
5864
}
5965

60-
public void setQueryUsingGET(Boolean queryUsingGET) {
61-
this.queryUsingGET = queryUsingGET;
66+
public String getPassword() {
67+
return password;
68+
}
69+
70+
public void setPassword(String password) {
71+
this.password = password;
6272
}
6373

6474
// Serialization methods
65-
75+
6676
public static TriplestoreConfiguration serialiseFromJson(String rawJson) {
6777
JsonObject body = null;
6878
try {
@@ -75,9 +85,14 @@ public static TriplestoreConfiguration serialiseFromJson(String rawJson) {
7585

7686
validatePayload( body, "updateEnpoint", ConfigurationException.EXCEPTION_CODE_2, "Provided JSON lacks of mandatory key \"updateEnpoint\" with the triplestore endpoint for updating data");
7787
validatePayload( body, "queryEnpoint", ConfigurationException.EXCEPTION_CODE_2, "Provided JSON lacks of mandatory key \"queryEnpoint\" with the triplestore endpoint for querying data");
78-
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.");
88+
if((!body.has("username") && body.has("password")) ||(body.has("username") && !body.has("password")))
89+
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");
90+
if(!body.has("username"))
91+
Directory.LOGGER.info("No 'username' has been provided for the remote triple store");
92+
if(!body.has("password"))
93+
Directory.LOGGER.info("No 'password' has been provided for the remote triple store");
7994

80-
return (new Gson()).fromJson(body, TriplestoreConfiguration.class);
95+
return (new GsonBuilder().serializeNulls().create()).fromJson(body, TriplestoreConfiguration.class);
8196
}
8297

8398

src/main/java/directory/things/Things.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ public class Things {
3939

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

42-
protected static String inject(JsonElement value, String injection) {
42+
protected static String inject(JsonObject json, String key, String injection) {
43+
JsonElement value = json.deepCopy().get(key);
4344
JsonArray values = new JsonArray();
4445
if(value instanceof JsonArray) {
4546
values = value.getAsJsonArray();
4647
}else if(value instanceof JsonPrimitive) {
47-
values.add(value.toString());
48+
values.add(value);
4849
}
4950
values.add(injection);
5051
return values.toString();

src/main/java/directory/things/ThingsService.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import java.util.List;
77
import org.apache.jena.rdf.model.Model;
88
import org.apache.jena.sparql.resultset.ResultsFormat;
9+
10+
import com.google.gson.Gson;
11+
import com.google.gson.JsonElement;
912
import com.google.gson.JsonObject;
1013
import directory.Utils;
1114
import directory.events.DirectoryEvent;
@@ -93,20 +96,20 @@ private static String prepareManagementInformation(JsonObject td, String graphId
9396

9497
String rawContext = td.get("@context").toString();
9598
if(Utils.InjectRegistrationInfo)
96-
rawContext = Things.inject(td.get("@context"), Things.TDD_RAW_CONTEXT);
99+
rawContext = Things.inject(td, "@context", Things.TDD_RAW_CONTEXT).toString();
97100

98101
String frame = Utils.buildEncoded(" { \"@context\" : ",rawContext , ", \"@type\" : \"Thing\" }");
99102
if(td.has("@type") && hasTypeThing) {
100103
frame = Utils.buildEncoded(" { \"@context\" : ",rawContext, ", \"@type\" : ",td.get("@type").toString()," }");
101104
}else if(td.has("@type") && !hasTypeThing) {
102-
frame = Utils.buildEncoded(" { \"@context\" : ",rawContext , ", \"@type\" : ",Things.inject(td.get("@type"), "Thing")," }");
105+
frame = Utils.buildEncoded(" { \"@context\" : ",rawContext , ", \"@type\" : ",Things.inject(td, "@type", "Thing").toString()," }");
103106
}
104107
return Utils.buildMessage("GRAPH <",MANAGEMENT_GRAPH,"> { <",graphId,"> <hive:b64:security> \"",security,"\" . <",graphId,"> <hive:b64:frame> \"",frame,"\" . <",graphId,"> <hive:b64:type> \"",hasTypeThing.toString(),"\"}");
105108
}
106109

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

125129
// Retrieve meta information of Thing
126-
String query = Utils.buildMessage("SELECT ?security ?frame ?type WHERE { <",graphId,"> <hive:b64:security> ?security ; <hive:b64:frame> ?frame; <hive:b64:type> ?type . } ");
130+
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 . } }");
127131
ByteArrayOutputStream baos = Sparql.query(query, ResultsFormat.FMT_RS_CSV);
128-
String[] rawResponse = baos.toString().replace("security,frame,type", "").trim().split(",");
132+
String baosRaw = baos.toString().replace("security,frame,type", "").trim();
133+
if(baosRaw.isEmpty())
134+
throw new ThingException(Utils.buildMessage("Requested Thing not found"));
135+
String[] rawResponse = baosRaw.split(",");
129136
String security = new String(Base64.getDecoder().decode(rawResponse[0].getBytes()));
130137
String frame = new String(Base64.getDecoder().decode(rawResponse[1].getBytes()));
131138
Boolean type = Boolean.valueOf(new String(rawResponse[2]));
@@ -140,7 +147,7 @@ protected static final JsonObject retrieveThing(String id) {
140147

141148
if(!type)
142149
Things.cleanThingType(response);
143-
150+
System.out.println(response);
144151
return response;
145152
}
146153

src/main/java/directory/triplestore/Sparql.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
import java.io.ByteArrayOutputStream;
44

5+
import org.apache.http.auth.AuthScope;
6+
import org.apache.http.auth.Credentials;
7+
import org.apache.http.auth.UsernamePasswordCredentials;
8+
import org.apache.http.impl.client.BasicCredentialsProvider;
9+
import org.apache.http.impl.client.CloseableHttpClient;
10+
import org.apache.http.impl.client.HttpClients;
511
import org.apache.jena.query.Dataset;
612
import org.apache.jena.query.DatasetFactory;
713
import org.apache.jena.query.Query;
@@ -20,6 +26,7 @@
2026
import org.apache.jena.update.UpdateFactory;
2127
import org.apache.jena.update.UpdateProcessor;
2228
import org.apache.jena.update.UpdateRequest;
29+
import org.eclipse.jetty.client.HttpClient;
2330

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

4552
public static ByteArrayOutputStream query(String sparql, ResultsFormat format) {
53+
return query(sparql, format, Directory.getConfiguration().getTriplestore().getQueryEnpoint().toString(), Directory.getConfiguration().getTriplestore().getUsername(), Directory.getConfiguration().getTriplestore().getPassword());
54+
}
55+
56+
public static ByteArrayOutputStream query(String sparql, ResultsFormat format, String endpoint, String username, String password) {
4657

4758
ByteArrayOutputStream stream = new ByteArrayOutputStream();
4859
try {
4960
Query query = QueryFactory.create(sparql) ;
50-
QueryExecution qexec = QueryExecutionFactory.sparqlService(Directory.getConfiguration().getTriplestore().getQueryEnpoint().toString(), query);
61+
QueryExecution qexec = QueryExecutionFactory.sparqlService(endpoint, query);
62+
if(username!= null && password!=null) {
63+
CloseableHttpClient client = connectPW(endpoint, username,password);
64+
qexec = QueryExecutionFactory.sparqlService(Directory.getConfiguration().getTriplestore().getQueryEnpoint().toString(), query, client);
65+
}
66+
5167
if(query.isSelectType()) {
5268
ResultSetFormatter.output(stream, qexec.execSelect(), format);
5369
}else if(query.isAskType()) {
@@ -81,12 +97,27 @@ public static ByteArrayOutputStream query(String sparql, ResultsFormat format) {
8197
return stream;
8298
}
8399

100+
public static CloseableHttpClient connectPW(String URL, String user, String password) {
101+
BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
102+
Credentials credentials = new UsernamePasswordCredentials(user, password);
103+
credsProvider.setCredentials(AuthScope.ANY, credentials);
104+
return HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
105+
106+
}
84107

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

86-
public static void update(String sparql) {
112+
public static void update(String sparql, String endpoint, String username, String password) {
87113
try {
88114
UpdateRequest updateRequest = UpdateFactory.create(sparql);
89-
UpdateProcessor updateProcessor = UpdateExecutionFactory.createRemote(updateRequest, Directory.getConfiguration().getTriplestore().getUpdateEnpoint().toString());
115+
UpdateProcessor updateProcessor = UpdateExecutionFactory.createRemote(updateRequest, endpoint);
116+
117+
if(username!=null && password!=null) {
118+
CloseableHttpClient client = connectPW(endpoint,username, password);
119+
updateProcessor = UpdateExecutionFactory.createRemote(updateRequest, endpoint, client);
120+
}
90121
updateProcessor.execute();
91122
}catch(QueryException e){
92123
throw new RemoteSparqlEndpointException(e.toString()); // syntax error

0 commit comments

Comments
 (0)