Removed MailHeaderGenerator. The MailSendingMessageConsumer handles the mapping of header values, but those values should be provided by a Transformer initially.
This commit is contained in:
@@ -1,126 +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.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
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
|
||||
* @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.
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
if (bcc != null) {
|
||||
mailMessage.setBcc(bcc);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,77 +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;
|
||||
|
||||
/**
|
||||
* The default implementation of {@link MailHeaderGenerator}. Configures the
|
||||
* {@link org.springframework.mail.MailMessage} properties based on attributes
|
||||
* provided with known attribute keys as defined in {@link MailHeaders}.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class DefaultMailHeaderGenerator extends AbstractMailHeaderGenerator {
|
||||
|
||||
@Override
|
||||
protected String getSubject(Message<?> message) {
|
||||
return this.retrieveAsString(message, MailHeaders.SUBJECT);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getTo(Message<?> message) {
|
||||
return this.retrieveAsStringArray(message, MailHeaders.TO);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getCc(Message<?> message) {
|
||||
return this.retrieveAsStringArray(message, MailHeaders.CC);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getBcc(Message<?> message) {
|
||||
return this.retrieveAsStringArray(message, MailHeaders.BCC);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getFrom(Message<?> message) {
|
||||
return this.retrieveAsString(message, MailHeaders.FROM);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getReplyTo(Message<?> message) {
|
||||
return this.retrieveAsString(message, MailHeaders.REPLY_TO);
|
||||
}
|
||||
|
||||
|
||||
private String retrieveAsString(Message<?> message, String key) {
|
||||
Object value = message.getHeaders().get(key);
|
||||
return (value instanceof String) ? (String) value : null;
|
||||
}
|
||||
|
||||
private String[] retrieveAsStringArray(Message<?> message, String key) {
|
||||
Object value = message.getHeaders().get(key);
|
||||
if (value instanceof String[]) {
|
||||
return (String[]) value;
|
||||
}
|
||||
if (value instanceof String) {
|
||||
return new String[] { (String) value };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,39 +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.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);
|
||||
|
||||
}
|
||||
@@ -23,6 +23,7 @@ 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.MessageHeaders;
|
||||
import org.springframework.mail.MailMessage;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
@@ -35,9 +36,10 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* <p>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 <code>toString()</code> value as the Mail text.
|
||||
* attachment, and in that case, the {@link MailHeaders#ATTACHMENT_FILENAME}
|
||||
* header is required. For any other payload type, a {@link SimpleMailMessage}
|
||||
* will be created with the payload's <code>toString()</code> value as the Mail
|
||||
* text.
|
||||
*
|
||||
* @see MailHeaders
|
||||
*
|
||||
@@ -48,8 +50,6 @@ public class MailSendingMessageConsumer implements MessageConsumer {
|
||||
|
||||
private final JavaMailSender mailSender;
|
||||
|
||||
private volatile MailHeaderGenerator mailHeaderGenerator = new DefaultMailHeaderGenerator();
|
||||
|
||||
|
||||
/**
|
||||
* Create a MailSendingMessageConsumer.
|
||||
@@ -63,34 +63,8 @@ public class MailSendingMessageConsumer implements MessageConsumer {
|
||||
}
|
||||
|
||||
|
||||
public void setHeaderGenerator(MailHeaderGenerator mailHeaderGenerator) {
|
||||
Assert.notNull(mailHeaderGenerator, "'mailHeaderGenerator' must not be null");
|
||||
this.mailHeaderGenerator = mailHeaderGenerator;
|
||||
}
|
||||
|
||||
public final void onMessage(Message<?> message) {
|
||||
MailMessage mailMessage = this.convertMessageToMailMessage(message);
|
||||
this.mailHeaderGenerator.populateMailMessageHeader(mailMessage, message);
|
||||
this.sendMailMessage(mailMessage);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private MailMessage convertMessageToMailMessage(Message<?> message) {
|
||||
MailMessage mailMessage = null;
|
||||
if (message.getPayload() instanceof MailMessage) {
|
||||
mailMessage = (MailMessage) message.getPayload();
|
||||
}
|
||||
else if (message.getPayload() instanceof byte[]) {
|
||||
mailMessage = this.createMailMessageFromByteArrayMessage((Message<byte[]>) message);
|
||||
}
|
||||
else {
|
||||
mailMessage = new SimpleMailMessage();
|
||||
mailMessage.setText(message.getPayload().toString());
|
||||
}
|
||||
return mailMessage;
|
||||
}
|
||||
|
||||
private void sendMailMessage(MailMessage mailMessage) {
|
||||
if (mailMessage instanceof SimpleMailMessage) {
|
||||
this.mailSender.send((SimpleMailMessage) mailMessage);
|
||||
}
|
||||
@@ -103,6 +77,23 @@ public class MailSendingMessageConsumer implements MessageConsumer {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private MailMessage convertMessageToMailMessage(Message<?> message) {
|
||||
if (message.getPayload() instanceof MailMessage) {
|
||||
return (MailMessage) message.getPayload();
|
||||
}
|
||||
MailMessage mailMessage = null;
|
||||
if (message.getPayload() instanceof byte[]) {
|
||||
mailMessage = this.createMailMessageFromByteArrayMessage((Message<byte[]>) message);
|
||||
}
|
||||
else {
|
||||
mailMessage = new SimpleMailMessage();
|
||||
mailMessage.setText(message.getPayload().toString());
|
||||
}
|
||||
this.configureHeaderValues(mailMessage, message.getHeaders());
|
||||
return mailMessage;
|
||||
}
|
||||
|
||||
private MailMessage createMailMessageFromByteArrayMessage(Message<byte[]> message) {
|
||||
String attachmentFileName = message.getHeaders().get(MailHeaders.ATTACHMENT_FILENAME, String.class);
|
||||
if (attachmentFileName == null) {
|
||||
@@ -123,4 +114,44 @@ public class MailSendingMessageConsumer implements MessageConsumer {
|
||||
}
|
||||
}
|
||||
|
||||
private void configureHeaderValues(MailMessage mailMessage, MessageHeaders headers) {
|
||||
String subject = headers.get(MailHeaders.SUBJECT, String.class);
|
||||
if (subject != null) {
|
||||
mailMessage.setSubject(subject);
|
||||
}
|
||||
String[] to = this.retrieveAsStringArray(headers, MailHeaders.TO);
|
||||
if (to != null) {
|
||||
mailMessage.setTo(to);
|
||||
}
|
||||
String[] cc = this.retrieveAsStringArray(headers, MailHeaders.CC);
|
||||
if (cc != null) {
|
||||
mailMessage.setCc(cc);
|
||||
}
|
||||
String[] bcc = this.retrieveAsStringArray(headers, MailHeaders.BCC);
|
||||
if (bcc != null) {
|
||||
mailMessage.setBcc(bcc);
|
||||
}
|
||||
String from = headers.get(MailHeaders.FROM, String.class);
|
||||
if (from != null) {
|
||||
mailMessage.setFrom(from);
|
||||
}
|
||||
String replyTo = headers.get(MailHeaders.REPLY_TO, String.class);
|
||||
if (replyTo != null) {
|
||||
mailMessage.setReplyTo(replyTo);
|
||||
}
|
||||
}
|
||||
|
||||
private String[] retrieveAsStringArray(MessageHeaders headers, String key) {
|
||||
Object value = headers.get(key);
|
||||
if (value != null) {
|
||||
if (value instanceof String[]) {
|
||||
return (String[]) value;
|
||||
}
|
||||
if (value instanceof String) {
|
||||
return new String[] { (String) value };
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,90 +0,0 @@
|
||||
/*
|
||||
* 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.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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -25,7 +25,6 @@
|
||||
</xsd:annotation>
|
||||
<xsd:attribute name="id" type="xsd:string"/>
|
||||
<xsd:attribute name="mail-sender" type="xsd:string"/>
|
||||
<xsd:attribute name="header-generator" type="xsd:string"/>
|
||||
<xsd:attribute name="host" type="xsd:string"/>
|
||||
<xsd:attribute name="username" type="xsd:string"/>
|
||||
<xsd:attribute name="password" type="xsd:string"/>
|
||||
|
||||
Reference in New Issue
Block a user