use of org.exist.EXistException in project exist by eXist-db.
the class LocalCollection method storeXMLResource.
private void storeXMLResource(final LocalXMLResource res) throws XMLDBException {
final XmldbURI resURI;
try {
resURI = XmldbURI.xmldbUriFor(res.getId());
} catch (final URISyntaxException e) {
throw new XMLDBException(ErrorCodes.INVALID_URI, e);
}
modify().apply((collection, broker, transaction) -> {
String uri = null;
if (res.file != null) {
uri = res.file.toUri().toASCIIString();
}
try (final ManagedDocumentLock documentLock = broker.getBrokerPool().getLockManager().acquireDocumentWriteLock(collection.getURI().append(resURI))) {
final String strMimeType = res.getMimeType(broker, transaction);
final MimeType mimeType = strMimeType != null ? MimeTable.getInstance().getContentType(strMimeType) : null;
if (res.root != null) {
collection.storeDocument(transaction, broker, resURI, res.root, mimeType, res.datecreated, res.datemodified, null, null, null);
} else {
final InputSource source;
if (uri != null) {
source = new InputSource(uri);
} else if (res.inputSource != null) {
source = res.inputSource;
} else {
source = new StringInputSource(res.content);
}
final XMLReader reader;
if (useHtmlReader(broker, transaction, res)) {
reader = getHtmlReader();
} else {
reader = null;
}
broker.storeDocument(transaction, resURI, source, mimeType, res.datecreated, res.datemodified, null, null, reader, collection);
}
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
collection.close();
return null;
} catch (final EXistException | SAXException e) {
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
collection.close();
throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
}
});
}
use of org.exist.EXistException in project exist by eXist-db.
the class AbstractLocal method withDb.
/**
* Higher-order-function for performing an XMLDB operation on
* the database
*
* @param dbOperation The operation to perform on the database
* @param <R> The return type of the operation
*
* @return the result of the operation
*
* @throws XMLDBException if an error occurs when executing the operation.
*/
protected <R> R withDb(final LocalXmldbFunction<R> dbOperation) throws XMLDBException {
try (final DBBroker broker = brokerPool.get(Optional.ofNullable(user));
final Txn transaction = transaction().apply(broker)) {
try {
final R result = dbOperation.apply(broker, transaction);
transaction.commit();
return result;
} catch (final XMLDBException | EXistException e) {
transaction.abort();
throw e;
}
} catch (final EXistException e) {
throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
}
}
use of org.exist.EXistException in project exist by eXist-db.
the class Conditional method process.
@Override
public long process(Txn transaction) throws PermissionDeniedException, LockException, EXistException, XPathException, TriggerException {
LOG.debug("Processing xupdate:if ...");
final XQuery xquery = broker.getBrokerPool().getXQueryService();
final XQueryPool pool = broker.getBrokerPool().getXQueryPool();
final Source source = new StringSource(selectStmt);
CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, source);
XQueryContext context;
if (compiled == null) {
context = new XQueryContext(broker.getBrokerPool());
} else {
context = compiled.getContext();
context.prepareForReuse();
}
// context.setBackwardsCompatibility(true);
context.setStaticallyKnownDocuments(docs);
declareNamespaces(context);
declareVariables(context);
if (compiled == null)
try {
compiled = xquery.compile(context, source);
} catch (final IOException e) {
throw new EXistException("An exception occurred while compiling the query: " + e.getMessage());
}
Sequence seq = null;
try {
seq = xquery.execute(broker, compiled, null);
} finally {
context.runCleanupTasks();
pool.returnCompiledXQuery(source, compiled);
}
if (seq.effectiveBooleanValue()) {
long mods = 0;
for (final Modification modification : modifications) {
mods += modification.process(transaction);
broker.flush();
}
if (LOG.isDebugEnabled()) {
LOG.debug("{} modifications processed.", mods);
}
return mods;
} else {
return 0;
}
}
use of org.exist.EXistException in project exist by eXist-db.
the class Remove method process.
@Override
public long process(Txn transaction) throws PermissionDeniedException, LockException, EXistException, XPathException, TriggerException {
try {
final StoredNode[] ql = selectAndLock(transaction);
final NotificationService notifier = broker.getBrokerPool().getNotificationService();
for (final StoredNode node : ql) {
final DocumentImpl doc = node.getOwnerDocument();
if (!doc.getPermissions().validate(broker.getCurrentSubject(), Permission.WRITE)) {
throw new PermissionDeniedException("User '" + broker.getCurrentSubject().getName() + "' does not have permission to write to the document '" + doc.getDocumentURI() + "'!");
}
final NodeImpl parent = (NodeImpl) getParent(node);
if (parent == null || parent.getNodeType() != Node.ELEMENT_NODE) {
throw new EXistException("you cannot remove the document element. Use update " + "instead");
} else {
parent.removeChild(transaction, node);
}
doc.setLastModified(System.currentTimeMillis());
modifiedDocuments.add(doc);
broker.storeXMLResource(transaction, doc);
notifier.notifyUpdate(doc, UpdateListener.UPDATE);
}
checkFragmentation(transaction, modifiedDocuments);
return ql.length;
} finally {
unlockDocuments(transaction);
}
}
use of org.exist.EXistException in project exist by eXist-db.
the class Update method process.
@Override
public long process(Txn transaction) throws PermissionDeniedException, LockException, EXistException, XPathException, TriggerException {
final NodeList children = content;
if (children.getLength() == 0) {
return 0;
}
int modifications = children.getLength();
try {
final StoredNode[] ql = selectAndLock(transaction);
final NotificationService notifier = broker.getBrokerPool().getNotificationService();
for (final StoredNode node : ql) {
if (node == null) {
LOG.warn("select {} returned empty node", selectStmt);
continue;
}
final DocumentImpl doc = node.getOwnerDocument();
if (!doc.getPermissions().validate(broker.getCurrentSubject(), Permission.WRITE)) {
throw new PermissionDeniedException("User '" + broker.getCurrentSubject().getName() + "' does not have permission to write to the document '" + doc.getDocumentURI() + "'!");
}
switch(node.getNodeType()) {
case Node.ELEMENT_NODE:
if (modifications == 0) {
modifications = 1;
}
((ElementImpl) node).update(transaction, children);
break;
case Node.TEXT_NODE:
final ElementImpl textParent = (ElementImpl) node.getParentNode();
final Node textTemp = children.item(0);
final TextImpl text = new TextImpl(textTemp.getNodeValue());
modifications = 1;
text.setOwnerDocument(doc);
textParent.updateChild(transaction, node, text);
break;
case Node.ATTRIBUTE_NODE:
final ElementImpl attrParent = (ElementImpl) ((Attr) node).getOwnerElement();
if (attrParent == null) {
LOG.warn("parent node not found for {}", node.getNodeId());
break;
}
final AttrImpl attr = (AttrImpl) node;
final Node attrTemp = children.item(0);
final AttrImpl attribute = new AttrImpl(attr.getQName(), attrTemp.getNodeValue(), broker.getBrokerPool().getSymbols());
attribute.setOwnerDocument(doc);
attrParent.updateChild(transaction, node, attribute);
break;
default:
throw new EXistException("unsupported node-type");
}
doc.setLastModified(System.currentTimeMillis());
modifiedDocuments.add(doc);
broker.storeXMLResource(transaction, doc);
notifier.notifyUpdate(doc, UpdateListener.UPDATE);
}
checkFragmentation(transaction, modifiedDocuments);
} finally {
unlockDocuments(transaction);
}
return modifications;
}
Aggregations