Search in sources :

Example 21 with BinaryResource

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

the class LoginModuleTest method beforeClass.

@BeforeClass
public static void beforeClass() throws XMLDBException {
    root = DatabaseManager.getCollection("xmldb:exist://localhost:" + existWebServer.getPort() + "/xmlrpc" + XmldbURI.ROOT_COLLECTION, TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD);
    final BinaryResource res = (BinaryResource) root.createResource(XQUERY_FILENAME, "BinaryResource");
    ((EXistResource) res).setMimeType("application/xquery");
    res.setContent(XQUERY);
    root.storeResource(res);
    final UserManagementService ums = (UserManagementService) root.getService("UserManagementService", "1.0");
    ums.chmod(res, 0777);
    final BasicCookieStore store = new BasicCookieStore();
    client = HttpClientBuilder.create().setDefaultCookieStore(store).build();
}
Also used : EXistResource(org.exist.xmldb.EXistResource) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BinaryResource(org.xmldb.api.modules.BinaryResource) UserManagementService(org.exist.xmldb.UserManagementService) BeforeClass(org.junit.BeforeClass)

Example 22 with BinaryResource

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

the class XMLDBStore method evalWithCollection.

@Override
public Sequence evalWithCollection(Collection collection, Sequence[] args, Sequence contextSequence) throws XPathException {
    String docName = args[1].isEmpty() ? null : args[1].getStringValue();
    if (docName != null && docName.isEmpty()) {
        docName = null;
    } else if (docName != null) {
        docName = new AnyURIValue(docName).toXmldbURI().toString();
    }
    final Item item = args[2].itemAt(0);
    // determine the mime type
    final boolean storeAsBinary = isCalledAs(FS_STORE_BINARY_NAME);
    MimeType mimeType = null;
    if (getSignature().getArgumentCount() == 4) {
        final String strMimeType = args[3].getStringValue();
        mimeType = MimeTable.getInstance().getContentType(strMimeType);
    }
    if (mimeType == null && docName != null) {
        mimeType = MimeTable.getInstance().getContentTypeFor(docName);
    }
    if (mimeType == null) {
        mimeType = (storeAsBinary || !Type.subTypeOf(item.getType(), Type.NODE)) ? MimeType.BINARY_TYPE : MimeType.XML_TYPE;
    } else if (storeAsBinary) {
        mimeType = new MimeType(mimeType.getName(), MimeType.BINARY);
    }
    Resource resource;
    try {
        if (Type.subTypeOf(item.getType(), Type.JAVA_OBJECT)) {
            final Object obj = ((JavaObjectValue) item).getObject();
            if (obj instanceof java.io.File) {
                resource = loadFromFile(collection, ((java.io.File) obj).toPath(), docName, mimeType);
            } else if (obj instanceof java.nio.file.Path) {
                resource = loadFromFile(collection, (Path) obj, docName, mimeType);
            } else {
                LOGGER.error("Passed java object should be either a java.nio.file.Path or java.io.File");
                throw new XPathException(this, "Passed java object should be either a java.nio.file.Path or java.io.File");
            }
        } else if (Type.subTypeOf(item.getType(), Type.ANY_URI)) {
            try {
                final URI uri = new URI(item.getStringValue());
                resource = loadFromURI(collection, uri, docName, mimeType);
            } catch (final URISyntaxException e) {
                LOGGER.error("Invalid URI: {}", item.getStringValue());
                throw new XPathException(this, "Invalid URI: " + item.getStringValue(), e);
            }
        } else {
            if (mimeType.isXMLType()) {
                resource = collection.createResource(docName, "XMLResource");
            } else {
                resource = collection.createResource(docName, "BinaryResource");
            }
            if (Type.subTypeOf(item.getType(), Type.STRING)) {
                resource.setContent(item.getStringValue());
            } else if (item.getType() == Type.BASE64_BINARY) {
                resource.setContent(((BinaryValue) item).toJavaObject());
            } else if (Type.subTypeOf(item.getType(), Type.NODE)) {
                if (mimeType.isXMLType()) {
                    final ContentHandler handler = ((XMLResource) resource).setContentAsSAX();
                    handler.startDocument();
                    item.toSAX(context.getBroker(), handler, SERIALIZATION_PROPERTIES);
                    handler.endDocument();
                } else {
                    try (final StringWriter writer = new StringWriter()) {
                        final SAXSerializer serializer = new SAXSerializer();
                        serializer.setOutput(writer, null);
                        item.toSAX(context.getBroker(), serializer, SERIALIZATION_PROPERTIES);
                        resource.setContent(writer.toString());
                    } catch (final IOException e) {
                        LOGGER.error(e.getMessage(), e);
                    }
                }
            } else {
                LOGGER.error("Data should be either a node or a string");
                throw new XPathException(this, "Data should be either a node or a string");
            }
            ((EXistResource) resource).setMimeType(mimeType.getName());
            collection.storeResource(resource);
        }
    } catch (final XMLDBException e) {
        LOGGER.error(e.getMessage(), e);
        throw new XPathException(this, "XMLDB reported an exception while storing document: " + e.getMessage(), e);
    } catch (final SAXException e) {
        LOGGER.error(e.getMessage());
        throw new XPathException(this, "SAX reported an exception while storing document", e);
    }
    if (resource == null) {
        return Sequence.EMPTY_SEQUENCE;
    } else {
        try {
            // TODO : use dedicated function in XmldbURI
            return new StringValue(collection.getName() + "/" + resource.getId());
        } catch (final XMLDBException e) {
            LOGGER.error(e.getMessage());
            throw new XPathException(this, "XMLDB reported an exception while retrieving the " + "stored document", e);
        }
    }
}
Also used : XPathException(org.exist.xquery.XPathException) AnyURIValue(org.exist.xquery.value.AnyURIValue) BinaryResource(org.xmldb.api.modules.BinaryResource) XMLResource(org.xmldb.api.modules.XMLResource) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) XMLDBException(org.xmldb.api.base.XMLDBException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) MimeType(org.exist.util.MimeType) ContentHandler(org.xml.sax.ContentHandler) SAXException(org.xml.sax.SAXException) Item(org.exist.xquery.value.Item) StringWriter(java.io.StringWriter) JavaObjectValue(org.exist.xquery.value.JavaObjectValue) SAXSerializer(org.exist.util.serializer.SAXSerializer) StringValue(org.exist.xquery.value.StringValue) Path(java.nio.file.Path)

Example 23 with BinaryResource

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

the class RemoteCollectionTest method createBinaryResourceFromString.

@Test
public void createBinaryResourceFromString() throws XMLDBException {
    final Collection collection = getCollection();
    final String resourceName = "testresource.bin";
    final Resource resource = collection.createResource(resourceName, BinaryResource.RESOURCE_TYPE);
    assertNotNull(resource);
    assertEquals(collection, resource.getParentCollection());
    final String bin = "binary data: " + System.currentTimeMillis();
    resource.setContent(bin);
    collection.storeResource(resource);
    final Resource retrievedResource = collection.getResource(resourceName);
    assertNotNull(retrievedResource);
    assertEquals(BinaryResource.RESOURCE_TYPE, retrievedResource.getResourceType());
    assertTrue(retrievedResource instanceof BinaryResource);
    final byte[] result = (byte[]) retrievedResource.getContent();
    assertNotNull(result);
    assertEquals(bin, new String(result, UTF_8));
}
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) Collection(org.xmldb.api.base.Collection) Test(org.junit.Test)

Example 24 with BinaryResource

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

the class RemoteCollectionTest method createEmptyBinaryResource.

@Test
public void createEmptyBinaryResource() throws XMLDBException, IOException {
    final Collection collection = getCollection();
    final String resourceName = "empty.dtd";
    final Resource resource = collection.createResource(resourceName, BinaryResource.RESOURCE_TYPE);
    ((EXistResource) resource).setMimeType("application/xml-dtd");
    final byte[] bin = new byte[0];
    try (final InputStream is = new UnsynchronizedByteArrayInputStream(bin)) {
        final InputSource inputSource = new InputSource();
        inputSource.setByteStream(is);
        inputSource.setSystemId("empty.dtd");
        resource.setContent(inputSource);
        collection.storeResource(resource);
    }
    final Resource retrievedResource = collection.getResource(resourceName);
    assertNotNull(retrievedResource);
    assertEquals(BinaryResource.RESOURCE_TYPE, retrievedResource.getResourceType());
    assertTrue(retrievedResource instanceof BinaryResource);
    final byte[] result = (byte[]) retrievedResource.getContent();
    assertNotNull(result);
    assertArrayEquals(bin, result);
}
Also used : InputSource(org.xml.sax.InputSource) BinaryResource(org.xmldb.api.modules.BinaryResource) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) BinaryResource(org.xmldb.api.modules.BinaryResource) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) Test(org.junit.Test)

Example 25 with BinaryResource

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

the class BinaryResourceUpdateTest method updateBinary.

@Test
public void updateBinary() throws XMLDBException, URISyntaxException {
    for (int i = 0; i < REPEAT; i++) {
        BinaryResource binaryResource = (BinaryResource) testCollection.createResource("test1.xml", "BinaryResource");
        binaryResource.setContent(Paths.get(binFile.toURI()));
        testCollection.storeResource(binaryResource);
        Resource resource = testCollection.getResource("test1.xml");
        assertNotNull(resource);
        XMLResource xmlResource = (XMLResource) testCollection.createResource("test2.xml", "XMLResource");
        xmlResource.setContent(Paths.get(xmlFile.toURI()));
        testCollection.storeResource(xmlResource);
        resource = testCollection.getResource("test2.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)

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