Added MailTargetAdapter and associated support classes (INT-107).
This commit is contained in:
@@ -3,8 +3,12 @@
|
||||
<classpathentry kind="src" path="src/main/java"/>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/javax.activation/activation/activation-1.1.jar" sourcepath="/IVY_CACHE/javax.activation/activation/activation-sources-1.1.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/javax.jms/jms/jms-1.1.jar" sourcepath="/IVY_CACHE/javax.jms/jms/jms-sources-1.1.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/javax.mail/mail/mail-1.4.jar" sourcepath="/IVY_CACHE/javax.mail/mail/mail-sources-1.4.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.aopalliance/aopalliance/aopalliance-1.0.jar" sourcepath="IVY_CACHE/org.aopalliance/aopalliance/aopalliance-sources-1.0.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.apache/commons-logging/commons-logging-1.1.jar" sourcepath="/IVY_CACHE/org.apache/commons-logging/commons-logging-sources-1.1.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.apache/commons-pool/commons-pool-1.3.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.junit/junit/junit-4.4.jar" sourcepath="/IVY_CACHE/org.junit/junit/junit-sources-4.4.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.springframework/spring-aop/spring-aop-2.5.0.jar" sourcepath="/IVY_CACHE/org.springframework/spring-aop/spring-aop-sources-2.5.0.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.springframework/spring-beans/spring-beans-2.5.0.jar" sourcepath="/IVY_CACHE/org.springframework/spring-beans/spring-beans-sources-2.5.0.jar"/>
|
||||
@@ -13,8 +17,6 @@
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.springframework/spring-core/spring-core-2.5.0.jar" sourcepath="/IVY_CACHE/org.springframework/spring-core/spring-core-sources-2.5.0.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.springframework/spring-jms/spring-jms-2.5.0.jar" sourcepath="/IVY_CACHE/org.springframework/spring-jms/spring-jms-sources-2.5.0.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.springframework/spring-tx/spring-tx-2.5.0.jar" sourcepath="/IVY_CACHE/org.springframework/spring-tx/spring-tx-sources-2.5.0.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.apache/commons-pool/commons-pool-1.3.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/javax.jms/jms/jms-1.1.jar" sourcepath="/IVY_CACHE/javax.jms/jms/jms-sources-1.1.jar"/>
|
||||
<classpathentry combineaccessrules="false" kind="src" path="/spring-integration-core"/>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
||||
|
||||
@@ -20,7 +20,9 @@
|
||||
</publications>
|
||||
|
||||
<dependencies>
|
||||
<dependency org="javax.activation" name="activation" rev="1.1" conf="compile->default"/>
|
||||
<dependency org="javax.jms" name="jms" rev="1.1" conf="compile->default"/>
|
||||
<dependency org="javax.mail" name="mail" rev="1.4" conf="compile->default"/>
|
||||
<dependency org="org.junit" name="junit" rev="4.4" conf="test->default"/>
|
||||
<dependency org="org.springframework.integration" name="spring-integration-core" rev="latest.integration" conf="compile"/>
|
||||
<dependency org="org.springframework" name="spring-context" rev="2.5.0" conf="compile->default"/>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<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 Message<byte[]> toMessage(MailMessage source) {
|
||||
throw new UnsupportedOperationException("mapping from MailMessage to byte array not supported");
|
||||
}
|
||||
|
||||
public MailMessage fromMessage(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 MessageHandlingException("failed to create MimeMessage", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
@@ -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<String, MailMessage> textMessageMapper;
|
||||
|
||||
private volatile MessageMapper<byte[], MailMessage> byteArrayMessageMapper;
|
||||
|
||||
private volatile MessageMapper<Object, MailMessage> 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<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 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<String>) message);
|
||||
}
|
||||
else if (message.getPayload() instanceof byte[]) {
|
||||
return this.byteArrayMessageMapper.fromMessage((Message<byte[]>) message);
|
||||
}
|
||||
return this.objectMessageMapper.fromMessage((Message<Object>) 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<Object, MailMessage> {
|
||||
|
||||
public Message<Object> toMessage(MailMessage source) {
|
||||
throw new UnsupportedOperationException("mapping from MailMessage to Object not supported");
|
||||
}
|
||||
|
||||
public MailMessage fromMessage(Message<Object> objectMessage) {
|
||||
SimpleMailMessage message = new SimpleMailMessage();
|
||||
message.setText(objectMessage.getPayload().toString());
|
||||
return message;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String, MailMessage> {
|
||||
|
||||
public Message<String> toMessage(MailMessage source) {
|
||||
Assert.isInstanceOf(SimpleMailMessage.class, source, "source must be a SimpleMailMessage");
|
||||
return new StringMessage(((SimpleMailMessage) source).getText());
|
||||
}
|
||||
|
||||
public MailMessage fromMessage(Message<String> stringMessage) {
|
||||
SimpleMailMessage mailMessage = new SimpleMailMessage();
|
||||
mailMessage.setText(stringMessage.getPayload());
|
||||
return mailMessage;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<byte[]>(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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<MimeMessage> sentMimeMessages = new ArrayList<MimeMessage>();
|
||||
|
||||
private final List<SimpleMailMessage> sentSimpleMailMessages = new ArrayList<SimpleMailMessage>();
|
||||
|
||||
|
||||
public StubJavaMailSender(MimeMessage uniqueMessage) {
|
||||
this.uniqueMessage = uniqueMessage;
|
||||
}
|
||||
|
||||
|
||||
public List<MimeMessage> getSentMimeMessages() {
|
||||
return this.sentMimeMessages;
|
||||
}
|
||||
|
||||
public List<SimpleMailMessage> 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();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user