INT-909 Added 'extractPayload' property (might rename) to the XmlPayloadMarshallingTransformer. The default is TRUE, but setting it to FALSE enables marshalling of the entire Message rather than just the payload.

This commit is contained in:
Mark Fisher
2009-12-07 14:33:32 +00:00
parent 0b152a0a45
commit 53beb01ae7
4 changed files with 121 additions and 46 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2009 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.
@@ -21,21 +21,21 @@ import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessagingException;
import org.springframework.integration.transformer.AbstractPayloadTransformer;
import org.springframework.integration.transformer.AbstractTransformer;
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 PayloadTransformer} that delegates to an OXM
* {@link Marshaller}.
* An implementation of {@link AbstractTransformer} that delegates to an OXM {@link Marshaller}.
*
* @author Mark Fisher
* @author Jonas Partner
*/
public class XmlPayloadMarshallingTransformer extends AbstractPayloadTransformer<Object, Object> {
public class XmlPayloadMarshallingTransformer extends AbstractTransformer {
private final Marshaller marshaller;
@@ -43,12 +43,15 @@ public class XmlPayloadMarshallingTransformer extends AbstractPayloadTransformer
private final ResultTransformer resultTransformer;
private volatile boolean extractPayload = true;
public XmlPayloadMarshallingTransformer(Marshaller marshaller, ResultTransformer resultTransformer)
throws ParserConfigurationException {
Assert.notNull(marshaller, "a marshaller is required");
this.marshaller = marshaller;
this.resultTransformer = resultTransformer;
resultFactory = new DomResultFactory();
this.resultFactory = new DomResultFactory();
}
public XmlPayloadMarshallingTransformer(Marshaller marshaller) throws ParserConfigurationException {
@@ -61,25 +64,36 @@ public class XmlPayloadMarshallingTransformer extends AbstractPayloadTransformer
this.resultFactory = resultFactory;
}
/**
* Specify whether the source Message's payload should be extracted prior
* to marshalling. This value is set to "true" by default. To send the
* Message itself as input to the Marshaller instead, set this to "false".
*/
public void setExtractPayload(boolean extractPayload) {
this.extractPayload = extractPayload;
}
@Override
public Object transformPayload(Object payload) {
public Object doTransform(Message<?> message) {
Object source = (this.extractPayload) ? message.getPayload() : message;
Object transformedPayload = null;
Result result = this.resultFactory.createResult(payload);
Result result = this.resultFactory.createResult(source);
if (result == null) {
throw new MessagingException(
"Unable to marshal payload, ResultFactory returned null.");
}
try {
this.marshaller.marshal(payload, result);
this.marshaller.marshal(source, result);
transformedPayload = result;
} catch (IOException e) {
}
catch (IOException e) {
throw new MessagingException("Failed to marshal payload", e);
}
if (transformedPayload == null) {
throw new MessagingException("Failed to transform payload");
}
if (resultTransformer != null) {
transformedPayload = resultTransformer.transformResult(result);
if (this.resultTransformer != null) {
transformedPayload = this.resultTransformer.transformResult(result);
}
return transformedPayload;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2009 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.
@@ -17,6 +17,7 @@
package org.springframework.integration.xml.transformer;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import java.io.IOException;
import java.util.ArrayList;
@@ -26,6 +27,9 @@ import javax.xml.transform.Result;
import javax.xml.transform.dom.DOMResult;
import org.junit.Test;
import org.springframework.integration.core.Message;
import org.springframework.integration.message.StringMessage;
import org.springframework.integration.xml.result.StringResultFactory;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.XmlMappingException;
@@ -39,39 +43,59 @@ public class XmlPayloadMarshallingTransformerTests {
@Test
public void testStringToStringResult() throws Exception {
TestMarshaller marshaller = new TestMarshaller();
XmlPayloadMarshallingTransformer transformer = new XmlPayloadMarshallingTransformer(
marshaller);
XmlPayloadMarshallingTransformer transformer = new XmlPayloadMarshallingTransformer(marshaller);
transformer.setResultFactory(new StringResultFactory());
Object result = transformer.transformPayload("world");
assertEquals(StringResult.class, result.getClass());
assertEquals("hello world", result.toString());
Message<?> resultMessage = transformer.transform(new StringMessage("world"));
Object resultPayload = resultMessage.getPayload();
assertEquals(StringResult.class, resultPayload.getClass());
assertEquals("hello world", resultPayload.toString());
assertEquals("world", marshaller.payloads.get(0));
}
@Test
public void testDefaultResultFactory() throws Exception {
TestMarshaller marshaller = new TestMarshaller();
XmlPayloadMarshallingTransformer transformer = new XmlPayloadMarshallingTransformer(
marshaller);
Object result = transformer.transformPayload("world");
assertEquals(DOMResult.class, result.getClass());
XmlPayloadMarshallingTransformer transformer = new XmlPayloadMarshallingTransformer(marshaller);
Message<?> resultMessage = transformer.transform(new StringMessage("world"));
Object resultPayload = resultMessage.getPayload();
assertEquals(DOMResult.class, resultPayload.getClass());
assertEquals("world", marshaller.payloads.get(0));
}
@Test
public void testMarshallingEntireMessage() throws Exception {
TestMarshaller marshaller = new TestMarshaller();
XmlPayloadMarshallingTransformer transformer = new XmlPayloadMarshallingTransformer(marshaller);
transformer.setExtractPayload(false);
Message<?> message = new StringMessage("test");
transformer.transform(message);
assertEquals(0, marshaller.payloads.size());
assertEquals(1, marshaller.messages.size());
assertSame(message, marshaller.messages.get(0));
}
private static class TestMarshaller implements Marshaller {
private List<Object> payloads = new ArrayList<Object>();
private final List<Message<?>> messages = new ArrayList<Message<?>>();
private final List<Object> payloads = new ArrayList<Object>();
@SuppressWarnings("unchecked")
public boolean supports(Class clazz) {
return true;
}
public void marshal(Object originalPayload, Result result)
throws XmlMappingException, IOException {
payloads.add(originalPayload);
@SuppressWarnings("unchecked")
public void marshal(Object source, Result result) throws XmlMappingException, IOException {
if (source instanceof Message) {
this.messages.add((Message<?>) source);
}
else {
this.payloads.add(source);
}
if (result instanceof StringResult) {
((StringResult) result).getWriter().write("hello world");
((StringResult) result).getWriter().write("hello " + source);
}
}