Skip to content

Commit 8dcca5b

Browse files
akryvtsunartembilan
authored andcommitted
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
1 parent 0efd494 commit 8dcca5b

File tree

3 files changed

+163
-5
lines changed

3 files changed

+163
-5
lines changed

spring-integration-zip/src/main/java/org/springframework/integration/zip/splitter/UnZipResultSplitter.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2016 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.
@@ -21,29 +21,36 @@
2121
import java.util.Map;
2222

2323
import org.apache.commons.io.FilenameUtils;
24+
2425
import org.springframework.integration.file.FileHeaders;
2526
import org.springframework.integration.support.MessageBuilder;
2627
import org.springframework.integration.zip.ZipHeaders;
2728
import org.springframework.messaging.Message;
29+
import org.springframework.messaging.MessageHeaders;
2830

2931
/**
3032
*
3133
* @author Gunnar Hillert
32-
* @since 1.0
34+
* @author Andriy Kryvtsun
3335
*
36+
* @since 1.0
3437
*/
3538
public class UnZipResultSplitter {
3639

37-
public List<Message<Object>> splitUnzippedMap(Map<String, Object> unzippedEntries) {
40+
public List<Message<Object>> splitUnzippedMap(Message<Map<String, Object>> message) {
41+
MessageHeaders headers = message.getHeaders();
42+
Map<String, Object> unzippedEntries = message.getPayload();
3843

39-
final List<Message<Object>> messages = new ArrayList<Message<Object>>(unzippedEntries.size());
44+
List<Message<Object>> messages = new ArrayList<Message<Object>>(unzippedEntries.size());
4045

4146
for (Map.Entry<String, Object> entry : unzippedEntries.entrySet()) {
4247
final String path = FilenameUtils.getPath(entry.getKey());
4348
final String filename = FilenameUtils.getName(entry.getKey());
4449
final Message<Object> splitMessage = MessageBuilder.withPayload(entry.getValue())
4550
.setHeader(FileHeaders.FILENAME, filename)
46-
.setHeader(ZipHeaders.ZIP_ENTRY_PATH, path).build();
51+
.setHeader(ZipHeaders.ZIP_ENTRY_PATH, path)
52+
.copyHeadersIfAbsent(headers)
53+
.build();
4754
messages.add(splitMessage);
4855
}
4956
return messages;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:int="http://www.springframework.org/schema/integration"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans
6+
http://www.springframework.org/schema/beans/spring-beans.xsd
7+
http://www.springframework.org/schema/integration
8+
http://www.springframework.org/schema/integration/spring-integration.xsd">
9+
10+
<int:channel id="input"/>
11+
12+
<int:splitter input-channel="input" output-channel="output">
13+
<bean class="org.springframework.integration.zip.splitter.UnZipResultSplitter"/>
14+
</int:splitter>
15+
16+
<int:channel id="output">
17+
<int:queue/>
18+
</int:channel>
19+
20+
</beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.integration.zip.splitter;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNotNull;
21+
22+
import java.util.LinkedHashMap;
23+
import java.util.Map;
24+
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.integration.channel.QueueChannel;
30+
import org.springframework.integration.file.FileHeaders;
31+
import org.springframework.integration.support.MessageBuilder;
32+
import org.springframework.integration.zip.ZipHeaders;
33+
import org.springframework.messaging.Message;
34+
import org.springframework.messaging.MessageChannel;
35+
import org.springframework.test.context.ContextConfiguration;
36+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
37+
38+
/**
39+
* @author Andriy Kryvtsun
40+
*/
41+
@RunWith(SpringJUnit4ClassRunner.class)
42+
@ContextConfiguration
43+
public class UnZipResultSplitterTests {
44+
45+
private static final String DIR_1 = "dir1/";
46+
47+
private static final String DIR_2 = "dir2/";
48+
49+
private static final String FILE_1 = "file1";
50+
51+
private static final String FILE_2 = "file2";
52+
53+
private static final String DATA_1 = "data1";
54+
55+
private static final String DATA_2 = "data2";
56+
57+
private static int TIMEOUT = 10000;
58+
59+
@Autowired
60+
private MessageChannel input;
61+
62+
@Autowired
63+
private QueueChannel output;
64+
65+
@Test
66+
public void splitPreservingSourceMessageHeaderValues() {
67+
68+
final String headerName = "headerName";
69+
final String headerValue = "headerValue";
70+
71+
Message<?> inMessage = MessageBuilder.withPayload(createPayload())
72+
.setHeader(headerName, headerValue)
73+
.build();
74+
75+
input.send(inMessage);
76+
77+
Message<?> message1 = output.receive(TIMEOUT);
78+
checkMessageWithHeaderValue(message1, headerName, headerValue, DATA_1);
79+
80+
Message<?> message2 = output.receive(TIMEOUT);
81+
checkMessageWithHeaderValue(message2, headerName, headerValue, DATA_2);
82+
}
83+
84+
private static void checkMessageWithHeaderValue(Message<?> message, String headerName, String headerValue,
85+
String payload) {
86+
assertNotNull(message);
87+
checkHeaderValue(message, headerName, headerValue);
88+
checkPayload(message, payload);
89+
}
90+
91+
@Test
92+
public void splitPreservingServiceHeaderValues() {
93+
94+
Message<?> inMessage = MessageBuilder.withPayload(createPayload())
95+
.setHeader(ZipHeaders.ZIP_ENTRY_PATH, "dir")
96+
.setHeader(FileHeaders.FILENAME, "filename")
97+
.build();
98+
99+
input.send(inMessage);
100+
101+
Message<?> message1 = output.receive(TIMEOUT);
102+
checkMessageWithServiceHeaderValues(message1, DIR_1, FILE_1, DATA_1);
103+
104+
Message<?> message2 = output.receive(TIMEOUT);
105+
checkMessageWithServiceHeaderValues(message2, DIR_2, FILE_2, DATA_2);
106+
}
107+
108+
private static void checkMessageWithServiceHeaderValues(Message<?> message, String path, String filename,
109+
String payload) {
110+
assertNotNull(message);
111+
checkHeaderValue(message, ZipHeaders.ZIP_ENTRY_PATH, path);
112+
checkHeaderValue(message, FileHeaders.FILENAME, filename);
113+
checkPayload(message, payload);
114+
}
115+
116+
private static Map<String, Object> createPayload() {
117+
Map<String, Object> payload = new LinkedHashMap<String, Object>();
118+
payload.put(DIR_1 + FILE_1, DATA_1);
119+
payload.put(DIR_2 + FILE_2, DATA_2);
120+
return payload;
121+
}
122+
123+
private static void checkPayload(Message<?> message, String payload) {
124+
assertEquals(payload, message.getPayload());
125+
}
126+
127+
private static void checkHeaderValue(Message<?> message, String headerName, String headerValue) {
128+
assertEquals(headerValue, message.getHeaders().get(headerName));
129+
}
130+
131+
}

0 commit comments

Comments
 (0)