Search in sources :

Example 1 with RepositoryException

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;
}
Also used : Form(org.restlet.data.Form) RepositoryException(org.openrdf.repository.RepositoryException) DatasetImpl(org.openrdf.query.impl.DatasetImpl) URI(org.openrdf.model.URI) Value(org.openrdf.model.Value) ResourceException(org.restlet.resource.ResourceException)

Example 2 with RepositoryException

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);
    }
}
Also used : Graph(com.thoughtworks.studios.shine.semweb.Graph) ShineRuntimeException(com.thoughtworks.studios.shine.ShineRuntimeException) RepositoryException(org.openrdf.repository.RepositoryException)

Example 3 with RepositoryException

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);
        }
    }
}
Also used : ShineRuntimeException(com.thoughtworks.studios.shine.ShineRuntimeException) RDFHandlerException(org.openrdf.rio.RDFHandlerException) RDFXMLWriter(org.openrdf.rio.rdfxml.RDFXMLWriter) Statement(org.openrdf.model.Statement) RepositoryException(org.openrdf.repository.RepositoryException) IOException(java.io.IOException)

Example 4 with RepositoryException

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);
}
Also used : ShineRuntimeException(com.thoughtworks.studios.shine.ShineRuntimeException) RepositoryException(org.openrdf.repository.RepositoryException) URI(org.openrdf.model.URI)

Example 5 with RepositoryException

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);
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) RepositoryException(org.openrdf.repository.RepositoryException) Test(org.junit.Test)

Aggregations

RepositoryException (org.openrdf.repository.RepositoryException)36 RepositoryConnection (org.openrdf.repository.RepositoryConnection)20 ShineRuntimeException (com.thoughtworks.studios.shine.ShineRuntimeException)12 URI (org.openrdf.model.URI)12 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)10 Test (org.junit.Test)7 RdfRepresentation (org.apache.stanbol.entityhub.model.sesame.RdfRepresentation)5 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)5 IOException (java.io.IOException)4 Value (org.openrdf.model.Value)4 Repository (org.openrdf.repository.Repository)4 InputStreamReader (java.io.InputStreamReader)3 SparqlFieldQuery (org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery)3 BindingSet (org.openrdf.query.BindingSet)3 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)3 TupleQueryResult (org.openrdf.query.TupleQueryResult)3 SailRepository (org.openrdf.repository.sail.SailRepository)3 Graph (com.thoughtworks.studios.shine.semweb.Graph)2 RdfValueFactory (org.apache.stanbol.entityhub.model.sesame.RdfValueFactory)2 Model (org.openrdf.model.Model)2