diff --git a/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/ByteArrayMailMessageMapper.java b/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/ByteArrayMailMessageMapper.java deleted file mode 100644 index e7c5c8c195..0000000000 --- a/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/ByteArrayMailMessageMapper.java +++ /dev/null @@ -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 { - - 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 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); - } - } - -} diff --git a/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/MailHeaders.java b/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/MailHeaders.java index cc17ca6e9f..426a21de13 100644 --- a/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/MailHeaders.java +++ b/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/MailHeaders.java @@ -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"; } diff --git a/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/MailSendingMessageConsumer.java b/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/MailSendingMessageConsumer.java index bf3a366011..cdad645eed 100644 --- a/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/MailSendingMessageConsumer.java +++ b/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/MailSendingMessageConsumer.java @@ -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. * + *

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 toString() 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 textMessageMapper; - - private volatile MessageMapper byteArrayMessageMapper; - - private volatile MessageMapper 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 textMessageMapper) { - this.textMessageMapper = textMessageMapper; - } - - public void setByteArrayMessageMapper(MessageMapper byteArrayMessageMapper) { - this.byteArrayMessageMapper = byteArrayMessageMapper; - } - - public void setObjectMessageMapper(MessageMapper 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) message); + MailMessage mailMessage = null; + if (message.getPayload() instanceof MailMessage) { + mailMessage = (MailMessage) message.getPayload(); } else if (message.getPayload() instanceof byte[]) { - return this.byteArrayMessageMapper.mapMessage((Message) message); + mailMessage = this.createMailMessageFromByteArrayMessage((Message) message); } - return this.objectMessageMapper.mapMessage((Message) 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 { - - public Message toMessage(MailMessage source) { - throw new UnsupportedOperationException("mapping from MailMessage to Object not supported"); + private MailMessage createMailMessageFromByteArrayMessage(Message 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 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); + } } } diff --git a/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/TextMailMessageMapper.java b/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/TextMailMessageMapper.java deleted file mode 100644 index 7a156d112a..0000000000 --- a/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/TextMailMessageMapper.java +++ /dev/null @@ -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 { - - public MailMessage mapMessage(Message message) { - SimpleMailMessage mailMessage = new SimpleMailMessage(); - mailMessage.setText(message.getPayload().toString()); - return mailMessage; - } - -} diff --git a/org.springframework.integration.mail/src/test/java/org/springframework/integration/mail/MailSendingMessageConsumerContextTests.java b/org.springframework.integration.mail/src/test/java/org/springframework/integration/mail/MailSendingMessageConsumerContextTests.java index ed10c2f814..65e4498540 100644 --- a/org.springframework.integration.mail/src/test/java/org/springframework/integration/mail/MailSendingMessageConsumerContextTests.java +++ b/org.springframework.integration.mail/src/test/java/org/springframework/integration/mail/MailSendingMessageConsumerContextTests.java @@ -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(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(payload)); + } + } diff --git a/org.springframework.integration.mail/src/test/java/org/springframework/integration/mail/MailSendingMessageConsumerTests.java b/org.springframework.integration.mail/src/test/java/org/springframework/integration/mail/MailSendingMessageConsumerTests.java index 36cb02c0fa..26d5212f2e 100644 --- a/org.springframework.integration.mail/src/test/java/org/springframework/integration/mail/MailSendingMessageConsumerTests.java +++ b/org.springframework.integration.mail/src/test/java/org/springframework/integration/mail/MailSendingMessageConsumerTests.java @@ -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(payload)); + org.springframework.integration.message.Message 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 message = MessageBuilder.withPayload(MailTestsHelper.MESSAGE_TEXT) .setHeader(MailHeaders.SUBJECT, MailTestsHelper.SUBJECT) diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/endpoint/AbstractMessageHandlingEndpoint.java b/org.springframework.integration/src/main/java/org/springframework/integration/endpoint/AbstractMessageHandlingEndpoint.java index 98074580fa..63e423f430 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/endpoint/AbstractMessageHandlingEndpoint.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/endpoint/AbstractMessageHandlingEndpoint.java @@ -87,7 +87,7 @@ public abstract class AbstractMessageHandlingEndpoint extends AbstractMessageCon if (result == null) { if (this.requiresReply) { throw new MessageHandlingException(message, "endpoint '" + this.getName() - + " requires a reply, but no reply was received"); + + "' requires a reply, but no reply was received"); } return; }