Search in sources :

Example 16 with BinaryResource

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

the class XMLDBSecurityTest method setGidXQueryCanWriteRestrictedCollection.

@Test
public void setGidXQueryCanWriteRestrictedCollection() throws XMLDBException {
    final Collection test = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest2", "test1", "test1");
    final long timestamp = System.currentTimeMillis();
    final String content = "<setgid>" + timestamp + "</setgid>";
    // create an XQuery /db/securityTest1/setuid.xquery
    final String xquery = "xmldb:store('/db/securityTest2/forSetGidWrite', 'setgid.xml', " + content + ")";
    Resource xqueryResource = test.createResource("setgid.xquery", "BinaryResource");
    xqueryResource.setContent(xquery);
    test.storeResource(xqueryResource);
    // set the xquery to be owned by 'test1':'users' and set it 'setgid', and set it 'rx' by ohers, so 'test3' can execute it!
    UserManagementService ums = (UserManagementService) test.getService("UserManagementService", "1.0");
    xqueryResource = test.getResource("setgid.xquery");
    ums.chown(xqueryResource, ums.getAccount("test1"), "users");
    // setgid
    ums.chmod(xqueryResource, 02705);
    // create a collection for the XQuery to write into
    final CollectionManagementService cms = (CollectionManagementService) test.getService("CollectionManagementService", "1.0");
    final Collection colForSetUid = cms.createCollection("forSetGidWrite");
    // only allow the group 'users' to write into the collection
    ums = (UserManagementService) colForSetUid.getService("UserManagementService", "1.0");
    ums.chmod(0570);
    // execute the XQuery as the 'test3' user... it should become 'setgid' of 'users' and succeed.
    final Collection test3 = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest2", "test3", "test3");
    final EXistXPathQueryService queryService = (EXistXPathQueryService) test3.getService("XPathQueryService", "1.0");
    final ResourceSet result = queryService.executeStoredQuery("/db/securityTest2/setgid.xquery");
    assertEquals("/db/securityTest2/forSetGidWrite/setgid.xml", result.getResource(0).getContent());
    // check the written content
    final Resource writtenXmlResource = colForSetUid.getResource("setgid.xml");
    assertEquals(content, writtenXmlResource.getContent());
}
Also used : EXistCollectionManagementService(org.exist.xmldb.EXistCollectionManagementService) CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) BinaryResource(org.xmldb.api.modules.BinaryResource) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) UserManagementService(org.exist.xmldb.UserManagementService) ResourceSet(org.xmldb.api.base.ResourceSet) Test(org.junit.Test)

Example 17 with BinaryResource

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

the class XQueryTriggerTest method storeDocument_invalidTriggerForPrepare.

@Test
public void storeDocument_invalidTriggerForPrepare() throws XMLDBException {
    final BinaryResource invalidModule = (BinaryResource) testCollection.createResource(MODULE_NAME, "BinaryResource");
    ((EXistResource) invalidModule).setMimeType("application/xquery");
    invalidModule.setContent(INVALID_MODULE.getBytes());
    testCollection.storeResource(invalidModule);
    // configure the Collection with the trigger under test
    final IndexQueryService idxConf = (IndexQueryService) testCollection.getService("IndexQueryService", "1.0");
    idxConf.configureCollection(COLLECTION_CONFIG);
    final int max_store_attempts = 10;
    int count_prepare_exceptions = 0;
    for (int i = 0; i < max_store_attempts; i++) {
        try {
            // this will fire the trigger
            final XMLResource doc = (XMLResource) testCollection.createResource(DOCUMENT_NAME, "XMLResource");
            doc.setContent(DOCUMENT_CONTENT);
            testCollection.storeResource(doc);
        } catch (XMLDBException xdbe) {
            if (xdbe.getCause() instanceof TriggerException) {
                if (xdbe.getCause().getMessage().equals(XQueryTrigger.PREPARE_EXCEPTION_MESSAGE)) {
                    count_prepare_exceptions++;
                }
            }
        }
    }
    assertEquals(max_store_attempts, count_prepare_exceptions);
}
Also used : EXistResource(org.exist.xmldb.EXistResource) BinaryResource(org.xmldb.api.modules.BinaryResource) IndexQueryService(org.exist.xmldb.IndexQueryService) XMLDBException(org.xmldb.api.base.XMLDBException) XMLResource(org.xmldb.api.modules.XMLResource)

Example 18 with BinaryResource

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

the class GetDataTest method afterClass.

@AfterClass
public static void afterClass() throws XMLDBException {
    BinaryResource res = (BinaryResource) root.getResource(XQUERY_FILENAME);
    root.removeResource(res);
}
Also used : BinaryResource(org.xmldb.api.modules.BinaryResource) AfterClass(org.junit.AfterClass)

Example 19 with BinaryResource

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

the class GetDataTest method beforeClass.

@BeforeClass
public static void beforeClass() throws XMLDBException {
    root = DatabaseManager.getCollection("xmldb:exist://localhost:" + existWebServer.getPort() + "/xmlrpc/db", "admin", "");
    BinaryResource res = (BinaryResource) root.createResource(XQUERY_FILENAME, "BinaryResource");
    ((EXistResource) res).setMimeType("application/xquery");
    res.setContent(XQUERY);
    root.storeResource(res);
    UserManagementService ums = (UserManagementService) root.getService("UserManagementService", "1.0");
    ums.chmod(res, 0777);
}
Also used : EXistResource(org.exist.xmldb.EXistResource) BinaryResource(org.xmldb.api.modules.BinaryResource) UserManagementService(org.exist.xmldb.UserManagementService) BeforeClass(org.junit.BeforeClass)

Example 20 with BinaryResource

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

the class VariablesTest method writeModule.

private static void writeModule(final Collection collection, final String modulename, final String module) throws XMLDBException {
    final BinaryResource res = (BinaryResource) collection.createResource(modulename, "BinaryResource");
    ((EXistResource) res).setMimeType("application/xquery");
    res.setContent(module.getBytes());
    collection.storeResource(res);
    collection.close();
}
Also used : EXistResource(org.exist.xmldb.EXistResource) BinaryResource(org.xmldb.api.modules.BinaryResource)

Aggregations

BinaryResource (org.xmldb.api.modules.BinaryResource)25 XMLResource (org.xmldb.api.modules.XMLResource)13 EXistResource (org.exist.xmldb.EXistResource)11 Resource (org.xmldb.api.base.Resource)11 Test (org.junit.Test)8 Collection (org.xmldb.api.base.Collection)8 UserManagementService (org.exist.xmldb.UserManagementService)7 ResourceSet (org.xmldb.api.base.ResourceSet)7 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)6 EXistCollectionManagementService (org.exist.xmldb.EXistCollectionManagementService)5 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)4 Path (java.nio.file.Path)3 IndexQueryService (org.exist.xmldb.IndexQueryService)3 AfterClass (org.junit.AfterClass)3 BeforeClass (org.junit.BeforeClass)3 XMLDBException (org.xmldb.api.base.XMLDBException)2 XPathQueryService (org.xmldb.api.modules.XPathQueryService)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 StringWriter (java.io.StringWriter)1