Search in sources :

Example 11 with BinaryResource

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);
}
Also used : StringInputSource(org.exist.util.StringInputSource) BinaryResource(org.xmldb.api.modules.BinaryResource) Collection(org.xmldb.api.base.Collection)

Example 12 with BinaryResource

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);
}
Also used : BinaryResource(org.xmldb.api.modules.BinaryResource) AfterClass(org.junit.AfterClass)

Example 13 with BinaryResource

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());
}
Also used : Path(java.nio.file.Path) CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) BinaryResource(org.xmldb.api.modules.BinaryResource) Collection(org.xmldb.api.base.Collection) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 14 with BinaryResource

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();
}
Also used : BinaryResource(org.xmldb.api.modules.BinaryResource)

Example 15 with BinaryResource

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

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