Search in sources :

Example 6 with RepositoryException

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

the class RDF4JProtocolSession method commitTransaction.

public synchronized void commitTransaction() throws RDF4JException, IOException, UnauthorizedException {
    checkRepositoryURL();
    if (transactionURL == null) {
        throw new IllegalStateException("Transaction URL has not been set");
    }
    HttpPut method = null;
    try {
        URIBuilder url = new URIBuilder(transactionURL);
        url.addParameter(Protocol.ACTION_PARAM_NAME, Action.COMMIT.toString());
        method = new HttpPut(url.build());
        final HttpResponse response = execute(method);
        try {
            int code = response.getStatusLine().getStatusCode();
            if (code == HttpURLConnection.HTTP_OK) {
                // we're done.
                transactionURL = null;
                if (ping != null) {
                    ping.cancel(false);
                }
            } else {
                throw new RepositoryException("unable to commit transaction. HTTP error code " + code);
            }
        } finally {
            EntityUtils.consumeQuietly(response.getEntity());
        }
    } catch (URISyntaxException e) {
        logger.error("could not create URL for transaction commit", e);
        throw new RuntimeException(e);
    } finally {
        if (method != null) {
            method.reset();
        }
    }
}
Also used : HttpResponse(org.apache.http.HttpResponse) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) URISyntaxException(java.net.URISyntaxException) HttpPut(org.apache.http.client.methods.HttpPut) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 7 with RepositoryException

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

the class RDF4JProtocolSession method getStatements.

/*---------------------------*
	 * Get/add/remove statements *
	 *---------------------------*/
public void getStatements(Resource subj, IRI pred, Value obj, boolean includeInferred, RDFHandler handler, Resource... contexts) throws IOException, RDFHandlerException, RepositoryException, UnauthorizedException, QueryInterruptedException {
    checkRepositoryURL();
    try {
        String transactionURL = getTransactionURL();
        final boolean useTransaction = transactionURL != null;
        String baseLocation = useTransaction ? transactionURL : Protocol.getStatementsLocation(getQueryURL());
        URIBuilder url = new URIBuilder(baseLocation);
        if (subj != null) {
            url.setParameter(Protocol.SUBJECT_PARAM_NAME, Protocol.encodeValue(subj));
        }
        if (pred != null) {
            url.setParameter(Protocol.PREDICATE_PARAM_NAME, Protocol.encodeValue(pred));
        }
        if (obj != null) {
            url.setParameter(Protocol.OBJECT_PARAM_NAME, Protocol.encodeValue(obj));
        }
        for (String encodedContext : Protocol.encodeContexts(contexts)) {
            url.addParameter(Protocol.CONTEXT_PARAM_NAME, encodedContext);
        }
        url.setParameter(Protocol.INCLUDE_INFERRED_PARAM_NAME, Boolean.toString(includeInferred));
        if (useTransaction) {
            url.setParameter(Protocol.ACTION_PARAM_NAME, Action.GET.toString());
        }
        HttpRequestBase method = useTransaction ? new HttpPut(url.build()) : new HttpGet(url.build());
        try {
            getRDF(method, handler, true);
        } catch (MalformedQueryException e) {
            logger.warn("Server reported unexpected malfored query error", e);
            throw new RepositoryException(e.getMessage(), e);
        } finally {
            method.reset();
        }
    } catch (URISyntaxException e) {
        throw new AssertionError(e);
    }
    pingTransaction();
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpGet(org.apache.http.client.methods.HttpGet) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) URISyntaxException(java.net.URISyntaxException) HttpPut(org.apache.http.client.methods.HttpPut) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 8 with RepositoryException

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

the class RDF4JProtocolSession method getServerProtocol.

/*------------------*
	 * Protocol version *
	 *------------------*/
public String getServerProtocol() throws IOException, RepositoryException, UnauthorizedException {
    checkServerURL();
    HttpGet method = new HttpGet(Protocol.getProtocolLocation(serverURL));
    try {
        return EntityUtils.toString(executeOK(method).getEntity());
    } catch (RepositoryException e) {
        throw e;
    } catch (RDF4JException e) {
        throw new RepositoryException(e);
    } finally {
        method.reset();
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) RDF4JException(org.eclipse.rdf4j.RDF4JException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException)

Example 9 with RepositoryException

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

the class SPARQLProtocolSession method getRDF.

/**
 * Parse the response in this thread using the provided {@link RDFHandler}. All HTTP connections are
 * closed and released in this method
 */
protected void getRDF(HttpUriRequest method, RDFHandler handler, boolean requireContext) throws IOException, RDFHandlerException, RepositoryException, MalformedQueryException, UnauthorizedException, QueryInterruptedException {
    // Specify which formats we support using Accept headers
    Set<RDFFormat> rdfFormats = RDFParserRegistry.getInstance().getKeys();
    if (rdfFormats.isEmpty()) {
        throw new RepositoryException("No tuple RDF parsers have been registered");
    }
    // send the tuple query
    HttpResponse response = sendGraphQueryViaHttp(method, requireContext, rdfFormats);
    try {
        String mimeType = getResponseMIMEType(response);
        try {
            RDFFormat format = RDFFormat.matchMIMEType(mimeType, rdfFormats).orElseThrow(() -> new RepositoryException("Server responded with an unsupported file format: " + mimeType));
            RDFParser parser = Rio.createParser(format, getValueFactory());
            parser.setParserConfig(getParserConfig());
            parser.setParseErrorListener(new ParseErrorLogger());
            parser.setRDFHandler(handler);
            parser.parse(response.getEntity().getContent(), method.getURI().toASCIIString());
        } catch (RDFParseException e) {
            throw new RepositoryException("Malformed query result from server", e);
        }
    } finally {
        EntityUtils.consumeQuietly(response.getEntity());
    }
}
Also used : ParseErrorLogger(org.eclipse.rdf4j.rio.helpers.ParseErrorLogger) HttpResponse(org.apache.http.HttpResponse) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) RDFParser(org.eclipse.rdf4j.rio.RDFParser) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Example 10 with RepositoryException

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

the class SPARQLProtocolSession method execute.

protected HttpResponse execute(HttpUriRequest method) throws IOException, RDF4JException {
    boolean consume = true;
    if (params != null) {
        method.setParams(params);
    }
    HttpResponse response = httpClient.execute(method, httpContext);
    try {
        int httpCode = response.getStatusLine().getStatusCode();
        if (httpCode >= 200 && httpCode < 300 || httpCode == HttpURLConnection.HTTP_NOT_FOUND) {
            consume = false;
            // everything OK, control flow can continue
            return response;
        } else {
            switch(httpCode) {
                case // 401
                HttpURLConnection.HTTP_UNAUTHORIZED:
                    throw new UnauthorizedException();
                case // 503
                HttpURLConnection.HTTP_UNAVAILABLE:
                    throw new QueryInterruptedException();
                default:
                    ErrorInfo errInfo = getErrorInfo(response);
                    // Throw appropriate exception
                    if (errInfo.getErrorType() == ErrorType.MALFORMED_DATA) {
                        throw new RDFParseException(errInfo.getErrorMessage());
                    } else if (errInfo.getErrorType() == ErrorType.UNSUPPORTED_FILE_FORMAT) {
                        throw new UnsupportedRDFormatException(errInfo.getErrorMessage());
                    } else if (errInfo.getErrorType() == ErrorType.MALFORMED_QUERY) {
                        throw new MalformedQueryException(errInfo.getErrorMessage());
                    } else if (errInfo.getErrorType() == ErrorType.UNSUPPORTED_QUERY_LANGUAGE) {
                        throw new UnsupportedQueryLanguageException(errInfo.getErrorMessage());
                    } else if (errInfo.toString().length() > 0) {
                        throw new RepositoryException(errInfo.toString());
                    } else {
                        throw new RepositoryException(response.getStatusLine().getReasonPhrase());
                    }
            }
        }
    } finally {
        if (consume) {
            EntityUtils.consumeQuietly(response.getEntity());
        }
    }
}
Also used : UnsupportedRDFormatException(org.eclipse.rdf4j.rio.UnsupportedRDFormatException) ErrorInfo(org.eclipse.rdf4j.http.protocol.error.ErrorInfo) UnauthorizedException(org.eclipse.rdf4j.http.protocol.UnauthorizedException) HttpResponse(org.apache.http.HttpResponse) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) UnsupportedQueryLanguageException(org.eclipse.rdf4j.query.UnsupportedQueryLanguageException) QueryInterruptedException(org.eclipse.rdf4j.query.QueryInterruptedException) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

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