Search in sources :

Example 16 with TriggerException

use of org.exist.collections.triggers.TriggerException in project exist by eXist-db.

the class ExampleTrigger method afterCreateDocument.

@Override
public void afterCreateDocument(DBBroker broker, Txn txn, DocumentImpl document) throws TriggerException {
    if (document.getFileURI().toString().contains("copied")) {
        LOG.info("Prevent recreation of document {}", document.getFileURI().toString());
        return;
    }
    LOG.info("Created document {}", document.getDocumentURI());
    final byte[] data = "<a>dummy data</a>".getBytes();
    final XmldbURI newDocumentURI = XmldbURI.create(document.getFileURI().toString() + "-copied.xml");
    try (final Collection collection = broker.openCollection(document.getCollection().getURI(), Lock.LockMode.WRITE_LOCK)) {
        // Stream into database
        broker.storeDocument(txn, newDocumentURI, new StringInputSource(data), MimeType.XML_TYPE, collection);
    } catch (Exception e) {
        LOG.error(e);
        throw new TriggerException(e);
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) Collection(org.exist.collections.Collection) TriggerException(org.exist.collections.triggers.TriggerException) XmldbURI(org.exist.xmldb.XmldbURI) TriggerException(org.exist.collections.triggers.TriggerException)

Example 17 with TriggerException

use of org.exist.collections.triggers.TriggerException in project exist by eXist-db.

the class Deployment method storeBinaryResources.

private void storeBinaryResources(final DBBroker broker, final Txn transaction, final Path directory, final Collection targetCollection, final Optional<RequestedPerms> requestedPerms, final List<String> errors) throws IOException, EXistException, PermissionDeniedException, LockException, TriggerException {
    try (final DirectoryStream<Path> stream = Files.newDirectoryStream(directory)) {
        for (final Path entry : stream) {
            if (!Files.isDirectory(entry)) {
                final XmldbURI name = XmldbURI.create(FileUtils.fileName(entry));
                try {
                    final Permission permission = PermissionFactory.getDefaultResourcePermission(broker.getBrokerPool().getSecurityManager());
                    setPermissions(broker, requestedPerms, false, MimeType.BINARY_TYPE, permission);
                    storeBinary(broker, transaction, targetCollection, entry, MimeType.BINARY_TYPE, name, permission);
                } catch (final Exception e) {
                    LOG.error(e.getMessage(), e);
                    errors.add(e.getMessage());
                }
            }
        }
    }
}
Also used : Path(java.nio.file.Path) UnixStylePermission(org.exist.security.UnixStylePermission) Permission(org.exist.security.Permission) XmldbURI(org.exist.xmldb.XmldbURI) PermissionDeniedException(org.exist.security.PermissionDeniedException) EXistException(org.exist.EXistException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) TriggerException(org.exist.collections.triggers.TriggerException)

Example 18 with TriggerException

use of org.exist.collections.triggers.TriggerException in project exist by eXist-db.

the class Deployment method uninstall.

/**
 * Delete the target collection of the package. If there's no repo.xml descriptor,
 * target will be null.
 *
 * @param pkg
 * @param target
 * @throws PackageException
 */
private void uninstall(final DBBroker broker, final Txn transaction, final Package pkg, final Optional<ElementImpl> target) throws PackageException {
    // determine target collection
    final Optional<String> targetPath = target.map(ElementImpl::getStringValue).filter(s -> !s.isEmpty());
    final XmldbURI targetCollection = targetPath.map(s -> XmldbURI.create(getTargetCollection(broker, s))).orElseGet(() -> getTargetFallback(pkg));
    try {
        Collection collection = broker.getOrCreateCollection(transaction, targetCollection);
        if (collection != null) {
            broker.removeCollection(transaction, collection);
        }
        if (target != null) {
            final XmldbURI configCollection = XmldbURI.CONFIG_COLLECTION_URI.append(targetCollection);
            collection = broker.getOrCreateCollection(transaction, configCollection);
            if (collection != null) {
                broker.removeCollection(transaction, collection);
            }
        }
    } catch (final PermissionDeniedException | IOException | TriggerException e) {
        LOG.error("Exception occurred while removing package.", e);
    }
}
Also used : DependencyVersion(org.expath.pkg.repo.deps.DependencyVersion) Txn(org.exist.storage.txn.Txn) java.util(java.util) BufferedInputStream(java.io.BufferedInputStream) QName(org.exist.dom.QName) SequenceIterator(org.exist.xquery.value.SequenceIterator) PermissionDeniedException(org.exist.security.PermissionDeniedException) org.exist.xquery(org.exist.xquery) DirectoryStream(java.nio.file.DirectoryStream) JarEntry(java.util.jar.JarEntry) org.exist.dom.memtree(org.exist.dom.memtree) Collection(org.exist.collections.Collection) UnixStylePermission(org.exist.security.UnixStylePermission) XmldbURI(org.exist.xmldb.XmldbURI) Attributes(org.xml.sax.Attributes) JarInputStream(java.util.jar.JarInputStream) EXistException(org.exist.EXistException) DocUtils(org.exist.xquery.util.DocUtils) DateTimeValue(org.exist.xquery.value.DateTimeValue) SystemProperties(org.exist.SystemProperties) Path(java.nio.file.Path) Permission(org.exist.security.Permission) Nullable(javax.annotation.Nullable) BatchUserInteraction(org.expath.pkg.repo.tui.BatchUserInteraction) PermissionFactory(org.exist.security.PermissionFactory) InputSource(org.xml.sax.InputSource) Files(java.nio.file.Files) GroupAider(org.exist.security.internal.aider.GroupAider) Type(org.exist.xquery.value.Type) FileSource(org.exist.source.FileSource) IOException(java.io.IOException) UserAider(org.exist.security.internal.aider.UserAider) Either(com.evolvedbinary.j8fu.Either) org.expath.pkg.repo(org.expath.pkg.repo) Logger(org.apache.logging.log4j.Logger) Element(org.w3c.dom.Element) Stream(java.util.stream.Stream) DBBroker(org.exist.storage.DBBroker) SAXException(org.xml.sax.SAXException) org.exist.util(org.exist.util) Sequence(org.exist.xquery.value.Sequence) TriggerException(org.exist.collections.triggers.TriggerException) LogManager(org.apache.logging.log4j.LogManager) Package(org.expath.pkg.repo.Package) AttrList(org.exist.util.serializer.AttrList) InputStream(java.io.InputStream) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) IOException(java.io.IOException) TriggerException(org.exist.collections.triggers.TriggerException) XmldbURI(org.exist.xmldb.XmldbURI)

Example 19 with TriggerException

use of org.exist.collections.triggers.TriggerException in project exist by eXist-db.

the class PluginsManagerImpl method start.

@Override
public void start(final DBBroker broker, final Txn transaction) throws EXistException {
    boolean interrupted = false;
    try {
        try {
            collection = broker.getCollection(COLLETION_URI);
            if (collection == null) {
                collection = broker.getOrCreateCollection(transaction, COLLETION_URI);
                if (collection == null) {
                    return;
                }
                // if db corrupted it can lead to unrunnable issue
                // throw new ConfigurationException("Collection '/db/system/plugins' can't be created.");
                collection.setPermissions(broker, Permission.DEFAULT_SYSTEM_SECURITY_COLLECTION_PERM);
                broker.saveCollection(transaction, collection);
            }
        } catch (final TriggerException | PermissionDeniedException | IOException | LockException e) {
            LOG.warn("Loading PluginsManager configuration failed: {}", e.getMessage());
        }
        final Configuration _config_ = Configurator.parse(this, broker, collection, CONFIG_FILE_URI);
        configuration = Configurator.configure(this, _config_);
        // load plugins by META-INF/services/
        try {
            final MethodHandles.Lookup lookup = MethodHandles.lookup();
            for (final Class<? extends Plug> pluginClazz : listServices(Plug.class)) {
                try {
                    final MethodHandle methodHandle = lookup.findConstructor(pluginClazz, methodType(void.class, PluginsManager.class));
                    final Function<PluginsManager, Plug> ctor = (Function<PluginsManager, Plug>) LambdaMetafactory.metafactory(lookup, "apply", methodType(Function.class), methodHandle.type().erase(), methodHandle, methodHandle.type()).getTarget().invokeExact();
                    final Plug plgn = ctor.apply(this);
                    jacks.put(pluginClazz.getName(), plgn);
                } catch (final Throwable e) {
                    if (e instanceof InterruptedException) {
                        // NOTE: must set interrupted flag
                        interrupted = true;
                    }
                    LOG.error(e);
                }
            }
        } catch (final Throwable e) {
            LOG.error(e);
        }
        for (final Plug jack : jacks.values()) {
            jack.start(broker, transaction);
        }
    } finally {
        if (interrupted) {
            // NOTE: must set interrupted flag
            Thread.currentThread().interrupt();
        }
    }
}
Also used : Configuration(org.exist.config.Configuration) MethodHandles(java.lang.invoke.MethodHandles) Function(java.util.function.Function) LockException(org.exist.util.LockException) PermissionDeniedException(org.exist.security.PermissionDeniedException) TriggerException(org.exist.collections.triggers.TriggerException) MethodHandle(java.lang.invoke.MethodHandle)

Example 20 with TriggerException

use of org.exist.collections.triggers.TriggerException in project exist by eXist-db.

the class InTxnLocalCollectionManagementService method createCollection.

@Override
public Collection createCollection(final XmldbURI name, final Date created) throws XMLDBException {
    final XmldbURI collName = resolve(name);
    withDb((broker, transaction) -> {
        try {
            final org.exist.collections.Collection coll = broker.getOrCreateCollection(transaction, collName);
            if (created != null) {
                coll.setCreated(created.getTime());
            }
            broker.saveCollection(transaction, coll);
            return null;
        } catch (final TriggerException e) {
            throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
        }
    });
    return new InTxnLocalCollection(user, brokerPool, collection, collName);
}
Also used : XMLDBException(org.xmldb.api.base.XMLDBException) TriggerException(org.exist.collections.triggers.TriggerException) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

TriggerException (org.exist.collections.triggers.TriggerException)25 EXistException (org.exist.EXistException)18 PermissionDeniedException (org.exist.security.PermissionDeniedException)18 Txn (org.exist.storage.txn.Txn)13 XmldbURI (org.exist.xmldb.XmldbURI)11 IOException (java.io.IOException)10 Collection (org.exist.collections.Collection)10 LockException (org.exist.util.LockException)10 DocumentImpl (org.exist.dom.persistent.DocumentImpl)9 DBBroker (org.exist.storage.DBBroker)7 TransactionManager (org.exist.storage.txn.TransactionManager)6 Sequence (org.exist.xquery.value.Sequence)6 LockedDocument (org.exist.dom.persistent.LockedDocument)5 NotificationService (org.exist.storage.NotificationService)5 StringValue (org.exist.xquery.value.StringValue)5 ValueSequence (org.exist.xquery.value.ValueSequence)5 StoredNode (org.exist.dom.persistent.StoredNode)4 XPathException (org.exist.xquery.XPathException)4 URISyntaxException (java.net.URISyntaxException)3 Path (java.nio.file.Path)3