Search in sources :

Example 1 with RepositoryException

use of org.eclipse.rdf4j.repository.RepositoryException in project rdf4j by eclipse.

the class Repositories method get.

/**
 * Opens a {@link RepositoryConnection} to the given Repository within a transaction, sends the connection
 * to the given {@link Function}, before either rolling back the transaction if it failed, or committing
 * the transaction if it was successful.
 *
 * @param <T>
 *        The type of the return value.
 * @param repository
 *        The {@link Repository} to open a connection to.
 * @param processFunction
 *        A {@link Function} that performs an action on the connection and returns a result.
 * @return The result of applying the function.
 * @throws RepositoryException
 *         If there was an exception dealing with the Repository.
 * @throws UnknownTransactionStateException
 *         If the transaction state was not properly recognised. (Optional specific exception)
 */
public static <T> T get(Repository repository, Function<RepositoryConnection, T> processFunction) throws RepositoryException, UnknownTransactionStateException {
    RepositoryConnection conn = null;
    try {
        conn = repository.getConnection();
        conn.begin();
        T result = processFunction.apply(conn);
        conn.commit();
        return result;
    } catch (RepositoryException e) {
        if (conn != null && conn.isActive()) {
            conn.rollback();
        }
        throw e;
    } finally {
        if (conn != null && conn.isOpen()) {
            conn.close();
        }
    }
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException)

Example 2 with RepositoryException

use of org.eclipse.rdf4j.repository.RepositoryException in project rdf4j by eclipse.

the class AbstractRepositoryConnection method remove.

@Override
public void remove(Iterable<? extends Statement> statements, Resource... contexts) throws RepositoryException {
    OpenRDFUtil.verifyContextNotNull(contexts);
    boolean localTransaction = startLocalTransaction();
    try {
        for (Statement st : statements) {
            remove(st, contexts);
        }
        conditionalCommit(localTransaction);
    } catch (RepositoryException e) {
        conditionalRollback(localTransaction);
        throw e;
    } catch (RuntimeException e) {
        conditionalRollback(localTransaction);
        throw e;
    }
}
Also used : Statement(org.eclipse.rdf4j.model.Statement) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException)

Example 3 with RepositoryException

use of org.eclipse.rdf4j.repository.RepositoryException in project rdf4j by eclipse.

the class RDF4JProtocolSession method upload.

protected void upload(HttpEntity reqEntity, String baseURI, boolean overwrite, boolean preserveNodeIds, Action action, Resource... contexts) throws IOException, RDFParseException, RepositoryException, UnauthorizedException {
    OpenRDFUtil.verifyContextNotNull(contexts);
    checkRepositoryURL();
    String transactionURL = getTransactionURL();
    boolean useTransaction = transactionURL != null;
    try {
        String baseLocation = useTransaction ? transactionURL : Protocol.getStatementsLocation(getQueryURL());
        URIBuilder url = new URIBuilder(baseLocation);
        // Set relevant query parameters
        for (String encodedContext : Protocol.encodeContexts(contexts)) {
            url.addParameter(Protocol.CONTEXT_PARAM_NAME, encodedContext);
        }
        if (baseURI != null && baseURI.trim().length() != 0) {
            String encodedBaseURI = Protocol.encodeValue(SimpleValueFactory.getInstance().createIRI(baseURI));
            url.setParameter(Protocol.BASEURI_PARAM_NAME, encodedBaseURI);
        }
        if (preserveNodeIds) {
            url.setParameter(Protocol.PRESERVE_BNODE_ID_PARAM_NAME, "true");
        }
        if (useTransaction) {
            if (action == null) {
                throw new IllegalArgumentException("action can not be null on transaction operation");
            }
            url.setParameter(Protocol.ACTION_PARAM_NAME, action.toString());
        }
        // Select appropriate HTTP method
        HttpEntityEnclosingRequestBase method = null;
        try {
            if (overwrite || useTransaction) {
                method = new HttpPut(url.build());
            } else {
                method = new HttpPost(url.build());
            }
            // Set payload
            method.setEntity(reqEntity);
            // Send request
            try {
                executeNoContent((HttpUriRequest) method);
            } catch (RepositoryException e) {
                throw e;
            } catch (RDFParseException e) {
                throw e;
            } catch (RDF4JException e) {
                throw new RepositoryException(e);
            }
        } finally {
            if (method != null) {
                method.reset();
            }
        }
    } catch (URISyntaxException e) {
        throw new AssertionError(e);
    }
    pingTransaction();
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) RDF4JException(org.eclipse.rdf4j.RDF4JException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) URISyntaxException(java.net.URISyntaxException) HttpPut(org.apache.http.client.methods.HttpPut) URIBuilder(org.apache.http.client.utils.URIBuilder) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Example 4 with RepositoryException

use of org.eclipse.rdf4j.repository.RepositoryException in project rdf4j by eclipse.

the class RDF4JProtocolSession method sendTransaction.

/**
 * Sends a transaction list as serialized XML to the server.
 *
 * @deprecated since 2.8.0
 * @param txn
 * @throws IOException
 * @throws RepositoryException
 * @throws UnauthorizedException
 */
@Deprecated
public void sendTransaction(final Iterable<? extends TransactionOperation> txn) throws IOException, RepositoryException, UnauthorizedException {
    checkRepositoryURL();
    HttpPost method = new HttpPost(Protocol.getStatementsLocation(getQueryURL()));
    try {
        // Create a RequestEntity for the transaction data
        method.setEntity(new AbstractHttpEntity() {

            public long getContentLength() {
                // don't know
                return -1;
            }

            public Header getContentType() {
                return new BasicHeader("Content-Type", Protocol.TXN_MIME_TYPE);
            }

            public boolean isRepeatable() {
                return true;
            }

            public boolean isStreaming() {
                return true;
            }

            public InputStream getContent() throws IOException, IllegalStateException {
                ByteArrayOutputStream buf = new ByteArrayOutputStream();
                writeTo(buf);
                return new ByteArrayInputStream(buf.toByteArray());
            }

            public void writeTo(OutputStream out) throws IOException {
                TransactionWriter txnWriter = new TransactionWriter();
                txnWriter.serialize(txn, out);
            }
        });
        try {
            executeNoContent(method);
        } catch (RepositoryException e) {
            throw e;
        } catch (RDF4JException e) {
            throw new RepositoryException(e);
        }
    } finally {
        method.reset();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) TransactionWriter(org.eclipse.rdf4j.http.protocol.transaction.TransactionWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) ByteArrayInputStream(java.io.ByteArrayInputStream) RDF4JException(org.eclipse.rdf4j.RDF4JException) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) BasicHeader(org.apache.http.message.BasicHeader)

Example 5 with RepositoryException

use of org.eclipse.rdf4j.repository.RepositoryException in project rdf4j by eclipse.

the class RDF4JProtocolSession method rollbackTransaction.

public synchronized void rollbackTransaction() throws RDF4JException, IOException, UnauthorizedException {
    checkRepositoryURL();
    if (transactionURL == null) {
        throw new IllegalStateException("Transaction URL has not been set");
    }
    String requestURL = transactionURL;
    HttpDelete method = new HttpDelete(requestURL);
    try {
        final HttpResponse response = execute(method);
        try {
            int code = response.getStatusLine().getStatusCode();
            if (code == HttpURLConnection.HTTP_NO_CONTENT) {
                // we're done.
                transactionURL = null;
                if (ping != null) {
                    ping.cancel(false);
                }
            } else {
                throw new RepositoryException("unable to rollback transaction. HTTP error code " + code);
            }
        } finally {
            EntityUtils.consumeQuietly(response.getEntity());
        }
    } finally {
        method.reset();
    }
}
Also used : HttpDelete(org.apache.http.client.methods.HttpDelete) HttpResponse(org.apache.http.HttpResponse) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException)

Aggregations

RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)67 IOException (java.io.IOException)24 MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)20 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)15 RDF4JException (org.eclipse.rdf4j.RDF4JException)13 HttpResponse (org.apache.http.HttpResponse)11 TupleQueryResult (org.eclipse.rdf4j.query.TupleQueryResult)10 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)8 HttpGet (org.apache.http.client.methods.HttpGet)7 Resource (org.eclipse.rdf4j.model.Resource)7 Statement (org.eclipse.rdf4j.model.Statement)6 ArrayList (java.util.ArrayList)5 HttpPut (org.apache.http.client.methods.HttpPut)5 RDF4JProtocolSession (org.eclipse.rdf4j.http.client.RDF4JProtocolSession)5 UnauthorizedException (org.eclipse.rdf4j.http.protocol.UnauthorizedException)5 BindingSet (org.eclipse.rdf4j.query.BindingSet)5 TupleQuery (org.eclipse.rdf4j.query.TupleQuery)5 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4