Allow XML components injection

**Cherry-pick to 5.0.x & 4.3.x**

* Polishing after rebase
* Copyright to 2019

* Rebase and update according upstream deps
This commit is contained in:
Artem Bilan
2018-12-10 18:43:15 -05:00
parent 0d1312874c
commit 59c69ed40d
17 changed files with 347 additions and 176 deletions

View File

@@ -53,8 +53,14 @@
<header name="foo" xpath-expression="/person/@name" overwrite="true" />
</xpath-header-enricher>
<xpath-header-enricher id="customHeaderEnricher" input-channel="customInput" default-overwrite="true" should-skip-nulls="false">
<xpath-header-enricher id="customHeaderEnricher"
input-channel="customInput"
default-overwrite="true"
should-skip-nulls="false"
converter="xmlPayloadConverter">
<header name="foo" xpath-expression="/person/@name" overwrite="false" />
</xpath-header-enricher>
<beans:bean id="xmlPayloadConverter" class="org.springframework.integration.xml.DefaultXmlPayloadConverter"/>
</beans:beans>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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,12 +17,16 @@
package org.springframework.integration.xml.config;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -37,12 +41,13 @@ import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.support.SmartLifecycleRoleController;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.integration.transformer.support.HeaderValueMessageProcessor;
import org.springframework.integration.xml.transformer.support.XPathExpressionEvaluatingHeaderValueMessageProcessor;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.PollableChannel;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.MultiValueMap;
/**
@@ -52,8 +57,7 @@ import org.springframework.util.MultiValueMap;
*
* @since 2.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@RunWith(SpringRunner.class)
@DirtiesContext
public class XPathHeaderEnricherParserTests {
@@ -66,11 +70,12 @@ public class XPathHeaderEnricherParserTests {
@Autowired
private ApplicationContext context;
private final Message<?> message = MessageBuilder.withPayload("<person name='John Doe' age='42' married='true'/>").build();
private final Message<?> message =
MessageBuilder.withPayload("<person name='John Doe' age='42' married='true'/>").build();
@Test
public void testParse() throws Exception {
public void testParse() {
EventDrivenConsumer consumer = (EventDrivenConsumer) context.getBean("parseOnly");
assertEquals(2, TestUtils.getPropertyValue(consumer, "handler.order"));
assertEquals(123L, TestUtils.getPropertyValue(consumer, "handler.messagingTemplate.sendTimeout"));
@@ -115,35 +120,36 @@ public class XPathHeaderEnricherParserTests {
public void nodeListResult() {
Message<?> result = this.getResultMessage();
Object header = result.getHeaders().get("node-list-test");
assertTrue(List.class.isAssignableFrom(header.getClass()));
assertThat(header, instanceOf(List.class));
List<Node> nodeList = (List<Node>) header;
assertNotNull(nodeList);
assertEquals(3, nodeList.size());
}
@Test
public void expressionRef() {
Message<?> result = this.getResultMessage();
assertEquals(new Double(84), result.getHeaders().get("ref-test"));
Message<?> result = getResultMessage();
assertEquals(84d, result.getHeaders().get("ref-test"));
}
@Test
public void defaultOverwrite() {
assertEquals(false, this.getEnricherProperty("defaultHeaderEnricher", "defaultOverwrite"));
public void testDefaultHeaderEnricher() {
assertFalse(getEnricherProperty("defaultHeaderEnricher", "defaultOverwrite"));
assertTrue(getEnricherProperty("defaultHeaderEnricher", "shouldSkipNulls"));
}
@Test
public void defaultShouldSkipNulls() {
assertEquals(true, this.getEnricherProperty("defaultHeaderEnricher", "shouldSkipNulls"));
}
@Test
public void customOverwrite() {
assertEquals(true, this.getEnricherProperty("customHeaderEnricher", "defaultOverwrite"));
}
@Test
public void customShouldSkipNulls() {
assertEquals(false, this.getEnricherProperty("customHeaderEnricher", "shouldSkipNulls"));
@SuppressWarnings("unchecked")
public void testCustomHeaderEnricher() {
assertTrue(getEnricherProperty("customHeaderEnricher", "defaultOverwrite"));
assertFalse(getEnricherProperty("customHeaderEnricher", "shouldSkipNulls"));
Map<String, ? extends HeaderValueMessageProcessor<?>> headersToAdd =
TestUtils.getPropertyValue(this.context.getBean("customHeaderEnricher"),
"handler.transformer.headersToAdd", Map.class);
HeaderValueMessageProcessor<?> headerValueMessageProcessor = headersToAdd.get("foo");
assertThat(headerValueMessageProcessor, instanceOf(XPathExpressionEvaluatingHeaderValueMessageProcessor.class));
assertSame(this.context.getBean("xmlPayloadConverter"),
TestUtils.getPropertyValue(headerValueMessageProcessor, "converter"));
}
@Test
@@ -155,6 +161,7 @@ public class XPathHeaderEnricherParserTests {
.build();
this.context.getBean("defaultInput", MessageChannel.class).send(request);
Message<?> reply = replyChannel.receive();
assertNotNull(reply);
assertEquals("John Doe", reply.getHeaders().get("foo"));
}
@@ -167,6 +174,7 @@ public class XPathHeaderEnricherParserTests {
.build();
this.context.getBean("customInput", MessageChannel.class).send(request);
Message<?> reply = replyChannel.receive();
assertNotNull(reply);
assertEquals("bar", reply.getHeaders().get("foo"));
}
@@ -180,7 +188,7 @@ public class XPathHeaderEnricherParserTests {
Object endpoint = this.context.getBean(beanName);
Object handler = new DirectFieldAccessor(endpoint).getPropertyValue("handler");
Object enricher = new DirectFieldAccessor(handler).getPropertyValue("transformer");
return ((Boolean) new DirectFieldAccessor(enricher).getPropertyValue(propertyName)).booleanValue();
return (boolean) new DirectFieldAccessor(enricher).getPropertyValue(propertyName);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@@ -19,21 +19,26 @@ package org.springframework.integration.xml.transformer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.nio.charset.StandardCharsets;
import java.io.File;
import java.io.IOException;
import javax.xml.transform.Result;
import javax.xml.transform.Templates;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMResult;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.Mockito;
import org.w3c.dom.Document;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.integration.xml.result.DomResultFactory;
import org.springframework.integration.xml.result.StringResultFactory;
@@ -41,6 +46,7 @@ import org.springframework.integration.xml.util.XmlTestUtil;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.util.FileCopyUtils;
import org.springframework.xml.transform.StringResult;
import org.springframework.xml.transform.StringSource;
@@ -61,9 +67,12 @@ public class XsltPayloadTransformerTests {
private final String outputAsString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><bob>test</bob>";
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Before
public void setUp() {
this.transformer = new XsltPayloadTransformer(getXslResource());
public void setUp() throws Exception {
this.transformer = new XsltPayloadTransformer(getXslTemplates());
this.transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
this.transformer.setAlwaysUseResultFactory(false);
this.transformer.afterPropertiesSet();
@@ -127,8 +136,8 @@ public class XsltPayloadTransformerTests {
@Test
public void testSourceWithResultTransformer() throws Exception {
Integer returnValue = 13;
XsltPayloadTransformer transformer = new XsltPayloadTransformer(getXslResource(),
new StubResultTransformer(returnValue));
XsltPayloadTransformer transformer =
new XsltPayloadTransformer(getXslTemplates(), new StubResultTransformer(returnValue));
transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
transformer.afterPropertiesSet();
Object transformed = transformer
@@ -137,10 +146,10 @@ public class XsltPayloadTransformerTests {
}
@Test
public void testXsltPayloadWithTransformerFactoryClassname() throws Exception {
public void testXsltPayloadWithTransformerFactoryClassName() throws Exception {
Integer returnValue = 13;
XsltPayloadTransformer transformer =
new XsltPayloadTransformer(getXslResource(), new StubResultTransformer(returnValue),
new XsltPayloadTransformer(getXslResourceThatOutputsText(), new StubResultTransformer(returnValue),
"com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
transformer.afterPropertiesSet();
@@ -151,8 +160,8 @@ public class XsltPayloadTransformerTests {
}
@Test
public void testXsltPayloadWithBadTransformerFactoryClassname() {
XsltPayloadTransformer transformer = new XsltPayloadTransformer(getXslResource(), "foo.bar.Baz");
public void testXsltPayloadWithBadTransformerFactoryClassName() throws IOException {
XsltPayloadTransformer transformer = new XsltPayloadTransformer(getXslResourceThatOutputsText(), "foo.bar.Baz");
transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
assertThatThrownBy(transformer::afterPropertiesSet)
.isExactlyInstanceOf(TransformerFactoryConfigurationError.class);
@@ -227,23 +236,29 @@ public class XsltPayloadTransformerTests {
assertThat(transformed.toString()).isEqualTo("hello world");
}
private Resource getXslResource() {
private Templates getXslTemplates() throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
String xsl = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
"<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" +
" <xsl:template match=\"order\">" +
" <bob>test</bob>" +
" </xsl:template>" +
"</xsl:stylesheet>";
return new ByteArrayResource(xsl.getBytes(StandardCharsets.UTF_8));
return transformerFactory.newTemplates(new StringSource(xsl));
}
private Resource getXslResourceThatOutputsText() {
private Resource getXslResourceThatOutputsText() throws IOException {
String xsl = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
"<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" +
" <xsl:output method=\"text\" encoding=\"UTF-8\" />" +
" <xsl:template match=\"order\">hello world</xsl:template>" +
"</xsl:stylesheet>";
return new ByteArrayResource(xsl.getBytes(StandardCharsets.UTF_8));
File xsltFile = this.temporaryFolder.newFile();
FileCopyUtils.copy(xsl.getBytes(), xsltFile);
return new FileSystemResource(xsltFile);
}
public static class StubResultTransformer implements ResultTransformer {