Search in sources :

Example 1 with CollectionDoesNotExistException

use of org.exist.webdav.exceptions.CollectionDoesNotExistException in project exist by eXist-db.

the class MiltonCollection method createNew.

/* ===============
     * PutableResource
     * =============== */
@Override
public Resource createNew(String newName, InputStream is, Long length, String contentType) throws IOException, ConflictException {
    if (LOG.isTraceEnabled()) {
        LOG.trace("Create '{}' in '{}'", newName, resourceXmldbUri);
    }
    Resource resource = null;
    try {
        // submit
        XmldbURI resourceURI = existCollection.createFile(newName, is, length, contentType);
        resource = new MiltonDocument(host, resourceURI, brokerPool, subject);
    } catch (PermissionDeniedException | CollectionDoesNotExistException | IOException e) {
        LOG.debug(e.getMessage());
        throw new ConflictException(this, "Create New '" + getXmldbUri().append(newName) + "' failed: " + e.getMessage());
    }
    return resource;
}
Also used : CollectionDoesNotExistException(org.exist.webdav.exceptions.CollectionDoesNotExistException) PermissionDeniedException(org.exist.security.PermissionDeniedException) IOException(java.io.IOException) XmldbURI(org.exist.xmldb.XmldbURI)

Example 2 with CollectionDoesNotExistException

use of org.exist.webdav.exceptions.CollectionDoesNotExistException in project exist by eXist-db.

the class ExistCollection method createFile.

public XmldbURI createFile(String newName, InputStream is, Long length, String contentType) throws IOException, PermissionDeniedException, CollectionDoesNotExistException {
    if (LOG.isDebugEnabled())
        LOG.debug("Create '{}' in '{}'", newName, xmldbUri);
    XmldbURI newNameUri = XmldbURI.create(newName);
    // Get mime, or NULL when not available
    MimeType mime = MimeTable.getInstance().getContentTypeFor(newName);
    if (mime == null) {
        mime = MimeType.BINARY_TYPE;
    }
    // XML documents are not supported a small file will be created.
    if (mime.isXMLType() && length == 0) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Creating dummy XML file for null resource lock '{}'", newNameUri);
        }
        is = new UnsynchronizedByteArrayInputStream("<null_resource/>".getBytes(StandardCharsets.UTF_8));
    }
    final TransactionManager txnManager = brokerPool.getTransactionManager();
    try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
        final Txn txn = txnManager.beginTransaction();
        final Collection collection = broker.openCollection(xmldbUri, LockMode.WRITE_LOCK)) {
        // by ResourceFactory
        if (collection == null) {
            LOG.debug("Collection {} does not exist", xmldbUri);
            txnManager.abort(txn);
            throw new CollectionDoesNotExistException(xmldbUri + "");
        }
        if (LOG.isDebugEnabled()) {
            if (mime.isXMLType()) {
                LOG.debug("Inserting XML document '{}'", mime.getName());
            } else {
                LOG.debug("Inserting BINARY document '{}'", mime.getName());
            }
        }
        // Stream into database
        try (final FilterInputStreamCache cache = FilterInputStreamCacheFactory.getCacheInstance(() -> (String) broker.getConfiguration().getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY), is);
            final CachingFilterInputStream cfis = new CachingFilterInputStream(cache);
            final EXistInputSource eis = new CachingFilterInputStreamInputSource(cfis)) {
            broker.storeDocument(txn, newNameUri, eis, mime, collection);
        }
        // Commit change
        txnManager.commit(txn);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Document created successfully");
        }
    } catch (EXistException | SAXException e) {
        LOG.error(e);
        throw new IOException(e);
    } catch (LockException e) {
        LOG.error(e);
        throw new PermissionDeniedException(xmldbUri + "");
    } catch (IOException | PermissionDeniedException e) {
        LOG.error(e);
        throw e;
    } finally {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finished creation");
        }
    }
    // Send the result back to the client
    XmldbURI newResource = xmldbUri.append(newName);
    return newResource;
}
Also used : Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) IOException(java.io.IOException) FilterInputStreamCache(org.exist.util.io.FilterInputStreamCache) SAXException(org.xml.sax.SAXException) DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) CollectionDoesNotExistException(org.exist.webdav.exceptions.CollectionDoesNotExistException) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) CachingFilterInputStream(org.exist.util.io.CachingFilterInputStream) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

IOException (java.io.IOException)2 PermissionDeniedException (org.exist.security.PermissionDeniedException)2 CollectionDoesNotExistException (org.exist.webdav.exceptions.CollectionDoesNotExistException)2 XmldbURI (org.exist.xmldb.XmldbURI)2 UnsynchronizedByteArrayInputStream (org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)1 EXistException (org.exist.EXistException)1 Collection (org.exist.collections.Collection)1 DBBroker (org.exist.storage.DBBroker)1 TransactionManager (org.exist.storage.txn.TransactionManager)1 Txn (org.exist.storage.txn.Txn)1 CachingFilterInputStream (org.exist.util.io.CachingFilterInputStream)1 FilterInputStreamCache (org.exist.util.io.FilterInputStreamCache)1 SAXException (org.xml.sax.SAXException)1