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;
}
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;
}
Aggregations