Removed MailMessageMappers. MailMessageSendingConsumer now handles any MailMessage payload or byte array payload. For all other payload types, it calls 'toString' to generate the Mail text.
This commit is contained in:
@@ -1,74 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.mail;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.integration.adapter.MessageMappingException;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageMapper;
|
||||
import org.springframework.mail.MailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.mail.javamail.MimeMailMessage;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Message mapper used for mapping byte array messages to mail messages.
|
||||
* Generates an e-mail message with the byte array as an attachment. The
|
||||
* multipart mode and attachment name are configurable.
|
||||
*
|
||||
* @author Marius Bogoevici
|
||||
*/
|
||||
public class ByteArrayMailMessageMapper implements MessageMapper<byte[], MailMessage> {
|
||||
|
||||
private final JavaMailSender mailSender;
|
||||
|
||||
private volatile int multipartMode = MimeMessageHelper.MULTIPART_MODE_MIXED;
|
||||
|
||||
private volatile String attachmentFilename = "content";
|
||||
|
||||
|
||||
public ByteArrayMailMessageMapper(JavaMailSender mailSender) {
|
||||
Assert.notNull(mailSender, "'mailSender' must not be null");
|
||||
this.mailSender = mailSender;
|
||||
}
|
||||
|
||||
|
||||
public void setMultipartMode(int multipartMode) {
|
||||
this.multipartMode = multipartMode;
|
||||
}
|
||||
|
||||
public void setAttachmentFilename(String attachmentFilename) {
|
||||
this.attachmentFilename = attachmentFilename;
|
||||
}
|
||||
|
||||
public MailMessage mapMessage(Message<byte[]> message) {
|
||||
try {
|
||||
MimeMessage mimeMessage = this.mailSender.createMimeMessage();
|
||||
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, this.multipartMode);
|
||||
helper.addAttachment(this.attachmentFilename, new ByteArrayResource(message.getPayload()));
|
||||
return new MimeMailMessage(helper);
|
||||
}
|
||||
catch (MessagingException e) {
|
||||
throw new MessageMappingException(message, "failed to create MimeMessage", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,18 +24,22 @@ package org.springframework.integration.mail;
|
||||
*/
|
||||
public class MailHeaders {
|
||||
|
||||
public static final String TRANSFPORT_PREFIX = "spring.integration.transport.mail.";
|
||||
public static final String TRANSPORT_PREFIX = "spring.integration.transport.mail.";
|
||||
|
||||
public static final String SUBJECT = TRANSFPORT_PREFIX + "SUBJECT";
|
||||
public static final String SUBJECT = TRANSPORT_PREFIX + "SUBJECT";
|
||||
|
||||
public static final String TO = TRANSFPORT_PREFIX + "TO";
|
||||
public static final String TO = TRANSPORT_PREFIX + "TO";
|
||||
|
||||
public static final String CC = TRANSFPORT_PREFIX + "CC";
|
||||
public static final String CC = TRANSPORT_PREFIX + "CC";
|
||||
|
||||
public static final String BCC = TRANSFPORT_PREFIX + "BCC";
|
||||
public static final String BCC = TRANSPORT_PREFIX + "BCC";
|
||||
|
||||
public static final String FROM = TRANSFPORT_PREFIX + "FROM";
|
||||
public static final String FROM = TRANSPORT_PREFIX + "FROM";
|
||||
|
||||
public static final String REPLY_TO = TRANSFPORT_PREFIX+ "REPLY_TO";
|
||||
public static final String REPLY_TO = TRANSPORT_PREFIX+ "REPLY_TO";
|
||||
|
||||
public static final String MULTIPART_MODE = TRANSPORT_PREFIX + "MULTIPART_MODE";
|
||||
|
||||
public static final String ATTACHMENT_FILENAME = TRANSPORT_PREFIX + "ATTACHMENT_FILENAME";
|
||||
|
||||
}
|
||||
|
||||
@@ -16,34 +16,40 @@
|
||||
|
||||
package org.springframework.integration.mail;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.integration.adapter.MessageMappingException;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageConsumer;
|
||||
import org.springframework.integration.message.MessageMapper;
|
||||
import org.springframework.mail.MailMessage;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.mail.javamail.MimeMailMessage;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A {@link MessageConsumer} implementation for sending mail.
|
||||
*
|
||||
* <p>If the Message is an instance of {@link MailMessage}, it will be passed
|
||||
* as-is. If the Message payload is a byte array, it will be passed as an
|
||||
* attachment, and the {@link MailHeaders#ATTACHMENT_FILENAME} header is
|
||||
* required. For any other payload type, a {@link SimpleMailMessage} will be
|
||||
* created with the payload's <code>toString()</code> value as the Mail text.
|
||||
*
|
||||
* @see MailHeaders
|
||||
*
|
||||
* @author Marius Bogoevici
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class MailSendingMessageConsumer implements MessageConsumer, InitializingBean {
|
||||
public class MailSendingMessageConsumer implements MessageConsumer {
|
||||
|
||||
private final JavaMailSender mailSender;
|
||||
|
||||
private volatile MailHeaderGenerator mailHeaderGenerator = new DefaultMailHeaderGenerator();
|
||||
|
||||
private volatile MessageMapper<String, MailMessage> textMessageMapper;
|
||||
|
||||
private volatile MessageMapper<byte[], MailMessage> byteArrayMessageMapper;
|
||||
|
||||
private volatile MessageMapper<Object, MailMessage> objectMessageMapper;
|
||||
|
||||
|
||||
/**
|
||||
* Create a MailSendingMessageConsumer.
|
||||
@@ -57,32 +63,11 @@ public class MailSendingMessageConsumer implements MessageConsumer, Initializing
|
||||
}
|
||||
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
this.textMessageMapper = (this.textMessageMapper != null) ?
|
||||
this.textMessageMapper : new TextMailMessageMapper();
|
||||
this.byteArrayMessageMapper = (byteArrayMessageMapper != null) ?
|
||||
this.byteArrayMessageMapper : new ByteArrayMailMessageMapper(this.mailSender);
|
||||
this.objectMessageMapper = (objectMessageMapper != null) ?
|
||||
this.objectMessageMapper : new DefaultObjectMailMessageMapper();
|
||||
}
|
||||
|
||||
public void setHeaderGenerator(MailHeaderGenerator mailHeaderGenerator) {
|
||||
Assert.notNull(mailHeaderGenerator, "'mailHeaderGenerator' must not be null");
|
||||
this.mailHeaderGenerator = mailHeaderGenerator;
|
||||
}
|
||||
|
||||
public void setTextMessageMapper(MessageMapper<String, MailMessage> textMessageMapper) {
|
||||
this.textMessageMapper = textMessageMapper;
|
||||
}
|
||||
|
||||
public void setByteArrayMessageMapper(MessageMapper<byte[], MailMessage> byteArrayMessageMapper) {
|
||||
this.byteArrayMessageMapper = byteArrayMessageMapper;
|
||||
}
|
||||
|
||||
public void setObjectMessageMapper(MessageMapper<Object, MailMessage> objectMessageMapper) {
|
||||
this.objectMessageMapper = objectMessageMapper;
|
||||
}
|
||||
|
||||
public final void onMessage(Message<?> message) {
|
||||
MailMessage mailMessage = this.convertMessageToMailMessage(message);
|
||||
this.mailHeaderGenerator.populateMailMessageHeader(mailMessage, message);
|
||||
@@ -91,13 +76,18 @@ public class MailSendingMessageConsumer implements MessageConsumer, Initializing
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private MailMessage convertMessageToMailMessage(Message<?> message) {
|
||||
if (message.getPayload() instanceof String) {
|
||||
return this.textMessageMapper.mapMessage((Message<String>) message);
|
||||
MailMessage mailMessage = null;
|
||||
if (message.getPayload() instanceof MailMessage) {
|
||||
mailMessage = (MailMessage) message.getPayload();
|
||||
}
|
||||
else if (message.getPayload() instanceof byte[]) {
|
||||
return this.byteArrayMessageMapper.mapMessage((Message<byte[]>) message);
|
||||
mailMessage = this.createMailMessageFromByteArrayMessage((Message<byte[]>) message);
|
||||
}
|
||||
return this.objectMessageMapper.mapMessage((Message<Object>) message);
|
||||
else {
|
||||
mailMessage = new SimpleMailMessage();
|
||||
mailMessage.setText(message.getPayload().toString());
|
||||
}
|
||||
return mailMessage;
|
||||
}
|
||||
|
||||
private void sendMailMessage(MailMessage mailMessage) {
|
||||
@@ -109,23 +99,28 @@ public class MailSendingMessageConsumer implements MessageConsumer, Initializing
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException(
|
||||
"MailMessage subclass '" + mailMessage.getClass().getName() + "' not supported");
|
||||
"Unsupported MailMessage type [" + mailMessage.getClass().getName() + "].");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class DefaultObjectMailMessageMapper implements MessageMapper<Object, MailMessage> {
|
||||
|
||||
public Message<Object> toMessage(MailMessage source) {
|
||||
throw new UnsupportedOperationException("mapping from MailMessage to Object not supported");
|
||||
private MailMessage createMailMessageFromByteArrayMessage(Message<byte[]> message) {
|
||||
String attachmentFileName = message.getHeaders().get(MailHeaders.ATTACHMENT_FILENAME, String.class);
|
||||
if (attachmentFileName == null) {
|
||||
throw new MessageMappingException(message, "Header '" + MailHeaders.ATTACHMENT_FILENAME
|
||||
+ "' is required when mapping a Message with a byte array payload to a MailMessage.");
|
||||
}
|
||||
|
||||
public MailMessage mapMessage(Message<Object> objectMessage) {
|
||||
SimpleMailMessage message = new SimpleMailMessage();
|
||||
message.setText(objectMessage.getPayload().toString());
|
||||
return message;
|
||||
Integer multipartMode = message.getHeaders().get(MailHeaders.MULTIPART_MODE, Integer.class);
|
||||
if (multipartMode == null) {
|
||||
multipartMode = MimeMessageHelper.MULTIPART_MODE_MIXED;
|
||||
}
|
||||
|
||||
MimeMessage mimeMessage = this.mailSender.createMimeMessage();
|
||||
try {
|
||||
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, multipartMode);
|
||||
helper.addAttachment(attachmentFileName, new ByteArrayResource(message.getPayload()));
|
||||
return new MimeMailMessage(helper);
|
||||
} catch (MessagingException e) {
|
||||
throw new MessageMappingException(message, "failed to create MimeMessage", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.mail;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageMapper;
|
||||
import org.springframework.mail.MailMessage;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
|
||||
/**
|
||||
* Message mapper for transforming integration messages into simple text
|
||||
* e-mail messages. The body of the e-mail message will be the result of
|
||||
* invoking the message payload's toString() method.
|
||||
*
|
||||
* @author Marius Bogoevici
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class TextMailMessageMapper implements MessageMapper<String, MailMessage> {
|
||||
|
||||
public MailMessage mapMessage(Message<String> message) {
|
||||
SimpleMailMessage mailMessage = new SimpleMailMessage();
|
||||
mailMessage.setText(message.getPayload().toString());
|
||||
return mailMessage;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -31,8 +31,9 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.integration.mail.MailSendingMessageConsumer;
|
||||
import org.springframework.integration.adapter.MessageMappingException;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
@@ -58,7 +59,7 @@ public class MailSendingMessageConsumerContextTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringMesssagesWithConfiguration() {
|
||||
public void stringMesssagesWithConfiguration() {
|
||||
this.consumer.onMessage(new StringMessage(MailTestsHelper.MESSAGE_TEXT));
|
||||
SimpleMailMessage message = MailTestsHelper.createSimpleMailMessage();
|
||||
assertEquals("no mime message should have been sent",
|
||||
@@ -70,9 +71,13 @@ public class MailSendingMessageConsumerContextTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByteArrayMessage() throws Exception {
|
||||
public void byteArrayMessage() throws Exception {
|
||||
byte[] payload = {1, 2, 3};
|
||||
this.consumer.onMessage(new GenericMessage<byte[]>(payload));
|
||||
org.springframework.integration.message.Message<?> message =
|
||||
MessageBuilder.withPayload(payload)
|
||||
.setHeader(MailHeaders.ATTACHMENT_FILENAME, "attachment.txt")
|
||||
.build();
|
||||
this.consumer.onMessage(message);
|
||||
assertEquals("no mime message should have been sent",
|
||||
1, this.mailSender.getSentMimeMessages().size());
|
||||
assertEquals("only one simple message must be sent",
|
||||
@@ -88,4 +93,10 @@ public class MailSendingMessageConsumerContextTests {
|
||||
assertEquals(mimeMessage.getRecipients(Message.RecipientType.TO).length, MailTestsHelper.TO.length);
|
||||
}
|
||||
|
||||
@Test(expected = MessageMappingException.class)
|
||||
public void byteArrayMessageWithoutAttachmentFileName() throws Exception {
|
||||
byte[] payload = {1, 2, 3};
|
||||
this.consumer.onMessage(new GenericMessage<byte[]>(payload));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -31,10 +31,6 @@ import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.mail.MailHeaders;
|
||||
import org.springframework.integration.mail.MailSendingMessageConsumer;
|
||||
import org.springframework.integration.mail.StaticMailHeaderGenerator;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
@@ -62,7 +58,6 @@ public class MailSendingMessageConsumerTests {
|
||||
this.staticMailHeaderGenerator.setSubject(MailTestsHelper.SUBJECT);
|
||||
this.staticMailHeaderGenerator.setTo(MailTestsHelper.TO);
|
||||
this.consumer = new MailSendingMessageConsumer(this.mailSender);
|
||||
this.consumer.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@After
|
||||
@@ -72,7 +67,7 @@ public class MailSendingMessageConsumerTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void testTextMessage() {
|
||||
public void textMessage() {
|
||||
this.consumer.setHeaderGenerator(this.staticMailHeaderGenerator);
|
||||
this.consumer.onMessage(new StringMessage(MailTestsHelper.MESSAGE_TEXT));
|
||||
SimpleMailMessage message = MailTestsHelper.createSimpleMailMessage();
|
||||
@@ -85,10 +80,14 @@ public class MailSendingMessageConsumerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByteArrayMessage() throws Exception {
|
||||
public void byteArrayMessage() throws Exception {
|
||||
this.consumer.setHeaderGenerator(this.staticMailHeaderGenerator);
|
||||
byte[] payload = {1, 2, 3};
|
||||
this.consumer.onMessage(new GenericMessage<byte[]>(payload));
|
||||
org.springframework.integration.message.Message<byte[]> message =
|
||||
MessageBuilder.withPayload(payload)
|
||||
.setHeader(MailHeaders.ATTACHMENT_FILENAME, "attachment.txt")
|
||||
.build();
|
||||
this.consumer.onMessage(message);
|
||||
byte[] buffer = new byte[1024];
|
||||
MimeMessage mimeMessage = this.mailSender.getSentMimeMessages().get(0);
|
||||
assertTrue("message must be multipart", mimeMessage.getContent() instanceof Multipart);
|
||||
@@ -101,7 +100,7 @@ public class MailSendingMessageConsumerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultMailHeaderGenerator() {
|
||||
public void defaultMailHeaderGenerator() {
|
||||
org.springframework.integration.message.Message<String> message =
|
||||
MessageBuilder.withPayload(MailTestsHelper.MESSAGE_TEXT)
|
||||
.setHeader(MailHeaders.SUBJECT, MailTestsHelper.SUBJECT)
|
||||
|
||||
Reference in New Issue
Block a user