INT-4469: XML Unmarshaller support byte[] payloads

JIRA: https://jira.spring.io/browse/INT-4469
This commit is contained in:
Gary Russell
2018-05-16 14:05:15 -04:00
committed by Artem Bilan
parent 470d6d880e
commit 549ef87a8e
2 changed files with 28 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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,6 +16,7 @@
package org.springframework.integration.xml.transformer;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
@@ -110,6 +111,9 @@ public class UnmarshallingTransformer extends AbstractPayloadTransformer<Object,
else if (payload instanceof String) {
source = new StringSource((String) payload);
}
else if (payload instanceof byte[]) {
source = new StreamSource(new ByteArrayInputStream((byte[]) payload));
}
else if (payload instanceof File) {
source = new StreamSource((File) payload);
}
@@ -136,7 +140,7 @@ public class UnmarshallingTransformer extends AbstractPayloadTransformer<Object,
private static class MimeMessageUnmarshallerHelper {
private Unmarshaller delegate;
private final Unmarshaller delegate;
MimeMessageUnmarshallerHelper(Unmarshaller unmarshaller) {
this.delegate = unmarshaller;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -22,6 +22,7 @@ import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import org.junit.Test;
@@ -35,9 +36,19 @@ import org.springframework.xml.transform.StringSource;
/**
* @author Jonas Partner
* @author Mark Fisher
* @author Gary Russell
*/
public class UnmarshallingTransformerTests {
@Test
public void testBytesToString() {
Unmarshaller unmarshaller = new TestUnmarshaller(false);
UnmarshallingTransformer transformer = new UnmarshallingTransformer(unmarshaller);
Object transformed = transformer.transformPayload("world".getBytes());
assertEquals(String.class, transformed.getClass());
assertEquals("hello world", transformed.toString());
}
@Test
public void testStringSourceToString() {
Unmarshaller unmarshaller = new TestUnmarshaller(false);
@@ -74,6 +85,7 @@ public class UnmarshallingTransformerTests {
this.returnMessage = returnMessage;
}
@Override
public Object unmarshal(Source source) throws XmlMappingException, IOException {
if (source instanceof StringSource) {
char[] chars = new char[8];
@@ -83,9 +95,18 @@ public class UnmarshallingTransformerTests {
}
return "hello " + new String(chars).trim();
}
else if (source instanceof StreamSource) {
byte[] bytes = new byte[8];
((StreamSource) source).getInputStream().read(bytes);
if (returnMessage) {
return new GenericMessage<String>("message: " + new String(bytes).trim());
}
return "hello " + new String(bytes).trim();
}
return null;
}
@Override
public boolean supports(Class<?> clazz) {
return true;
}