Search in sources :

Example 61 with CollectionManagementService

use of org.xmldb.api.modules.CollectionManagementService in project exist by eXist-db.

the class BackupRestoreSecurityPrincipalsTest method restoreConflictingUsername.

/**
 * 1. With an empty database we create three
 *    users: 'frank', 'joe', and 'jack'.
 *
 * 2. We create a backup of the database which contains
 *    the three users from (1).
 *
 * 3. We destroy the database, restart the server,
 *    and start again with a clean database.
 *
 * 4. With an (again) empty database we create two
 *    users: 'frank', and 'jack'.
 *
 * 5. We then try and restore the database backup from (2), which
 *    contains the original 'frank', 'joe', and 'jack' users.
 *
 * frank will have the same username and user id in the current
 * database and the backup we are trying to restore.
 *
 * joe does not exist in the current database, but his user id
 * in the backup will collide with that of jack in the current database.
 *
 * jack will have a different user id in the backup when compared to the current
 * database, however he will have the same username.
 *
 * We want to make sure that after the restore, all three users are present
 * that they have distinct and expected user ids and that any resources
 * that were owned by them are still correctly owner by them (and not some other user).
 */
@Test
public void restoreConflictingUsername() throws PermissionDeniedException, EXistException, SAXException, ParserConfigurationException, IOException, URISyntaxException, XMLDBException, IllegalAccessException, ClassNotFoundException, InstantiationException {
    // creates a database with new users: 'frank(id=11)', 'joe(id=12)', and 'jack(id=13)'
    createInitialUsers(FRANK_USER, JOE_USER, JACK_USER);
    // create a backup of the database (which has the initial users)
    final Path backupFile = backupDatabase();
    // reset database to empty
    server.restart(true);
    // create new users: 'frank(id=11)' and 'jack(id=12)'
    createInitialUsers(FRANK_USER, JACK_USER);
    final String accountQuery = "declare namespace c = 'http://exist-db.org/Configuration';\n" + "for $account in //c:account\n" + "return\n" + "<user id='{$account/@id}' name='{$account/c:name}'/>";
    final XPathQueryService xqs = (XPathQueryService) server.getRoot().getService("XPathQueryService", "1.0");
    final SecurityManagerImpl sm = (SecurityManagerImpl) BrokerPool.getInstance().getSecurityManager();
    // check the current user accounts
    ResourceSet result = xqs.query(accountQuery);
    assertUser(RealmImpl.ADMIN_ACCOUNT_ID, SecurityManager.DBA_USER, ((XMLResource) result.getResource(0)).getContentAsDOM());
    assertUser(RealmImpl.GUEST_ACCOUNT_ID, SecurityManager.GUEST_USER, ((XMLResource) result.getResource(1)).getContentAsDOM());
    assertUser(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 1, "frank", ((XMLResource) result.getResource(2)).getContentAsDOM());
    assertUser(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 2, "jack", ((XMLResource) result.getResource(3)).getContentAsDOM());
    // check the last user id
    // last account id should be that of 'jack'
    assertEquals(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 2, sm.getLastAccountId());
    // create a test collection and give everyone access
    final CollectionManagementService cms = (CollectionManagementService) server.getRoot().getService("CollectionManagementService", "1.0");
    final Collection test = cms.createCollection("test");
    final UserManagementService testUms = (UserManagementService) test.getService("UserManagementService", "1.0");
    testUms.chmod("rwxrwxrwx");
    // create and store a new document as 'frank'
    final Collection frankTest = DatabaseManager.getCollection("xmldb:exist:///db/test", FRANK_USER, FRANK_USER);
    final String FRANKS_DOCUMENT = "franks-document.xml";
    final Resource frankDoc = frankTest.createResource(FRANKS_DOCUMENT, XMLResource.RESOURCE_TYPE);
    frankDoc.setContent("<hello>frank</hello>");
    frankTest.storeResource(frankDoc);
    // create and store a new document as 'jack'
    final Collection jackTest = DatabaseManager.getCollection("xmldb:exist:///db/test", JACK_USER, JACK_USER);
    final String JACKS_DOCUMENT = "jacks-document.xml";
    final Resource jackDoc = jackTest.createResource(JACKS_DOCUMENT, XMLResource.RESOURCE_TYPE);
    jackDoc.setContent("<hello>jack</hello>");
    jackTest.storeResource(jackDoc);
    // restore the database backup
    final EXistRestoreService service = (EXistRestoreService) server.getRoot().getService("RestoreService", "1.0");
    service.restore(backupFile.normalize().toAbsolutePath().toString(), null, new NullRestoreServiceTaskListener(), false);
    // check the current user accounts after the restore
    result = xqs.query(accountQuery);
    assertUser(RealmImpl.ADMIN_ACCOUNT_ID, SecurityManager.DBA_USER, ((XMLResource) result.getResource(0)).getContentAsDOM());
    assertUser(RealmImpl.GUEST_ACCOUNT_ID, SecurityManager.GUEST_USER, ((XMLResource) result.getResource(1)).getContentAsDOM());
    assertUser(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 1, FRANK_USER, ((XMLResource) result.getResource(2)).getContentAsDOM());
    assertUser(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 2, JACK_USER, ((XMLResource) result.getResource(3)).getContentAsDOM());
    assertUser(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 3, JOE_USER, ((XMLResource) result.getResource(4)).getContentAsDOM());
    // check the last user id after the restore
    // last account id should be that of 'joe'
    assertEquals(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 3, sm.getLastAccountId());
    // check the owner of frank's document after restore
    final Resource fDoc = test.getResource(FRANKS_DOCUMENT);
    final Permission franksDocPermissions = testUms.getPermissions(fDoc);
    assertEquals(FRANK_USER, franksDocPermissions.getOwner().getName());
    // check the owner of jack's document after restore
    final Resource jDoc = test.getResource(JACKS_DOCUMENT);
    final Permission jacksDocPermissions = testUms.getPermissions(jDoc);
    assertEquals(JACK_USER, jacksDocPermissions.getOwner().getName());
}
Also used : Path(java.nio.file.Path) CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) EXistRestoreService(org.exist.xmldb.EXistRestoreService) XPathQueryService(org.xmldb.api.modules.XPathQueryService) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) NullRestoreServiceTaskListener(org.exist.xmldb.NullRestoreServiceTaskListener) Collection(org.xmldb.api.base.Collection) UserManagementService(org.exist.xmldb.UserManagementService) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 62 with CollectionManagementService

use of org.xmldb.api.modules.CollectionManagementService in project exist by eXist-db.

the class QuerySessionTest method startServer.

@BeforeClass
public static void startServer() throws ClassNotFoundException, IllegalAccessException, InstantiationException, XMLDBException, SAXException {
    // initialize XML:DB driver
    Class<?> cl = Class.forName("org.exist.xmldb.DatabaseImpl");
    Database database = (Database) cl.newInstance();
    DatabaseManager.registerDatabase(database);
    Collection root = DatabaseManager.getCollection(getBaseUri() + "/db", TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD);
    CollectionManagementService mgmt = (CollectionManagementService) root.getService("CollectionManagementService", "1.0");
    Collection test = mgmt.createCollection("rpctest");
    final TestDataGenerator generator = new TestDataGenerator("xdb", DOC_COUNT);
    final Path[] files = generator.generate(test, generateXQ);
    for (int i = 0; i < files.length; i++) {
        Resource resource = test.createResource(files[i].getFileName().toString(), "XMLResource");
        resource.setContent(files[i].toFile());
        test.storeResource(resource);
    }
    generator.releaseAll();
}
Also used : Path(java.nio.file.Path) CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) TestDataGenerator(org.exist.TestDataGenerator)

Example 63 with CollectionManagementService

use of org.xmldb.api.modules.CollectionManagementService in project exist by eXist-db.

the class DocumentUpdateTest method setUp.

@Before
public void setUp() throws ClassNotFoundException, IllegalAccessException, InstantiationException, XMLDBException {
    CollectionManagementService service = (CollectionManagementService) existEmbeddedServer.getRoot().getService("CollectionManagementService", "1.0");
    testCollection = service.createCollection(TEST_COLLECTION_NAME);
    assertNotNull(testCollection);
}
Also used : CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) Before(org.junit.Before)

Example 64 with CollectionManagementService

use of org.xmldb.api.modules.CollectionManagementService in project exist by eXist-db.

the class DocumentUpdateTest method tearDown.

@After
public void tearDown() throws XMLDBException {
    CollectionManagementService service = (CollectionManagementService) existEmbeddedServer.getRoot().getService("CollectionManagementService", "1.0");
    service.removeCollection(TEST_COLLECTION_NAME);
    testCollection = null;
}
Also used : CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) After(org.junit.After)

Example 65 with CollectionManagementService

use of org.xmldb.api.modules.CollectionManagementService in project exist by eXist-db.

the class ConcurrentTestBase method startupDb.

@Before
public final void startupDb() throws Exception {
    final Collection rootCol = existXmldbEmbeddedServer.getRoot();
    assertNotNull(rootCol);
    final IndexQueryService idxConf = (IndexQueryService) rootCol.getService("IndexQueryService", "1.0");
    idxConf.configureCollection(COLLECTION_CONFIG);
    testCol = rootCol.getChildCollection(getTestCollectionName());
    if (testCol != null) {
        CollectionManagementService mgr = DBUtils.getCollectionManagementService(rootCol);
        mgr.removeCollection(getTestCollectionName());
    }
    testCol = DBUtils.addCollection(rootCol, getTestCollectionName());
    assertNotNull(testCol);
}
Also used : CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) IndexQueryService(org.exist.xmldb.IndexQueryService) Collection(org.xmldb.api.base.Collection) Before(org.junit.Before)

Aggregations

CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)148 Collection (org.xmldb.api.base.Collection)84 XMLResource (org.xmldb.api.modules.XMLResource)33 Resource (org.xmldb.api.base.Resource)25 Before (org.junit.Before)23 EXistCollectionManagementService (org.exist.xmldb.EXistCollectionManagementService)21 After (org.junit.After)21 Test (org.junit.Test)19 UserManagementService (org.exist.xmldb.UserManagementService)14 ResourceSet (org.xmldb.api.base.ResourceSet)14 BinaryResource (org.xmldb.api.modules.BinaryResource)13 XPathQueryService (org.xmldb.api.modules.XPathQueryService)9 Account (org.exist.security.Account)7 IndexQueryService (org.exist.xmldb.IndexQueryService)6 AfterClass (org.junit.AfterClass)6 Database (org.xmldb.api.base.Database)6 XMLDBException (org.xmldb.api.base.XMLDBException)6 Path (java.nio.file.Path)5 BeforeClass (org.junit.BeforeClass)5 InputStream (java.io.InputStream)4