Search in sources :

Example 46 with BrokerPool

use of org.exist.storage.BrokerPool in project exist by eXist-db.

the class PermissionsFunctionChmodTest method cleanupDb.

@AfterClass
public static void cleanupDb() throws EXistException, PermissionDeniedException, IOException, TriggerException {
    final BrokerPool pool = existWebServer.getBrokerPool();
    final SecurityManager sm = pool.getSecurityManager();
    try (final DBBroker broker = pool.get(Optional.of(sm.getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        removeUser(sm, USER2_NAME);
        removeUser(sm, USER1_NAME);
        removeCollection(broker, transaction, TestConstants.TEST_COLLECTION_URI);
        transaction.commit();
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) SecurityManager(org.exist.security.SecurityManager) Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool)

Example 47 with BrokerPool

use of org.exist.storage.BrokerPool in project exist by eXist-db.

the class AccountManagementFunctionRemoveAccountTest method xqueryRemoveAccount.

private Sequence xqueryRemoveAccount(final String username, final Optional<Subject> asUser) throws EXistException, PermissionDeniedException, XPathException {
    final BrokerPool pool = existWebServer.getBrokerPool();
    final String query = "import module namespace sm = 'http://exist-db.org/xquery/securitymanager';\n" + "sm:remove-account('" + username + "')";
    try (final DBBroker broker = pool.get(asUser)) {
        final XQuery xquery = existWebServer.getBrokerPool().getXQueryService();
        final Sequence result = xquery.execute(broker, query, null);
        return result;
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool)

Example 48 with BrokerPool

use of org.exist.storage.BrokerPool in project exist by eXist-db.

the class Util method withCompiledQuery.

static <T> T withCompiledQuery(final DBBroker broker, final Source source, final Function2E<CompiledXQuery, T, XPathException, PermissionDeniedException> op) throws XPathException, PermissionDeniedException, IOException {
    final BrokerPool pool = broker.getBrokerPool();
    final XQuery xqueryService = pool.getXQueryService();
    final XQueryPool xqueryPool = pool.getXQueryPool();
    final CompiledXQuery compiledQuery = compileQuery(broker, xqueryService, xqueryPool, source);
    try {
        return op.apply(compiledQuery);
    } finally {
        if (compiledQuery != null) {
            xqueryPool.returnCompiledXQuery(source, compiledQuery);
        }
    }
}
Also used : XQueryPool(org.exist.storage.XQueryPool) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) BrokerPool(org.exist.storage.BrokerPool)

Example 49 with BrokerPool

use of org.exist.storage.BrokerPool in project exist by eXist-db.

the class SendEmailIT method sendEmailJavaMail.

@Test
public void sendEmailJavaMail() throws EXistException, XPathException, PermissionDeniedException, IOException, MessagingException {
    final String from = "sender@place1.com";
    final String to = "recipient@place2.com";
    final String subject = "some email subject";
    final String messageText = UUIDGenerator.getUUIDversion4();
    final String query = "import module namespace mail = \"http://exist-db.org/xquery/mail\";\n" + "let $session := mail:get-mail-session(\n" + "    <properties>\n" + "        <property name=\"mail.transport.protocol\" value=\"smtp\"/>\n" + "        <property name=\"mail.smtp.port\" value=\"" + smtpPort + "\"/>\n" + "        <property name=\"mail.smtp.host\" value=\"127.0.0.1\"/>\n" + "    </properties>\n" + ")\n" + "return\n" + "    mail:send-email(\n" + "        $session,\n" + "        <mail><from>" + from + "</from><to>" + to + "</to><subject>" + subject + "</subject><message><text>" + messageText + "</text></message></mail>\n" + "    )";
    // send the email from XQuery via SMTP
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final Source source = new StringSource(query);
    try (final DBBroker broker = pool.getBroker();
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        // execute query
        final Boolean sendResult = withCompiledQuery(broker, source, compiledXQuery -> {
            executeQuery(broker, compiledXQuery);
            return true;
        });
        transaction.commit();
        assertTrue(sendResult.booleanValue());
    }
    // check the SMTP server received the email
    final MimeMessage[] receivedMessages = greenMail.getReceivedMessages();
    assertEquals(1, receivedMessages.length);
    final MimeMessage receivedMessage = receivedMessages[0];
    // final Address sender = receivedMessage.getSender();
    // assertEquals(from, sender.toString());
    final Address[] recipients = receivedMessage.getRecipients(Message.RecipientType.TO);
    assertEquals(1, recipients.length);
    assertEquals(to, recipients[0].toString());
    assertEquals(subject, receivedMessage.getSubject());
    assertEquals(messageText, GreenMailUtil.getBody(receivedMessage));
}
Also used : DBBroker(org.exist.storage.DBBroker) Address(jakarta.mail.Address) MimeMessage(jakarta.mail.internet.MimeMessage) StringSource(org.exist.source.StringSource) Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source) Test(org.junit.Test)

Example 50 with BrokerPool

use of org.exist.storage.BrokerPool in project exist by eXist-db.

the class SendEmailIT method sendEmailSmtpDirect.

@Test
public void sendEmailSmtpDirect() throws EXistException, XPathException, PermissionDeniedException, IOException, MessagingException {
    final String from = "sender@place1.com";
    final String to = "recipient@place2.com";
    final String subject = "some email subject";
    final String messageText = UUIDGenerator.getUUIDversion4();
    final String query = "import module namespace mail = \"http://exist-db.org/xquery/mail\";\n" + "mail:send-email(\n" + "    <mail><from>" + from + "</from><to>" + to + "</to><subject>" + subject + "</subject><message><text>" + messageText + "</text></message></mail>,\n" + "    '127.0.0.1:" + smtpPort + "',\n" + "    ()\n" + ")";
    // send the email from XQuery via SMTP
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final Source source = new StringSource(query);
    try (final DBBroker broker = pool.getBroker();
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        // execute query
        final Boolean sendResult = withCompiledQuery(broker, source, compiledXQuery -> {
            final Sequence result = executeQuery(broker, compiledXQuery);
            return result.itemAt(0).toJavaObject(boolean.class);
        });
        transaction.commit();
        assertTrue(sendResult.booleanValue());
    }
    // check the SMTP server received the email
    final MimeMessage[] receivedMessages = greenMail.getReceivedMessages();
    assertEquals(1, receivedMessages.length);
    final MimeMessage receivedMessage = receivedMessages[0];
    // final Address sender = receivedMessage.getSender();
    // assertEquals(from, sender.toString());
    final Address[] recipients = receivedMessage.getRecipients(Message.RecipientType.TO);
    assertEquals(1, recipients.length);
    assertEquals(to, recipients[0].toString());
    assertEquals(subject, receivedMessage.getSubject());
    assertEquals(messageText, GreenMailUtil.getBody(receivedMessage));
}
Also used : DBBroker(org.exist.storage.DBBroker) Address(jakarta.mail.Address) MimeMessage(jakarta.mail.internet.MimeMessage) StringSource(org.exist.source.StringSource) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source) Test(org.junit.Test)

Aggregations

BrokerPool (org.exist.storage.BrokerPool)381 DBBroker (org.exist.storage.DBBroker)300 Txn (org.exist.storage.txn.Txn)180 Sequence (org.exist.xquery.value.Sequence)157 Test (org.junit.Test)115 XQuery (org.exist.xquery.XQuery)105 Collection (org.exist.collections.Collection)71 StringInputSource (org.exist.util.StringInputSource)66 TransactionManager (org.exist.storage.txn.TransactionManager)61 Source (org.exist.source.Source)43 StringSource (org.exist.source.StringSource)40 CompiledXQuery (org.exist.xquery.CompiledXQuery)38 Path (java.nio.file.Path)22 XmldbURI (org.exist.xmldb.XmldbURI)21 XPathException (org.exist.xquery.XPathException)21 Properties (java.util.Properties)20 LockedDocument (org.exist.dom.persistent.LockedDocument)20 InputSource (org.xml.sax.InputSource)20 IOException (java.io.IOException)19 XQueryContext (org.exist.xquery.XQueryContext)19