use of org.openrdf.repository.RepositoryException in project qi4j-sdk by Qi4j.
the class SPARQLResource method getQuery.
private Query getQuery(Repository repository, RepositoryConnection repositoryCon, String queryStr) throws ResourceException {
Form form = getRequest().getResourceRef().getQueryAsForm();
Query result;
// default query language is SPARQL
QueryLanguage queryLn = QueryLanguage.SPARQL;
// determine if inferred triples should be included in query evaluation
boolean includeInferred = true;
// build a dataset, if specified
String[] defaultGraphURIs = form.getValuesArray(DEFAULT_GRAPH_PARAM_NAME);
String[] namedGraphURIs = form.getValuesArray(NAMED_GRAPH_PARAM_NAME);
DatasetImpl dataset = null;
if (defaultGraphURIs.length > 0 || namedGraphURIs.length > 0) {
dataset = new DatasetImpl();
if (defaultGraphURIs.length > 0) {
for (String defaultGraphURI : defaultGraphURIs) {
try {
URI uri = repository.getValueFactory().createURI(defaultGraphURI);
dataset.addDefaultGraph(uri);
} catch (IllegalArgumentException e) {
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Illegal URI for default graph: " + defaultGraphURI);
}
}
}
if (namedGraphURIs.length > 0) {
for (String namedGraphURI : namedGraphURIs) {
try {
URI uri = repository.getValueFactory().createURI(namedGraphURI);
dataset.addNamedGraph(uri);
} catch (IllegalArgumentException e) {
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Illegal URI for named graph: " + namedGraphURI);
}
}
}
}
try {
result = repositoryCon.prepareQuery(queryLn, queryStr);
result.setIncludeInferred(includeInferred);
if (dataset != null) {
result.setDataset(dataset);
}
// determine if any variable bindings have been set on this query.
@SuppressWarnings("unchecked") Enumeration<String> parameterNames = Collections.enumeration(form.getValuesMap().keySet());
while (parameterNames.hasMoreElements()) {
String parameterName = parameterNames.nextElement();
if (parameterName.startsWith(BINDING_PREFIX) && parameterName.length() > BINDING_PREFIX.length()) {
String bindingName = parameterName.substring(BINDING_PREFIX.length());
Value bindingValue = parseValueParam(repository, form, parameterName);
result.setBinding(bindingName, bindingValue);
}
}
} catch (UnsupportedQueryLanguageException e) {
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, e.getMessage());
} catch (MalformedQueryException e) {
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, e.getMessage());
} catch (RepositoryException e) {
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e.getMessage());
}
return result;
}
use of org.openrdf.repository.RepositoryException in project gocd by gocd.
the class SesameGraph method close.
public void close() {
try {
for (Graph tempGraph : tempGraphs) {
tempGraph.close();
}
tempGraphs.clear();
conn.commit();
conn.close();
} catch (RepositoryException e) {
throw new ShineRuntimeException("Could not close graph!", e);
}
}
use of org.openrdf.repository.RepositoryException in project gocd by gocd.
the class SesameGraph method dumpTriplesNotInContext.
private void dumpTriplesNotInContext(Writer writer) {
try {
writer.append("Statements not in any context: \n");
} catch (IOException e) {
throw new RuntimeException(e);
}
RDFXMLWriter xmlWriter = new RDFXMLWriter(writer);
xmlWriter.startRDF();
try {
RepositoryResult<Statement> result = conn.getStatements(null, null, null, false);
while (result.hasNext()) {
Statement statement = result.next();
if (statement.getContext() == null) {
xmlWriter.handleStatement(statement);
}
}
} catch (RepositoryException | RDFHandlerException e) {
throw new RuntimeException(e);
} finally {
try {
xmlWriter.endRDF();
} catch (RDFHandlerException e) {
throw new RuntimeException(e);
}
}
}
use of org.openrdf.repository.RepositoryException in project gocd by gocd.
the class SesameGraph method createURIReference.
public URIReference createURIReference(RDFType type, String uri) {
ArgumentUtil.guaranteeNotNull(type, "Type may not be null.");
ArgumentUtil.guaranteeNotNull(uri, "URI may not be null.");
ArgumentUtil.guaranteeFalse("URI may not be a blank node!", uri.startsWith("_:"));
URI sesameNativeURI = conn.getValueFactory().createURI(uri);
try {
conn.add(sesameNativeURI, RDF.TYPE, conn.getValueFactory().createURI(type.getURIText()), contextResource);
} catch (RepositoryException e) {
throw new ShineRuntimeException(e);
}
return new SesameURIReference(sesameNativeURI);
}
use of org.openrdf.repository.RepositoryException in project gocd by gocd.
the class SesameGraphTest method checkWhenAddStatementWithBooleanObjectExplodesItThrowsAShineRuntimeException.
@Test(expected = ShineRuntimeException.class)
public void checkWhenAddStatementWithBooleanObjectExplodesItThrowsAShineRuntimeException() throws RepositoryException {
RDFProperty property = new RDFProperty("http://www.example.com/ontology#foo");
RepositoryConnection badConnection = mock(RepositoryConnection.class);
doThrow(new RepositoryException("")).when(badConnection).add((org.openrdf.model.Resource) any(), (org.openrdf.model.URI) any(), (org.openrdf.model.Literal) any());
org.openrdf.model.ValueFactory stubValueFactory = mock(org.openrdf.model.ValueFactory.class);
when(badConnection.getValueFactory()).thenReturn(stubValueFactory);
SesameURIReference stubSubject = mock(SesameURIReference.class);
when(stubSubject.getSesameNativeResource()).thenReturn(null);
URIReference stubPredicate = mock(URIReference.class);
when(stubPredicate.getURIText()).thenReturn("");
SesameGraph badGraph = new SesameGraph(badConnection, null);
badGraph.addStatement(stubSubject, property, false);
}
Aggregations