use of org.xmldb.api.modules.BinaryResource in project exist by eXist-db.
the class ResourceTest method setContentAsSourceBinary.
@Test
public void setContentAsSourceBinary() throws XMLDBException {
final Collection testCollection = DatabaseManager.getCollection(XmldbURI.LOCAL_DB + "/" + TEST_COLLECTION);
assertNotNull(testCollection);
final BinaryResource doc = (BinaryResource) testCollection.createResource("source.bin", "BinaryResource");
final byte[] bin = "Stuff And Things".getBytes(UTF_8);
doc.setContent(new StringInputSource(bin));
testCollection.storeResource(doc);
final BinaryResource newDoc = (BinaryResource) testCollection.getResource("source.bin");
final byte[] newDocBin = (byte[]) newDoc.getContent();
assertArrayEquals(bin, newDocBin);
}
use of org.xmldb.api.modules.BinaryResource in project exist by eXist-db.
the class GetParameterTest method afterClass.
@AfterClass
public static void afterClass() throws XMLDBException {
BinaryResource res = (BinaryResource) root.getResource(XQUERY_FILENAME);
root.removeResource(res);
}
use of org.xmldb.api.modules.BinaryResource in project exist by eXist-db.
the class XQueryFunctionsTest method base64BinaryCast.
@Test
public void base64BinaryCast() throws XMLDBException, URISyntaxException {
final String TEST_BINARY_COLLECTION = "testBinary";
final String TEST_COLLECTION = "/db/" + TEST_BINARY_COLLECTION;
final String BINARY_RESOURCE_FILENAME = "logo.jpg";
final String XML_RESOURCE_FILENAME = "logo.xml";
// create a test collection
CollectionManagementService colService = (CollectionManagementService) existEmbeddedServer.getRoot().getService("CollectionManagementService", "1.0");
Collection testCollection = colService.createCollection(TEST_BINARY_COLLECTION);
assertNotNull(testCollection);
final Path fLogo = Paths.get(getClass().getResource("value/logo.jpg").toURI());
// store the eXist logo in the test collection
BinaryResource br = (BinaryResource) testCollection.createResource(BINARY_RESOURCE_FILENAME, "BinaryResource");
br.setContent(fLogo);
testCollection.storeResource(br);
// create an XML resource with the logo base64 embedded in it
String queryStore = "xquery version \"1.0\";\n\n" + "let $embedded := <logo><image>{util:binary-doc(\"" + TEST_COLLECTION + "/" + BINARY_RESOURCE_FILENAME + "\")}</image></logo> return\n" + "xmldb:store(\"" + TEST_COLLECTION + "\", \"" + XML_RESOURCE_FILENAME + "\", $embedded)";
ResourceSet resultStore = existEmbeddedServer.executeQuery(queryStore);
assertEquals("store, Expect single result", 1, resultStore.getSize());
assertEquals("Expect stored filename as result", TEST_COLLECTION + "/" + XML_RESOURCE_FILENAME, resultStore.getResource(0).getContent().toString());
// retrieve the base64 image from the XML resource and try to cast to xs:base64Binary
String queryRetreive = "xquery version \"1.0\";\n\n" + "let $image := doc(\"" + TEST_COLLECTION + "/" + XML_RESOURCE_FILENAME + "\")/logo/image return\n" + "$image/text() cast as xs:base64Binary";
ResourceSet resultRetreive = existEmbeddedServer.executeQuery(queryRetreive);
assertEquals("retreive, Expect single result", 1, resultRetreive.getSize());
}
use of org.xmldb.api.modules.BinaryResource in project exist by eXist-db.
the class EvalTest method writeModule.
private void writeModule(Collection collection, String modulename, String module) throws XMLDBException {
BinaryResource res = (BinaryResource) collection.createResource(modulename, "BinaryResource");
((EXistResource) res).setMimeType("application/xquery");
res.setContent(module.getBytes());
collection.storeResource(res);
collection.close();
}
use of org.xmldb.api.modules.BinaryResource in project exist by eXist-db.
the class XMLDBSecurityTest method setUidXQueryCanWriteRestrictedCollection.
@Test
public void setUidXQueryCanWriteRestrictedCollection() throws XMLDBException {
final Collection test = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest1", "test1", "test1");
final long timestamp = System.currentTimeMillis();
final String content = "<setuid>" + timestamp + "</setuid>";
// create an XQuery /db/securityTest1/setuid.xquery
final String xquery = "xmldb:store('/db/securityTest1/forSetUidWrite', 'setuid.xml', " + content + ")";
Resource xqueryResource = test.createResource("setuid.xquery", "BinaryResource");
xqueryResource.setContent(xquery);
test.storeResource(xqueryResource);
// set the xquery to be owned by 'test1' and set it 'setuid', and set it 'rx' by 'users' group so 'test2' can execute it!
UserManagementService ums = (UserManagementService) test.getService("UserManagementService", "1.0");
xqueryResource = test.getResource("setuid.xquery");
ums.chmod(xqueryResource, 04750);
// 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/setuid.xquery");
assertEquals("/db/securityTest1/forSetUidWrite/setuid.xml", result.getResource(0).getContent());
// check the written content
final Resource writtenXmlResource = colForSetUid.getResource("setuid.xml");
assertEquals(content, writtenXmlResource.getContent());
}
Aggregations