
com.citytechinc.aem.groovy.console.notification.impl.EmailNotificationService.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aem-groovy-console Show documentation
Show all versions of aem-groovy-console Show documentation
The AEM Groovy Console provides an interface for running Groovy scripts in the AEM (Adobe CQ) container. Scripts
can be created to manipulate content in the JCR, call OSGi services, or execute arbitrary code using the AEM,
Sling, or JCR APIs.
package com.citytechinc.aem.groovy.console.notification.impl
import com.citytechinc.aem.groovy.console.configuration.ConfigurationService
import com.citytechinc.aem.groovy.console.notification.NotificationService
import com.citytechinc.aem.groovy.console.response.RunScriptResponse
import com.day.cq.mailer.MailService
import groovy.text.GStringTemplateEngine
import groovy.util.logging.Slf4j
import org.apache.commons.lang3.CharEncoding
import org.apache.commons.mail.HtmlEmail
import org.apache.felix.scr.annotations.Component
import org.apache.felix.scr.annotations.Reference
import org.apache.felix.scr.annotations.ReferenceCardinality
import org.apache.felix.scr.annotations.Service
import javax.jcr.Session
@Service(NotificationService)
@Component
@Slf4j("LOG")
class EmailNotificationService implements NotificationService {
static final def SUBJECT = "Groovy Console Script Execution Result"
static final def TEMPLATE_PATH_SUCCESS = "/email-success.template"
static final def TEMPLATE_PATH_FAIL = "/email-fail.template"
static final def FORMAT_TIMESTAMP = "yyyy-MM-dd hh:mm:ss"
@Reference
ConfigurationService configurationService
@Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY)
MailService mailService
@Override
void notify(Session session, RunScriptResponse response) {
if (configurationService.emailEnabled && mailService) {
def recipients = configurationService.emailRecipients
if (recipients) {
def binding = createBinding(session, response)
def templatePath = response.exceptionStackTrace ? TEMPLATE_PATH_FAIL : TEMPLATE_PATH_SUCCESS
def email = createEmail(recipients, binding, templatePath)
LOG.debug "sending email, recipients = {}", recipients
Thread.start {
mailService.send(email)
}
} else {
LOG.error "email enabled but no recipients configured"
}
} else {
LOG.info "email disabled or mail service unavailable"
}
}
private def createEmail(Set recipients, Map binding, String templatePath) {
def email = new HtmlEmail()
email.charset = CharEncoding.UTF_8
recipients.each { name ->
email.addTo(name)
}
email.subject = SUBJECT
def template = new GStringTemplateEngine().createTemplate(this.class.getResource(templatePath))
email.htmlMsg = template.make(binding).toString()
email
}
private static def createBinding(Session session, RunScriptResponse response) {
def binding = [
username : session.userID,
timestamp: new Date().format(FORMAT_TIMESTAMP),
script : response.script
]
if (response.exceptionStackTrace) {
binding.stackTrace = response.exceptionStackTrace
} else {
binding.putAll([
result : response.result,
output : response.output,
runningTime: response.runningTime
])
}
binding
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy