Search in sources :

Example 41 with XMLDBException

use of org.xmldb.api.base.XMLDBException in project exist by eXist-db.

the class LocalCollectionManagementService method createCollection.

@Override
public Collection createCollection(final XmldbURI name, final Date created) throws XMLDBException {
    final XmldbURI collName = resolve(name);
    withDb((broker, transaction) -> {
        try {
            final org.exist.collections.Collection coll = broker.getOrCreateCollection(transaction, collName, Optional.ofNullable(created).map(c -> new Tuple2<>(null, c.getTime())));
            try (final ManagedCollectionLock collectionLock = broker.getBrokerPool().getLockManager().acquireCollectionWriteLock(collName)) {
                broker.saveCollection(transaction, coll);
            }
            return null;
        } catch (final LockException | TriggerException e) {
            throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
        }
    });
    return new LocalCollection(user, brokerPool, collection, collName);
}
Also used : XMLDBException(org.xmldb.api.base.XMLDBException) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2) ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) LockedDocument(org.exist.dom.persistent.LockedDocument) BrokerPool(org.exist.storage.BrokerPool) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock) PreserveType(org.exist.storage.DBBroker.PreserveType) LockException(org.exist.util.LockException) Subject(org.exist.security.Subject) Optional(java.util.Optional) DocumentImpl(org.exist.dom.persistent.DocumentImpl) EXistException(org.exist.EXistException) TriggerException(org.exist.collections.triggers.TriggerException) ErrorCodes(org.xmldb.api.base.ErrorCodes) Lock(org.exist.storage.lock.Lock) Nullable(javax.annotation.Nullable) Collection(org.xmldb.api.base.Collection) LockException(org.exist.util.LockException) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2) XMLDBException(org.xmldb.api.base.XMLDBException) TriggerException(org.exist.collections.triggers.TriggerException) ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock)

Example 42 with XMLDBException

use of org.xmldb.api.base.XMLDBException in project exist by eXist-db.

the class LocalCollectionManagementService method moveResource.

@Override
public void moveResource(final XmldbURI src, final XmldbURI dest, final XmldbURI name) throws XMLDBException {
    final XmldbURI srcPath = resolve(src);
    final XmldbURI destPath = dest == null ? srcPath.removeLastSegment() : resolve(dest);
    final XmldbURI newName;
    if (name == null) {
        newName = srcPath.lastSegment();
    } else {
        newName = name;
    }
    withDb((broker, transaction) -> modify(broker, transaction, srcPath.removeLastSegment()).apply((sourceCol, b1, t1) -> {
        try (final LockedDocument lockedSource = sourceCol.getDocumentWithLock(b1, srcPath.lastSegment(), Lock.LockMode.WRITE_LOCK)) {
            final DocumentImpl source = lockedSource == null ? null : lockedSource.getDocument();
            if (source == null) {
                // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
                sourceCol.close();
                throw new XMLDBException(ErrorCodes.NO_SUCH_RESOURCE, "Resource " + srcPath + " not found");
            }
            return modify(b1, t1, destPath).apply((destinationCol, b2, t2) -> {
                try (final ManagedDocumentLock lockedDestination = b2.getBrokerPool().getLockManager().acquireDocumentWriteLock(destinationCol.getURI().append(newName))) {
                    b2.moveResource(t2, source, destinationCol, newName);
                    // NOTE: early release of Collection locks inline with Asymmetrical Locking scheme
                    destinationCol.close();
                    sourceCol.close();
                }
                return null;
            });
        }
    }));
}
Also used : XMLDBException(org.xmldb.api.base.XMLDBException) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2) ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) LockedDocument(org.exist.dom.persistent.LockedDocument) BrokerPool(org.exist.storage.BrokerPool) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock) PreserveType(org.exist.storage.DBBroker.PreserveType) LockException(org.exist.util.LockException) Subject(org.exist.security.Subject) Optional(java.util.Optional) DocumentImpl(org.exist.dom.persistent.DocumentImpl) EXistException(org.exist.EXistException) TriggerException(org.exist.collections.triggers.TriggerException) ErrorCodes(org.xmldb.api.base.ErrorCodes) Lock(org.exist.storage.lock.Lock) Nullable(javax.annotation.Nullable) Collection(org.xmldb.api.base.Collection) ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) LockedDocument(org.exist.dom.persistent.LockedDocument) XMLDBException(org.xmldb.api.base.XMLDBException) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 43 with XMLDBException

use of org.xmldb.api.base.XMLDBException in project exist by eXist-db.

the class LocalCollection method storeXMLResource.

private void storeXMLResource(final LocalXMLResource res) throws XMLDBException {
    final XmldbURI resURI;
    try {
        resURI = XmldbURI.xmldbUriFor(res.getId());
    } catch (final URISyntaxException e) {
        throw new XMLDBException(ErrorCodes.INVALID_URI, e);
    }
    modify().apply((collection, broker, transaction) -> {
        String uri = null;
        if (res.file != null) {
            uri = res.file.toUri().toASCIIString();
        }
        try (final ManagedDocumentLock documentLock = broker.getBrokerPool().getLockManager().acquireDocumentWriteLock(collection.getURI().append(resURI))) {
            final String strMimeType = res.getMimeType(broker, transaction);
            final MimeType mimeType = strMimeType != null ? MimeTable.getInstance().getContentType(strMimeType) : null;
            if (res.root != null) {
                collection.storeDocument(transaction, broker, resURI, res.root, mimeType, res.datecreated, res.datemodified, null, null, null);
            } else {
                final InputSource source;
                if (uri != null) {
                    source = new InputSource(uri);
                } else if (res.inputSource != null) {
                    source = res.inputSource;
                } else {
                    source = new StringInputSource(res.content);
                }
                final XMLReader reader;
                if (useHtmlReader(broker, transaction, res)) {
                    reader = getHtmlReader();
                } else {
                    reader = null;
                }
                broker.storeDocument(transaction, resURI, source, mimeType, res.datecreated, res.datemodified, null, null, reader, collection);
            }
            // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
            collection.close();
            return null;
        } catch (final EXistException | SAXException e) {
            // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
            collection.close();
            throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
        }
    });
}
Also used : ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) InputSource(org.xml.sax.InputSource) XMLDBException(org.xmldb.api.base.XMLDBException) URISyntaxException(java.net.URISyntaxException) EXistException(org.exist.EXistException) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Example 44 with XMLDBException

use of org.xmldb.api.base.XMLDBException in project exist by eXist-db.

the class LocalCollection method createResource.

@Override
public Resource createResource(String id, final String type) throws XMLDBException {
    if (id == null) {
        id = createId();
    }
    final XmldbURI idURI;
    try {
        idURI = XmldbURI.xmldbUriFor(id);
    } catch (final URISyntaxException e) {
        throw new XMLDBException(ErrorCodes.INVALID_URI, e);
    }
    final Resource r;
    switch(type) {
        case XMLResource.RESOURCE_TYPE:
            r = new LocalXMLResource(user, brokerPool, this, idURI);
            break;
        case BinaryResource.RESOURCE_TYPE:
            r = new LocalBinaryResource(user, brokerPool, this, idURI);
            break;
        default:
            throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "Unknown resource type: " + type);
    }
    ((AbstractEXistResource) r).isNewResource = true;
    return r;
}
Also used : BinaryResource(org.xmldb.api.modules.BinaryResource) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) XMLDBException(org.xmldb.api.base.XMLDBException) URISyntaxException(java.net.URISyntaxException)

Example 45 with XMLDBException

use of org.xmldb.api.base.XMLDBException in project exist by eXist-db.

the class LocalCollection method getChildCollection.

@Override
public org.xmldb.api.base.Collection getChildCollection(final String name) throws XMLDBException {
    final XmldbURI childURI;
    try {
        childURI = XmldbURI.xmldbUriFor(name);
    } catch (final URISyntaxException e) {
        throw new XMLDBException(ErrorCodes.INVALID_URI, e);
    }
    final XmldbURI nameUri = this.<XmldbURI>read().apply((collection, broker, transaction) -> {
        XmldbURI childName = null;
        if (collection.hasChildCollection(broker, childURI)) {
            childName = getPathURI().append(childURI);
        }
        return childName;
    });
    if (nameUri != null) {
        return new LocalCollection(user, brokerPool, this, nameUri);
    } else {
        return null;
    }
}
Also used : XMLDBException(org.xmldb.api.base.XMLDBException) URISyntaxException(java.net.URISyntaxException)

Aggregations

XMLDBException (org.xmldb.api.base.XMLDBException)174 Collection (org.xmldb.api.base.Collection)66 Resource (org.xmldb.api.base.Resource)34 URISyntaxException (java.net.URISyntaxException)30 XMLResource (org.xmldb.api.modules.XMLResource)30 ResourceSet (org.xmldb.api.base.ResourceSet)23 BuildException (org.apache.tools.ant.BuildException)21 IOException (java.io.IOException)19 XPathException (org.exist.xquery.XPathException)18 PermissionDeniedException (org.exist.security.PermissionDeniedException)16 SAXException (org.xml.sax.SAXException)16 EXistException (org.exist.EXistException)15 UserManagementService (org.exist.xmldb.UserManagementService)15 XPathQueryService (org.xmldb.api.modules.XPathQueryService)13 BinaryResource (org.xmldb.api.modules.BinaryResource)12 ArrayList (java.util.ArrayList)11 Account (org.exist.security.Account)11 EXistResource (org.exist.xmldb.EXistResource)10 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)10 Properties (java.util.Properties)9