use of org.exist.xmldb.XmldbURI in project exist by eXist-db.
the class ImportModuleTest method storeModules.
private void storeModules(final DBBroker broker, final Txn transaction, final String collectionUri, final Tuple2<String, String>... modules) throws PermissionDeniedException, IOException, SAXException, LockException, EXistException {
// store modules
try (final Collection collection = broker.openCollection(XmldbURI.create(collectionUri), Lock.LockMode.WRITE_LOCK)) {
for (final Tuple2<String, String> module : modules) {
final XmldbURI moduleName = XmldbURI.create(module._1);
broker.storeDocument(transaction, moduleName, new StringInputSource(module._2.getBytes(UTF_8)), MimeType.XQUERY_TYPE, collection);
}
}
}
use of org.exist.xmldb.XmldbURI in project exist by eXist-db.
the class GetField method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
XmldbURI uri = XmldbURI.createInternal(args[0].getStringValue());
String field = args[1].getStringValue();
try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(uri, LockMode.READ_LOCK)) {
if (lockedDoc == null) {
return Sequence.EMPTY_SEQUENCE;
}
// Get the lucene worker
final LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
final String content = index.getFieldContent(lockedDoc.getDocument().getDocId(), field);
return content == null ? Sequence.EMPTY_SEQUENCE : new org.exist.xquery.value.StringValue(content);
} catch (PermissionDeniedException e) {
throw new XPathException(this, LuceneModule.EXXQDYFT0001, "Permission denied to read document " + args[0].getStringValue());
} catch (IOException e) {
throw new XPathException(this, LuceneModule.EXXQDYFT0002, "IO error while reading document " + args[0].getStringValue());
}
}
use of org.exist.xmldb.XmldbURI 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.xmldb.XmldbURI 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.xmldb.XmldbURI 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;
}
Aggregations