Search in sources :

Example 1 with ExQueryException

use of org.exquery.ExQueryException in project exist by eXist-db.

the class RestXqTrigger method after.

private void after(final DBBroker broker, final DocumentImpl document) throws TriggerException {
    final ExistXqueryRegistry xqueryRegistry = ExistXqueryRegistry.getInstance();
    if (xqueryRegistry.isXquery(document)) {
        try {
            final List<RestXqService> services = xqueryRegistry.findServices(broker, document);
            xqueryRegistry.registerServices(broker, services);
        } catch (final ExQueryException eqe) {
            throw new TriggerException(eqe.getMessage(), eqe);
        }
    }
}
Also used : RestXqService(org.exquery.restxq.RestXqService) ExQueryException(org.exquery.ExQueryException) TriggerException(org.exist.collections.triggers.TriggerException)

Example 2 with ExQueryException

use of org.exquery.ExQueryException in project exist by eXist-db.

the class XQueryInspector method findServices.

public static List<RestXqService> findServices(final CompiledXQuery compiled) throws ExQueryException {
    final List<RestXqService> services = new ArrayList<>();
    try {
        // look at each function
        final Iterator<UserDefinedFunction> itFunctions = compiled.getContext().localFunctions();
        final Set<URI> xqueryLocations = new HashSet<>();
        while (itFunctions.hasNext()) {
            final UserDefinedFunction function = itFunctions.next();
            final Annotation[] annotations = function.getSignature().getAnnotations();
            Set<org.exquery.xquery3.Annotation> functionRestAnnotations = null;
            // process the function annotations
            for (final Annotation annotation : annotations) {
                if (RestAnnotationFactory.isRestXqAnnotation(annotation.getName().toJavaQName())) {
                    final org.exquery.xquery3.Annotation restAnnotation = RestAnnotationFactory.getAnnotation(new AnnotationAdapter(annotation));
                    if (functionRestAnnotations == null) {
                        functionRestAnnotations = new HashSet<>();
                    }
                    functionRestAnnotations.add(restAnnotation);
                }
            }
            if (functionRestAnnotations != null) {
                final ResourceFunction resourceFunction = ResourceFunctionFactory.create(new URI(compiled.getSource().path()), functionRestAnnotations);
                final RestXqService service = new RestXqServiceImpl(resourceFunction, compiled.getContext().getBroker().getBrokerPool());
                // record the xquerylocation
                xqueryLocations.add(resourceFunction.getXQueryLocation());
                // add the service to the list of services for this query
                services.add(service);
            }
        }
        for (final URI xqueryLocation : xqueryLocations) {
            // add service location and compiled query to the cache
            RestXqServiceCompiledXQueryCacheImpl.getInstance().returnCompiledQuery(xqueryLocation, compiled);
        }
    } catch (final URISyntaxException | AnnotationException use) {
        throw new ExQueryException(use.getMessage(), use);
    }
    return services;
}
Also used : AnnotationAdapter(org.exist.extensions.exquery.restxq.impl.adapters.AnnotationAdapter) UserDefinedFunction(org.exist.xquery.UserDefinedFunction) ExQueryException(org.exquery.ExQueryException) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Annotation(org.exist.xquery.Annotation) RestXqService(org.exquery.restxq.RestXqService) ResourceFunction(org.exquery.restxq.ResourceFunction) AnnotationException(org.exquery.annotation.AnnotationException) HashSet(java.util.HashSet)

Example 3 with ExQueryException

use of org.exquery.ExQueryException in project exist by eXist-db.

the class RestXqServiceRegistryPersistence method loadRegistry.

private void loadRegistry(final Path fRegistry) {
    log.info("Loading RESTXQ registry from: {}", fRegistry.toAbsolutePath().toString());
    try (final LineNumberReader reader = new LineNumberReader(Files.newBufferedReader(fRegistry));
        final DBBroker broker = getBrokerPool().getBroker()) {
        // read version line first
        String line = reader.readLine();
        final String versionStr = line.substring(line.indexOf(VERSION_LABEL) + VERSION_LABEL.length() + LABEL_SEP.length());
        if (REGISTRY_FILE_VERSION != Integer.parseInt(versionStr)) {
            log.error("Unable to load RESTXQ registry file: {}. Expected version: " + REGISTRY_FILE_VERSION + " but saw version: {}", fRegistry.toAbsolutePath().toString(), versionStr);
        } else {
            while ((line = reader.readLine()) != null) {
                final String xqueryLocation = line.substring(0, line.indexOf(FIELD_SEP));
                final CompiledXQuery xquery = XQueryCompiler.compile(broker, new URI(xqueryLocation));
                final List<RestXqService> services = XQueryInspector.findServices(xquery);
                getRegistry().register(services);
            }
        }
    } catch (final ExQueryException | IOException | EXistException | URISyntaxException eqe) {
        log.error(eqe.getMessage(), eqe);
    }
    log.info("RESTXQ registry loaded.");
}
Also used : RestXqService(org.exquery.restxq.RestXqService) DBBroker(org.exist.storage.DBBroker) ExQueryException(org.exquery.ExQueryException) CompiledXQuery(org.exist.xquery.CompiledXQuery) IOException(java.io.IOException) EXistException(org.exist.EXistException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) LineNumberReader(java.io.LineNumberReader)

Example 4 with ExQueryException

use of org.exquery.ExQueryException in project exist by eXist-db.

the class ExistXqueryRegistry method reexamineModulesWithResolvedDependencies.

/**
 * Gets the modules that have a missing dependency
 * on the module indicated by compiledModuleURI
 * and attempts to re-compile them and register their
 * services
 */
private void reexamineModulesWithResolvedDependencies(final DBBroker broker, final String compiledModuleUri) {
    final Set<String> dependants;
    synchronized (missingDependencies) {
        final Set<String> deps = missingDependencies.get(compiledModuleUri);
        if (deps != null) {
            dependants = new HashSet<>(deps);
        } else {
            dependants = new HashSet<>();
        }
    }
    for (final String dependant : dependants) {
        try {
            final DocumentImpl dependantModule = broker.getResource(XmldbURI.create(dependant), Permission.READ);
            /*
                 * This null check is needed, as a dependency module may have been renamed,
                 * and so is no longer accessible under its old URI.
                 *
                 * However if its dependant module (compiledModuleUri) compiles
                 * (which it must have for this function to be invoked)
                 * then we can assume that the dependant module references the new
                 * module dependency (in the case of a module move/rename)
                 * or the dependency has been removed
                 */
            if (dependantModule != null) {
                LOG.info("Missing dependency '{}' has been added to the database, re-examining '{}'...", compiledModuleUri, dependant);
                final List<RestXqService> services = findServices(broker, dependantModule);
                LOG.info("Discovered {} resource functions for {}", services.size(), dependant);
                registerServices(broker, services);
            } else {
                LOG.info("Dependant '{}' has been resolved. Dependency on: {} was removed", compiledModuleUri, dependant);
                // we need to remove dependant from the dependenciesTree of dependant
                removeDependency(dependant, compiledModuleUri);
            }
        } catch (final PermissionDeniedException | ExQueryException pde) {
            LOG.error(pde.getMessage(), pde);
        }
        // remove the resolve dependencies from the missing dependencies
        removeMissingDependency(compiledModuleUri, dependant);
    }
}
Also used : RestXqService(org.exquery.restxq.RestXqService) ExQueryException(org.exquery.ExQueryException) PermissionDeniedException(org.exist.security.PermissionDeniedException) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 5 with ExQueryException

use of org.exquery.ExQueryException in project exist by eXist-db.

the class RegistryFunctions method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    final ExistXqueryRegistry xqueryRegistry = ExistXqueryRegistry.getInstance();
    final RestXqServiceRegistry registry = RestXqServiceRegistryManager.getRegistry(getContext().getBroker().getBrokerPool());
    Sequence result = Sequence.EMPTY_SEQUENCE;
    context.pushDocumentContext();
    try {
        if (isCalledAs(qnRegisterModule.getLocalPart())) {
            final XmldbURI moduleUri = args[0].toJavaObject(XmldbURI.class);
            final DocumentImpl module = getContext().getBroker().getResource(moduleUri, Permission.READ);
            if (xqueryRegistry.isXquery(module)) {
                try {
                    final List<RestXqService> resourceFunctions = xqueryRegistry.findServices(getContext().getBroker(), module);
                    xqueryRegistry.registerServices(getContext().getBroker(), resourceFunctions);
                    result = (NodeValue) org.exist.extensions.exquery.restxq.impl.xquery.RegistryFunctions.serializeRestXqServices(context.getDocumentBuilder(), resourceFunctions).getDocumentElement();
                } catch (final ExQueryException e) {
                    LOG.warn(e.getMessage(), e);
                    result = Sequence.EMPTY_SEQUENCE;
                }
            } else {
                result = Sequence.EMPTY_SEQUENCE;
            }
        } else if (isCalledAs(qnDeregisterModule.getLocalPart())) {
            final XmldbURI moduleUri = args[0].toJavaObject(XmldbURI.class);
            final DocumentImpl module = getContext().getBroker().getResource(moduleUri, Permission.READ);
            if (xqueryRegistry.isXquery(module)) {
                final List<RestXqService> deregisteringServices = new ArrayList<>();
                for (final RestXqService service : registry) {
                    if (XmldbURI.create(service.getResourceFunction().getXQueryLocation()).equals(moduleUri)) {
                        deregisteringServices.add(service);
                    }
                }
                xqueryRegistry.deregisterServices(getContext().getBroker(), moduleUri);
                result = (NodeValue) org.exist.extensions.exquery.restxq.impl.xquery.RegistryFunctions.serializeRestXqServices(context.getDocumentBuilder(), deregisteringServices).getDocumentElement();
            } else {
                result = Sequence.EMPTY_SEQUENCE;
            }
        } else if (isCalledAs(qnFindResourceFunctions.getLocalPart())) {
            final XmldbURI moduleUri = args[0].toJavaObject(XmldbURI.class);
            final DocumentImpl module = getContext().getBroker().getResource(moduleUri, Permission.READ);
            if (xqueryRegistry.isXquery(module)) {
                try {
                    final List<RestXqService> resourceFunctions = xqueryRegistry.findServices(getContext().getBroker(), module);
                    xqueryRegistry.deregisterServices(getContext().getBroker(), moduleUri);
                    result = (NodeValue) org.exist.extensions.exquery.restxq.impl.xquery.RegistryFunctions.serializeRestXqServices(context.getDocumentBuilder(), resourceFunctions).getDocumentElement();
                } catch (final ExQueryException e) {
                    LOG.warn(e.getMessage(), e);
                    result = Sequence.EMPTY_SEQUENCE;
                }
            } else {
                result = Sequence.EMPTY_SEQUENCE;
            }
        } else if (isCalledAs(qnRegisterResourceFunction.getLocalPart())) {
            final XmldbURI moduleUri = args[0].toJavaObject(XmldbURI.class);
            final String resourceFunctionIdentifier = args[1].getStringValue();
            final DocumentImpl module = getContext().getBroker().getResource(moduleUri, Permission.READ);
            if (xqueryRegistry.isXquery(module)) {
                final SignatureDetail signatureDetail = extractSignatureDetail(resourceFunctionIdentifier);
                if (signatureDetail != null) {
                    try {
                        final RestXqService serviceToRegister = findService(xqueryRegistry.findServices(getContext().getBroker(), module).iterator(), signatureDetail);
                        if (serviceToRegister != null) {
                            xqueryRegistry.registerServices(context.getBroker(), Collections.singletonList(serviceToRegister));
                            result = BooleanValue.TRUE;
                        } else {
                            result = BooleanValue.FALSE;
                        }
                    } catch (final ExQueryException e) {
                        LOG.warn(e.getMessage(), e);
                        result = BooleanValue.FALSE;
                    }
                } else {
                    result = BooleanValue.FALSE;
                }
            } else {
                result = Sequence.EMPTY_SEQUENCE;
            }
        } else if (isCalledAs(qnDeregisterResourceFunction.getLocalPart())) {
            // TODO
            final String resourceFunctionIdentifier = args[1].getStringValue();
            final SignatureDetail signatureDetail = extractSignatureDetail(resourceFunctionIdentifier);
            if (signatureDetail != null) {
                final RestXqService serviceToDeregister = findService(xqueryRegistry.registered(context.getBroker()), signatureDetail);
                if (serviceToDeregister != null) {
                    xqueryRegistry.deregisterService(context.getBroker(), serviceToDeregister);
                    result = BooleanValue.TRUE;
                } else {
                    result = BooleanValue.FALSE;
                }
            } else {
                result = BooleanValue.FALSE;
            }
        } else if (isCalledAs(qnInvalidModules.getLocalPart())) {
            result = serializeInvalidQueries(xqueryRegistry.getInvalidQueries());
        } else if (isCalledAs(qnMissingDependencies.getLocalPart())) {
            result = serializeMissingDependencies(xqueryRegistry.getMissingDependencies());
        } else if (isCalledAs(qnDependencies.getLocalPart())) {
            result = serializeDependenciesTree(xqueryRegistry.getDependenciesTree());
        }
        return result;
    } catch (final PermissionDeniedException pde) {
        throw new XPathException(this, pde.getMessage(), pde);
    } finally {
        context.popDocumentContext();
    }
}
Also used : ExQueryException(org.exquery.ExQueryException) XPathException(org.exist.xquery.XPathException) RestXqServiceRegistry(org.exquery.restxq.RestXqServiceRegistry) ExistXqueryRegistry(org.exist.extensions.exquery.restxq.impl.ExistXqueryRegistry) DocumentImpl(org.exist.dom.persistent.DocumentImpl) RestXqService(org.exquery.restxq.RestXqService) PermissionDeniedException(org.exist.security.PermissionDeniedException) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

ExQueryException (org.exquery.ExQueryException)5 RestXqService (org.exquery.restxq.RestXqService)5 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 DocumentImpl (org.exist.dom.persistent.DocumentImpl)2 PermissionDeniedException (org.exist.security.PermissionDeniedException)2 IOException (java.io.IOException)1 LineNumberReader (java.io.LineNumberReader)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 EXistException (org.exist.EXistException)1 TriggerException (org.exist.collections.triggers.TriggerException)1 ExistXqueryRegistry (org.exist.extensions.exquery.restxq.impl.ExistXqueryRegistry)1 AnnotationAdapter (org.exist.extensions.exquery.restxq.impl.adapters.AnnotationAdapter)1 DBBroker (org.exist.storage.DBBroker)1 XmldbURI (org.exist.xmldb.XmldbURI)1 Annotation (org.exist.xquery.Annotation)1 CompiledXQuery (org.exist.xquery.CompiledXQuery)1 UserDefinedFunction (org.exist.xquery.UserDefinedFunction)1 XPathException (org.exist.xquery.XPathException)1