INTEXT-219 Remain request headers after splitting

JIRA: https://jira.spring.io/browse/INTEXT-219

INTEXT-219: add unit test

INTEXT-219: fix missed headers

INTEXT-219: update file headers

INTEXT-219: simplify context

INTEXT-219: add test for preserving service header values

INTEXT-219: rename auxiliary methods

Code style polishing
This commit is contained in:
englishman
2016-04-04 17:21:16 -04:00
committed by Artem Bilan
parent 0efd494337
commit 8dcca5b676
3 changed files with 163 additions and 5 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2016 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,29 +21,36 @@ import java.util.List;
import java.util.Map;
import org.apache.commons.io.FilenameUtils;
import org.springframework.integration.file.FileHeaders;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.zip.ZipHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
/**
*
* @author Gunnar Hillert
* @since 1.0
* @author Andriy Kryvtsun
*
* @since 1.0
*/
public class UnZipResultSplitter {
public List<Message<Object>> splitUnzippedMap(Map<String, Object> unzippedEntries) {
public List<Message<Object>> splitUnzippedMap(Message<Map<String, Object>> message) {
MessageHeaders headers = message.getHeaders();
Map<String, Object> unzippedEntries = message.getPayload();
final List<Message<Object>> messages = new ArrayList<Message<Object>>(unzippedEntries.size());
List<Message<Object>> messages = new ArrayList<Message<Object>>(unzippedEntries.size());
for (Map.Entry<String, Object> entry : unzippedEntries.entrySet()) {
final String path = FilenameUtils.getPath(entry.getKey());
final String filename = FilenameUtils.getName(entry.getKey());
final Message<Object> splitMessage = MessageBuilder.withPayload(entry.getValue())
.setHeader(FileHeaders.FILENAME, filename)
.setHeader(ZipHeaders.ZIP_ENTRY_PATH, path).build();
.setHeader(ZipHeaders.ZIP_ENTRY_PATH, path)
.copyHeadersIfAbsent(headers)
.build();
messages.add(splitMessage);
}
return messages;

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd">
<int:channel id="input"/>
<int:splitter input-channel="input" output-channel="output">
<bean class="org.springframework.integration.zip.splitter.UnZipResultSplitter"/>
</int:splitter>
<int:channel id="output">
<int:queue/>
</int:channel>
</beans>

View File

@@ -0,0 +1,131 @@
/*
* Copyright 2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.zip.splitter;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.file.FileHeaders;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.zip.ZipHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Andriy Kryvtsun
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class UnZipResultSplitterTests {
private static final String DIR_1 = "dir1/";
private static final String DIR_2 = "dir2/";
private static final String FILE_1 = "file1";
private static final String FILE_2 = "file2";
private static final String DATA_1 = "data1";
private static final String DATA_2 = "data2";
private static int TIMEOUT = 10000;
@Autowired
private MessageChannel input;
@Autowired
private QueueChannel output;
@Test
public void splitPreservingSourceMessageHeaderValues() {
final String headerName = "headerName";
final String headerValue = "headerValue";
Message<?> inMessage = MessageBuilder.withPayload(createPayload())
.setHeader(headerName, headerValue)
.build();
input.send(inMessage);
Message<?> message1 = output.receive(TIMEOUT);
checkMessageWithHeaderValue(message1, headerName, headerValue, DATA_1);
Message<?> message2 = output.receive(TIMEOUT);
checkMessageWithHeaderValue(message2, headerName, headerValue, DATA_2);
}
private static void checkMessageWithHeaderValue(Message<?> message, String headerName, String headerValue,
String payload) {
assertNotNull(message);
checkHeaderValue(message, headerName, headerValue);
checkPayload(message, payload);
}
@Test
public void splitPreservingServiceHeaderValues() {
Message<?> inMessage = MessageBuilder.withPayload(createPayload())
.setHeader(ZipHeaders.ZIP_ENTRY_PATH, "dir")
.setHeader(FileHeaders.FILENAME, "filename")
.build();
input.send(inMessage);
Message<?> message1 = output.receive(TIMEOUT);
checkMessageWithServiceHeaderValues(message1, DIR_1, FILE_1, DATA_1);
Message<?> message2 = output.receive(TIMEOUT);
checkMessageWithServiceHeaderValues(message2, DIR_2, FILE_2, DATA_2);
}
private static void checkMessageWithServiceHeaderValues(Message<?> message, String path, String filename,
String payload) {
assertNotNull(message);
checkHeaderValue(message, ZipHeaders.ZIP_ENTRY_PATH, path);
checkHeaderValue(message, FileHeaders.FILENAME, filename);
checkPayload(message, payload);
}
private static Map<String, Object> createPayload() {
Map<String, Object> payload = new LinkedHashMap<String, Object>();
payload.put(DIR_1 + FILE_1, DATA_1);
payload.put(DIR_2 + FILE_2, DATA_2);
return payload;
}
private static void checkPayload(Message<?> message, String payload) {
assertEquals(payload, message.getPayload());
}
private static void checkHeaderValue(Message<?> message, String headerName, String headerValue) {
assertEquals(headerValue, message.getHeaders().get(headerName));
}
}