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;
}
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;
}
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;
}
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);
}
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;
}
Aggregations