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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user