INT-3309 Pluggable MessageBuilder
This is still a work in process. There are a bunch of TODOs in classes that are not managed by Spring and so need to have the MessageBuilderFactory injected. But I am looking for feedback on the approach. INT-3309 Resolve TODOs Provide access to the MessageBuilderFactory in all classes. INT-3309 Polishing + Tests * Fallback to 'fromMessage()' if mutating and inbound message is not MutableMessage * Add 'alwaysMutate' boolean to MutableMessageBuilderFactory - coerces 'fromMessage' calls to 'mutateMessage' * Add tests INT-3309 Polishing; PR Comments Also add tests to parent/child contexts where the parent has the default message builder and the child has a mutable message builder. INT-3309 More Polish; PR Comments Also fix removeHeader in MMB.
This commit is contained in:
committed by
Artem Bilan
parent
4c775bce9d
commit
ff845b5069
@@ -36,7 +36,6 @@ import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.integration.endpoint.MessageProducerSupport;
|
||||
import org.springframework.integration.mail.event.MailIntegrationEvent;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.integration.transaction.IntegrationResourceHolder;
|
||||
import org.springframework.integration.transaction.TransactionSynchronizationFactory;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
@@ -247,7 +246,7 @@ public class ImapIdleChannelAdapter extends MessageProducerSupport implements Be
|
||||
@Override
|
||||
public void run() {
|
||||
org.springframework.messaging.Message<?> message =
|
||||
MessageBuilder.withPayload(mailMessage).build();
|
||||
ImapIdleChannelAdapter.this.getMessageBuilderFactory().withPayload(mailMessage).build();
|
||||
|
||||
if (TransactionSynchronizationManager.isActualTransactionActive()) {
|
||||
if (transactionSynchronizationFactory != null){
|
||||
|
||||
@@ -22,10 +22,15 @@ import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.integration.context.IntegrationContextUtils;
|
||||
import org.springframework.integration.core.MessageSource;
|
||||
import org.springframework.integration.support.DefaultMessageBuilderFactory;
|
||||
import org.springframework.integration.support.MessageBuilderFactory;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.integration.core.MessageSource;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
@@ -38,7 +43,8 @@ import org.springframework.util.Assert;
|
||||
* @author Gary Russell
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
public class MailReceivingMessageSource implements MessageSource<javax.mail.Message> {
|
||||
public class MailReceivingMessageSource implements MessageSource<javax.mail.Message>,
|
||||
BeanFactoryAware {
|
||||
|
||||
private final Log logger = LogFactory.getLog(this.getClass());
|
||||
|
||||
@@ -46,12 +52,31 @@ public class MailReceivingMessageSource implements MessageSource<javax.mail.Mess
|
||||
|
||||
private final Queue<javax.mail.Message> mailQueue = new ConcurrentLinkedQueue<javax.mail.Message>();
|
||||
|
||||
private volatile BeanFactory beanFactory;
|
||||
|
||||
private volatile MessageBuilderFactory messageBuilderFactory = new DefaultMessageBuilderFactory();
|
||||
|
||||
|
||||
public MailReceivingMessageSource(MailReceiver mailReceiver) {
|
||||
Assert.notNull(mailReceiver, "mailReceiver must not be null");
|
||||
this.mailReceiver = mailReceiver;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void setBeanFactory(BeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
this.messageBuilderFactory = IntegrationContextUtils.getMessageBuilderFactory(this.beanFactory);
|
||||
}
|
||||
|
||||
protected BeanFactory getBeanFactory() {
|
||||
return beanFactory;
|
||||
}
|
||||
|
||||
protected MessageBuilderFactory getMessageBuilderFactory() {
|
||||
return messageBuilderFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Message<javax.mail.Message> receive() {
|
||||
try {
|
||||
javax.mail.Message mailMessage = this.mailQueue.poll();
|
||||
@@ -66,7 +91,7 @@ public class MailReceivingMessageSource implements MessageSource<javax.mail.Mess
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("received mail message [" + mailMessage + "]");
|
||||
}
|
||||
return MessageBuilder.withPayload(mailMessage).build();
|
||||
return this.messageBuilderFactory.withPayload(mailMessage).build();
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -25,25 +25,47 @@ import javax.mail.Message.RecipientType;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.integration.context.IntegrationContextUtils;
|
||||
import org.springframework.integration.mail.MailHeaders;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.integration.support.AbstractIntegrationMessageBuilder;
|
||||
import org.springframework.integration.support.DefaultMessageBuilderFactory;
|
||||
import org.springframework.integration.support.MessageBuilderFactory;
|
||||
import org.springframework.integration.transformer.MessageTransformationException;
|
||||
import org.springframework.integration.transformer.Transformer;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Base class for Transformers that convert from a JavaMail Message to a
|
||||
* Spring Integration Message.
|
||||
*
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @author Gary Russell
|
||||
*/
|
||||
public abstract class AbstractMailMessageTransformer<T> implements Transformer {
|
||||
public abstract class AbstractMailMessageTransformer<T> implements Transformer,
|
||||
BeanFactoryAware {
|
||||
|
||||
protected final Log logger = LogFactory.getLog(this.getClass());
|
||||
|
||||
private volatile BeanFactory beanFactory;
|
||||
|
||||
private volatile MessageBuilderFactory messageBuilderFactory = new DefaultMessageBuilderFactory();
|
||||
|
||||
|
||||
@Override
|
||||
public final void setBeanFactory(BeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
this.messageBuilderFactory = IntegrationContextUtils.getMessageBuilderFactory(this.beanFactory);
|
||||
}
|
||||
|
||||
protected MessageBuilderFactory getMessageBuilderFactory() {
|
||||
return messageBuilderFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Message<?> transform(Message<?> message) {
|
||||
Object payload = message.getPayload();
|
||||
if (!(payload instanceof javax.mail.Message)) {
|
||||
@@ -51,7 +73,7 @@ public abstract class AbstractMailMessageTransformer<T> implements Transformer {
|
||||
+ " requires a javax.mail.Message payload");
|
||||
}
|
||||
javax.mail.Message mailMessage = (javax.mail.Message) payload;
|
||||
MessageBuilder<T> builder = null;
|
||||
AbstractIntegrationMessageBuilder<T> builder = null;
|
||||
try {
|
||||
builder = this.doTransform(mailMessage);
|
||||
}
|
||||
@@ -65,7 +87,7 @@ public abstract class AbstractMailMessageTransformer<T> implements Transformer {
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
protected abstract MessageBuilder<T> doTransform(javax.mail.Message mailMessage) throws Exception;
|
||||
protected abstract AbstractIntegrationMessageBuilder<T> doTransform(javax.mail.Message mailMessage) throws Exception;
|
||||
|
||||
|
||||
private Map<String, Object> extractHeaderMapFromMailMessage(javax.mail.Message mailMessage) {
|
||||
|
||||
@@ -21,7 +21,7 @@ import java.nio.charset.Charset;
|
||||
|
||||
import javax.mail.Multipart;
|
||||
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.integration.support.AbstractIntegrationMessageBuilder;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
@@ -31,6 +31,7 @@ import org.springframework.util.Assert;
|
||||
* an output stream of bytes using the provided charset (or UTF-8 by default).
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @author Gary Russell
|
||||
*/
|
||||
public class MailToStringTransformer extends AbstractMailMessageTransformer<String> {
|
||||
|
||||
@@ -50,15 +51,15 @@ public class MailToStringTransformer extends AbstractMailMessageTransformer<Stri
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MessageBuilder<String> doTransform(javax.mail.Message mailMessage) throws Exception {
|
||||
protected AbstractIntegrationMessageBuilder<String> doTransform(javax.mail.Message mailMessage) throws Exception {
|
||||
Object content = mailMessage.getContent();
|
||||
if (content instanceof String) {
|
||||
return MessageBuilder.withPayload((String) content);
|
||||
return this.getMessageBuilderFactory().withPayload((String) content);
|
||||
}
|
||||
if (content instanceof Multipart) {
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
((Multipart) content).writeTo(outputStream);
|
||||
return MessageBuilder.withPayload(
|
||||
return this.getMessageBuilderFactory().withPayload(
|
||||
new String(outputStream.toByteArray(), this.charset));
|
||||
}
|
||||
throw new IllegalArgumentException("failed to transform contentType ["
|
||||
|
||||
Reference in New Issue
Block a user