RemoteRepositoryManager extends RepositoryManager { @Override public void addRepositoryConfig(RepositoryConfig config) throws RepositoryException, RepositoryConfigException { try (RDF4JProtocolSession protocolSession = getSharedHttpClientSessionManager() .createRDF4JProtocolSession(serverURL)) { protocolSession.setUsernameAndPassword(username, password); int serverProtocolVersion = Integer.parseInt(protocolSession.getServerProtocol()); if (serverProtocolVersion < 9) { String baseURI = Protocol.getRepositoryLocation(serverURL, config.getID()); Resource ctx = SimpleValueFactory.getInstance().createIRI(baseURI + "#" + config.getID()); protocolSession.setRepository(Protocol.getRepositoryLocation(serverURL, SystemRepository.ID)); Model model = getModelFactory().createEmptyModel(); config.export(model, ctx); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Rio.write(model, baos, protocolSession.getPreferredRDFFormat()); removeRepository(config.getID()); try (InputStream contents = new ByteArrayInputStream(baos.toByteArray())) { protocolSession.upload(contents, baseURI, protocolSession.getPreferredRDFFormat(), false, true, ctx); } } else { if (hasRepositoryConfig(config.getID())) { protocolSession.updateRepository(config); } else { protocolSession.createRepository(config); } } } catch (IOException | QueryEvaluationException | UnauthorizedException | NumberFormatException e) { throw new RepositoryException(e); } } RemoteRepositoryManager(String serverURL); static RemoteRepositoryManager getInstance(String serverURL); static RemoteRepositoryManager getInstance(String serverURL, String username, String password); @Override HttpClient getHttpClient(); @Override void setHttpClient(HttpClient protocolSession); @Override void shutDown(); void setUsernameAndPassword(String username, String password); @Override URL getLocation(); String getServerURL(); @Override RepositoryConfig getRepositoryConfig(String id); @Override Collection<RepositoryInfo> getAllRepositoryInfos(boolean skipSystemRepo); @Override void addRepositoryConfig(RepositoryConfig config); @Override boolean removeRepository(String repositoryID);  }
@Test public void testAddRepositoryConfig() { wireMockRule.stubFor(get(urlEqualTo("/rdf4j-server/protocol")) .willReturn(aResponse().withStatus(200).withBody(Protocol.VERSION))); wireMockRule .stubFor(put(urlEqualTo("/rdf4j-server/repositories/test")).willReturn(aResponse().withStatus(204))); wireMockRule.stubFor(get(urlEqualTo("/rdf4j-server/repositories")) .willReturn(aResponse().withHeader("Content-type", TupleQueryResultFormat.SPARQL.getDefaultMIMEType()) .withBodyFile("repository-list-response.srx") .withStatus(200))); RepositoryConfig config = new RepositoryConfig("test"); subject.addRepositoryConfig(config); wireMockRule.verify( putRequestedFor(urlEqualTo("/rdf4j-server/repositories/test")).withRequestBody(matching("^BRDF.*")) .withHeader("Content-Type", equalTo("application/x-binary-rdf"))); }
@Test public void testAddRepositoryConfigExisting() throws Exception { wireMockRule.stubFor(get(urlEqualTo("/rdf4j-server/protocol")) .willReturn(aResponse().withStatus(200).withBody(Protocol.VERSION))); wireMockRule .stubFor(post(urlEqualTo("/rdf4j-server/repositories/mem-rdf/config")) .willReturn(aResponse().withStatus(204))); wireMockRule.stubFor(get(urlEqualTo("/rdf4j-server/repositories")) .willReturn(aResponse().withHeader("Content-type", TupleQueryResultFormat.SPARQL.getDefaultMIMEType()) .withBodyFile("repository-list-response.srx") .withStatus(200))); RepositoryConfig config = new RepositoryConfig("mem-rdf"); subject.addRepositoryConfig(config); wireMockRule.verify( postRequestedFor(urlEqualTo("/rdf4j-server/repositories/mem-rdf/config")) .withRequestBody(matching("^BRDF.*")) .withHeader("Content-Type", equalTo("application/x-binary-rdf"))); }
@Test public void testAddRepositoryConfigLegacy() { wireMockRule.stubFor( get(urlEqualTo("/rdf4j-server/protocol")).willReturn(aResponse().withStatus(200).withBody("8"))); wireMockRule.stubFor(post(urlPathEqualTo("/rdf4j-server/repositories/SYSTEM/statements")) .willReturn(aResponse().withStatus(204))); wireMockRule.stubFor(get(urlEqualTo("/rdf4j-server/repositories")) .willReturn(aResponse().withHeader("Content-type", TupleQueryResultFormat.SPARQL.getDefaultMIMEType()) .withBodyFile("repository-list-response.srx") .withStatus(200))); RepositoryConfig config = new RepositoryConfig("test"); subject.addRepositoryConfig(config); wireMockRule.verify(postRequestedFor(urlPathEqualTo("/rdf4j-server/repositories/SYSTEM/statements")) .withRequestBody(matching("^BRDF.*")) .withHeader("Content-Type", equalTo("application/x-binary-rdf"))); }