Search in sources :

Example 11 with Database

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);
    }
}
Also used : CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) Database(org.xmldb.api.base.Database) Permission(org.exist.security.Permission) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException) Test(org.junit.Test)

Example 12 with Database

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();
    }
}
Also used : DatabaseImpl(org.exist.xmldb.DatabaseImpl) Database(org.xmldb.api.base.Database) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) Test(org.junit.Test)

Example 13 with Database

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);
}
Also used : BasicAuthenticator(org.exist.http.servlets.BasicAuthenticator) AuthenticationException(org.exist.security.AuthenticationException) Database(org.xmldb.api.base.Database) EXistException(org.exist.EXistException) URISyntaxException(java.net.URISyntaxException) PermissionDeniedException(org.exist.security.PermissionDeniedException) LockException(org.exist.util.LockException) AuthenticationException(org.exist.security.AuthenticationException) SAXException(org.xml.sax.SAXException) EXistException(org.exist.EXistException) Subject(org.exist.security.Subject)

Example 14 with Database

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);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) Database(org.xmldb.api.base.Database) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool)

Example 15 with 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);
    }
}
Also used : CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) InputStream(java.io.InputStream) Database(org.xmldb.api.base.Database) IndexQueryService(org.exist.xmldb.IndexQueryService) Collection(org.xmldb.api.base.Collection) XMLResource(org.xmldb.api.modules.XMLResource)

Aggregations

Database (org.xmldb.api.base.Database)20 Collection (org.xmldb.api.base.Collection)8 XMLDBException (org.xmldb.api.base.XMLDBException)6 DatabaseImpl (org.exist.xmldb.DatabaseImpl)5 IOException (java.io.IOException)4 StartException (org.exist.start.StartException)4 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)4 XMLResource (org.xmldb.api.modules.XMLResource)4 InputStream (java.io.InputStream)3 Path (java.nio.file.Path)3 EXistException (org.exist.EXistException)3 Test (org.junit.Test)3 Properties (java.util.Properties)2 ServletException (javax.servlet.ServletException)2 AuthenticationException (org.exist.security.AuthenticationException)2 Subject (org.exist.security.Subject)2 BrokerPool (org.exist.storage.BrokerPool)2 DatabaseConfigurationException (org.exist.util.DatabaseConfigurationException)2 Resource (org.xmldb.api.base.Resource)2 File (java.io.File)1