Search in sources :

Example 46 with RepositoryException

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

the class RDF4JProtocolSession method setNamespacePrefix.

public void setNamespacePrefix(String prefix, String name) throws IOException, RepositoryException, UnauthorizedException {
    checkRepositoryURL();
    HttpPut method = new HttpPut(Protocol.getNamespacePrefixLocation(getQueryURL(), prefix));
    try {
        method.setEntity(new StringEntity(name, ContentType.create("text/plain", "UTF-8")));
        executeNoContent(method);
    } catch (RepositoryException e) {
        throw e;
    } catch (RDF4JException e) {
        throw new RepositoryException(e);
    } finally {
        method.reset();
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) RDF4JException(org.eclipse.rdf4j.RDF4JException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) HttpPut(org.apache.http.client.methods.HttpPut)

Example 47 with RepositoryException

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

the class RDF4JProtocolSession method getNamespaces.

public void getNamespaces(TupleQueryResultHandler handler) throws IOException, TupleQueryResultHandlerException, RepositoryException, UnauthorizedException, QueryInterruptedException {
    checkRepositoryURL();
    HttpGet method = new HttpGet(Protocol.getNamespacesLocation(getQueryURL()));
    try {
        getTupleQueryResult(method, handler);
    } catch (MalformedQueryException e) {
        logger.warn("Server reported unexpected malfored query error", e);
        throw new RepositoryException(e.getMessage(), e);
    } finally {
        method.reset();
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException)

Example 48 with RepositoryException

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

the class RDF4JProtocolSession method beginTransaction.

public synchronized void beginTransaction(IsolationLevel isolationLevel) throws RDF4JException, IOException, UnauthorizedException {
    checkRepositoryURL();
    if (transactionURL != null) {
        throw new IllegalStateException("Transaction URL is already set");
    }
    HttpPost method = new HttpPost(Protocol.getTransactionsLocation(getRepositoryURL()));
    try {
        method.setHeader("Content-Type", Protocol.FORM_MIME_TYPE + "; charset=utf-8");
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        if (isolationLevel != null) {
            params.add(new BasicNameValuePair(Protocol.ISOLATION_LEVEL_PARAM_NAME, isolationLevel.getURI().stringValue()));
        }
        method.setEntity(new UrlEncodedFormEntity(params, UTF8));
        HttpResponse response = execute(method);
        try {
            int code = response.getStatusLine().getStatusCode();
            if (code == HttpURLConnection.HTTP_CREATED) {
                transactionURL = response.getFirstHeader("Location").getValue();
                if (transactionURL == null) {
                    throw new RepositoryException("no valid transaction ID received in server response.");
                } else {
                    pingTransaction();
                }
            } else {
                throw new RepositoryException("unable to start transaction. HTTP error code " + code);
            }
        } finally {
            EntityUtils.consume(response.getEntity());
        }
    } finally {
        method.reset();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity)

Example 49 with RepositoryException

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

the class RDF4JProtocolSession method size.

/*-------------------------*
	 * Repository/context size *
	 *-------------------------*/
public long size(Resource... contexts) throws IOException, RepositoryException, UnauthorizedException {
    checkRepositoryURL();
    try {
        String transactionURL = getTransactionURL();
        final boolean useTransaction = transactionURL != null;
        String baseLocation = useTransaction ? appendAction(transactionURL, Action.SIZE) : Protocol.getSizeLocation(getQueryURL());
        URIBuilder url = new URIBuilder(baseLocation);
        String[] encodedContexts = Protocol.encodeContexts(contexts);
        for (int i = 0; i < encodedContexts.length; i++) {
            url.addParameter(Protocol.CONTEXT_PARAM_NAME, encodedContexts[i]);
        }
        final HttpRequestBase method = useTransaction ? new HttpPut(url.build()) : new HttpGet(url.build());
        try {
            String response = EntityUtils.toString(executeOK(method).getEntity());
            pingTransaction();
            try {
                return Long.parseLong(response);
            } catch (NumberFormatException e) {
                throw new RepositoryException("Server responded with invalid size value: " + response);
            }
        } finally {
            method.reset();
        }
    } catch (URISyntaxException e) {
        throw new AssertionError(e);
    } catch (RepositoryException e) {
        throw e;
    } catch (RDF4JException e) {
        throw new RepositoryException(e);
    }
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpGet(org.apache.http.client.methods.HttpGet) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) URISyntaxException(java.net.URISyntaxException) HttpPut(org.apache.http.client.methods.HttpPut) URIBuilder(org.apache.http.client.utils.URIBuilder) RDF4JException(org.eclipse.rdf4j.RDF4JException)

Example 50 with RepositoryException

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

the class SPARQLProtocolSession method getErrorInfo.

protected ErrorInfo getErrorInfo(HttpResponse response) throws RepositoryException {
    try {
        ErrorInfo errInfo = ErrorInfo.parse(EntityUtils.toString(response.getEntity()));
        logger.warn("Server reports problem: {}", errInfo.getErrorMessage());
        return errInfo;
    } catch (IOException e) {
        logger.warn("Unable to retrieve error info from server");
        throw new RepositoryException("Unable to retrieve error info from server", e);
    }
}
Also used : ErrorInfo(org.eclipse.rdf4j.http.protocol.error.ErrorInfo) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) IOException(java.io.IOException)

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