Search in sources :

Example 61 with Resource

use of org.xmldb.api.base.Resource in project exist by eXist-db.

the class XmldbApiSecurityTest method createBinResource.

@Override
protected void createBinResource(String resourceUri, byte[] content, String uid, String pwd) throws ApiException {
    Collection col = null;
    try {
        col = DatabaseManager.getCollection(getBaseUri() + getCollectionUri(resourceUri), uid, pwd);
        Resource resource = col.createResource(getResourceName(resourceUri), BinaryResource.RESOURCE_TYPE);
        resource.setContent(content);
        col.storeResource(resource);
    } catch (final XMLDBException xmldbe) {
        throw new ApiException(xmldbe);
    } finally {
        if (col != null) {
            try {
                col.close();
            } catch (final XMLDBException xmldbe) {
                throw new ApiException(xmldbe);
            }
        }
    }
}
Also used : BinaryResource(org.xmldb.api.modules.BinaryResource) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException)

Example 62 with Resource

use of org.xmldb.api.base.Resource 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 63 with Resource

use of org.xmldb.api.base.Resource in project exist by eXist-db.

the class UpdateRecoverTest method xmldbRead.

private void xmldbRead(final BrokerPool pool) throws XMLDBException {
    final org.xmldb.api.base.Collection test2 = DatabaseManager.getCollection("xmldb:exist://" + TestConstants.TEST_COLLECTION_URI2, "admin", "");
    assertNotNull(test2);
    final Resource res = test2.getResource("test_xmldb.xml");
    assertNotNull("Document should not be null", res);
    final org.xmldb.api.base.Collection root = DatabaseManager.getCollection(XmldbURI.LOCAL_DB, "admin", "");
    assertNotNull(root);
    final EXistCollectionManagementService mgr = (EXistCollectionManagementService) root.getService("CollectionManagementService", "1.0");
    assertNotNull(mgr);
    mgr.removeCollection("test");
}
Also used : EXistCollectionManagementService(org.exist.xmldb.EXistCollectionManagementService) Resource(org.xmldb.api.base.Resource) Database(org.xmldb.api.base.Database)

Example 64 with Resource

use of org.xmldb.api.base.Resource in project exist by eXist-db.

the class UpdateRecoverTest method xmldbStore.

private void xmldbStore(final BrokerPool pool) throws IllegalAccessException, DatabaseConfigurationException, InstantiationException, ClassNotFoundException, XMLDBException, EXistException {
    final org.xmldb.api.base.Collection root = DatabaseManager.getCollection(XmldbURI.LOCAL_DB, "admin", "");
    assertNotNull(root);
    final EXistCollectionManagementService mgr = (EXistCollectionManagementService) root.getService("CollectionManagementService", "1.0");
    assertNotNull(mgr);
    org.xmldb.api.base.Collection test = root.getChildCollection("test");
    if (test == null) {
        test = mgr.createCollection(TestConstants.TEST_COLLECTION_URI.toString());
    }
    assertNotNull(test);
    org.xmldb.api.base.Collection test2 = test.getChildCollection("test2");
    if (test2 == null) {
        test2 = mgr.createCollection(TestConstants.TEST_COLLECTION_URI2.toString());
    }
    assertNotNull(test2);
    final Resource res = test2.createResource("test_xmldb.xml", "XMLResource");
    assertNotNull(res);
    res.setContent(TEST_XML);
    test2.storeResource(res);
    final XUpdateQueryService service = (XUpdateQueryService) test2.getService("XUpdateQueryService", "1.0");
    assertNotNull(service);
    // insert some nodes
    for (int i = 1; i <= 200; i++) {
        final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:insert-before select=\"/products/product[1]\">" + "       <product>" + "           <description>Product " + i + "</description>" + "           <price>" + (i * 2.5) + "</price>" + "           <stock>" + (i * 10) + "</stock>" + "       </product>" + "   </xu:insert-before>" + "</xu:modifications>";
        service.updateResource("test_xmldb.xml", xupdate);
    }
    // add attribute
    for (int i = 1; i <= 200; i++) {
        final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:append select=\"/products/product[" + i + "]\">" + "         <xu:attribute name=\"id\">" + i + "</xu:attribute>" + " </xu:append>" + "</xu:modifications>";
        service.updateResource("test_xmldb.xml", xupdate);
    }
    // replace some
    for (int i = 1; i <= 100; i++) {
        final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:replace select=\"/products/product[" + i + "]\">" + "     <product id=\"" + i + "\">" + "         <description>Replaced product</description>" + "         <price>" + (i * 0.75) + "</price>" + "     </product>" + " </xu:replace>" + "</xu:modifications>";
        service.updateResource("test_xmldb.xml", xupdate);
    }
    // remove some
    for (int i = 1; i <= 100; i++) {
        final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:remove select=\"/products/product[last()]\"/>" + "</xu:modifications>";
        service.updateResource("test_xmldb.xml", xupdate);
    }
    for (int i = 1; i <= 100; i++) {
        final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:append select=\"/products\">" + "       <product>" + "           <xu:attribute name=\"id\"><xu:value-of select=\"count(/products/product) + 1\"/></xu:attribute>" + "           <description>Product " + i + "</description>" + "           <price>" + (i * 2.5) + "</price>" + "           <stock>" + (i * 10) + "</stock>" + "       </product>" + "   </xu:append>" + "</xu:modifications>";
        service.updateResource("test_xmldb.xml", xupdate);
    }
    // rename element "description" to "descript"
    String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:rename select=\"/products/product/description\">descript</xu:rename>" + "</xu:modifications>";
    service.updateResource("test_xmldb.xml", xupdate);
    // update attribute values
    for (int i = 1; i <= 200; i++) {
        xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:update select=\"/products/product[" + i + "]/@id\">" + i + "u</xu:update>" + "</xu:modifications>";
        service.updateResource("test_xmldb.xml", xupdate);
    }
    // append new element to records
    for (int i = 1; i <= 200; i++) {
        xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:append select=\"/products/product[" + i + "]\">" + "       <date><xu:value-of select=\"current-dateTime()\"/></date>" + "   </xu:append>" + "</xu:modifications>";
        service.updateResource("test_xmldb.xml", xupdate);
    }
    // update element content
    for (int i = 1; i <= 200; i++) {
        xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:update select=\"/products/product[" + i + "]/price\">19.99</xu:update>" + "</xu:modifications>";
        service.updateResource("test_xmldb.xml", xupdate);
    }
}
Also used : XUpdateQueryService(org.xmldb.api.modules.XUpdateQueryService) EXistCollectionManagementService(org.exist.xmldb.EXistCollectionManagementService) Resource(org.xmldb.api.base.Resource) Database(org.xmldb.api.base.Database)

Example 65 with Resource

use of org.xmldb.api.base.Resource in project exist by eXist-db.

the class MoveResourceRecoveryTest method xmldbRead.

private void xmldbRead() throws XMLDBException {
    final org.xmldb.api.base.Collection test = DatabaseManager.getCollection(XmldbURI.LOCAL_DB + "/test", TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD);
    final Resource res = test.getResource("new_test3.xml");
    assertNotNull("Document should not be null", res);
    final org.xmldb.api.base.Collection root = DatabaseManager.getCollection(XmldbURI.LOCAL_DB, TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD);
    final EXistCollectionManagementService mgr = (EXistCollectionManagementService) root.getService("CollectionManagementService", "1.0");
    mgr.removeCollection(XmldbURI.create("test"));
    mgr.removeCollection(XmldbURI.create("test2"));
}
Also used : EXistCollectionManagementService(org.exist.xmldb.EXistCollectionManagementService) Resource(org.xmldb.api.base.Resource) Database(org.xmldb.api.base.Database)

Aggregations

Resource (org.xmldb.api.base.Resource)173 XMLResource (org.xmldb.api.modules.XMLResource)126 Collection (org.xmldb.api.base.Collection)111 BinaryResource (org.xmldb.api.modules.BinaryResource)86 Test (org.junit.Test)77 UserManagementService (org.exist.xmldb.UserManagementService)52 ResourceSet (org.xmldb.api.base.ResourceSet)46 XMLDBException (org.xmldb.api.base.XMLDBException)38 EXistCollectionManagementService (org.exist.xmldb.EXistCollectionManagementService)32 EXistResource (org.exist.xmldb.EXistResource)27 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)25 XPathQueryService (org.xmldb.api.modules.XPathQueryService)18 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)16 Path (java.nio.file.Path)11 Database (org.xmldb.api.base.Database)11 XPathException (org.exist.xquery.XPathException)10 InputStream (java.io.InputStream)9 Source (javax.xml.transform.Source)9 BuildException (org.apache.tools.ant.BuildException)9 Diff (org.xmlunit.diff.Diff)9