use of org.exist.collections.Collection in project exist by eXist-db.
the class ExistCollection method createCollection.
public XmldbURI createCollection(String name) throws PermissionDeniedException, CollectionExistsException, EXistException {
if (LOG.isDebugEnabled()) {
LOG.debug("Create '{}' in '{}'", name, xmldbUri);
}
XmldbURI newCollection = xmldbUri.append(name);
final TransactionManager txnManager = brokerPool.getTransactionManager();
try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
final Txn txn = txnManager.beginTransaction();
final Collection collection = broker.openCollection(newCollection, LockMode.WRITE_LOCK)) {
if (collection != null) {
final String msg = "Collection already exists";
LOG.debug(msg);
// XXX: double "abort" is bad thing!!!
txnManager.abort(txn);
throw new CollectionExistsException(msg);
}
// Create collection
try (final Collection created = broker.getOrCreateCollection(txn, newCollection)) {
broker.saveCollection(txn, created);
broker.flush();
// Commit change
txnManager.commit(txn);
if (LOG.isDebugEnabled()) {
LOG.debug("Collection created sucessfully");
}
}
} catch (EXistException | PermissionDeniedException e) {
LOG.error(e);
throw e;
} catch (Throwable e) {
LOG.error(e);
throw new EXistException(e);
} finally {
if (LOG.isDebugEnabled()) {
LOG.debug("Finished creation");
}
}
return newCollection;
}
use of org.exist.collections.Collection in project exist by eXist-db.
the class ExistCollection method delete.
/*
* Delete document or collection.
*/
void delete() {
if (LOG.isDebugEnabled()) {
LOG.debug("Deleting '{}'", xmldbUri);
}
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)) {
// Open collection if possible, else abort
if (collection == null) {
txnManager.abort(txn);
return;
}
// Remove collection
broker.removeCollection(txn, collection);
// Commit change
txnManager.commit(txn);
if (LOG.isDebugEnabled()) {
LOG.debug("Document deleted sucessfully");
}
} catch (EXistException | IOException | PermissionDeniedException | TriggerException e) {
LOG.error(e);
} finally {
if (LOG.isDebugEnabled()) {
LOG.debug("Finished delete");
}
}
}
use of org.exist.collections.Collection in project exist by eXist-db.
the class EXistServlet method doPatch.
protected void doPatch(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
// first, adjust the path
String path = adjustPath(request);
// second, perform descriptor actions
final Descriptor descriptor = Descriptor.getDescriptorSingleton();
if (descriptor != null) {
// TODO: figure out a way to log PATCH requests with
// HttpServletRequestWrapper and
// Descriptor.doLogRequestInReplayLog()
// map's the path if a mapping is specified in the descriptor
path = descriptor.mapPath(path);
}
// third, authenticate the user
final Subject user = authenticate(request, response);
if (user == null) {
// You now get a HTTP Authentication challenge if there is no user
return;
}
// fourth, process the request
try (final DBBroker broker = getPool().get(Optional.of(user));
final Txn transaction = getPool().getTransactionManager().beginTransaction()) {
final XmldbURI dbpath = XmldbURI.createInternal(path);
try (final Collection collection = broker.getCollection(dbpath)) {
if (collection != null) {
transaction.abort();
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "A PATCH request is not allowed against a plain collection path.");
return;
}
}
try {
srvREST.doPatch(broker, transaction, dbpath, request, response);
transaction.commit();
} catch (final Throwable t) {
transaction.abort();
throw t;
}
} catch (final MethodNotAllowedException e) {
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, e.getMessage());
} catch (final BadRequestException e) {
if (response.isCommitted()) {
throw new ServletException(e.getMessage(), e);
}
response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
} catch (final PermissionDeniedException e) {
// Else return a FORBIDDEN Error
if (user.equals(getDefaultUser())) {
getAuthenticator().sendChallenge(request, response);
} else {
response.sendError(HttpServletResponse.SC_FORBIDDEN, e.getMessage());
}
} catch (final EXistException e) {
if (response.isCommitted()) {
throw new ServletException(e.getMessage(), e);
}
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
} catch (final Throwable e) {
LOG.error(e);
throw new ServletException("An unknown error occurred: " + e.getMessage(), e);
}
}
use of org.exist.collections.Collection in project exist by eXist-db.
the class RpcConnection method createCollection.
private boolean createCollection(final XmldbURI collUri, final Date created) throws PermissionDeniedException, EXistException {
withDb((broker, transaction) -> {
Collection current = broker.getCollection(collUri);
if (current != null) {
return true;
}
current = broker.getOrCreateCollection(transaction, collUri, Optional.ofNullable(created).map(c -> new Tuple2<>(null, c.getTime())));
try (final ManagedCollectionLock collectionLock = broker.getBrokerPool().getLockManager().acquireCollectionWriteLock(collUri)) {
broker.saveCollection(transaction, current);
}
return null;
});
LOG.info("collection {} has been created", collUri);
return true;
}
use of org.exist.collections.Collection in project exist by eXist-db.
the class RpcConnection method beginProtected.
protected LockedDocumentMap beginProtected(final DBBroker broker, final Map<String, Object> parameters) throws EXistException, PermissionDeniedException {
final String protectColl = (String) parameters.get(RpcAPI.PROTECTED_MODE);
if (protectColl == null) {
return null;
}
int retries = BEGIN_PROTECTED_MAX_LOCKING_RETRIES == -1 ? -1 : BEGIN_PROTECTED_MAX_LOCKING_RETRIES - 2;
do {
MutableDocumentSet docs = null;
final LockedDocumentMap lockedDocuments = new LockedDocumentMap();
final LockMode documentLockMode = LockMode.WRITE_LOCK;
final LockMode collectionLockMode = broker.getBrokerPool().getLockManager().relativeCollectionLockMode(LockMode.READ_LOCK, documentLockMode);
try (final Collection coll = broker.openCollection(XmldbURI.createInternal(protectColl), collectionLockMode)) {
docs = new DefaultDocumentSet();
coll.allDocs(broker, docs, true, lockedDocuments, documentLockMode);
return lockedDocuments;
} catch (final LockException e) {
LOG.warn("Deadlock detected. Starting over again. Docs: {}; locked: {}. Cause: {}", docs.getDocumentCount(), lockedDocuments.size(), e.getMessage());
lockedDocuments.unlock();
}
retries--;
} while (retries >= -1);
throw new EXistException("Unable to beginProtected after " + BEGIN_PROTECTED_MAX_LOCKING_RETRIES + " retries");
}
Aggregations