use of org.exist.EXistException in project exist by eXist-db.
the class ExistDocument method unlock.
/**
* Unlock document in database.
*/
void unlock() throws PermissionDeniedException, DocumentNotLockedException, EXistException {
if (LOG.isDebugEnabled()) {
LOG.debug("unlock {}", xmldbUri);
}
final TransactionManager txnManager = brokerPool.getTransactionManager();
// Try to get document
try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
final Txn txn = txnManager.beginTransaction();
final LockedDocument lockedDocument = broker.getXMLResource(xmldbUri, LockMode.WRITE_LOCK)) {
final DocumentImpl document = lockedDocument.getDocument();
if (document == null) {
final String msg = String.format("No resource found for path: %s", xmldbUri);
LOG.debug(msg);
throw new EXistException(msg);
}
// Get current userlock
Account lock = document.getUserLock();
// Check if Resource is already locked.
if (lock == null) {
LOG.debug("Resource {} is not locked.", xmldbUri);
throw new DocumentNotLockedException("" + xmldbUri);
}
// Check if Resource is from subject
if (!lock.getName().equals(subject.getName()) && !subject.hasDbaRole()) {
LOG.debug("Resource lock is from user {}", lock.getName());
throw new PermissionDeniedException(lock.getName());
}
// Update document
document.setUserLock(null);
document.setLockToken(null);
// Make it persistant
broker.storeMetadata(txn, document);
txnManager.commit(txn);
} catch (EXistException | PermissionDeniedException e) {
LOG.error(e);
throw e;
} catch (TriggerException e) {
LOG.error(e);
throw new EXistException(e);
} finally {
if (LOG.isDebugEnabled()) {
LOG.debug("Finished create lock");
}
}
}
use of org.exist.EXistException in project exist by eXist-db.
the class ExistDocument method initMetadata.
/**
* Initialize Collection, authenticate() is required first
*/
@Override
public void initMetadata() {
if (subject == null) {
LOG.error("User not initialized yet");
return;
}
// check if initialization is required
if (isInitialized) {
LOG.debug("Already initialized");
return;
}
try (final DBBroker broker = brokerPool.get(Optional.of(subject))) {
// If it is not a collection, check if it is a document
try (final LockedDocument lockedDocument = broker.getXMLResource(xmldbUri, LockMode.READ_LOCK)) {
final DocumentImpl document = lockedDocument.getDocument();
if (document.getResourceType() == DocumentImpl.XML_FILE) {
isXmlDocument = true;
}
// Get meta data
creationTime = document.getCreated();
lastModified = document.getLastModified();
mimeType = document.getMimeType();
// Retrieve perssions
permissions = document.getPermissions();
readAllowed = permissions.validate(subject, Permission.READ);
writeAllowed = permissions.validate(subject, Permission.WRITE);
executeAllowed = permissions.validate(subject, Permission.EXECUTE);
ownerUser = permissions.getOwner().getUsername();
ownerGroup = permissions.getGroup().getName();
// Get (estimated) file size
contentLength = document.getContentLength();
}
} catch (final EXistException | PermissionDeniedException e) {
LOG.error(e);
}
isInitialized = true;
}
use of org.exist.EXistException in project exist by eXist-db.
the class ExistDocument method delete.
/**
* Remove document from database.
*/
void delete() {
if (LOG.isDebugEnabled()) {
LOG.debug("Deleting {}", xmldbUri);
}
// Need to split path into collection and document name
final XmldbURI collName = xmldbUri.removeLastSegment();
final XmldbURI docName = xmldbUri.lastSegment();
final TransactionManager txnManager = brokerPool.getTransactionManager();
try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
final Txn txn = txnManager.beginTransaction();
final Collection collection = broker.openCollection(collName, LockMode.WRITE_LOCK)) {
// Open collection if possible, else abort
if (collection == null) {
LOG.debug("Collection does not exist");
txnManager.abort(txn);
return;
}
// Open document if possible, else abort
try (final LockedDocument lockedResource = collection.getDocumentWithLock(broker, docName, LockMode.WRITE_LOCK)) {
if (lockedResource == null) {
LOG.debug("No resource found for path: {}", xmldbUri);
txnManager.abort(txn);
return;
}
final DocumentImpl resource = lockedResource.getDocument();
if (resource.getResourceType() == DocumentImpl.BINARY_FILE) {
collection.removeBinaryResource(txn, broker, resource.getFileURI());
} else {
collection.removeXMLResource(txn, broker, resource.getFileURI());
}
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
collection.close();
// Commit change
txnManager.commit(txn);
if (LOG.isDebugEnabled()) {
LOG.debug("Document deleted sucessfully");
}
}
} catch (final LockException e) {
LOG.error("Resource is locked.", e);
} catch (final EXistException | IOException | TriggerException | PermissionDeniedException e) {
LOG.error(e);
} finally {
if (LOG.isDebugEnabled()) {
LOG.debug("Finished delete");
}
}
}
use of org.exist.EXistException in project exist by eXist-db.
the class ExistCollection method initMetadata.
/**
* Initialize Collection, authenticate() is required first
*/
@Override
public void initMetadata() {
if (subject == null) {
LOG.error("User not initialized yet");
return;
}
// check if initialization is required
if (isInitialized) {
LOG.debug("Already initialized");
return;
}
try (final DBBroker broker = brokerPool.get(Optional.of(subject));
final Collection collection = broker.openCollection(xmldbUri, LockMode.READ_LOCK)) {
if (collection == null) {
LOG.error("Collection for {} cannot be opened for metadata", xmldbUri);
return;
}
// Retrieve some meta data
permissions = collection.getPermissionsNoLock();
readAllowed = permissions.validate(subject, Permission.READ);
writeAllowed = permissions.validate(subject, Permission.WRITE);
executeAllowed = permissions.validate(subject, Permission.EXECUTE);
creationTime = collection.getCreated();
// Collection does not have more information.
lastModified = creationTime;
ownerUser = permissions.getOwner().getUsername();
ownerGroup = permissions.getGroup().getName();
} catch (final PermissionDeniedException | EXistException pde) {
LOG.error(pde);
}
// Set flag
isInitialized = true;
}
use of org.exist.EXistException 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;
}
Aggregations