diff --git a/spring-integration-adapters/.classpath b/spring-integration-adapters/.classpath index 38dd41852e..26a5a6e354 100644 --- a/spring-integration-adapters/.classpath +++ b/spring-integration-adapters/.classpath @@ -3,8 +3,12 @@ + + + + @@ -13,8 +17,6 @@ - - diff --git a/spring-integration-adapters/ivy.xml b/spring-integration-adapters/ivy.xml index e38e284111..138323cb53 100644 --- a/spring-integration-adapters/ivy.xml +++ b/spring-integration-adapters/ivy.xml @@ -20,7 +20,9 @@ + + diff --git a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/AbstractMailHeaderGenerator.java b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/AbstractMailHeaderGenerator.java new file mode 100644 index 0000000000..e0ca92fb10 --- /dev/null +++ b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/AbstractMailHeaderGenerator.java @@ -0,0 +1,110 @@ +/* + * Copyright 2002-2007 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.adapter.mail; + +import org.springframework.integration.message.Message; +import org.springframework.mail.MailMessage; + +/** + * Base implementation for {@link MailHeaderGenerator MailHeaderGenerators}. + * This class is abstract. Subclasses must implement the corresponding template + * methods to retrieve the values for the mail message's subject, recipients, + * and from/reply-to addresses based on the integration {@link Message}. + * + * @author Marius Bogoevici + */ +public abstract class AbstractMailHeaderGenerator implements MailHeaderGenerator { + + /** + * Retrieve the subject of an e-mail message from an integration message. + * + * @param message the integration {@link Message} + * @return the e-mail message subject + */ + protected abstract String getSubject(Message message); + + /** + * Retrieve the recipients list from an integration message. + * + * @param message the integration {@link Message} + * @return recipients list (TO) + */ + protected abstract String[] getTo(Message message); + + /** + * Retrieve the CC recipients list from an integration message. + * + * @param message the integration {@link Message} + * @return CC recipients list (e-mail addresses) + */ + protected abstract String[] getCc(Message message); + + /** + * Retrieve the BCC recipients list from an integration message. + * + * @param message the integration {@link Message} + * @return BCC recipients list (e-mail addresses) + */ + protected abstract String[] getBcc(Message message); + + /** + * Retrieve the From: e-mail address from an integration message. + * + * @param message the integration {@link Message} + * @return the From: e-mail address + */ + protected abstract String getFrom(Message message); + + /** + * Retrieve the Reply To: e-mail address from an integration message. + * + * @param message the integration {@link Message} + * @return the ReplyTo: e-mail address + */ + protected abstract String getReplyTo(Message message); + + /** + * Populate the mail message using the results of the template methods. + */ + public final void populateMailMessageHeader(MailMessage mailMessage, Message message) { + final String subject = getSubject(message); + final String[] to = getTo(message); + final String[] cc = getCc(message); + final String[] bcc = getBcc(message); + final String from = getFrom(message); + final String replyTo = getReplyTo(message); + if (subject != null) { + mailMessage.setSubject(subject); + } + if (to != null) { + mailMessage.setTo(to); + } + if (cc != null) { + mailMessage.setCc(cc); + } + if (bcc != null) { + mailMessage.setBcc(bcc); + } + if (from != null) { + mailMessage.setFrom(from); + } + if (replyTo != null) { + mailMessage.setReplyTo(replyTo); + } + } + +} diff --git a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/ByteArrayMailMessageMapper.java b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/ByteArrayMailMessageMapper.java new file mode 100644 index 0000000000..3ab0a524b8 --- /dev/null +++ b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/ByteArrayMailMessageMapper.java @@ -0,0 +1,78 @@ +/* + * Copyright 2002-2007 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.adapter.mail; + +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; + +import org.springframework.core.io.ByteArrayResource; +import org.springframework.integration.message.AbstractMessageMapper; +import org.springframework.integration.message.Message; +import org.springframework.integration.message.MessageHandlingException; +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 extends AbstractMessageMapper { + + 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 Message toMessage(MailMessage source) { + throw new UnsupportedOperationException("mapping from MailMessage to byte array not supported"); + } + + public MailMessage fromMessage(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 MessageHandlingException("failed to create MimeMessage", e); + } + } + +} diff --git a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/MailHeaderGenerator.java b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/MailHeaderGenerator.java new file mode 100644 index 0000000000..aa639c406c --- /dev/null +++ b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/MailHeaderGenerator.java @@ -0,0 +1,39 @@ +/* + * Copyright 2002-2007 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.adapter.mail; + +import org.springframework.integration.message.Message; +import org.springframework.mail.MailMessage; + +/** + * Strategy interface for generating header information for an e-mail message + * from the content of the integration message. Minimal configuration should + * include the recipients list, the subject, from/reply-to, etc. However, this + * strategy allows the implementation of more complex business logic, when these + * parameters are depending on the integration message itself. + * + * @author Marius Bogoevici + */ +public interface MailHeaderGenerator { + + /** + * Populate the e-mail message header based on the content of the + * integration message. + */ + void populateMailMessageHeader(MailMessage mailMessage, Message message); + +} diff --git a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/MailTargetAdapter.java b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/MailTargetAdapter.java new file mode 100644 index 0000000000..764411a282 --- /dev/null +++ b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/MailTargetAdapter.java @@ -0,0 +1,126 @@ +/* + * Copyright 2002-2007 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.adapter.mail; + +import org.springframework.beans.factory.InitializingBean; +import org.springframework.integration.handler.MessageHandler; +import org.springframework.integration.message.AbstractMessageMapper; +import org.springframework.integration.message.Message; +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.util.Assert; + +/** + * A target adapter for sending mail. + * + * @author Marius Bogoevici + * @author Mark Fisher + */ +public class MailTargetAdapter implements MessageHandler, InitializingBean { + + private volatile JavaMailSender mailSender; + + private volatile MailHeaderGenerator mailHeaderGenerator; + + private volatile MessageMapper textMessageMapper; + + private volatile MessageMapper byteArrayMessageMapper; + + private volatile MessageMapper objectMessageMapper; + + + public void afterPropertiesSet() throws Exception { + Assert.notNull(this.mailSender, "'mailSender' must not be null"); + Assert.notNull(this.mailHeaderGenerator, "'mailHeaderGenerator' must not be null"); + 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 setMailSender(JavaMailSender mailSender) { + Assert.notNull(mailSender, "'mailSender' must not be null"); + this.mailSender = mailSender; + } + + 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 Message handle(Message message) { + MailMessage mailMessage = this.convertMessageToMailMessage(message); + this.mailHeaderGenerator.populateMailMessageHeader(mailMessage, message); + this.sendMailMessage(mailMessage); + return null; + } + + public MailMessage convertMessageToMailMessage(Message message) { + if (message.getPayload() instanceof String) { + return this.textMessageMapper.fromMessage((Message) message); + } + else if (message.getPayload() instanceof byte[]) { + return this.byteArrayMessageMapper.fromMessage((Message) message); + } + return this.objectMessageMapper.fromMessage((Message) message); + } + + private void sendMailMessage(MailMessage mailMessage) { + if (mailMessage instanceof SimpleMailMessage) { + this.mailSender.send((SimpleMailMessage) mailMessage); + } + else if (mailMessage instanceof MimeMailMessage) { + this.mailSender.send(((MimeMailMessage) mailMessage).getMimeMessage()); + } + else { + throw new IllegalArgumentException( + "MailMessage subclass '" + mailMessage.getClass().getName() + "' not supported"); + } + } + + + private static class DefaultObjectMailMessageMapper extends AbstractMessageMapper { + + public Message toMessage(MailMessage source) { + throw new UnsupportedOperationException("mapping from MailMessage to Object not supported"); + } + + public MailMessage fromMessage(Message objectMessage) { + SimpleMailMessage message = new SimpleMailMessage(); + message.setText(objectMessage.getPayload().toString()); + return message; + } + } + +} diff --git a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/StaticMailHeaderGenerator.java b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/StaticMailHeaderGenerator.java new file mode 100644 index 0000000000..66de5c2d2f --- /dev/null +++ b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/StaticMailHeaderGenerator.java @@ -0,0 +1,90 @@ +/* + * Copyright 2002-2007 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.adapter.mail; + +import org.springframework.integration.message.Message; + +/** + * Mail header generator implementation that populates a mail message header + * from statically configured properties. + * + * @author Marius Bogoevici + */ +public class StaticMailHeaderGenerator extends AbstractMailHeaderGenerator { + + private String subject; + + private String[] to; + + private String[] cc; + + private String[] bcc; + + private String from; + + private String replyTo; + + + public void setSubject(String subject) { + this.subject = subject; + } + + protected String getSubject(Message message) { + return this.subject; + } + + public void setTo(String[] to) { + this.to = to; + } + + protected String[] getTo(Message message) { + return this.to; + } + + public void setCc(String[] cc) { + this.cc = cc; + } + + protected String[] getCc(Message message) { + return this.cc; + } + + public void setBcc(String[] bcc) { + this.bcc = bcc; + } + + protected String[] getBcc(Message message) { + return this.bcc; + } + + public void setFrom(String from) { + this.from = from; + } + + protected String getFrom(Message message) { + return this.from; + } + + public void setReplyTo(String replyTo) { + this.replyTo = replyTo; + } + + protected String getReplyTo(Message message) { + return this.replyTo; + } + +} diff --git a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/TextMailMessageMapper.java b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/TextMailMessageMapper.java new file mode 100644 index 0000000000..7085f7e9be --- /dev/null +++ b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/TextMailMessageMapper.java @@ -0,0 +1,46 @@ +/* + * Copyright 2002-2007 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.adapter.mail; + +import org.springframework.integration.message.AbstractMessageMapper; +import org.springframework.integration.message.Message; +import org.springframework.integration.message.StringMessage; +import org.springframework.mail.MailMessage; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.util.Assert; + +/** + * Message mapper for transforming integration messages with a String payload + * into simple text e-mail messages. The body of the e-mail message will be the + * content of the integration message's payload. + * + * @author Marius Bogoevici + */ +public class TextMailMessageMapper extends AbstractMessageMapper { + + public Message toMessage(MailMessage source) { + Assert.isInstanceOf(SimpleMailMessage.class, source, "source must be a SimpleMailMessage"); + return new StringMessage(((SimpleMailMessage) source).getText()); + } + + public MailMessage fromMessage(Message stringMessage) { + SimpleMailMessage mailMessage = new SimpleMailMessage(); + mailMessage.setText(stringMessage.getPayload()); + return mailMessage; + } + +} diff --git a/spring-integration-adapters/src/test/java/org/springframework/integration/mail/MailTargetAdapterTests.java b/spring-integration-adapters/src/test/java/org/springframework/integration/mail/MailTargetAdapterTests.java new file mode 100644 index 0000000000..e71caf9c81 --- /dev/null +++ b/spring-integration-adapters/src/test/java/org/springframework/integration/mail/MailTargetAdapterTests.java @@ -0,0 +1,98 @@ +/* + * Copyright 2002-2007 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 static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.DataInputStream; + +import javax.mail.Message; +import javax.mail.Multipart; +import javax.mail.Session; +import javax.mail.internet.MimeMessage; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import org.springframework.integration.adapter.mail.MailTargetAdapter; +import org.springframework.integration.adapter.mail.StaticMailHeaderGenerator; +import org.springframework.integration.message.GenericMessage; +import org.springframework.integration.message.StringMessage; +import org.springframework.mail.SimpleMailMessage; + +/** + * @author Marius Bogoevici + */ +public class MailTargetAdapterTests { + + private MailTargetAdapter mailTargetAdapter; + + private StubJavaMailSender mailSender; + + + @Before + public void setUp() throws Exception { + this.mailSender = new StubJavaMailSender(new MimeMessage((Session) null)); + StaticMailHeaderGenerator mailHeaderGenerator = new StaticMailHeaderGenerator(); + mailHeaderGenerator.setBcc(MailTestsHelper.BCC); + mailHeaderGenerator.setCc(MailTestsHelper.CC); + mailHeaderGenerator.setFrom(MailTestsHelper.FROM); + mailHeaderGenerator.setReplyTo(MailTestsHelper.REPLY_TO); + mailHeaderGenerator.setSubject(MailTestsHelper.SUBJECT); + mailHeaderGenerator.setTo(MailTestsHelper.TO); + this.mailTargetAdapter = new MailTargetAdapter(); + this.mailTargetAdapter.setMailSender(this.mailSender); + this.mailTargetAdapter.setHeaderGenerator(mailHeaderGenerator); + this.mailTargetAdapter.afterPropertiesSet(); + } + + @Test + public void testTextMessage() { + this.mailTargetAdapter.handle(new StringMessage(MailTestsHelper.MESSAGE_TEXT)); + SimpleMailMessage message = MailTestsHelper.createSimpleMailMessage(); + assertEquals("no messages should have been sent yet", + 0, mailSender.getSentMimeMessages().size()); + assertEquals("only one simple message must be sent", + 1, mailSender.getSentSimpleMailMessages().size()); + assertEquals("message content different from expected", + message, mailSender.getSentSimpleMailMessages().get(0)); + } + + @Test + public void testByteArrayMessage() throws Exception { + byte[] payload = {1,2,3}; + this.mailTargetAdapter.handle(new GenericMessage(payload)); + byte[] buffer = new byte[1024]; + MimeMessage mimeMessage = this.mailSender.getSentMimeMessages().get(0); + assertTrue("message must be multipart", mimeMessage.getContent() instanceof Multipart); + int size = new DataInputStream(((Multipart) mimeMessage.getContent()).getBodyPart(0).getInputStream()).read(buffer); + assertEquals("buffer size does not match", payload.length, size); + byte[] messageContent = new byte[size]; + System.arraycopy(buffer, 0, messageContent, 0, payload.length); + assertArrayEquals("buffer content does not match", payload, messageContent); + assertEquals(mimeMessage.getRecipients(Message.RecipientType.TO).length, MailTestsHelper.TO.length); + } + + @After + public void reset() { + this.mailSender.reset(); + } + +} diff --git a/spring-integration-adapters/src/test/java/org/springframework/integration/mail/MailTestsHelper.java b/spring-integration-adapters/src/test/java/org/springframework/integration/mail/MailTestsHelper.java new file mode 100644 index 0000000000..001ef519b5 --- /dev/null +++ b/spring-integration-adapters/src/test/java/org/springframework/integration/mail/MailTestsHelper.java @@ -0,0 +1,56 @@ +/* + * Copyright 2002-2007 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.mail.SimpleMailMessage; + +/** + * @author Marius Bogoevici + */ +public class MailTestsHelper { + + public static final String SUBJECT = "Some subject"; + + public static final String MESSAGE_TEXT = "Some text"; + + public static final String[] TO = new String[] { + "toRecipient1@springframework.org", "toRecipient2@springframework.org" }; + + public static final String[] CC = new String[] { + "ccRecipient1@springframework.org", "ccRecipient2@springframework.org" }; + + public static final String[] BCC = new String[] { + "bccRecipient1@springframework.org", "bccRecipient2@springframework.org" }; + + public static final String FROM = "from@springframework.org"; + + public static final String REPLY_TO = "replyTo@springframework.org"; + + + public static SimpleMailMessage createSimpleMailMessage() { + SimpleMailMessage message = new SimpleMailMessage(); + message.setBcc(BCC); + message.setCc(CC); + message.setTo(TO); + message.setSubject(SUBJECT); + message.setReplyTo(REPLY_TO); + message.setFrom(FROM); + message.setText(MESSAGE_TEXT); + return message; + } + +} diff --git a/spring-integration-adapters/src/test/java/org/springframework/integration/mail/StubJavaMailSender.java b/spring-integration-adapters/src/test/java/org/springframework/integration/mail/StubJavaMailSender.java new file mode 100644 index 0000000000..b77da51ed9 --- /dev/null +++ b/spring-integration-adapters/src/test/java/org/springframework/integration/mail/StubJavaMailSender.java @@ -0,0 +1,93 @@ +/* + * Copyright 2002-2007 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 java.io.InputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import javax.mail.internet.MimeMessage; + +import org.springframework.mail.MailException; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessagePreparator; + +/** + * @author Marius Bogoevici + */ +public class StubJavaMailSender implements JavaMailSender { + + private MimeMessage uniqueMessage; + + private final List sentMimeMessages = new ArrayList(); + + private final List sentSimpleMailMessages = new ArrayList(); + + + public StubJavaMailSender(MimeMessage uniqueMessage) { + this.uniqueMessage = uniqueMessage; + } + + + public List getSentMimeMessages() { + return this.sentMimeMessages; + } + + public List getSentSimpleMailMessages() { + return this.sentSimpleMailMessages; + } + + public MimeMessage createMimeMessage() { + return this.uniqueMessage; + } + + public MimeMessage createMimeMessage(InputStream contentStream) throws MailException { + return this.uniqueMessage; + } + + public void send(MimeMessage mimeMessage) throws MailException { + this.sentMimeMessages.add(mimeMessage); + } + + public void send(MimeMessage[] mimeMessages) throws MailException { + this.sentMimeMessages.addAll(Arrays.asList(mimeMessages)); + } + + public void send(MimeMessagePreparator mimeMessagePreparator) throws MailException { + throw new UnsupportedOperationException("MimeMessagePreparator not supported"); + } + + public void send(MimeMessagePreparator[] mimeMessagePreparators) throws MailException { + throw new UnsupportedOperationException("MimeMessagePreparator not supported"); + } + + public void send(SimpleMailMessage simpleMessage) throws MailException { + this.sentSimpleMailMessages.add(simpleMessage); + } + + public void send(SimpleMailMessage[] simpleMessages) throws MailException { + this.sentSimpleMailMessages.addAll(Arrays.asList(simpleMessages)); + } + + public void reset() { + this.sentMimeMessages.clear(); + this.sentSimpleMailMessages.clear(); + } + +}