use of org.exist.dom.persistent.DocumentImpl 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.dom.persistent.DocumentImpl 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.dom.persistent.DocumentImpl in project exist by eXist-db.
the class DocumentNameOrId method getResourceByAbsoluteId.
private Sequence getResourceByAbsoluteId(final IntegerValue ivAbsoluteId) throws XPathException, PermissionDeniedException, LockException, IOException {
final BigInteger absoluteId = ivAbsoluteId.toJavaObject(BigInteger.class);
final Tuple3<Integer, Integer, Byte> decoded = decodeAbsoluteResourceId(absoluteId);
final DocumentImpl doc = context.getBroker().getResourceById(decoded._1, decoded._3, decoded._2);
if (doc != null) {
try (final ManagedDocumentLock docLock = context.getBroker().getBrokerPool().getLockManager().acquireDocumentReadLock(doc.getURI())) {
if (doc instanceof BinaryDocument) {
final BinaryDocument bin = (BinaryDocument) doc;
final InputStream is = context.getBroker().getBinaryResource(bin);
return Base64BinaryDocument.getInstance(context, is);
} else {
return new NodeProxy(doc);
}
}
}
return Sequence.EMPTY_SEQUENCE;
}
use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.
the class FunDoctype method eval.
/* (non-Javadoc)
* @see org.exist.xquery.Expression#eval(org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
*/
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
if (context.getProfiler().isEnabled()) {
context.getProfiler().start(this);
context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
if (contextSequence != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
}
if (contextItem != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
}
}
final MutableDocumentSet docs = new DefaultDocumentSet();
for (int i = 0; i < getArgumentCount(); i++) {
final Sequence seq = getArgument(i).eval(contextSequence, contextItem);
for (final SequenceIterator j = seq.iterate(); j.hasNext(); ) {
final String next = j.nextItem().getStringValue();
try {
context.getBroker().getXMLResourcesByDoctype(next, docs);
} catch (final PermissionDeniedException | LockException e) {
LOG.error(e.getMessage(), e);
throw new XPathException(this, e);
}
}
}
final NodeSet result = new ExtArrayNodeSet(1);
for (final Iterator<DocumentImpl> i = docs.getDocumentIterator(); i.hasNext(); ) {
result.add(new NodeProxy(i.next(), NodeId.DOCUMENT_NODE));
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.
the class Modification method finishTriggers.
protected void finishTriggers(Txn transaction) throws TriggerException {
final Iterator<DocumentImpl> iterator = modifiedDocuments.getDocumentIterator();
while (iterator.hasNext()) {
final DocumentImpl doc = iterator.next();
context.addModifiedDoc(doc);
finishTrigger(transaction, doc);
}
triggers.clear();
}
Aggregations