Skip to content

Commit 549ef87

Browse files
garyrussellartembilan
authored andcommitted
INT-4469: XML Unmarshaller support byte[] payloads
JIRA: https://jira.spring.io/browse/INT-4469
1 parent 470d6d8 commit 549ef87

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

spring-integration-xml/src/main/java/org/springframework/integration/xml/transformer/UnmarshallingTransformer.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.integration.xml.transformer;
1818

19+
import java.io.ByteArrayInputStream;
1920
import java.io.File;
2021
import java.io.IOException;
2122

@@ -110,6 +111,9 @@ public Object transformPayload(Object payload) {
110111
else if (payload instanceof String) {
111112
source = new StringSource((String) payload);
112113
}
114+
else if (payload instanceof byte[]) {
115+
source = new StreamSource(new ByteArrayInputStream((byte[]) payload));
116+
}
113117
else if (payload instanceof File) {
114118
source = new StreamSource((File) payload);
115119
}
@@ -136,7 +140,7 @@ else if (payload instanceof Source) {
136140

137141
private static class MimeMessageUnmarshallerHelper {
138142

139-
private Unmarshaller delegate;
143+
private final Unmarshaller delegate;
140144

141145
MimeMessageUnmarshallerHelper(Unmarshaller unmarshaller) {
142146
this.delegate = unmarshaller;

spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/UnmarshallingTransformerTests.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import java.io.IOException;
2323

2424
import javax.xml.transform.Source;
25+
import javax.xml.transform.stream.StreamSource;
2526

2627
import org.junit.Test;
2728

@@ -35,9 +36,19 @@
3536
/**
3637
* @author Jonas Partner
3738
* @author Mark Fisher
39+
* @author Gary Russell
3840
*/
3941
public class UnmarshallingTransformerTests {
4042

43+
@Test
44+
public void testBytesToString() {
45+
Unmarshaller unmarshaller = new TestUnmarshaller(false);
46+
UnmarshallingTransformer transformer = new UnmarshallingTransformer(unmarshaller);
47+
Object transformed = transformer.transformPayload("world".getBytes());
48+
assertEquals(String.class, transformed.getClass());
49+
assertEquals("hello world", transformed.toString());
50+
}
51+
4152
@Test
4253
public void testStringSourceToString() {
4354
Unmarshaller unmarshaller = new TestUnmarshaller(false);
@@ -74,6 +85,7 @@ private static class TestUnmarshaller implements Unmarshaller {
7485
this.returnMessage = returnMessage;
7586
}
7687

88+
@Override
7789
public Object unmarshal(Source source) throws XmlMappingException, IOException {
7890
if (source instanceof StringSource) {
7991
char[] chars = new char[8];
@@ -83,9 +95,18 @@ public Object unmarshal(Source source) throws XmlMappingException, IOException {
8395
}
8496
return "hello " + new String(chars).trim();
8597
}
98+
else if (source instanceof StreamSource) {
99+
byte[] bytes = new byte[8];
100+
((StreamSource) source).getInputStream().read(bytes);
101+
if (returnMessage) {
102+
return new GenericMessage<String>("message: " + new String(bytes).trim());
103+
}
104+
return "hello " + new String(bytes).trim();
105+
}
86106
return null;
87107
}
88108

109+
@Override
89110
public boolean supports(Class<?> clazz) {
90111
return true;
91112
}

0 commit comments

Comments
 (0)