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);
}
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);
}
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);
}
}
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;
}
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;
}
Aggregations