Message payload can no longer be set. MessageTransformer's transform() method now returns a Message (instead of void). ChannelInterceptor preSend() and postReceive() methods now return a Message instead of boolean.

This commit is contained in:
Mark Fisher
2008-07-15 22:43:47 +00:00
parent 0056edd10d
commit d344504e38
37 changed files with 303 additions and 273 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* 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.
@@ -16,18 +16,17 @@
package org.springframework.integration.xml.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.xml.transformer.XmlPayloadUnmarshallingTransfomer;
import org.springframework.integration.xml.transformer.XmlPayloadUnmarshallingTransformer;
import org.springframework.util.Assert;
import org.w3c.dom.Element;
/**
*
* @author Jonas Partner
*
*/
public class XmlUnmarshallingTransformerParser extends AbstractSingleBeanDefinitionParser {
@@ -45,7 +44,7 @@ public class XmlUnmarshallingTransformerParser extends AbstractSingleBeanDefinit
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
String unmarshaller = element.getAttribute("unmarshaller");
Assert.hasText(unmarshaller, "A unmarshaller attribute is required");
builder.getBeanDefinition().setBeanClass(XmlPayloadUnmarshallingTransfomer.class);
builder.getBeanDefinition().setBeanClass(XmlPayloadUnmarshallingTransformer.class);
builder.getBeanDefinition().getConstructorArgumentValues().addGenericArgumentValue(
new RuntimeBeanReference(unmarshaller));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* 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.
@@ -24,23 +24,26 @@ import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.dom.DOMResult;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.transformer.MessageTransformer;
import org.springframework.xml.transform.StringResult;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
/**
* Creates a {@link Document} from a {@link Result} payload
* @author Jonas Partner
* Creates a {@link Document} from a {@link Result} payload.
*
* @author Jonas Partner
*/
public class ResultToDocumentTransformer implements MessageTransformer {
// Not guaranteed to be thread safe
private final DocumentBuilderFactory documentBuilderFactory;
public ResultToDocumentTransformer(DocumentBuilderFactory documentBuilderFactory) {
this.documentBuilderFactory = documentBuilderFactory;
}
@@ -49,23 +52,21 @@ public class ResultToDocumentTransformer implements MessageTransformer {
this(DocumentBuilderFactory.newInstance());
}
@SuppressWarnings("unchecked")
public void transform(Message message) {
Document doc;
public Message<?> transform(Message message) {
Document doc = null;
if (DOMResult.class.isAssignableFrom(message.getPayload().getClass())) {
doc = createDocumentFromDomResult(message, (DOMResult) message.getPayload());
}
else if (StringResult.class.isAssignableFrom(message.getPayload().getClass())) {
doc = createDocumentFromStringResult(message, (StringResult) message.getPayload());
}
else {
throw new MessagingException(message, "Could not create document from payload type "
+ message.getPayload().getClass().getName());
throw new MessagingException(message, "Failed to create document from payload type ["
+ message.getPayload().getClass().getName() + "]");
}
message.setPayload(doc);
return new GenericMessage<Document>(doc, message.getHeader());
}
@SuppressWarnings("unchecked")
@@ -87,8 +88,8 @@ public class ResultToDocumentTransformer implements MessageTransformer {
try {
return documentBuilderFactory.newDocumentBuilder();
}
catch (ParserConfigurationException parseE) {
throw new MessagingException("Failed to create a new DocumentBuilder", parseE);
catch (ParserConfigurationException e) {
throw new MessagingException("Failed to create a new DocumentBuilder", e);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* 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.
@@ -18,33 +18,34 @@ package org.springframework.integration.xml.transformer;
import javax.xml.transform.Source;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.transformer.MessageTransformer;
import org.springframework.integration.xml.source.DomSourceFactory;
import org.springframework.integration.xml.source.SourceFactory;
/**
* Transforms the payload to a {@link Source} using a {@link SourceFactory}
* Default to using a {@link DomSourceFactory} if alternative is not provided
* Transforms the payload to a {@link Source} using a {@link SourceFactory}.
* Defaults to using a {@link DomSourceFactory} if alternative is not provided.
*
* @author Jonas Partner
*
*/
public class SourceCreatingTransformer implements MessageTransformer {
private final SourceFactory sourceFactory;
public SourceCreatingTransformer() {
sourceFactory = new DomSourceFactory();
this.sourceFactory = new DomSourceFactory();
}
public SourceCreatingTransformer(SourceFactory sourceFactory) {
this.sourceFactory = sourceFactory;
}
@SuppressWarnings("unchecked")
public void transform(Message message) {
message.setPayload(sourceFactory.getSourceForMessage(message));
public Message<?> transform(Message<?> message) {
Source source = this.sourceFactory.getSourceForMessage(message);
return new GenericMessage<Source>(source, message.getHeader());
}
}

View File

@@ -20,17 +20,19 @@ import java.io.IOException;
import javax.xml.transform.Result;
import org.springframework.oxm.Marshaller;
import org.springframework.util.Assert;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageHandlingException;
import org.springframework.integration.transformer.MessageTransformer;
import org.springframework.integration.xml.result.DomResultFactory;
import org.springframework.integration.xml.result.ResultFactory;
import org.springframework.oxm.Marshaller;
import org.springframework.util.Assert;
/**
* An implementation of {@link MessageTransformer} that delegates to an OXM
* {@link Marshaller}
* {@link Marshaller}.
*
* @author Mark Fisher
* @author Jonas Partner
@@ -41,18 +43,20 @@ public class XmlPayloadMarshallingTransformer implements MessageTransformer {
private ResultFactory resultFactory = new DomResultFactory();
public XmlPayloadMarshallingTransformer(Marshaller marshaller) {
Assert.notNull(marshaller, " a marshaller is required");
Assert.notNull(marshaller, "a marshaller is required");
this.marshaller = marshaller;
}
public void setResultFactory(ResultFactory resultFactory) {
Assert.notNull(resultFactory, " the ResultFactory can not be null");
Assert.notNull(resultFactory, "ResultFactory must not be null");
this.resultFactory = resultFactory;
}
@SuppressWarnings("unchecked")
public void transform(Message message) {
public Message<?> transform(Message<?> message) {
Object transformedPayload = null;
Object originalPayload = message.getPayload();
Result result = this.resultFactory.getNewResult(message);
@@ -70,7 +74,7 @@ public class XmlPayloadMarshallingTransformer implements MessageTransformer {
if (transformedPayload == null) {
throw new MessageHandlingException(message, "failed to transform payload");
}
message.setPayload(transformedPayload);
return new GenericMessage(transformedPayload, message.getHeader());
}
}

View File

@@ -20,6 +20,7 @@ import java.io.IOException;
import javax.xml.transform.Source;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.transformer.MessageTransformer;
@@ -29,34 +30,36 @@ import org.springframework.oxm.Unmarshaller;
/**
* An implementation of {@link MessageTransformer} that delegates to an OXM
* {@link Unmarshaller} Expects the payload to be of type {@link Source} or to
* {@link Unmarshaller}. Expects the payload to be of type {@link Source} or to
* have an instance of {@link SourceFactory} that can convert to a
* {@link Source}
* {@link Source}.
*
* @author Jonas Partner
*/
public class XmlPayloadUnmarshallingTransfomer implements MessageTransformer {
public class XmlPayloadUnmarshallingTransformer implements MessageTransformer {
private final Unmarshaller unmarshaller;
private SourceFactory sourceFactory = new DomSourceFactory();
public XmlPayloadUnmarshallingTransfomer(Unmarshaller unmarshaller) {
public XmlPayloadUnmarshallingTransformer(Unmarshaller unmarshaller) {
this.unmarshaller = unmarshaller;
}
public void setSourceFactory(SourceFactory sourceFactory) {
this.sourceFactory = sourceFactory;
}
@SuppressWarnings("unchecked")
public void transform(Message message) {
public Message<?> transform(Message<?> message) {
Source source = null;
if (Source.class.isAssignableFrom(message.getPayload().getClass())) {
source = (Source) message.getPayload();
}
else if (sourceFactory != null) {
source = sourceFactory.getSourceForMessage(message);
else if (this.sourceFactory != null) {
source = this.sourceFactory.getSourceForMessage(message);
}
if (source == null) {
@@ -64,14 +67,13 @@ public class XmlPayloadUnmarshallingTransfomer implements MessageTransformer {
"Could not transform message, payload not assignable from javax.xml.transform.Source and no conversion possible");
}
Object unmarshalled;
try {
unmarshalled = unmarshaller.unmarshal(source);
Object unmarshalled = this.unmarshaller.unmarshal(source);
return new GenericMessage(unmarshalled, message.getHeader());
}
catch (IOException ioE) {
throw new MessagingException(message, "Exception unamrshalling payload", ioE);
catch (IOException e) {
throw new MessagingException(message, "Failed to unamrshal payload", e);
}
message.setPayload(unmarshalled);
}
}

View File

@@ -23,7 +23,10 @@ import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
import org.springframework.core.io.Resource;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.transformer.MessageTransformer;
@@ -31,7 +34,6 @@ import org.springframework.integration.xml.result.DomResultFactory;
import org.springframework.integration.xml.result.ResultFactory;
import org.springframework.integration.xml.source.DomSourceFactory;
import org.springframework.integration.xml.source.SourceFactory;
import org.w3c.dom.Document;
/**
* Simple XSLT transformer implementation which returns a transformed
@@ -47,6 +49,7 @@ public class XsltPayloadTransformer implements MessageTransformer {
private ResultFactory resultFactory = new DomResultFactory();
public XsltPayloadTransformer(Templates templates) {
this.templates = templates;
}
@@ -55,16 +58,15 @@ public class XsltPayloadTransformer implements MessageTransformer {
this.templates = TransformerFactory.newInstance().newTemplates(new StreamSource(xslResource.getInputStream()));
}
@SuppressWarnings("unchecked")
public void transform(Message message) {
public Message<Result> transform(Message message) {
try {
if (Source.class.isAssignableFrom(message.getPayload().getClass())) {
transformSource(message, (Source) message.getPayload());
}
else {
Source source = sourceFactory.getSourceForMessage(message);
transformSource(message, source);
return this.transformSource(message, (Source) message.getPayload());
}
Source source = this.sourceFactory.getSourceForMessage(message);
return this.transformSource(message, source);
}
catch (TransformerException e) {
throw new MessagingException(message, "XSLT transformation failed", e);
@@ -72,10 +74,10 @@ public class XsltPayloadTransformer implements MessageTransformer {
}
@SuppressWarnings("unchecked")
protected void transformSource(Message message, Source source) throws TransformerException {
protected Message<Result> transformSource(Message message, Source source) throws TransformerException {
Result result = resultFactory.getNewResult(message);
this.templates.newTransformer().transform(source, result);
message.setPayload(result);
return new GenericMessage<Result>(result, message.getHeader());
}
public void setSourceFactory(SourceFactory sourceFactory) {