|
|
@@ -9,8 +9,6 @@ import org.apache.jena.sparql.util.Context;
|
|
|
import org.apache.jena.sparql.util.Symbol;
|
|
|
import org.apache.jena.update.UpdateFactory;
|
|
|
import org.apache.jena.update.UpdateRequest;
|
|
|
-import org.apache.jena.riot.RIOT;
|
|
|
-
|
|
|
|
|
|
public class FusekiWrapper {
|
|
|
|
|
|
@@ -19,18 +17,12 @@ public class FusekiWrapper {
|
|
|
private RDFConnectionRemoteBuilder builder;
|
|
|
|
|
|
private String serviceURI;
|
|
|
-
|
|
|
+
|
|
|
+ private static final Symbol SYM_JSON_READER_LENIENT = Symbol.create("http://jena.apache.org/riot#jsonReaderLenient");
|
|
|
|
|
|
public String getServiceURI() {
|
|
|
return serviceURI;
|
|
|
}
|
|
|
-
|
|
|
- // Just to prevent the Malformed Json error
|
|
|
- static {
|
|
|
- Symbol symJsonReaderLenient = Symbol.create("http://jena.apache.org/riot#jsonReaderLenient");
|
|
|
- Context context = RIOT.getContext();
|
|
|
- context.set(symJsonReaderLenient, true);
|
|
|
- }
|
|
|
|
|
|
private FusekiWrapper() {
|
|
|
|
|
|
@@ -50,43 +42,47 @@ public class FusekiWrapper {
|
|
|
}
|
|
|
|
|
|
public ResultSet execQuery(String queryStr) {
|
|
|
- if (!"".equals(serviceURI)) {
|
|
|
- QueryExecution q = QueryExecutionHTTP.service(serviceURI, queryStr);
|
|
|
- return q.execSelect();
|
|
|
+ if (serviceURI == null || serviceURI.isEmpty()) {
|
|
|
+ throw new NullPointerException("Service URI not specified");
|
|
|
}
|
|
|
- throw new NullPointerException("Service URI not specified");
|
|
|
|
|
|
+ QueryExecution qExec = QueryExecutionHTTP.service(serviceURI, queryStr);
|
|
|
+
|
|
|
+ qExec.getContext().set(SYM_JSON_READER_LENIENT, true);
|
|
|
+
|
|
|
+ return qExec.execSelect();
|
|
|
}
|
|
|
|
|
|
public boolean updateQuery(String query) {
|
|
|
- if (!"".equals(serviceURI)) {
|
|
|
- try {
|
|
|
- RDFConnectionFuseki conn = (RDFConnectionFuseki) builder.build();
|
|
|
- UpdateRequest request = UpdateFactory.create();
|
|
|
- request.add(query);
|
|
|
- conn.update(request);
|
|
|
- conn.close();
|
|
|
- return true;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
+ if (serviceURI == null || serviceURI.isEmpty()) {
|
|
|
+ throw new NullPointerException("Service URI not specified");
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ RDFConnectionFuseki conn = (RDFConnectionFuseki) builder.build();
|
|
|
+ UpdateRequest request = UpdateFactory.create();
|
|
|
+ request.add(query);
|
|
|
+ conn.update(request);
|
|
|
+ conn.close();
|
|
|
+ return true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return false;
|
|
|
}
|
|
|
- throw new NullPointerException("Service URI not specified");
|
|
|
-
|
|
|
}
|
|
|
|
|
|
public boolean execAsk(String queryStr) {
|
|
|
- if (!"".equals(serviceURI)) {
|
|
|
- try (QueryExecution qExec = QueryExecutionHTTP.service(serviceURI, queryStr)) {
|
|
|
- return qExec.execAsk();
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return false;
|
|
|
- }
|
|
|
+ if (serviceURI == null || serviceURI.isEmpty()) {
|
|
|
+ throw new NullPointerException("Service URI not specified");
|
|
|
+ }
|
|
|
+
|
|
|
+ try (QueryExecution qExec = QueryExecutionHTTP.service(serviceURI, queryStr)) {
|
|
|
+ qExec.getContext().set(SYM_JSON_READER_LENIENT, true);
|
|
|
+ return qExec.execAsk();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return false;
|
|
|
}
|
|
|
- throw new NullPointerException("Service URI not specified");
|
|
|
}
|
|
|
|
|
|
public boolean testEndpoint() {
|
|
|
@@ -94,13 +90,13 @@ public class FusekiWrapper {
|
|
|
+ "SELECT ?pm WHERE {\n"
|
|
|
+ " ?pm a pm:Model .\n"
|
|
|
+ "}";
|
|
|
- QueryExecutionHTTP service = QueryExecutionHTTP.service(serviceURI, query);
|
|
|
- ResultSet execSelect;
|
|
|
- try {
|
|
|
- execSelect = service.execSelect();
|
|
|
+
|
|
|
+ try (QueryExecutionHTTP service = QueryExecutionHTTP.service(serviceURI, query)) {
|
|
|
+ service.getContext().set(SYM_JSON_READER_LENIENT, true);
|
|
|
+ ResultSet execSelect = service.execSelect();
|
|
|
+ return execSelect.hasNext();
|
|
|
} catch (Exception e) {
|
|
|
return false;
|
|
|
}
|
|
|
- return execSelect.hasNext();
|
|
|
}
|
|
|
}
|