Search in sources :

Example 1 with ServiceExecutionError

use of io.automatiko.engine.api.workflow.ServiceExecutionError in project automatiko-engine by automatiko-io.

the class SendEmailService method sendSimple.

/**
 * Sends email to the given list of recipients with given subject and body.
 * Optionally given attachments will be put on the message as well
 *
 * In case of error a <code>ServiceExecutionError</code> will be thrown with error code set to <code>sendEmailFailure</code>
 * so it can be used within workflow definition to handle it
 *
 * @param tos comma separated list of recipients
 * @param subject subject of the email
 * @param body body of the email
 * @param attachments optional attachments
 */
public void sendSimple(String tos, String subject, String body, File<byte[]>... attachments) {
    try {
        for (String to : tos.split(",")) {
            Mail mail = Mail.withHtml(to, subject, body);
            for (File<byte[]> attachment : attachments) {
                if (attachment == null) {
                    continue;
                }
                mail.addAttachment(attachment.name(), attachment.content(), attachment.type());
            }
            mailer.send(mail);
        }
    } catch (Exception e) {
        throw new ServiceExecutionError("sendEmailFailure", e.getMessage(), e);
    }
}
Also used : ServiceExecutionError(io.automatiko.engine.api.workflow.ServiceExecutionError) Mail(io.quarkus.mailer.Mail)

Example 2 with ServiceExecutionError

use of io.automatiko.engine.api.workflow.ServiceExecutionError in project automatiko-engine by automatiko-io.

the class SendEmailService method sendCorrelatedWithCC.

/**
 * Sends email to the given list of recipients and cc recipients with given subject and body that is created based on the
 * given template.
 * Optionally given attachments will be put on the message as well
 *
 * In case of error a <code>ServiceExecutionError</code> will be thrown with error code set to <code>sendEmailFailure</code>
 * so it can be used within workflow definition to handle it
 *
 * In case template cannot be found with given name a <code>ServiceExecutionError</code> will
 * be thrown with error code set to <code>emailTemplateNotFound</code>
 *
 * @param correlation correlation information to be attached to the message
 * @param tos comma separated list of recipients
 * @param ccs comma separated list of CC recipients
 * @param subject subject of the email
 * @param templateName name of the email template to be used to create the body of the email message
 * @param body body of the email
 * @param attachments optional attachments
 */
public void sendCorrelatedWithCC(String correlation, String tos, String ccs, String subject, String templateName, Object body, File<byte[]>... attachments) {
    Template template = getTemplate(templateName);
    try {
        Map<String, Object> templateData = new HashMap<>();
        templateData.put("body", body);
        String content = template.instance().data(templateData).render();
        for (String to : tos.split(",")) {
            Mail mail = Mail.withHtml(to, subject, content);
            for (String cc : ccs.split(",")) {
                mail.addCc(cc);
            }
            for (File<byte[]> attachment : attachments) {
                if (attachment == null) {
                    continue;
                }
                mail.addAttachment(attachment.name(), attachment.content(), attachment.type());
            }
            mail.addHeader("Message-ID", EmailUtils.messageIdWithCorrelation(correlation, host.orElse("localhost")));
            mailer.send(mail);
        }
    } catch (Exception e) {
        throw new ServiceExecutionError("sendEmailFailure", e.getMessage(), e);
    }
}
Also used : ServiceExecutionError(io.automatiko.engine.api.workflow.ServiceExecutionError) Mail(io.quarkus.mailer.Mail) HashMap(java.util.HashMap) Template(io.quarkus.qute.Template)

Example 3 with ServiceExecutionError

use of io.automatiko.engine.api.workflow.ServiceExecutionError in project automatiko-engine by automatiko-io.

the class SendEmailService method send.

/**
 * Sends email to the given list of recipients with given subject and body that is created based on the given template.
 * Optionally given attachments will be put on the message as well
 *
 * In case of error a <code>ServiceExecutionError</code> will be thrown with error code set to <code>sendEmailFailure</code>
 * so it can be used within workflow definition to handle it
 *
 * In case template cannot be found with given name a <code>ServiceExecutionError</code> will
 * be thrown with error code set to <code>emailTemplateNotFound</code>
 *
 * @param tos comma separated list of recipients
 * @param subject subject of the email
 * @param templateName name of the email template to be used to create the body of the email message
 * @param body body of the email
 * @param attachments optional attachments
 */
public void send(String tos, String subject, String templateName, Object body, File<byte[]>... attachments) {
    Template template = getTemplate(templateName);
    try {
        Map<String, Object> templateData = new HashMap<>();
        templateData.put("body", body);
        String content = template.instance().data(templateData).render();
        for (String to : tos.split(",")) {
            Mail mail = Mail.withHtml(to, subject, content);
            for (File<byte[]> attachment : attachments) {
                if (attachment == null) {
                    continue;
                }
                mail.addAttachment(attachment.name(), attachment.content(), attachment.type());
            }
            mailer.send(mail);
        }
    } catch (Exception e) {
        throw new ServiceExecutionError("sendEmailFailure", e.getMessage(), e);
    }
}
Also used : ServiceExecutionError(io.automatiko.engine.api.workflow.ServiceExecutionError) Mail(io.quarkus.mailer.Mail) HashMap(java.util.HashMap) Template(io.quarkus.qute.Template)

Example 4 with ServiceExecutionError

use of io.automatiko.engine.api.workflow.ServiceExecutionError in project automatiko-engine by automatiko-io.

the class FragileService method maybeFail.

private void maybeFail() {
    // introduce some artificial failures
    final Long invocationNumber = counter.getAndIncrement();
    if (failing.get()) {
        LOGGER.error("Invocation {} failing", invocationNumber);
        throw new ServiceExecutionError(failingCode, "Service failed.");
    }
    if (block.get()) {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    LOGGER.info("Invocation {} OK", invocationNumber);
}
Also used : ServiceExecutionError(io.automatiko.engine.api.workflow.ServiceExecutionError) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 5 with ServiceExecutionError

use of io.automatiko.engine.api.workflow.ServiceExecutionError in project automatiko-engine by automatiko-io.

the class ArchiveService method zip.

/**
 * Builds an archive with given files. built archive will be named based on the given
 * name so it should include the extension as well.
 *
 * In case of error a <code>ServiceExecutionError</code> will be thrown with error code set to <code>zipFailure</code>
 * so it can be used within workflow definition to handle it
 *
 * @param name name of the archive
 * @param files files to be included in the archive
 * @return built archive with given files
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public Archive zip(String name, File... files) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    List<String> entries = new ArrayList<>();
    try (ZipOutputStream zipOut = new ZipOutputStream(output)) {
        for (File<byte[]> file : files) {
            if (file == null || file.content() == null) {
                continue;
            }
            if (entries.contains(file.name())) {
                LOGGER.warn("Duplicated archive entry with name '{}', will be ignored", file.name());
                continue;
            }
            ZipEntry zipEntry = new ZipEntry(file.name());
            zipOut.putNextEntry(zipEntry);
            zipOut.write(file.content());
            entries.add(file.name());
        }
    } catch (IOException e) {
        throw new ServiceExecutionError("zipFailure", e.getMessage());
    }
    Archive archive = new Archive(name, output.toByteArray());
    archive.attributes().put(Archive.ENTRIES_ATTR, entries.stream().collect(Collectors.joining(",")));
    return archive;
}
Also used : ServiceExecutionError(io.automatiko.engine.api.workflow.ServiceExecutionError) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

ServiceExecutionError (io.automatiko.engine.api.workflow.ServiceExecutionError)15 Mail (io.quarkus.mailer.Mail)12 Template (io.quarkus.qute.Template)6 HashMap (java.util.HashMap)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ZipEntry (java.util.zip.ZipEntry)2 File (io.automatiko.engine.api.workflow.files.File)1 ByteArrayFile (io.automatiko.engine.workflow.file.ByteArrayFile)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 ZipInputStream (java.util.zip.ZipInputStream)1 ZipOutputStream (java.util.zip.ZipOutputStream)1