Search in sources :

Example 1 with BinaryResource

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

the class InteractiveClient method storeBinary.

private void storeBinary(final String fileName) throws XMLDBException {
    final Path file = Paths.get(fileName).normalize();
    if (Files.isReadable(file)) {
        final MimeType mime = MimeTable.getInstance().getContentTypeFor(FileUtils.fileName(file));
        final BinaryResource resource = (BinaryResource) current.createResource(FileUtils.fileName(file), BinaryResource.RESOURCE_TYPE);
        resource.setContent(file);
        ((EXistResource) resource).setMimeType(mime == null ? "application/octet-stream" : mime.getName());
        current.storeResource(resource);
    }
}
Also used : Path(java.nio.file.Path) EXistResource(org.exist.xmldb.EXistResource) BinaryResource(org.xmldb.api.modules.BinaryResource)

Example 2 with BinaryResource

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

the class XMLDBSecurityTest method nonSetGidXQueryCannotWriteRestrictedCollection.

@Test(expected = XMLDBException.class)
public void nonSetGidXQueryCannotWriteRestrictedCollection() throws XMLDBException {
    final Collection test = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest2", "test1", "test1");
    final long timestamp = System.currentTimeMillis();
    final String content = "<not_setgid>" + timestamp + "</not_setgid>";
    // create an XQuery /db/securityTest1/not_setgid.xquery
    final String xquery = "xmldb:store('/db/securityTest2/forSetGidWrite', 'not_setgid.xml', " + content + ")";
    Resource xqueryResource = test.createResource("not_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("not_setgid.xquery");
    // NOT setgid
    ums.chmod(xqueryResource, 00705);
    // 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(0070);
    // 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/not_setgid.xquery");
    assertFalse("/db/securityTest2/forSetGidWrite/not_setgid.xml".equals(result.getResource(0).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 3 with BinaryResource

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

the class XMLDBSecurityTest method nonSetUidXQueryCannotWriteRestrictedCollection.

@Test(expected = XMLDBException.class)
public void nonSetUidXQueryCannotWriteRestrictedCollection() throws XMLDBException {
    final Collection test = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest1", "test1", "test1");
    final long timestamp = System.currentTimeMillis();
    final String content = "<not_setuid>" + timestamp + "</not_setuid>";
    // create an XQuery /db/securityTest1/not_setuid.xquery
    final String xquery = "xmldb:store('/db/securityTest1/forSetUidWrite', 'not_setuid.xml', " + content + ")";
    Resource xqueryResource = test.createResource("not_setuid.xquery", "BinaryResource");
    xqueryResource.setContent(xquery);
    test.storeResource(xqueryResource);
    // set the xquery to be owned by 'test1' and do NOT set it 'setuid', and do set it 'rx' by 'users' group so 'test2' can execute it!
    UserManagementService ums = (UserManagementService) test.getService("UserManagementService", "1.0");
    xqueryResource = test.getResource("not_setuid.xquery");
    // NOT SETUID
    ums.chmod(xqueryResource, 00750);
    // create a collection for the XQuery to write into
    final CollectionManagementService cms = (CollectionManagementService) test.getService("CollectionManagementService", "1.0");
    final Collection colForSetUid = cms.createCollection("forSetUidWrite");
    // only allow the user 'test1' to write into the collection
    ums = (UserManagementService) colForSetUid.getService("UserManagementService", "1.0");
    ums.chmod(0700);
    // execute the XQuery as the 'test2' user... it should become 'setuid' of 'test1' and succeed.
    final Collection test2 = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest1", "test2", "test2");
    final EXistXPathQueryService queryService = (EXistXPathQueryService) test2.getService("XPathQueryService", "1.0");
    final ResourceSet result = queryService.executeStoredQuery("/db/securityTest1/not_setuid.xquery");
    assertFalse("/db/securityTest1/forSetUidWrite/not_setuid.xml".equals(result.getResource(0).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 4 with BinaryResource

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

the class BinaryResourceUpdateTest method updateBinary_windows.

// with same docname test fails for windows
@Test
public void updateBinary_windows() throws XMLDBException, URISyntaxException {
    for (int i = 0; i < REPEAT; i++) {
        BinaryResource binaryResource = (BinaryResource) testCollection.createResource("test.xml", "BinaryResource");
        binaryResource.setContent(Paths.get(binFile.toURI()));
        testCollection.storeResource(binaryResource);
        Resource resource = testCollection.getResource("test.xml");
        assertNotNull(resource);
        XMLResource xmlResource = (XMLResource) testCollection.createResource("test.xml", "XMLResource");
        xmlResource.setContent(Paths.get(xmlFile.toURI()));
        testCollection.storeResource(xmlResource);
        resource = testCollection.getResource("test.xml");
        assertNotNull(resource);
    }
}
Also used : BinaryResource(org.xmldb.api.modules.BinaryResource) BinaryResource(org.xmldb.api.modules.BinaryResource) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) XMLResource(org.xmldb.api.modules.XMLResource) Test(org.junit.Test)

Example 5 with BinaryResource

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

the class GetParameterTest 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)

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