use of org.xmldb.api.base.Database in project exist by eXist-db.
the class RemoteDatabaseImplTest method testGetCollection.
@Test
public void testGetCollection() throws ClassNotFoundException, IllegalAccessException, InstantiationException, XMLDBException, SyntaxException, PermissionDeniedException {
Class<?> cl = Class.forName(DB_DRIVER);
Database database = (Database) cl.newInstance();
DatabaseManager.registerDatabase(database);
Collection rootCollection = DatabaseManager.getCollection(getUri() + XmldbURI.ROOT_COLLECTION, "admin", "");
CollectionManagementService cms = (CollectionManagementService) rootCollection.getService("CollectionManagementService", "1.0");
Collection adminCollection = cms.createCollection(ADMIN_COLLECTION_NAME);
UserManagementService ums = (UserManagementService) rootCollection.getService("UserManagementService", "1.0");
if (ums != null) {
Permission p = ums.getPermissions(adminCollection);
p.setMode(Permission.USER_STRING + "=+read,+write," + Permission.GROUP_STRING + "=-read,-write," + Permission.OTHER_STRING + "=-read,-write");
ums.setPermissions(adminCollection, p);
Collection guestCollection = DatabaseManager.getCollection(getUri() + XmldbURI.ROOT_COLLECTION + "/" + ADMIN_COLLECTION_NAME, "guest", "guest");
Resource resource = guestCollection.createResource("testguest", "BinaryResource");
resource.setContent("123".getBytes());
try {
guestCollection.storeResource(resource);
fail();
} catch (XMLDBException e) {
}
cms.removeCollection(ADMIN_COLLECTION_NAME);
}
}
use of org.xmldb.api.base.Database in project exist by eXist-db.
the class CDataIntergationTest method cdataXmlDbApi.
@Test
public void cdataXmlDbApi() throws XMLDBException {
final String docName = "xmldb-cdata-test.xml";
final Database database = new DatabaseImpl();
DatabaseManager.registerDatabase(database);
// store document
Collection root = DatabaseManager.getCollection(XmldbURI.LOCAL_DB, TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD);
try {
final Resource resource = root.createResource(docName, XMLResource.RESOURCE_TYPE);
resource.setContent(cdata_xml);
root.storeResource(resource);
} finally {
root.close();
}
// retrieve document
root = DatabaseManager.getCollection(XmldbURI.LOCAL_DB, TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD);
try {
final Resource resource = root.getResource(docName);
assertNotNull(resource);
assertEquals(cdata_xml, resource.getContent().toString());
} finally {
root.close();
}
}
use of org.xmldb.api.base.Database in project exist by eXist-db.
the class XQueryURLRewrite method configure.
private void configure() throws ServletException {
if (pool != null) {
return;
}
try {
final Class<?> driver = Class.forName(DRIVER);
final Database database = (Database) driver.newInstance();
database.setProperty("create-database", "true");
DatabaseManager.registerDatabase(database);
if (LOG.isDebugEnabled()) {
LOG.debug("Initialized database");
}
} catch (final Exception e) {
final String errorMessage = "Failed to initialize database driver";
LOG.error(errorMessage, e);
throw new ServletException(errorMessage + ": " + e.getMessage(), e);
}
try {
pool = BrokerPool.getInstance();
} catch (final EXistException e) {
throw new ServletException("Could not initialize db: " + e.getMessage(), e);
}
defaultUser = pool.getSecurityManager().getGuestSubject();
final String username = config.getInitParameter("user");
if (username != null) {
final String password = config.getInitParameter("password");
try {
final Subject user = pool.getSecurityManager().authenticate(username, password);
if (user != null && user.isAuthenticated()) {
defaultUser = user;
}
} catch (final AuthenticationException e) {
LOG.error("User can not be authenticated ({}), using default user.", username);
}
}
authenticator = new BasicAuthenticator(pool);
}
use of org.xmldb.api.base.Database in project exist by eXist-db.
the class DeadlockTest method startDB.
@BeforeClass
public static void startDB() throws DatabaseConfigurationException, EXistException, PermissionDeniedException, IOException, SAXException, CollectionConfigurationException, LockException, ClassNotFoundException, IllegalAccessException, InstantiationException, XMLDBException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final TransactionManager transact = pool.getTransactionManager();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = transact.beginTransaction()) {
final Collection root = broker.getOrCreateCollection(transaction, XmldbURI.ROOT_COLLECTION_URI);
assertNotNull(root);
broker.saveCollection(transaction, root);
final Collection test = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
assertNotNull(test);
broker.saveCollection(transaction, test);
transact.commit(transaction);
// initialize XML:DB driver
final Class<?> cl = Class.forName("org.exist.xmldb.DatabaseImpl");
final Database database = (Database) cl.newInstance();
DatabaseManager.registerDatabase(database);
}
}
use of org.xmldb.api.base.Database in project exist by eXist-db.
the class OptimizerTest method initDatabase.
@BeforeClass
public static void initDatabase() throws ClassNotFoundException, IllegalAccessException, InstantiationException, XMLDBException, IOException, URISyntaxException {
// initialize driver
Class<?> cl = Class.forName("org.exist.xmldb.DatabaseImpl");
Database database = (Database) cl.newInstance();
database.setProperty("create-database", "true");
DatabaseManager.registerDatabase(database);
Collection root = DatabaseManager.getCollection(XmldbURI.LOCAL_DB, "admin", "");
CollectionManagementService service = (CollectionManagementService) root.getService("CollectionManagementService", "1.0");
testCollection = service.createCollection("test");
Assert.assertNotNull(testCollection);
IndexQueryService idxConf = (IndexQueryService) testCollection.getService("IndexQueryService", "1.0");
idxConf.configureCollection(COLLECTION_CONFIG);
XMLResource resource = (XMLResource) testCollection.createResource("test.xml", "XMLResource");
resource.setContent(XML);
testCollection.storeResource(resource);
for (final String sampleName : SAMPLES.getShakespeareXmlSampleNames()) {
resource = (XMLResource) testCollection.createResource(sampleName, XMLResource.RESOURCE_TYPE);
try (final InputStream is = SAMPLES.getShakespeareSample(sampleName)) {
resource.setContent(InputStreamUtil.readString(is, UTF_8));
}
testCollection.storeResource(resource);
}
}
Aggregations