Search in sources :

Example 1 with ExistXqueryRegistry

use of org.exist.extensions.exquery.restxq.impl.ExistXqueryRegistry 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

DocumentImpl (org.exist.dom.persistent.DocumentImpl)1 ExistXqueryRegistry (org.exist.extensions.exquery.restxq.impl.ExistXqueryRegistry)1 PermissionDeniedException (org.exist.security.PermissionDeniedException)1 XmldbURI (org.exist.xmldb.XmldbURI)1 XPathException (org.exist.xquery.XPathException)1 ExQueryException (org.exquery.ExQueryException)1 RestXqService (org.exquery.restxq.RestXqService)1 RestXqServiceRegistry (org.exquery.restxq.RestXqServiceRegistry)1