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);
}
}
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()));
}
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()));
}
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);
}
}
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);
}
Aggregations