Search in sources :

Example 1 with MessageException

use of org.openmrs.notification.MessageException in project openmrs-core by openmrs.

the class MailMessageSender method send.

/**
 * Send the message.
 *
 * @param message the message to be sent
 */
@Override
public void send(Message message) throws MessageException {
    try {
        MimeMessage mimeMessage = createMimeMessage(message);
        Transport.send(mimeMessage);
    } catch (Exception e) {
        log.error("failed to send message", e);
        // catch mail-specific exception and re-throw it as app-specific exception
        throw new MessageException(e);
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessageException(org.openmrs.notification.MessageException) MessageException(org.openmrs.notification.MessageException)

Example 2 with MessageException

use of org.openmrs.notification.MessageException in project openmrs-core by openmrs.

the class MailMessageSender method createMimeMessage.

/**
 * Converts the message object to a mime message in order to prepare it to be sent.
 *
 * @param message
 * @return MimeMessage
 */
public MimeMessage createMimeMessage(Message message) throws Exception {
    if (message.getRecipients() == null) {
        throw new MessageException("Message must contain at least one recipient");
    }
    // set the content-type to the default if it isn't defined in Message
    if (!StringUtils.hasText(message.getContentType())) {
        String contentType = Context.getAdministrationService().getGlobalProperty("mail.default_content_type");
        message.setContentType(StringUtils.hasText(contentType) ? contentType : "text/plain");
    }
    MimeMessage mimeMessage = new MimeMessage(session);
    if (message.getSender() != null) {
        mimeMessage.setSender(new InternetAddress(message.getSender()));
    } else {
        String defaultFromMailAddress = Context.getAdministrationService().getGlobalProperty("mail.from");
        if (StringUtils.hasText(defaultFromMailAddress)) {
            mimeMessage.setSender(new InternetAddress(defaultFromMailAddress));
        }
    }
    mimeMessage.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(message.getRecipients(), false));
    mimeMessage.setSubject(message.getSubject());
    if (!message.hasAttachment()) {
        mimeMessage.setContent(message.getContent(), message.getContentType());
    } else {
        mimeMessage.setContent(createMultipart(message));
    }
    return mimeMessage;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MessageException(org.openmrs.notification.MessageException) MimeMessage(javax.mail.internet.MimeMessage)

Example 3 with MessageException

use of org.openmrs.notification.MessageException in project openmrs-core by openmrs.

the class MessageServiceImpl method prepareMessage.

/**
 * Prepare a message based on a template and data used for variable substitution within template.
 *
 * @param templateName name of the template to be used
 * @param data mapping used for variable substitution within template
 * @return the prepared Message
 */
@Override
@Transactional(readOnly = true)
public Message prepareMessage(String templateName, Map data) throws MessageException {
    try {
        Template template = (Template) getTemplatesByName(templateName).get(0);
        template.setData(data);
        return Context.getMessageService().prepareMessage(template);
    } catch (Exception e) {
        throw new MessageException("Could not prepare message with template " + templateName, e);
    }
}
Also used : MessageException(org.openmrs.notification.MessageException) MessageException(org.openmrs.notification.MessageException) Template(org.openmrs.notification.Template) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with MessageException

use of org.openmrs.notification.MessageException in project openmrs-core by openmrs.

the class AlertReminderTask method sendAlertNotifications.

/**
 * Send alerts
 *
 * @param alerts the unread alerts
 * @param users the users who have not read the alerts
 */
private void sendAlertNotifications(Collection<Alert> alerts) {
    try {
        // Create a new message
        Message message = Context.getMessageService().createMessage("Alert Reminder", "You have unread alerts.");
        // Get all recipients
        Collection<User> users = getRecipients(alerts);
        // Send a message to each person only once
        Context.getMessageService().sendMessage(message, users);
    } catch (MessageException e) {
        log.error("Failed to send message", e);
    }
}
Also used : User(org.openmrs.User) Message(org.openmrs.notification.Message) MessageException(org.openmrs.notification.MessageException)

Example 5 with MessageException

use of org.openmrs.notification.MessageException in project openmrs-core by openmrs.

the class VelocityMessagePreparator method prepare.

// TODO: need better error handling
@Override
public Message prepare(Template template) throws MessageException {
    VelocityContext context = new VelocityContext(template.getData());
    StringWriter writer = new StringWriter();
    try {
        // I have no idea what this is used for
        engine.evaluate(// I have no idea what this is used for
        context, // I have no idea what this is used for
        writer, // I have no idea what this is used for
        "template", template.getTemplate());
    } catch (Exception e) {
        // need better error handling
        log.error("Failed to prepare message using template " + e.getMessage(), e);
        throw new MessageException(e);
    }
    // Prepare the message
    Message message = new Message();
    message.setSubject(template.getSubject());
    message.setRecipients(template.getRecipients());
    message.setSender(template.getSender());
    message.setContent(writer.toString());
    return message;
}
Also used : StringWriter(java.io.StringWriter) Message(org.openmrs.notification.Message) MessageException(org.openmrs.notification.MessageException) VelocityContext(org.apache.velocity.VelocityContext) MessageException(org.openmrs.notification.MessageException)

Aggregations

MessageException (org.openmrs.notification.MessageException)5 MimeMessage (javax.mail.internet.MimeMessage)2 Message (org.openmrs.notification.Message)2 StringWriter (java.io.StringWriter)1 InternetAddress (javax.mail.internet.InternetAddress)1 VelocityContext (org.apache.velocity.VelocityContext)1 User (org.openmrs.User)1 Template (org.openmrs.notification.Template)1 Transactional (org.springframework.transaction.annotation.Transactional)1