Search in sources :

Example 1 with AnyURIValue

use of org.exist.xquery.value.AnyURIValue in project exist by eXist-db.

the class XQueryTrigger method finish.

private void finish(int event, DBBroker broker, Txn transaction, XmldbURI src, XmldbURI dst, boolean isCollection) {
    // get the query
    final Source query = getQuerySource(broker);
    if (query == null) {
        return;
    }
    // avoid infinite recursion by allowing just one trigger per thread
    if (!TriggerStatePerThread.verifyUniqueTriggerPerThreadBeforeFinish(this, src)) {
        return;
    }
    final XQueryContext context = new XQueryContext(broker.getBrokerPool());
    CompiledXQuery compiledQuery = null;
    try {
        // compile the XQuery
        compiledQuery = service.compile(context, query);
        // declare external variables
        context.declareVariable(bindingPrefix + "type", EVENT_TYPE_FINISH);
        context.declareVariable(bindingPrefix + "event", new StringValue(eventToString(event)));
        if (isCollection) {
            context.declareVariable(bindingPrefix + "collection", new AnyURIValue(src));
        } else {
            context.declareVariable(bindingPrefix + "collection", new AnyURIValue(src.removeLastSegment()));
        }
        context.declareVariable(bindingPrefix + "uri", new AnyURIValue(src));
        if (dst == null) {
            context.declareVariable(bindingPrefix + "new-uri", Sequence.EMPTY_SEQUENCE);
        } else {
            context.declareVariable(bindingPrefix + "new-uri", new AnyURIValue(dst));
        }
        // For backward compatibility
        context.declareVariable(bindingPrefix + "eventType", EVENT_TYPE_FINISH);
        context.declareVariable(bindingPrefix + "triggerEvent", new StringValue(eventToString(event)));
        if (isCollection) {
            context.declareVariable(bindingPrefix + "collectionName", new AnyURIValue(src));
        } else {
            context.declareVariable(bindingPrefix + "collectionName", new AnyURIValue(src.removeLastSegment()));
            context.declareVariable(bindingPrefix + "documentName", new AnyURIValue(src));
        }
        // declare user defined parameters as external variables
        for (Object o : userDefinedVariables.keySet()) {
            final String varName = (String) o;
            final String varValue = userDefinedVariables.getProperty(varName);
            context.declareVariable(bindingPrefix + varName, new StringValue(varValue));
        }
    } catch (final XPathException | IOException | PermissionDeniedException e) {
        // Should never be reached
        LOG.error(e);
    }
    // execute the XQuery
    try {
        // TODO : should we provide another contextSet ?
        final NodeSet contextSet = NodeSet.EMPTY_SET;
        service.execute(broker, compiledQuery, contextSet);
    // TODO : should we have a special processing ?
    } catch (final XPathException e) {
        // Should never be reached
        LOG.error("Error during trigger finish", e);
    } catch (final PermissionDeniedException e) {
        // Should never be reached
        LOG.error(e);
    }
    TriggerStatePerThread.setTriggerRunningState(TriggerStatePerThread.NO_TRIGGER_RUNNING, this, null);
    TriggerStatePerThread.setTransaction(null);
    LOG.debug("Trigger fired for finish");
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet) AnyURIValue(org.exist.xquery.value.AnyURIValue) PermissionDeniedException(org.exist.security.PermissionDeniedException) IOException(java.io.IOException) StringValue(org.exist.xquery.value.StringValue) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source) DBSource(org.exist.source.DBSource)

Example 2 with AnyURIValue

use of org.exist.xquery.value.AnyURIValue in project exist by eXist-db.

the class XQueryTrigger method prepare.

private void prepare(int event, DBBroker broker, Txn transaction, XmldbURI src, XmldbURI dst, boolean isCollection) throws TriggerException {
    // get the query
    final Source query = getQuerySource(broker);
    if (query == null) {
        return;
    }
    // avoid infinite recursion by allowing just one trigger per thread
    if (!TriggerStatePerThread.verifyUniqueTriggerPerThreadBeforePrepare(this, src)) {
        return;
    }
    TriggerStatePerThread.setTransaction(transaction);
    final XQueryContext context = new XQueryContext(broker.getBrokerPool());
    // TODO : further initialisations ?
    CompiledXQuery compiledQuery;
    try {
        // compile the XQuery
        compiledQuery = service.compile(context, query);
        // declare external variables
        context.declareVariable(bindingPrefix + "type", EVENT_TYPE_PREPARE);
        context.declareVariable(bindingPrefix + "event", new StringValue(eventToString(event)));
        if (isCollection) {
            context.declareVariable(bindingPrefix + "collection", new AnyURIValue(src));
        } else {
            context.declareVariable(bindingPrefix + "collection", new AnyURIValue(src.removeLastSegment()));
        }
        context.declareVariable(bindingPrefix + "uri", new AnyURIValue(src));
        if (dst == null) {
            context.declareVariable(bindingPrefix + "new-uri", Sequence.EMPTY_SEQUENCE);
        } else {
            context.declareVariable(bindingPrefix + "new-uri", new AnyURIValue(dst));
        }
        // For backward compatibility
        context.declareVariable(bindingPrefix + "eventType", EVENT_TYPE_PREPARE);
        context.declareVariable(bindingPrefix + "triggerEvent", new StringValue(eventToString(event)));
        if (isCollection) {
            context.declareVariable(bindingPrefix + "collectionName", new AnyURIValue(src));
        } else {
            context.declareVariable(bindingPrefix + "collectionName", new AnyURIValue(src.removeLastSegment()));
            context.declareVariable(bindingPrefix + "documentName", new AnyURIValue(src));
        }
        // declare user defined parameters as external variables
        for (Object o : userDefinedVariables.keySet()) {
            final String varName = (String) o;
            final String varValue = userDefinedVariables.getProperty(varName);
            context.declareVariable(bindingPrefix + varName, new StringValue(varValue));
        }
    } catch (final XPathException | IOException | PermissionDeniedException e) {
        TriggerStatePerThread.setTriggerRunningState(TriggerStatePerThread.NO_TRIGGER_RUNNING, this, null);
        TriggerStatePerThread.setTransaction(null);
        throw new TriggerException(PREPARE_EXCEPTION_MESSAGE, e);
    }
    // execute the XQuery
    try {
        // TODO : should we provide another contextSet ?
        final NodeSet contextSet = NodeSet.EMPTY_SET;
        service.execute(broker, compiledQuery, contextSet);
        // TODO : should we have a special processing ?
        LOG.debug("Trigger fired for prepare");
    } catch (final XPathException | PermissionDeniedException e) {
        TriggerStatePerThread.setTriggerRunningState(TriggerStatePerThread.NO_TRIGGER_RUNNING, this, null);
        TriggerStatePerThread.setTransaction(null);
        throw new TriggerException(PREPARE_EXCEPTION_MESSAGE, e);
    }
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet) AnyURIValue(org.exist.xquery.value.AnyURIValue) IOException(java.io.IOException) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source) DBSource(org.exist.source.DBSource) PermissionDeniedException(org.exist.security.PermissionDeniedException) StringValue(org.exist.xquery.value.StringValue)

Example 3 with AnyURIValue

use of org.exist.xquery.value.AnyURIValue in project exist by eXist-db.

the class XMLDBAbstractCollectionManipulator method createCollectionPath.

public static final Collection createCollectionPath(final Collection parentColl, final String relPath) throws XMLDBException, XPathException {
    Collection current = parentColl;
    final StringTokenizer tok = new StringTokenizer(new AnyURIValue(relPath).toXmldbURI().toString(), "/");
    while (tok.hasMoreTokens()) {
        final String token = tok.nextToken();
        current = createCollection(current, token);
    }
    return current;
}
Also used : StringTokenizer(java.util.StringTokenizer) AnyURIValue(org.exist.xquery.value.AnyURIValue) InTxnLocalCollection(org.exist.xmldb.txn.bridge.InTxnLocalCollection) LocalCollection(org.exist.xmldb.LocalCollection) Collection(org.xmldb.api.base.Collection)

Example 4 with AnyURIValue

use of org.exist.xquery.value.AnyURIValue in project exist by eXist-db.

the class XMLDBGetMimeType method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    final String path = new AnyURIValue(args[0].itemAt(0).getStringValue()).toString();
    if (path.matches("^[a-z]+://.*")) {
        // external
        final MimeTable mimeTable = MimeTable.getInstance();
        final MimeType mimeType = mimeTable.getContentTypeFor(path);
        if (mimeType != null) {
            return new StringValue(mimeType.getName());
        }
    } else {
        // database
        try {
            XmldbURI pathUri = XmldbURI.xmldbUriFor(path);
            // relative collection Path: add the current base URI
            pathUri = context.getBaseURI().toXmldbURI().resolveCollectionPath(pathUri);
            // try to open the document and acquire a lock
            try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(pathUri, LockMode.READ_LOCK)) {
                if (lockedDoc != null) {
                    return new StringValue(lockedDoc.getDocument().getMimeType());
                }
            }
        } catch (final Exception e) {
            logger.error(e.getMessage());
            throw new XPathException(this, e);
        }
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : MimeTable(org.exist.util.MimeTable) XPathException(org.exist.xquery.XPathException) AnyURIValue(org.exist.xquery.value.AnyURIValue) LockedDocument(org.exist.dom.persistent.LockedDocument) StringValue(org.exist.xquery.value.StringValue) MimeType(org.exist.util.MimeType) XmldbURI(org.exist.xmldb.XmldbURI) XPathException(org.exist.xquery.XPathException)

Example 5 with AnyURIValue

use of org.exist.xquery.value.AnyURIValue in project exist by eXist-db.

the class XMLDBMove method evalWithCollection.

/* (non-Javadoc)
     * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
     */
public Sequence evalWithCollection(Collection collection, Sequence[] args, Sequence contextSequence) throws XPathException {
    final XmldbURI destination = new AnyURIValue(args[1].itemAt(0).getStringValue()).toXmldbURI();
    if (getSignature().getArgumentCount() == 3) {
        final XmldbURI doc = new AnyURIValue(args[2].itemAt(0).getStringValue()).toXmldbURI();
        try {
            final Resource resource = collection.getResource(doc.toString());
            if (resource == null) {
                logger.error("Resource {} not found", doc);
                throw new XPathException(this, "Resource " + doc + " not found");
            }
            final EXistCollectionManagementService service = (EXistCollectionManagementService) collection.getService("CollectionManagementService", "1.0");
            service.moveResource(doc, destination, null);
        } catch (final XMLDBException e) {
            logger.error(e.getMessage());
            throw new XPathException(this, "XMLDB exception caught: " + e.getMessage(), e);
        }
    } else {
        try {
            final EXistCollectionManagementService service = (EXistCollectionManagementService) collection.getService("CollectionManagementService", "1.0");
            service.move(XmldbURI.xmldbUriFor(collection.getName()), destination, null);
        } catch (final XMLDBException e) {
            logger.error(e.getMessage());
            throw new XPathException(this, "Cannot move collection: " + e.getMessage(), e);
        } catch (final URISyntaxException e) {
            logger.error(e.getMessage());
            throw new XPathException(this, "URI exception: " + e.getMessage(), e);
        }
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : XPathException(org.exist.xquery.XPathException) EXistCollectionManagementService(org.exist.xmldb.EXistCollectionManagementService) AnyURIValue(org.exist.xquery.value.AnyURIValue) Resource(org.xmldb.api.base.Resource) XMLDBException(org.xmldb.api.base.XMLDBException) URISyntaxException(java.net.URISyntaxException) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

AnyURIValue (org.exist.xquery.value.AnyURIValue)27 Sequence (org.exist.xquery.value.Sequence)14 XPathException (org.exist.xquery.XPathException)12 BrokerPool (org.exist.storage.BrokerPool)9 DBBroker (org.exist.storage.DBBroker)9 URI (java.net.URI)8 XmldbURI (org.exist.xmldb.XmldbURI)8 XQueryContext (org.exist.xquery.XQueryContext)8 CompiledXQuery (org.exist.xquery.CompiledXQuery)7 XQuery (org.exist.xquery.XQuery)7 StringValue (org.exist.xquery.value.StringValue)7 IOException (java.io.IOException)6 URISyntaxException (java.net.URISyntaxException)6 PermissionDeniedException (org.exist.security.PermissionDeniedException)6 Source (org.exist.source.Source)4 MimeType (org.exist.util.MimeType)4 Resource (org.xmldb.api.base.Resource)4 XMLDBException (org.xmldb.api.base.XMLDBException)4 Path (java.nio.file.Path)3 DBSource (org.exist.source.DBSource)3