Search in sources :

Example 1 with RDF4JProtocolSession

use of org.eclipse.rdf4j.http.client.RDF4JProtocolSession in project rdf4j by eclipse.

the class HTTPRepository method useCompatibleMode.

/**
 * Verify if transaction handling should be done in backward-compatible mode (this is the case when
 * communicating with an older Sesame Server).
 *
 * @return <code>true</code> if the Server does not support the extended transaction protocol,
 *         <code>false</code> otherwise.
 * @throws RepositoryException
 *         if something went wrong while querying the server for the protocol version.
 */
boolean useCompatibleMode() throws RepositoryException {
    Boolean result = compatibleMode;
    if (result == null) {
        synchronized (this) {
            result = compatibleMode;
            if (result == null) {
                try (RDF4JProtocolSession client = createHTTPClient()) {
                    final String serverProtocolVersion = client.getServerProtocol();
                    // protocol version 7 supports the new transaction
                    // handling. If the server is older, we need to run in
                    // backward-compatible mode.
                    result = compatibleMode = (Integer.parseInt(serverProtocolVersion) < 7);
                } catch (NumberFormatException e) {
                    throw new RepositoryException("could not read protocol version from server: ", e);
                } catch (IOException e) {
                    throw new RepositoryException("could not read protocol version from server: ", e);
                }
            }
        }
    }
    return result;
}
Also used : RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) RDF4JProtocolSession(org.eclipse.rdf4j.http.client.RDF4JProtocolSession) IOException(java.io.IOException)

Example 2 with RDF4JProtocolSession

use of org.eclipse.rdf4j.http.client.RDF4JProtocolSession in project rdf4j by eclipse.

the class HTTPRepository method createHTTPClient.

/**
 * Creates a new HTTPClient object. Subclasses may override to return a more specific HTTPClient subtype.
 *
 * @return a HTTPClient object.
 */
protected RDF4JProtocolSession createHTTPClient() {
    // initialize HTTP client
    RDF4JProtocolSession httpClient = getHttpClientSessionManager().createRDF4JProtocolSession(serverURL);
    httpClient.setValueFactory(SimpleValueFactory.getInstance());
    if (repositoryURL != null) {
        httpClient.setRepository(repositoryURL);
    }
    if (tupleFormat != null) {
        httpClient.setPreferredTupleQueryResultFormat(tupleFormat);
    }
    if (rdfFormat != null) {
        httpClient.setPreferredRDFFormat(rdfFormat);
    }
    if (username != null) {
        httpClient.setUsernameAndPassword(username, password);
    }
    return httpClient;
}
Also used : RDF4JProtocolSession(org.eclipse.rdf4j.http.client.RDF4JProtocolSession)

Example 3 with RDF4JProtocolSession

use of org.eclipse.rdf4j.http.client.RDF4JProtocolSession in project rdf4j by eclipse.

the class HTTPRepository method isWritable.

@Override
public boolean isWritable() throws RepositoryException {
    if (!isInitialized()) {
        throw new IllegalStateException("HTTPRepository not initialized.");
    }
    boolean isWritable = false;
    try (RDF4JProtocolSession client = createHTTPClient()) {
        final String repositoryURL = client.getRepositoryURL();
        final TupleQueryResult repositoryList = client.getRepositoryList();
        try {
            while (repositoryList.hasNext()) {
                final BindingSet bindingSet = repositoryList.next();
                final Value uri = bindingSet.getValue("uri");
                if (uri != null && uri.stringValue().equals(repositoryURL)) {
                    isWritable = Literals.getBooleanValue(bindingSet.getValue("writable"), false);
                    break;
                }
            }
        } finally {
            repositoryList.close();
        }
    } catch (QueryEvaluationException e) {
        throw new RepositoryException(e);
    } catch (IOException e) {
        throw new RepositoryException(e);
    }
    return isWritable;
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) Value(org.eclipse.rdf4j.model.Value) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) RDF4JProtocolSession(org.eclipse.rdf4j.http.client.RDF4JProtocolSession) IOException(java.io.IOException) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult)

Example 4 with RDF4JProtocolSession

use of org.eclipse.rdf4j.http.client.RDF4JProtocolSession in project rdf4j by eclipse.

the class RemoteRepositoryManager method getRepositoryConfig.

@Override
public RepositoryConfig getRepositoryConfig(String id) throws RepositoryException {
    Model model = new LinkedHashModel();
    try (RDF4JProtocolSession httpClient = getSesameClient().createRDF4JProtocolSession(serverURL)) {
        httpClient.setUsernameAndPassword(username, password);
        httpClient.setRepository(Protocol.getRepositoryLocation(serverURL, SystemRepository.ID));
        httpClient.getStatements(null, null, null, true, new StatementCollector(model));
    } catch (IOException | QueryEvaluationException | UnauthorizedException ue) {
        throw new RepositoryException(ue);
    }
    return RepositoryConfigUtil.getRepositoryConfig(model, id);
}
Also used : QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) StatementCollector(org.eclipse.rdf4j.rio.helpers.StatementCollector) Model(org.eclipse.rdf4j.model.Model) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) UnauthorizedException(org.eclipse.rdf4j.http.protocol.UnauthorizedException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) RDF4JProtocolSession(org.eclipse.rdf4j.http.client.RDF4JProtocolSession) IOException(java.io.IOException)

Example 5 with RDF4JProtocolSession

use of org.eclipse.rdf4j.http.client.RDF4JProtocolSession in project rdf4j by eclipse.

the class RemoteRepositoryManager method getAllRepositoryInfos.

@Override
public Collection<RepositoryInfo> getAllRepositoryInfos(boolean skipSystemRepo) throws RepositoryException {
    List<RepositoryInfo> result = new ArrayList<RepositoryInfo>();
    try (RDF4JProtocolSession httpClient = getSesameClient().createRDF4JProtocolSession(serverURL)) {
        httpClient.setUsernameAndPassword(username, password);
        TupleQueryResult responseFromServer = httpClient.getRepositoryList();
        while (responseFromServer.hasNext()) {
            BindingSet bindingSet = responseFromServer.next();
            RepositoryInfo repInfo = new RepositoryInfo();
            String id = Literals.getLabel(bindingSet.getValue("id"), null);
            if (skipSystemRepo && id.equals(SystemRepository.ID)) {
                continue;
            }
            Value uri = bindingSet.getValue("uri");
            String description = Literals.getLabel(bindingSet.getValue("title"), null);
            boolean readable = Literals.getBooleanValue(bindingSet.getValue("readable"), false);
            boolean writable = Literals.getBooleanValue(bindingSet.getValue("writable"), false);
            if (uri instanceof IRI) {
                try {
                    repInfo.setLocation(new URL(uri.toString()));
                } catch (MalformedURLException e) {
                    logger.warn("Server reported malformed repository URL: {}", uri);
                }
            }
            repInfo.setId(id);
            repInfo.setDescription(description);
            repInfo.setReadable(readable);
            repInfo.setWritable(writable);
            result.add(repInfo);
        }
    } catch (IOException ioe) {
        logger.warn("Unable to retrieve list of repositories", ioe);
        throw new RepositoryException(ioe);
    } catch (QueryEvaluationException qee) {
        logger.warn("Unable to retrieve list of repositories", qee);
        throw new RepositoryException(qee);
    } catch (UnauthorizedException ue) {
        logger.warn("Not authorized to retrieve list of repositories", ue);
        throw new RepositoryException(ue);
    } catch (RepositoryException re) {
        logger.warn("Unable to retrieve list of repositories", re);
        throw re;
    }
    return result;
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) IRI(org.eclipse.rdf4j.model.IRI) MalformedURLException(java.net.MalformedURLException) ArrayList(java.util.ArrayList) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) IOException(java.io.IOException) URL(java.net.URL) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) Value(org.eclipse.rdf4j.model.Value) UnauthorizedException(org.eclipse.rdf4j.http.protocol.UnauthorizedException) RDF4JProtocolSession(org.eclipse.rdf4j.http.client.RDF4JProtocolSession) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult)

Aggregations

RDF4JProtocolSession (org.eclipse.rdf4j.http.client.RDF4JProtocolSession)7 IOException (java.io.IOException)6 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)5 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)4 UnauthorizedException (org.eclipse.rdf4j.http.protocol.UnauthorizedException)3 Model (org.eclipse.rdf4j.model.Model)2 Value (org.eclipse.rdf4j.model.Value)2 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)2 BindingSet (org.eclipse.rdf4j.query.BindingSet)2 TupleQueryResult (org.eclipse.rdf4j.query.TupleQueryResult)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 IRI (org.eclipse.rdf4j.model.IRI)1 Resource (org.eclipse.rdf4j.model.Resource)1 RepositoryConfigException (org.eclipse.rdf4j.repository.config.RepositoryConfigException)1 StatementCollector (org.eclipse.rdf4j.rio.helpers.StatementCollector)1