Search in sources :

Example 66 with XmldbURI

use of org.exist.xmldb.XmldbURI in project exist by eXist-db.

the class RecoverXmlTest method storeAndVerify.

@Override
protected void storeAndVerify(final DBBroker broker, final Txn transaction, final Collection collection, final InputSource data, final String dbFilename) throws EXistException, PermissionDeniedException, IOException, LockException {
    final XmldbURI docUri = XmldbURI.create(dbFilename);
    try {
        broker.storeDocument(transaction, docUri, data, MimeType.XML_TYPE, collection);
    } catch (final SAXException e) {
        throw new IOException(e);
    }
    final DocumentImpl doc = broker.getResource(collection.getURI().append(docUri), Permission.READ);
    assertNotNull(doc);
    final Source expected;
    if (data instanceof FileInputSource) {
        expected = Input.fromFile(((FileInputSource) data).getFile().toFile()).build();
    } else if (data instanceof StringInputSource) {
        try (final Reader reader = data.getCharacterStream()) {
            expected = Input.fromString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + readAll(reader)).build();
        }
    } else {
        throw new IllegalStateException();
    }
    final Source actual = Input.fromDocument(doc).build();
    final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForIdentical().build();
    assertFalse("XML identical: " + diff.toString(), diff.hasDifferences());
}
Also used : Diff(org.xmlunit.diff.Diff) Reader(java.io.Reader) IOException(java.io.IOException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) XmldbURI(org.exist.xmldb.XmldbURI) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXException(org.xml.sax.SAXException)

Example 67 with XmldbURI

use of org.exist.xmldb.XmldbURI in project exist by eXist-db.

the class BinaryDocumentTest method overwriteCollection.

@Test
public void overwriteCollection() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException {
    final XmldbURI testCollectionUri = XmldbURI.create("/db/overwrite-collection-test");
    final XmldbURI thingUri = testCollectionUri.append("thing");
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        // create a collection
        final Collection thingCollection = broker.getOrCreateCollection(transaction, thingUri);
        broker.saveCollection(transaction, thingCollection);
        // attempt to create a binary document with the same uri as the thingCollection (should fail)
        final Collection testCollection = broker.getCollection(testCollectionUri);
        try {
            broker.storeDocument(transaction, thingUri.lastSegment(), new StringInputSource("binary-file".getBytes(UTF_8)), MimeType.BINARY_TYPE, testCollection);
            fail("Should not have been able to overwrite Collection with Binary Document");
        } catch (final EXistException e) {
            assertEquals("The collection '" + testCollectionUri.getRawCollectionPath() + "' already has a sub-collection named '" + thingUri.lastSegment().toString() + "', you cannot create a Document with the same name as an existing collection.", e.getMessage());
        }
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) XmldbURI(org.exist.xmldb.XmldbURI) Test(org.junit.Test)

Example 68 with XmldbURI

use of org.exist.xmldb.XmldbURI in project exist by eXist-db.

the class CopyResourceTest method copyDoc.

private void copyDoc(final Subject execAsUser, final DBBroker.PreserveType preserve, final XmldbURI srcDocName, final XmldbURI destDocName) throws EXistException, PermissionDeniedException, LockException, IOException, TriggerException {
    final XmldbURI src = TEST_COLLECTION_URI.append(srcDocName);
    final XmldbURI dest = TEST_COLLECTION_URI.append(destDocName);
    final BrokerPool pool = existWebServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(execAsUser));
        final Txn transaction = pool.getTransactionManager().beginTransaction();
        final LockedDocument lockedSrcDoc = broker.getXMLResource(src, LockMode.READ_LOCK);
        final Collection destCol = broker.openCollection(dest.removeLastSegment(), LockMode.WRITE_LOCK)) {
        broker.copyResource(transaction, lockedSrcDoc.getDocument(), destCol, dest.lastSegment(), preserve);
        transaction.commit();
    }
    // check the copy of the document is the same as the original
    try (final DBBroker broker = pool.get(Optional.of(execAsUser));
        final LockedDocument lockedOriginal = broker.getXMLResource(src, LockMode.READ_LOCK);
        final LockedDocument lockedCopy = broker.getXMLResource(dest, LockMode.READ_LOCK)) {
        final Diff diff = DiffBuilder.compare(Input.fromDocument(lockedOriginal.getDocument())).withTest(Input.fromDocument(lockedCopy.getDocument())).build();
        assertFalse(diff.toString(), diff.hasDifferences());
    }
}
Also used : Diff(org.xmlunit.diff.Diff) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) XmldbURI(org.exist.xmldb.XmldbURI)

Example 69 with XmldbURI

use of org.exist.xmldb.XmldbURI in project exist by eXist-db.

the class StoreResourceTest method replaceXmlDoc.

private void replaceXmlDoc(final Subject execAsUser, final DBBroker.PreserveType preserve, final XmldbURI docName, final String content) throws EXistException, PermissionDeniedException, LockException, IOException, SAXException {
    final XmldbURI uri = TEST_COLLECTION_URI.append(docName);
    final BrokerPool pool = existWebServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(execAsUser));
        final Txn transaction = pool.getTransactionManager().beginTransaction();
        final Collection col = broker.openCollection(uri.removeLastSegment(), Lock.LockMode.WRITE_LOCK)) {
        broker.storeDocument(transaction, uri.lastSegment(), new StringInputSource(content), MimeType.XML_TYPE, col);
        transaction.commit();
    }
    // check the replaced document is correct
    try (final DBBroker broker = pool.get(Optional.of(execAsUser));
        final LockedDocument lockedDoc = broker.getXMLResource(uri, Lock.LockMode.READ_LOCK)) {
        final Serializer serializer = broker.borrowSerializer();
        try {
            final String docXml = serializer.serialize(lockedDoc.getDocument());
            final Diff diff = DiffBuilder.compare(Input.fromString(content)).withTest(Input.fromString(docXml)).build();
            assertFalse(diff.toString(), diff.hasDifferences());
        } finally {
            broker.returnSerializer(serializer);
        }
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) Diff(org.xmlunit.diff.Diff) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) XmldbURI(org.exist.xmldb.XmldbURI) Serializer(org.exist.storage.serializers.Serializer)

Example 70 with XmldbURI

use of org.exist.xmldb.XmldbURI in project exist by eXist-db.

the class StoreResourceTest method replaceBinDoc.

private void replaceBinDoc(final Subject execAsUser, final DBBroker.PreserveType preserve, final XmldbURI docName, final String content) throws EXistException, PermissionDeniedException, LockException, IOException, SAXException {
    final XmldbURI uri = TEST_COLLECTION_URI.append(docName);
    final BrokerPool pool = existWebServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(execAsUser));
        final Txn transaction = pool.getTransactionManager().beginTransaction();
        final Collection col = broker.openCollection(uri.removeLastSegment(), Lock.LockMode.WRITE_LOCK)) {
        broker.storeDocument(transaction, uri.lastSegment(), new StringInputSource(content.getBytes(UTF_8)), MimeType.BINARY_TYPE, col);
        transaction.commit();
    }
    // check the replaced document is correct
    try (final DBBroker broker = pool.get(Optional.of(execAsUser));
        final LockedDocument lockedDoc = broker.getXMLResource(uri, Lock.LockMode.READ_LOCK);
        final InputStream is = broker.getBinaryResource((BinaryDocument) lockedDoc.getDocument());
        final UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream()) {
        os.write(is);
        assertArrayEquals(content.getBytes(UTF_8), os.toByteArray());
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) InputStream(java.io.InputStream) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

XmldbURI (org.exist.xmldb.XmldbURI)260 Collection (org.exist.collections.Collection)100 PermissionDeniedException (org.exist.security.PermissionDeniedException)69 Test (org.junit.Test)56 Txn (org.exist.storage.txn.Txn)55 EXistException (org.exist.EXistException)42 URISyntaxException (java.net.URISyntaxException)39 LockedDocument (org.exist.dom.persistent.LockedDocument)39 IOException (java.io.IOException)38 DBBroker (org.exist.storage.DBBroker)38 DocumentImpl (org.exist.dom.persistent.DocumentImpl)34 SAXException (org.xml.sax.SAXException)33 Permission (org.exist.security.Permission)30 LockException (org.exist.util.LockException)27 Path (java.nio.file.Path)22 XPathException (org.exist.xquery.XPathException)22 BrokerPool (org.exist.storage.BrokerPool)21 TransactionManager (org.exist.storage.txn.TransactionManager)20 Subject (org.exist.security.Subject)19 StringInputSource (org.exist.util.StringInputSource)17