Added MailTargetAdapterParser (INT-132).
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
file-source=org.springframework.integration.adapter.file.config.FileSourceAdapterParser
|
||||
file-target=org.springframework.integration.adapter.file.config.FileTargetAdapterParser
|
||||
jms-source=org.springframework.integration.adapter.jms.config.JmsSourceAdapterParser
|
||||
jms-target=org.springframework.integration.adapter.jms.config.JmsTargetAdapterParser
|
||||
jms-target=org.springframework.integration.adapter.jms.config.JmsTargetAdapterParser
|
||||
mail-target=org.springframework.integration.adapter.mail.config.MailTargetAdapterParser
|
||||
@@ -93,4 +93,20 @@
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:element name="mail-target">
|
||||
<xsd:complexType>
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Defines a mail-sending target channel adapter.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:attribute name="id" type="xsd:string"/>
|
||||
<xsd:attribute name="mail-sender" type="xsd:string"/>
|
||||
<xsd:attribute name="host" type="xsd:string"/>
|
||||
<xsd:attribute name="username" type="xsd:string"/>
|
||||
<xsd:attribute name="password" type="xsd:string"/>
|
||||
<xsd:attribute name="channel" type="xsd:string" use="required"/>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
</xsd:schema>
|
||||
@@ -16,6 +16,9 @@
|
||||
|
||||
package org.springframework.integration.adapter.mail;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.mail.MailMessage;
|
||||
|
||||
@@ -26,9 +29,13 @@ import org.springframework.mail.MailMessage;
|
||||
* and from/reply-to addresses based on the integration {@link Message}.
|
||||
*
|
||||
* @author Marius Bogoevici
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public abstract class AbstractMailHeaderGenerator implements MailHeaderGenerator {
|
||||
|
||||
private final Log logger = LogFactory.getLog(this.getClass());
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve the subject of an e-mail message from an integration message.
|
||||
*
|
||||
@@ -90,9 +97,15 @@ public abstract class AbstractMailHeaderGenerator implements MailHeaderGenerator
|
||||
if (subject != null) {
|
||||
mailMessage.setSubject(subject);
|
||||
}
|
||||
else if (logger.isWarnEnabled()) {
|
||||
logger.warn("no 'SUBJECT' property available for mail message");
|
||||
}
|
||||
if (to != null) {
|
||||
mailMessage.setTo(to);
|
||||
}
|
||||
else if (logger.isWarnEnabled()) {
|
||||
logger.warn("no 'TO' property available for mail message");
|
||||
}
|
||||
if (cc != null) {
|
||||
mailMessage.setCc(cc);
|
||||
}
|
||||
@@ -102,6 +115,9 @@ public abstract class AbstractMailHeaderGenerator implements MailHeaderGenerator
|
||||
if (from != null) {
|
||||
mailMessage.setFrom(from);
|
||||
}
|
||||
else if (logger.isWarnEnabled()) {
|
||||
logger.warn("no 'FROM' property available for mail message");
|
||||
}
|
||||
if (replyTo != null) {
|
||||
mailMessage.setReplyTo(replyTo);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* 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.config;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import org.springframework.beans.factory.config.RuntimeBeanReference;
|
||||
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.MessagingConfigurationException;
|
||||
import org.springframework.integration.adapter.mail.MailTargetAdapter;
|
||||
import org.springframework.integration.endpoint.DefaultMessageEndpoint;
|
||||
import org.springframework.integration.scheduling.Subscription;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Parser for the <mail-target/> element.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class MailTargetAdapterParser extends AbstractSingleBeanDefinitionParser {
|
||||
|
||||
protected Class<?> getBeanClass(Element element) {
|
||||
return DefaultMessageEndpoint.class;
|
||||
}
|
||||
|
||||
protected boolean shouldGenerateId() {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected boolean shouldGenerateIdAsFallback() {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||
RootBeanDefinition adapterDef = new RootBeanDefinition(MailTargetAdapter.class);
|
||||
String mailSenderRef = element.getAttribute("mail-sender");
|
||||
String host = element.getAttribute("host");
|
||||
String username = element.getAttribute("username");
|
||||
String password = element.getAttribute("password");
|
||||
if (StringUtils.hasText(mailSenderRef)) {
|
||||
if (StringUtils.hasText(host) || StringUtils.hasText(username) || StringUtils.hasText(password)) {
|
||||
throw new MessagingConfigurationException("The 'host', 'username', and 'password' properties " +
|
||||
"should not be provided when using a 'mail-sender' reference.");
|
||||
}
|
||||
adapterDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference(mailSenderRef));
|
||||
}
|
||||
else if (StringUtils.hasText(host)) {
|
||||
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
|
||||
mailSender.setHost(host);
|
||||
if (StringUtils.hasText(username)) {
|
||||
mailSender.setUsername(username);
|
||||
}
|
||||
if (StringUtils.hasText(password)) {
|
||||
mailSender.setPassword(password);
|
||||
}
|
||||
adapterDef.getConstructorArgumentValues().addGenericArgumentValue(mailSender);
|
||||
}
|
||||
else {
|
||||
throw new MessagingConfigurationException("Either a 'mail-sender' reference or 'host' property is required.");
|
||||
}
|
||||
String adapterBeanName = parserContext.getReaderContext().generateBeanName(adapterDef);
|
||||
parserContext.registerBeanComponent(new BeanComponentDefinition(adapterDef, adapterBeanName));
|
||||
builder.addPropertyReference("handler", adapterBeanName);
|
||||
String channel = element.getAttribute("channel");
|
||||
Subscription subscription = new Subscription(channel);
|
||||
builder.addPropertyValue("subscription", subscription);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -31,8 +31,6 @@ 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;
|
||||
@@ -46,24 +44,26 @@ public class MailTargetAdapterTests {
|
||||
|
||||
private StubJavaMailSender mailSender;
|
||||
|
||||
private StaticMailHeaderGenerator staticMailHeaderGenerator;
|
||||
|
||||
|
||||
@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.staticMailHeaderGenerator = new StaticMailHeaderGenerator();
|
||||
this.staticMailHeaderGenerator.setBcc(MailTestsHelper.BCC);
|
||||
this.staticMailHeaderGenerator.setCc(MailTestsHelper.CC);
|
||||
this.staticMailHeaderGenerator.setFrom(MailTestsHelper.FROM);
|
||||
this.staticMailHeaderGenerator.setReplyTo(MailTestsHelper.REPLY_TO);
|
||||
this.staticMailHeaderGenerator.setSubject(MailTestsHelper.SUBJECT);
|
||||
this.staticMailHeaderGenerator.setTo(MailTestsHelper.TO);
|
||||
this.mailTargetAdapter = new MailTargetAdapter(this.mailSender);
|
||||
this.mailTargetAdapter.setHeaderGenerator(mailHeaderGenerator);
|
||||
this.mailTargetAdapter.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTextMessage() {
|
||||
this.mailTargetAdapter.setHeaderGenerator(this.staticMailHeaderGenerator);
|
||||
this.mailTargetAdapter.handle(new StringMessage(MailTestsHelper.MESSAGE_TEXT));
|
||||
SimpleMailMessage message = MailTestsHelper.createSimpleMailMessage();
|
||||
assertEquals("no mime message should have been sent",
|
||||
@@ -76,6 +76,7 @@ public class MailTargetAdapterTests {
|
||||
|
||||
@Test
|
||||
public void testByteArrayMessage() throws Exception {
|
||||
this.mailTargetAdapter.setHeaderGenerator(this.staticMailHeaderGenerator);
|
||||
byte[] payload = {1, 2, 3};
|
||||
this.mailTargetAdapter.handle(new GenericMessage<byte[]>(payload));
|
||||
byte[] buffer = new byte[1024];
|
||||
@@ -89,6 +90,25 @@ public class MailTargetAdapterTests {
|
||||
assertEquals(mimeMessage.getRecipients(Message.RecipientType.TO).length, MailTestsHelper.TO.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultMailHeaderGenerator() {
|
||||
StringMessage message = new StringMessage(MailTestsHelper.MESSAGE_TEXT);
|
||||
message.getHeader().setAttribute(MailTargetAdapter.SUBJECT, MailTestsHelper.SUBJECT);
|
||||
message.getHeader().setAttribute(MailTargetAdapter.TO, MailTestsHelper.TO);
|
||||
message.getHeader().setAttribute(MailTargetAdapter.CC, MailTestsHelper.CC);
|
||||
message.getHeader().setAttribute(MailTargetAdapter.BCC, MailTestsHelper.BCC);
|
||||
message.getHeader().setAttribute(MailTargetAdapter.FROM, MailTestsHelper.FROM);
|
||||
message.getHeader().setAttribute(MailTargetAdapter.REPLY_TO, MailTestsHelper.REPLY_TO);
|
||||
this.mailTargetAdapter.handle(message);
|
||||
SimpleMailMessage mailMessage = MailTestsHelper.createSimpleMailMessage();
|
||||
assertEquals("no mime message should have been sent",
|
||||
0, mailSender.getSentMimeMessages().size());
|
||||
assertEquals("only one simple message must be sent",
|
||||
1, mailSender.getSentSimpleMailMessages().size());
|
||||
assertEquals("message content different from expected",
|
||||
mailMessage, mailSender.getSentSimpleMailMessages().get(0));
|
||||
}
|
||||
|
||||
@After
|
||||
public void reset() {
|
||||
this.mailSender.reset();
|
||||
|
||||
Reference in New Issue
Block a user