Search in sources :

Example 46 with XPathException

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

the class ModifyFunction method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // Was context handle or DN specified?
    if (!(args[0].isEmpty()) && !(args[1].isEmpty())) {
        String dn = args[1].getStringValue();
        try {
            long ctxID = ((IntegerValue) args[0].itemAt(0)).getLong();
            DirContext ctx = (DirContext) JNDIModule.retrieveJNDIContext(context, ctxID);
            if (ctx == null) {
                logger.error("jndi:modify() - Invalid JNDI context handle provided: {}", ctxID);
            } else {
                ModificationItem[] items = parseAttributes(args[2]);
                if (items.length > 0) {
                    ctx.modifyAttributes(dn, items);
                }
            }
        } catch (NamingException ne) {
            logger.error("jndi:modify() Modify failed for dn [{}]: ", dn, ne);
            throw (new XPathException(this, "jndi:modify() Modify failed for dn [" + dn + "]: " + ne));
        }
    }
    return (Sequence.EMPTY_SEQUENCE);
}
Also used : ModificationItem(javax.naming.directory.ModificationItem) XPathException(org.exist.xquery.XPathException) IntegerValue(org.exist.xquery.value.IntegerValue) NamingException(javax.naming.NamingException) DirContext(javax.naming.directory.DirContext)

Example 47 with XPathException

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

the class SearchFunction method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    Sequence xmlResult = Sequence.EMPTY_SEQUENCE;
    // Was context handle or DN specified?
    if (!(args[0].isEmpty()) && !(args[1].isEmpty())) {
        String dn = args[1].getStringValue();
        try {
            long ctxID = ((IntegerValue) args[0].itemAt(0)).getLong();
            DirContext ctx = (DirContext) JNDIModule.retrieveJNDIContext(context, ctxID);
            if (ctx == null) {
                logger.error("jndi:search() - Invalid JNDI context handle provided: {}", ctxID);
            } else {
                NamingEnumeration<SearchResult> results = null;
                if (args.length == 3) {
                    // Attributes search
                    BasicAttributes attributes = JNDIModule.parseAttributes(args[2]);
                    results = ctx.search(dn, attributes);
                } else {
                    // Filter search
                    int scopeCode = 0;
                    String filter = args[2].getStringValue();
                    String scope = args[3].getStringValue();
                    if (scope.equalsIgnoreCase("object")) {
                        scopeCode = 0;
                    } else if (scope.equalsIgnoreCase("onelevel")) {
                        scopeCode = 1;
                    } else if (scope.equalsIgnoreCase("subtree")) {
                        scopeCode = 2;
                    }
                    results = ctx.search(dn, filter, new SearchControls(scopeCode, 0, 0, null, false, false));
                }
                xmlResult = renderSearchResultsAsDSML(results, dn);
            }
        } catch (NameNotFoundException nf) {
            logger.warn("jndi:search() Not found for dn [{}]", dn, nf);
        } catch (NamingException ne) {
            logger.error("jndi:search() Search failed for dn [{}]: ", dn, ne);
            throw (new XPathException(this, "jndi:search() Search failed for dn [" + dn + "]: " + ne));
        }
    }
    return (xmlResult);
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) NameNotFoundException(javax.naming.NameNotFoundException) XPathException(org.exist.xquery.XPathException) IntegerValue(org.exist.xquery.value.IntegerValue) SearchResult(javax.naming.directory.SearchResult) Sequence(org.exist.xquery.value.Sequence) DirContext(javax.naming.directory.DirContext) SearchControls(javax.naming.directory.SearchControls) NamingException(javax.naming.NamingException)

Example 48 with XPathException

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

the class LocalXPathQueryService method compileAndCheck.

private Either<XPathException, CompiledExpression> compileAndCheck(final DBBroker broker, final Txn transaction, final String query) throws XMLDBException {
    final long start = System.currentTimeMillis();
    final XQuery xquery = broker.getBrokerPool().getXQueryService();
    final XQueryContext context = new XQueryContext(broker.getBrokerPool());
    try {
        setupContext(null, context);
        final CompiledExpression expr = xquery.compile(context, query);
        if (LOG.isDebugEnabled()) {
            LOG.debug("compilation took {}", System.currentTimeMillis() - start);
        }
        return Either.Right(expr);
    } catch (final PermissionDeniedException e) {
        throw new XMLDBException(ErrorCodes.PERMISSION_DENIED, e.getMessage(), e);
    } catch (final IllegalArgumentException e) {
        throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
    } catch (final XPathException e) {
        return Either.Left(e);
    }
}
Also used : XPathException(org.exist.xquery.XPathException) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) PermissionDeniedException(org.exist.security.PermissionDeniedException)

Example 49 with XPathException

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

the class DatabaseResources method executeQuery.

public Sequence executeQuery(String queryPath, Map<String, String> params, Subject user) {
    final String namespace = params.get(TARGETNAMESPACE);
    final String publicId = params.get(PUBLICID);
    final String catalogPath = params.get(CATALOG);
    final String collection = params.get(COLLECTION);
    if (logger.isDebugEnabled()) {
        logger.debug("collection={} namespace={} publicId={} catalogPath={}", collection, namespace, publicId, catalogPath);
    }
    Sequence result = null;
    try (final DBBroker broker = brokerPool.get(Optional.ofNullable(user))) {
        final XQuery xquery = brokerPool.getXQueryService();
        final XQueryContext context = new XQueryContext(brokerPool);
        if (collection != null) {
            context.declareVariable(COLLECTION, collection);
        }
        if (namespace != null) {
            context.declareVariable(TARGETNAMESPACE, namespace);
        }
        if (publicId != null) {
            context.declareVariable(PUBLICID, publicId);
        }
        if (catalogPath != null) {
            context.declareVariable(CATALOG, catalogPath);
        }
        CompiledXQuery compiled = xquery.compile(context, new ClassLoaderSource(queryPath));
        result = xquery.execute(broker, compiled, null);
    } catch (final EXistException | XPathException | IOException | PermissionDeniedException ex) {
        logger.error("Problem executing xquery", ex);
        result = null;
    }
    return result;
}
Also used : ClassLoaderSource(org.exist.source.ClassLoaderSource) DBBroker(org.exist.storage.DBBroker) XPathException(org.exist.xquery.XPathException) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) PermissionDeniedException(org.exist.security.PermissionDeniedException) Sequence(org.exist.xquery.value.Sequence) EXistException(org.exist.EXistException) IOException(java.io.IOException)

Example 50 with XPathException

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

the class DatabaseResources method getAllResults.

/**
 *  Convert sequence into list of strings.
 *
 * @param   sequence  Result of query.
 * @return  List containing String objects.
 */
public List<String> getAllResults(Sequence sequence) {
    List<String> result = new ArrayList<>();
    try {
        final SequenceIterator i = sequence.iterate();
        while (i.hasNext()) {
            final String path = i.nextItem().getStringValue();
            result.add(path);
        }
    } catch (final XPathException ex) {
        logger.error("xQuery issue.", ex);
        result = null;
    }
    return result;
}
Also used : SequenceIterator(org.exist.xquery.value.SequenceIterator) XPathException(org.exist.xquery.XPathException)

Aggregations

XPathException (org.exist.xquery.XPathException)306 Sequence (org.exist.xquery.value.Sequence)86 IOException (java.io.IOException)61 SAXException (org.xml.sax.SAXException)43 StringValue (org.exist.xquery.value.StringValue)40 PermissionDeniedException (org.exist.security.PermissionDeniedException)34 NodeValue (org.exist.xquery.value.NodeValue)34 DBBroker (org.exist.storage.DBBroker)32 IntegerValue (org.exist.xquery.value.IntegerValue)32 ValueSequence (org.exist.xquery.value.ValueSequence)27 Item (org.exist.xquery.value.Item)26 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)24 EXistException (org.exist.EXistException)23 Path (java.nio.file.Path)22 XmldbURI (org.exist.xmldb.XmldbURI)22 BrokerPool (org.exist.storage.BrokerPool)21 Txn (org.exist.storage.txn.Txn)21 XQueryContext (org.exist.xquery.XQueryContext)21 Element (org.w3c.dom.Element)21 XQuery (org.exist.xquery.XQuery)20