Search in sources :

Example 61 with XQueryContext

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

the class PermissionsFunctionModeConversionTest method modeToOctal_invalidMode.

@Test(expected = XPathException.class)
public void modeToOctal_invalidMode() throws XPathException {
    final XQueryContext mckContext = EasyMock.createMock(XQueryContext.class);
    final PermissionsFunction permissionsFunctions = new PermissionsFunction(mckContext, PermissionsFunction.FNS_MODE_TO_OCTAL);
    Sequence[] args = { new StringValue("invalid") };
    permissionsFunctions.eval(args, null);
}
Also used : XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) StringValue(org.exist.xquery.value.StringValue) Test(org.junit.Test)

Example 62 with XQueryContext

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

the class PermissionsFunctionModeConversionTest method octalToMode.

@Test
public void octalToMode() throws XPathException {
    final XQueryContext mckContext = EasyMock.createMock(XQueryContext.class);
    final PermissionsFunction permissionsFunctions = new PermissionsFunction(mckContext, PermissionsFunction.FNS_OCTAL_TO_MODE);
    Sequence[] args = { new StringValue("0750") };
    final Sequence result = permissionsFunctions.eval(args, null);
    assertEquals(1, result.getItemCount());
    assertEquals("rwxr-x---", result.itemAt(0).toString());
}
Also used : XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) StringValue(org.exist.xquery.value.StringValue) Test(org.junit.Test)

Example 63 with XQueryContext

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

the class PermissionsFunctionModeConversionTest method modeToOctal.

/**
 * Test of eval method, of class PermissionsFunctions.
 */
@Test
public void modeToOctal() throws XPathException {
    final XQueryContext mckContext = EasyMock.createMock(XQueryContext.class);
    final PermissionsFunction permissionsFunctions = new PermissionsFunction(mckContext, PermissionsFunction.FNS_MODE_TO_OCTAL);
    Sequence[] args = { new StringValue("rwxr-x---") };
    final Sequence result = permissionsFunctions.eval(args, null);
    assertEquals(1, result.getItemCount());
    assertEquals("0750", result.itemAt(0).toString());
}
Also used : XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) StringValue(org.exist.xquery.value.StringValue) Test(org.junit.Test)

Example 64 with XQueryContext

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

the class IdFunctionTest method differingRealAndEffectiveUsers.

/**
 * Test of eval method, of class IdFunction.
 * when real and effective users are different
 */
@Test
public void differingRealAndEffectiveUsers() throws XPathException, XpathException {
    final XQueryContext mckContext = createMockBuilder(XQueryContext.class).addMockedMethod("pushDocumentContext").addMockedMethod("getDocumentBuilder", new Class[0]).addMockedMethod("popDocumentContext").addMockedMethod("getRealUser").addMockedMethod("getEffectiveUser").createMock();
    final Subject mckRealUser = EasyMock.createMock(Subject.class);
    final String realUsername = "real";
    mckContext.pushDocumentContext();
    expectLastCall().once();
    expect(mckContext.getDocumentBuilder()).andReturn(new MemTreeBuilder());
    mckContext.popDocumentContext();
    expectLastCall().once();
    expect(mckContext.getRealUser()).andReturn(mckRealUser).times(2);
    expect(mckRealUser.getName()).andReturn(realUsername);
    expect(mckRealUser.getGroups()).andReturn(new String[] { "realGroup1", "realGroup2" });
    expect(mckRealUser.getId()).andReturn(1);
    final Subject mckEffectiveUser = EasyMock.createMock(Subject.class);
    final String effectiveUsername = "effective";
    expect(mckContext.getEffectiveUser()).andReturn(mckEffectiveUser).times(2);
    expect(mckEffectiveUser.getId()).andReturn(2);
    expect(mckEffectiveUser.getName()).andReturn(effectiveUsername);
    expect(mckEffectiveUser.getGroups()).andReturn(new String[] { "effectiveGroup1", "effectiveGroup2" });
    replay(mckEffectiveUser, mckRealUser, mckContext);
    final IdFunction idFunctions = new IdFunction(mckContext, IdFunction.FNS_ID);
    final Sequence result = idFunctions.eval(new Sequence[] { Sequence.EMPTY_SEQUENCE }, null);
    assertEquals(1, result.getItemCount());
    final XpathEngine xpathEngine = XMLUnit.newXpathEngine();
    final Map<String, String> namespaces = new HashMap<>();
    namespaces.put("sm", "http://exist-db.org/xquery/securitymanager");
    xpathEngine.setNamespaceContext(new SimpleNamespaceContext(namespaces));
    final DocumentImpl resultDoc = (DocumentImpl) result.itemAt(0);
    final String actualRealUsername = xpathEngine.evaluate("/sm:id/sm:real/sm:username", resultDoc);
    assertEquals(realUsername, actualRealUsername);
    final String actualEffectiveUsername = xpathEngine.evaluate("/sm:id/sm:effective/sm:username", resultDoc);
    assertEquals(effectiveUsername, actualEffectiveUsername);
    verify(mckEffectiveUser, mckRealUser, mckContext);
}
Also used : MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) XpathEngine(org.custommonkey.xmlunit.XpathEngine) HashMap(java.util.HashMap) XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) SimpleNamespaceContext(org.custommonkey.xmlunit.SimpleNamespaceContext) DocumentImpl(org.exist.dom.memtree.DocumentImpl) Subject(org.exist.security.Subject) Test(org.junit.Test)

Example 65 with XQueryContext

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

the class LocalXPathQueryService method execute.

private ResourceSet execute(final DBBroker broker, final Txn transaction, XmldbURI[] docs, final Sequence contextSet, final CompiledExpression expression, final String sortExpr) throws XMLDBException {
    final long start = System.currentTimeMillis();
    final CompiledXQuery expr = (CompiledXQuery) expression;
    Sequence result = null;
    final XQueryContext context = expr.getContext();
    try {
        context.setStaticallyKnownDocuments(docs);
        if (lockedDocuments != null) {
            context.setProtectedDocs(lockedDocuments);
        }
        setupContext(null, context);
        final XQuery xquery = brokerPool.getXQueryService();
        result = xquery.execute(broker, expr, contextSet, properties);
    } catch (final Exception e) {
        // need to catch all runtime exceptions here to be able to release locked documents
        throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
    } finally {
        /*
             * Run the cleanup tasks, but don't close BinaryValues which
             * are in the result set as the user has not yet accessed them.
             *
             * Final cleanup of those BinaryValues is done by the user
             * calling EXistResource#close(), ResourceSet#clear() or CompiledExpression#reset().
             */
        final Sequence resSeq = result;
        context.runCleanupTasks(o -> {
            if (resSeq != null && o instanceof BinaryValue) {
                for (int i = 0; i < resSeq.getItemCount(); i++) {
                    if (resSeq.itemAt(i) == o) {
                        return false;
                    }
                }
            }
            return true;
        });
    }
    LOG.debug("query took {} ms.", System.currentTimeMillis() - start);
    if (result != null) {
        final Properties resourceSetProperties = new Properties(properties);
        resourceSetProperties.setProperty(EXistOutputKeys.XDM_SERIALIZATION, "yes");
        return new LocalResourceSet(user, brokerPool, collection, resourceSetProperties, result, sortExpr);
    } else {
        return null;
    }
}
Also used : CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) BinaryValue(org.exist.xquery.value.BinaryValue) Sequence(org.exist.xquery.value.Sequence) PermissionDeniedException(org.exist.security.PermissionDeniedException) LockException(org.exist.util.LockException) EXistException(org.exist.EXistException) XPathException(org.exist.xquery.XPathException)

Aggregations

XQueryContext (org.exist.xquery.XQueryContext)70 CompiledXQuery (org.exist.xquery.CompiledXQuery)37 Sequence (org.exist.xquery.value.Sequence)34 XQuery (org.exist.xquery.XQuery)33 DBBroker (org.exist.storage.DBBroker)24 Test (org.junit.Test)23 XPathException (org.exist.xquery.XPathException)18 BrokerPool (org.exist.storage.BrokerPool)17 IOException (java.io.IOException)11 Source (org.exist.source.Source)10 XQueryPool (org.exist.storage.XQueryPool)10 Node (org.w3c.dom.Node)10 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)8 PermissionDeniedException (org.exist.security.PermissionDeniedException)8 AnyURIValue (org.exist.xquery.value.AnyURIValue)8 URI (java.net.URI)7 StringValue (org.exist.xquery.value.StringValue)7 InputSource (org.xml.sax.InputSource)7 EXistException (org.exist.EXistException)6 QName (org.exist.dom.QName)6