Migrate Zip Extension as a core module
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
<?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"
|
||||
xmlns:int-zip="http://www.springframework.org/schema/integration/zip"
|
||||
xmlns:int-file="http://www.springframework.org/schema/integration/file"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
|
||||
http://www.springframework.org/schema/integration/file https://www.springframework.org/schema/integration/file/spring-integration-file.xsd
|
||||
http://www.springframework.org/schema/integration/zip https://www.springframework.org/schema/integration/zip/spring-integration-zip.xsd
|
||||
http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd">
|
||||
|
||||
<context:property-placeholder/>
|
||||
|
||||
<int:channel id="input"/>
|
||||
|
||||
<int:chain input-channel="input" output-channel="out">
|
||||
<int-zip:unzip-transformer result-type="BYTE_ARRAY"/>
|
||||
<int:splitter>
|
||||
<bean class="org.springframework.integration.zip.splitter.UnZipResultSplitter"/>
|
||||
</int:splitter>
|
||||
</int:chain>
|
||||
|
||||
<int:channel id="out">
|
||||
<int:interceptors>
|
||||
<int:wire-tap channel="logger"/>
|
||||
</int:interceptors>
|
||||
</int:channel>
|
||||
|
||||
<int:logging-channel-adapter id="logger" log-full-message="true" level="INFO"/>
|
||||
|
||||
<int-file:outbound-channel-adapter id="write-file" channel="out" directory-expression="'${workDir}/' + headers.zip_entryPath"/>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
* Copyright 2015-2023 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
|
||||
*
|
||||
* https://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;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.zeroturnaround.zip.ZipException;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.integration.transformer.MessageTransformationException;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
* @author Artem Bilan
|
||||
*
|
||||
* @since 6.1
|
||||
*/
|
||||
@SpringJUnitConfig
|
||||
@DirtiesContext
|
||||
public class UnZip2FileTests {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
@Autowired
|
||||
private MessageChannel input;
|
||||
|
||||
@TempDir
|
||||
public static File workDir;
|
||||
|
||||
@BeforeAll
|
||||
public static void setup() {
|
||||
System.setProperty("workDir", workDir.getAbsolutePath());
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void cleanUp() {
|
||||
cleanupDirectory(workDir);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unZipWithOneEntry() throws Exception {
|
||||
final Resource resource = this.context.getResource("classpath:testzipdata/single.zip");
|
||||
final InputStream is = resource.getInputStream();
|
||||
|
||||
byte[] zipdata = IOUtils.toByteArray(is);
|
||||
is.close();
|
||||
|
||||
final Message<byte[]> message = MessageBuilder.withPayload(zipdata).build();
|
||||
|
||||
input.send(message);
|
||||
|
||||
assertThat(workDir.list()).hasSize(1);
|
||||
|
||||
File fileInWorkDir = workDir.listFiles()[0];
|
||||
|
||||
assertThat(fileInWorkDir).isFile();
|
||||
assertThat(fileInWorkDir).hasName("single.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unZipWithMultipleEntries() throws Exception {
|
||||
final Resource resource = this.context.getResource("classpath:testzipdata/countries.zip");
|
||||
final InputStream is = resource.getInputStream();
|
||||
|
||||
byte[] zipdata = IOUtils.toByteArray(is);
|
||||
is.close();
|
||||
|
||||
final Message<byte[]> message = MessageBuilder.withPayload(zipdata).build();
|
||||
|
||||
input.send(message);
|
||||
|
||||
assertThat(workDir.list()).hasSize(4);
|
||||
|
||||
File[] files = workDir.listFiles();
|
||||
|
||||
boolean continents = false;
|
||||
boolean de = false;
|
||||
boolean fr = false;
|
||||
boolean pl = false;
|
||||
|
||||
for (File file : files) {
|
||||
if (file.getName().equals("continents")) {
|
||||
continents = true;
|
||||
assertThat(file).isDirectory();
|
||||
assertThat(file.list()).hasSize(2);
|
||||
}
|
||||
if (file.getName().equals("de.txt")) {
|
||||
de = true;
|
||||
assertThat(file).isFile();
|
||||
}
|
||||
if (file.getName().equals("fr.txt")) {
|
||||
fr = true;
|
||||
assertThat(file).isFile();
|
||||
}
|
||||
if (file.getName().equals("pl.txt")) {
|
||||
pl = true;
|
||||
assertThat(file).isFile();
|
||||
}
|
||||
}
|
||||
|
||||
assertThat(continents).isTrue();
|
||||
assertThat(de).isTrue();
|
||||
assertThat(fr).isTrue();
|
||||
assertThat(pl).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unZipTraversal() throws Exception {
|
||||
final Resource resource = this.context.getResource("classpath:testzipdata/zip-malicious-traversal.zip");
|
||||
final InputStream is = resource.getInputStream();
|
||||
byte[] zipdata = IOUtils.toByteArray(is);
|
||||
final Message<byte[]> message = MessageBuilder.withPayload(zipdata).build();
|
||||
assertThatExceptionOfType(MessageTransformationException.class)
|
||||
.isThrownBy(() -> input.send(message))
|
||||
.withRootCauseInstanceOf(ZipException.class)
|
||||
.withStackTraceContaining("is trying to leave the target output directory");
|
||||
}
|
||||
|
||||
private static void cleanupDirectory(File dir) {
|
||||
for (File file: dir.listFiles()) {
|
||||
if (file.isDirectory()) {
|
||||
cleanupDirectory(file);
|
||||
}
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?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"
|
||||
xmlns:int-zip="http://www.springframework.org/schema/integration/zip"
|
||||
xmlns:int-file="http://www.springframework.org/schema/integration/file"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
|
||||
http://www.springframework.org/schema/integration/file https://www.springframework.org/schema/integration/file/spring-integration-file.xsd
|
||||
http://www.springframework.org/schema/integration/zip https://www.springframework.org/schema/integration/zip/spring-integration-zip.xsd
|
||||
http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd">
|
||||
|
||||
<context:property-placeholder/>
|
||||
|
||||
<int:channel id="input"/>
|
||||
|
||||
<int-zip:zip-transformer input-channel="input" output-channel="write-file" result-type="BYTE_ARRAY">
|
||||
<int-zip:request-handler-advice-chain>
|
||||
<int:retry-advice/>
|
||||
</int-zip:request-handler-advice-chain>
|
||||
</int-zip:zip-transformer>
|
||||
|
||||
<int-file:outbound-channel-adapter id="write-file" directory="${workDir}" auto-create-directory="true"/>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,158 @@
|
||||
/*
|
||||
* Copyright 2015-2023 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
|
||||
*
|
||||
* https://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;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.integration.file.FileHeaders;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
* @author Artem Bilan
|
||||
*
|
||||
* @since 6.1
|
||||
*/
|
||||
@SpringJUnitConfig
|
||||
@DirtiesContext
|
||||
public class Zip2FileTests {
|
||||
|
||||
@Autowired
|
||||
private MessageChannel input;
|
||||
|
||||
@TempDir
|
||||
public static File workDir;
|
||||
|
||||
@BeforeAll
|
||||
public static void setup() {
|
||||
System.setProperty("workDir", workDir.getAbsolutePath());
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void cleanUp() {
|
||||
for (File file : workDir.listFiles()) {
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipStringWithDefaultFileName() {
|
||||
final Message<String> message = MessageBuilder.withPayload("Zip me up.").build();
|
||||
input.send(message);
|
||||
assertThat(workDir.list()).hasSize(1);
|
||||
|
||||
File fileInWorkDir = workDir.listFiles()[0];
|
||||
|
||||
assertThat(fileInWorkDir.isFile()).isTrue();
|
||||
assertThat(fileInWorkDir.getName()).contains(message.getHeaders().getId().toString());
|
||||
assertThat(fileInWorkDir.getName()).endsWith(".zip");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipStringWithExplicitFileName() {
|
||||
input.send(MessageBuilder.withPayload("Zip me up.")
|
||||
.setHeader(FileHeaders.FILENAME, "zipString.zip")
|
||||
.build());
|
||||
|
||||
assertThat(workDir.list()).hasSize(1);
|
||||
assertThat(workDir.list()[0]).isEqualTo("zipString.zip");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipBytesWithExplicitFileName() {
|
||||
input.send(MessageBuilder.withPayload("Zip me up.".getBytes())
|
||||
.setHeader(FileHeaders.FILENAME, "zipString.zip")
|
||||
.build());
|
||||
|
||||
assertThat(workDir.list()).hasSize(1);
|
||||
assertThat(workDir.list()[0]).isEqualTo("zipString.zip");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipFile() throws IOException {
|
||||
File fileToCompress = File.createTempFile("test1", "tmp");
|
||||
FileUtils.writeStringToFile(fileToCompress, "hello world", Charset.defaultCharset());
|
||||
|
||||
input.send(MessageBuilder.withPayload(fileToCompress).build());
|
||||
|
||||
assertThat(workDir.list()).hasSize(1);
|
||||
assertThat(workDir.list()[0]).isEqualTo(fileToCompress.getName() + ".zip");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipIterableWithMultipleStrings() {
|
||||
String stringToCompress1 = "String1";
|
||||
String stringToCompress2 = "String2";
|
||||
String stringToCompress3 = "String3";
|
||||
String stringToCompress4 = "String4";
|
||||
|
||||
List<String> stringsToCompress = new ArrayList<>(4);
|
||||
|
||||
stringsToCompress.add(stringToCompress1);
|
||||
stringsToCompress.add(stringToCompress2);
|
||||
stringsToCompress.add(stringToCompress3);
|
||||
stringsToCompress.add(stringToCompress4);
|
||||
|
||||
input.send(MessageBuilder.withPayload(stringsToCompress)
|
||||
.setHeader(FileHeaders.FILENAME, "zipWith4Strings.zip")
|
||||
.build());
|
||||
|
||||
assertThat(workDir.list()).hasSize(1);
|
||||
assertThat(workDir.list()[0]).isEqualTo("zipWith4Strings.zip");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipIterableWithDifferentTypes() throws IOException {
|
||||
|
||||
String stringToCompress = "String1";
|
||||
byte[] bytesToCompress = "String2".getBytes();
|
||||
File fileToCompress = File.createTempFile("test2", "tmp");
|
||||
FileUtils.writeStringToFile(fileToCompress, "hello world", Charset.defaultCharset());
|
||||
|
||||
final List<Object> objectsToCompress = new ArrayList<>(3);
|
||||
|
||||
objectsToCompress.add(stringToCompress);
|
||||
objectsToCompress.add(bytesToCompress);
|
||||
objectsToCompress.add(fileToCompress);
|
||||
|
||||
input.send(MessageBuilder.withPayload(objectsToCompress)
|
||||
.setHeader(FileHeaders.FILENAME, "objects-to-compress.zip")
|
||||
.build());
|
||||
|
||||
assertThat(workDir.list()).hasSize(1);
|
||||
assertThat(workDir.list()[0]).isEqualTo("objects-to-compress.zip");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?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"
|
||||
xmlns:int-zip="http://www.springframework.org/schema/integration/zip"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/zip https://www.springframework.org/schema/integration/zip/spring-integration-zip.xsd">
|
||||
|
||||
<int:channel id="input"/>
|
||||
<int:channel id="output"/>
|
||||
|
||||
<int-zip:unzip-transformer id="unzipTransformer"
|
||||
delete-files="true" input-channel="input" output-channel="output"
|
||||
result-type="FILE" expect-single-result="true"/>
|
||||
|
||||
<int-zip:unzip-transformer id="unzipTransformerWithDefaults"
|
||||
input-channel="input" output-channel="output" />
|
||||
</beans>
|
||||
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright 2015-2023 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
|
||||
*
|
||||
* https://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.config.xml;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.integration.channel.AbstractMessageChannel;
|
||||
import org.springframework.integration.endpoint.EventDrivenConsumer;
|
||||
import org.springframework.integration.file.DefaultFileNameGenerator;
|
||||
import org.springframework.integration.file.FileNameGenerator;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.integration.transformer.MessageTransformingHandler;
|
||||
import org.springframework.integration.zip.transformer.UnZipTransformer;
|
||||
import org.springframework.integration.zip.transformer.ZipResultType;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
* @author Artem Bilan
|
||||
*
|
||||
* @since 6.1
|
||||
*/
|
||||
@SpringJUnitConfig
|
||||
@DirtiesContext
|
||||
public class UnZipTransformerParserTests {
|
||||
|
||||
@Autowired
|
||||
private ConfigurableApplicationContext context;
|
||||
|
||||
@Test
|
||||
public void testUnZipTransformerParserWithDefaults() {
|
||||
EventDrivenConsumer consumer = this.context.getBean("unzipTransformerWithDefaults", EventDrivenConsumer.class);
|
||||
|
||||
final AbstractMessageChannel inputChannel = TestUtils.getPropertyValue(consumer, "inputChannel", AbstractMessageChannel.class);
|
||||
assertThat(inputChannel.getComponentName()).isEqualTo("input");
|
||||
|
||||
final MessageTransformingHandler handler = TestUtils.getPropertyValue(consumer, "handler", MessageTransformingHandler.class);
|
||||
|
||||
assertThat(TestUtils.getPropertyValue(handler, "outputChannelName")).isEqualTo("output");
|
||||
|
||||
final UnZipTransformer unZipTransformer = TestUtils.getPropertyValue(handler, "transformer", UnZipTransformer.class);
|
||||
|
||||
final Charset charset = TestUtils.getPropertyValue(unZipTransformer, "charset", Charset.class);
|
||||
final FileNameGenerator fileNameGenerator = TestUtils.getPropertyValue(unZipTransformer, "fileNameGenerator", FileNameGenerator.class);
|
||||
final ZipResultType zipResultType = TestUtils.getPropertyValue(unZipTransformer, "zipResultType", ZipResultType.class);
|
||||
final File workDirectory = TestUtils.getPropertyValue(unZipTransformer, "workDirectory", File.class);
|
||||
final Boolean deleteFiles = TestUtils.getPropertyValue(unZipTransformer, "deleteFiles", Boolean.class);
|
||||
final Boolean expectSingleResult = TestUtils.getPropertyValue(unZipTransformer, "expectSingleResult", Boolean.class);
|
||||
|
||||
assertThat(charset).isNotNull();
|
||||
assertThat(fileNameGenerator).isNotNull();
|
||||
assertThat(zipResultType).isNotNull();
|
||||
assertThat(workDirectory).isNotNull();
|
||||
assertThat(deleteFiles).isNotNull();
|
||||
assertThat(expectSingleResult).isNotNull();
|
||||
|
||||
assertThat(charset).isEqualTo(Charset.defaultCharset());
|
||||
assertThat(fileNameGenerator).isInstanceOf(DefaultFileNameGenerator.class);
|
||||
assertThat(zipResultType).isEqualTo(ZipResultType.FILE);
|
||||
assertThat(workDirectory)
|
||||
.isEqualTo(new File(System.getProperty("java.io.tmpdir") + File.separator + "ziptransformer"));
|
||||
assertThat(workDirectory.exists()).isTrue();
|
||||
assertThat(workDirectory.isDirectory()).isTrue();
|
||||
assertThat(deleteFiles).isFalse();
|
||||
assertThat(expectSingleResult).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnZipTransformerParserWithExplicitSettings() {
|
||||
EventDrivenConsumer consumer = this.context.getBean("unzipTransformer", EventDrivenConsumer.class);
|
||||
|
||||
final AbstractMessageChannel inputChannel = TestUtils.getPropertyValue(consumer, "inputChannel", AbstractMessageChannel.class);
|
||||
assertThat(inputChannel.getComponentName()).isEqualTo("input");
|
||||
|
||||
final MessageTransformingHandler handler = TestUtils.getPropertyValue(consumer, "handler", MessageTransformingHandler.class);
|
||||
|
||||
assertThat(TestUtils.getPropertyValue(handler, "outputChannelName")).isEqualTo("output");
|
||||
|
||||
final UnZipTransformer unZipTransformer = TestUtils.getPropertyValue(handler, "transformer", UnZipTransformer.class);
|
||||
|
||||
final Charset charset = TestUtils.getPropertyValue(unZipTransformer, "charset", Charset.class);
|
||||
final FileNameGenerator fileNameGenerator = TestUtils.getPropertyValue(unZipTransformer, "fileNameGenerator", FileNameGenerator.class);
|
||||
final ZipResultType zipResultType = TestUtils.getPropertyValue(unZipTransformer, "zipResultType", ZipResultType.class);
|
||||
final File workDirectory = TestUtils.getPropertyValue(unZipTransformer, "workDirectory", File.class);
|
||||
final Boolean deleteFiles = TestUtils.getPropertyValue(unZipTransformer, "deleteFiles", Boolean.class);
|
||||
final Boolean expectSingleResult = TestUtils.getPropertyValue(unZipTransformer, "expectSingleResult", Boolean.class);
|
||||
|
||||
assertThat(charset).isNotNull();
|
||||
assertThat(fileNameGenerator).isNotNull();
|
||||
assertThat(zipResultType).isNotNull();
|
||||
assertThat(workDirectory).isNotNull();
|
||||
assertThat(deleteFiles).isNotNull();
|
||||
assertThat(expectSingleResult).isNotNull();
|
||||
|
||||
assertThat(charset).isEqualTo(Charset.defaultCharset());
|
||||
assertThat(fileNameGenerator).isInstanceOf(DefaultFileNameGenerator.class);
|
||||
assertThat(zipResultType).isEqualTo(ZipResultType.FILE);
|
||||
assertThat(workDirectory)
|
||||
.isEqualTo(new File(System.getProperty("java.io.tmpdir") + File.separator + "ziptransformer"));
|
||||
assertThat(workDirectory.exists()).isTrue();
|
||||
assertThat(workDirectory.isDirectory()).isTrue();
|
||||
assertThat(deleteFiles).isTrue();
|
||||
assertThat(expectSingleResult).isTrue();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?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"
|
||||
xmlns:int-zip="http://www.springframework.org/schema/integration/zip"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/zip https://www.springframework.org/schema/integration/zip/spring-integration-zip.xsd">
|
||||
|
||||
<int:channel id="input"/>
|
||||
<int:channel id="output"/>
|
||||
|
||||
<int-zip:zip-transformer id="zipTransformer" compression-level="2"
|
||||
delete-files="true" input-channel="input" output-channel="output"
|
||||
result-type="BYTE_ARRAY"/>
|
||||
|
||||
<int-zip:zip-transformer id="zipTransformerWithDefaults"
|
||||
input-channel="input" output-channel="output" />
|
||||
</beans>
|
||||
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* Copyright 2015-2023 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
|
||||
*
|
||||
* https://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.config.xml;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.zip.Deflater;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.channel.AbstractMessageChannel;
|
||||
import org.springframework.integration.endpoint.EventDrivenConsumer;
|
||||
import org.springframework.integration.file.DefaultFileNameGenerator;
|
||||
import org.springframework.integration.file.FileNameGenerator;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.integration.transformer.MessageTransformingHandler;
|
||||
import org.springframework.integration.zip.transformer.ZipResultType;
|
||||
import org.springframework.integration.zip.transformer.ZipTransformer;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
* @author Gunnar Hillert
|
||||
* @author Artem Bilan
|
||||
*
|
||||
* @since 6.1
|
||||
*/
|
||||
@SpringJUnitConfig
|
||||
@DirtiesContext
|
||||
public class ZipTransformerParserTests {
|
||||
|
||||
@Autowired
|
||||
private ConfigurableApplicationContext context;
|
||||
|
||||
@Test
|
||||
public void testZipTransformerParserWithDefaults() {
|
||||
EventDrivenConsumer consumer = this.context.getBean("zipTransformerWithDefaults", EventDrivenConsumer.class);
|
||||
|
||||
final AbstractMessageChannel inputChannel = TestUtils.getPropertyValue(consumer, "inputChannel", AbstractMessageChannel.class);
|
||||
assertThat(inputChannel.getComponentName()).isEqualTo("input");
|
||||
|
||||
final MessageTransformingHandler handler = TestUtils.getPropertyValue(consumer, "handler", MessageTransformingHandler.class);
|
||||
|
||||
assertThat(TestUtils.getPropertyValue(handler, "outputChannelName")).isEqualTo("output");
|
||||
|
||||
final ZipTransformer zipTransformer = TestUtils.getPropertyValue(handler, "transformer", ZipTransformer.class);
|
||||
|
||||
final Charset charset = TestUtils.getPropertyValue(zipTransformer, "charset", Charset.class);
|
||||
final FileNameGenerator fileNameGenerator = TestUtils.getPropertyValue(zipTransformer, "fileNameGenerator", FileNameGenerator.class);
|
||||
final ZipResultType zipResultType = TestUtils.getPropertyValue(zipTransformer, "zipResultType", ZipResultType.class);
|
||||
final File workDirectory = TestUtils.getPropertyValue(zipTransformer, "workDirectory", File.class);
|
||||
final Integer compressionLevel = TestUtils.getPropertyValue(zipTransformer, "compressionLevel", Integer.class);
|
||||
final Boolean deleteFiles = TestUtils.getPropertyValue(zipTransformer, "deleteFiles", Boolean.class);
|
||||
|
||||
assertThat(charset).isNotNull();
|
||||
assertThat(fileNameGenerator).isNotNull();
|
||||
assertThat(zipResultType).isNotNull();
|
||||
assertThat(workDirectory).isNotNull();
|
||||
assertThat(deleteFiles).isNotNull();
|
||||
assertThat(compressionLevel).isNotNull();
|
||||
|
||||
assertThat(charset).isEqualTo(Charset.defaultCharset());
|
||||
assertThat(fileNameGenerator).isInstanceOf(DefaultFileNameGenerator.class);
|
||||
assertThat(zipResultType).isEqualTo(ZipResultType.FILE);
|
||||
assertThat(workDirectory)
|
||||
.isEqualTo(new File(System.getProperty("java.io.tmpdir") + File.separator + "ziptransformer"));
|
||||
assertThat(workDirectory.exists()).isTrue();
|
||||
assertThat(workDirectory.isDirectory()).isTrue();
|
||||
assertThat(deleteFiles).isFalse();
|
||||
assertThat(compressionLevel).isEqualTo(Deflater.DEFAULT_COMPRESSION);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZipTransformerParserWithExplicitSettings() {
|
||||
EventDrivenConsumer consumer = this.context.getBean("zipTransformer", EventDrivenConsumer.class);
|
||||
|
||||
final AbstractMessageChannel inputChannel = TestUtils.getPropertyValue(consumer, "inputChannel", AbstractMessageChannel.class);
|
||||
assertThat(inputChannel.getComponentName()).isEqualTo("input");
|
||||
|
||||
final MessageTransformingHandler handler = TestUtils.getPropertyValue(consumer, "handler", MessageTransformingHandler.class);
|
||||
|
||||
assertThat(TestUtils.getPropertyValue(handler, "outputChannelName")).isEqualTo("output");
|
||||
|
||||
final ZipTransformer zipTransformer = TestUtils.getPropertyValue(handler, "transformer", ZipTransformer.class);
|
||||
|
||||
final Charset charset = TestUtils.getPropertyValue(zipTransformer, "charset", Charset.class);
|
||||
final FileNameGenerator fileNameGenerator = TestUtils.getPropertyValue(zipTransformer, "fileNameGenerator", FileNameGenerator.class);
|
||||
final ZipResultType zipResultType = TestUtils.getPropertyValue(zipTransformer, "zipResultType", ZipResultType.class);
|
||||
final File workDirectory = TestUtils.getPropertyValue(zipTransformer, "workDirectory", File.class);
|
||||
final Integer compressionLevel = TestUtils.getPropertyValue(zipTransformer, "compressionLevel", Integer.class);
|
||||
final Boolean deleteFiles = TestUtils.getPropertyValue(zipTransformer, "deleteFiles", Boolean.class);
|
||||
|
||||
assertThat(charset).isNotNull();
|
||||
assertThat(fileNameGenerator).isNotNull();
|
||||
assertThat(zipResultType).isNotNull();
|
||||
assertThat(workDirectory).isNotNull();
|
||||
assertThat(deleteFiles).isNotNull();
|
||||
assertThat(compressionLevel).isNotNull();
|
||||
|
||||
assertThat(charset).isEqualTo(Charset.defaultCharset());
|
||||
assertThat(fileNameGenerator).isInstanceOf(DefaultFileNameGenerator.class);
|
||||
assertThat(zipResultType).isEqualTo(ZipResultType.BYTE_ARRAY);
|
||||
assertThat(workDirectory)
|
||||
.isEqualTo(new File(System.getProperty("java.io.tmpdir") + File.separator + "ziptransformer"));
|
||||
assertThat(workDirectory.exists()).isTrue();
|
||||
assertThat(workDirectory.isDirectory()).isTrue();
|
||||
assertThat(compressionLevel).isEqualTo(2);
|
||||
assertThat(deleteFiles).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZipTransformerParserWithIncorrectResultType() {
|
||||
|
||||
assertThatExceptionOfType(BeanCreationException.class)
|
||||
.isThrownBy(() ->
|
||||
new ClassPathXmlApplicationContext("ZipTransformerParserTestsWithIncorrectResultType.xml",
|
||||
getClass()))
|
||||
.withMessageContaining("Failed to convert property value of type 'java.lang.String' " +
|
||||
"to required type 'org.springframework.integration.zip.transformer.ZipResultType'");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?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"
|
||||
xmlns:int-zip="http://www.springframework.org/schema/integration/zip"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/zip https://www.springframework.org/schema/integration/zip/spring-integration-zip.xsd">
|
||||
|
||||
<int:channel id="input"/>
|
||||
<int:channel id="output"/>
|
||||
|
||||
<int-zip:zip-transformer id="zipTransformer" compression-level="2"
|
||||
delete-files="true" input-channel="input" output-channel="output"
|
||||
result-type="INCORRECT"/>
|
||||
|
||||
</beans>
|
||||
@@ -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
|
||||
https://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
https://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>
|
||||
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* Copyright 2016-2023 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
|
||||
*
|
||||
* https://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 java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
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.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Andriy Kryvtsun
|
||||
* @author Artem Bilan
|
||||
*
|
||||
* @since 6.1
|
||||
*/
|
||||
@SpringJUnitConfig
|
||||
@DirtiesContext
|
||||
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 final 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) {
|
||||
|
||||
assertThat(message).isNotNull();
|
||||
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) {
|
||||
|
||||
assertThat(message).isNotNull();
|
||||
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<>();
|
||||
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) {
|
||||
assertThat(message.getPayload()).isEqualTo(payload);
|
||||
}
|
||||
|
||||
private static void checkHeaderValue(Message<?> message, String headerName, String headerValue) {
|
||||
assertThat(message.getHeaders().get(headerName)).isEqualTo(headerValue);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,247 @@
|
||||
/*
|
||||
* Copyright 2015-2023 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
|
||||
*
|
||||
* https://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.transformer;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.zeroturnaround.zip.ZipException;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.integration.transformer.MessageTransformationException;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
* @author Artem Bilan
|
||||
* @author Ingo Dueppe
|
||||
*
|
||||
* @since 6.1
|
||||
*/
|
||||
@SpringJUnitConfig
|
||||
@DirtiesContext
|
||||
public class UnZipTransformerTests {
|
||||
|
||||
@TempDir
|
||||
public File workDir;
|
||||
|
||||
@Autowired
|
||||
private ResourceLoader resourceLoader;
|
||||
|
||||
@Test
|
||||
public void unzipFlatFileEntryZip() throws IOException {
|
||||
final Resource zipResource = this.resourceLoader.getResource("classpath:testzipdata/flatfileentry.zip");
|
||||
final InputStream is = zipResource.getInputStream();
|
||||
|
||||
final Message<InputStream> message = MessageBuilder.withPayload(is).build();
|
||||
|
||||
final UnZipTransformer unZipTransformer = new UnZipTransformer();
|
||||
unZipTransformer.setZipResultType(ZipResultType.FILE);
|
||||
unZipTransformer.afterPropertiesSet();
|
||||
|
||||
final Message<?> resultMessage = unZipTransformer.transform(message);
|
||||
|
||||
assertThat(resultMessage).isNotNull();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, byte[]> unzippedData = (Map<String, byte[]>) resultMessage.getPayload();
|
||||
|
||||
assertThat(unzippedData).isNotNull().hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unzipSingleFileAsInputStreamToByteArray() throws IOException {
|
||||
final Resource resource = this.resourceLoader.getResource("classpath:testzipdata/single.zip");
|
||||
final InputStream is = resource.getInputStream();
|
||||
|
||||
final Message<InputStream> message = MessageBuilder.withPayload(is).build();
|
||||
|
||||
final UnZipTransformer unZipTransformer = new UnZipTransformer();
|
||||
unZipTransformer.setZipResultType(ZipResultType.BYTE_ARRAY);
|
||||
unZipTransformer.afterPropertiesSet();
|
||||
|
||||
final Message<?> resultMessage = unZipTransformer.transform(message);
|
||||
|
||||
assertThat(resultMessage).isNotNull();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, byte[]> unzippedData = (Map<String, byte[]>) resultMessage.getPayload();
|
||||
|
||||
assertThat(unzippedData).isNotNull().hasSize(1);
|
||||
assertThat(new String(unzippedData.values().iterator().next())).isEqualTo("Spring Integration Rocks!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unzipSingleFileToByteArray() throws IOException {
|
||||
final Resource resource = this.resourceLoader.getResource("classpath:testzipdata/single.zip");
|
||||
final InputStream is = resource.getInputStream();
|
||||
|
||||
final File inputFile = new File(this.workDir, "unzipSingleFileToByteArray");
|
||||
|
||||
FileOutputStream out = new FileOutputStream(inputFile);
|
||||
IOUtils.copy(is, out);
|
||||
is.close();
|
||||
out.close();
|
||||
|
||||
final Message<File> message = MessageBuilder.withPayload(inputFile).build();
|
||||
|
||||
final UnZipTransformer unZipTransformer = new UnZipTransformer();
|
||||
unZipTransformer.setZipResultType(ZipResultType.BYTE_ARRAY);
|
||||
unZipTransformer.afterPropertiesSet();
|
||||
|
||||
final Message<?> resultMessage = unZipTransformer.transform(message);
|
||||
|
||||
assertThat(resultMessage).isNotNull();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, byte[]> unzippedData = (Map<String, byte[]>) resultMessage.getPayload();
|
||||
|
||||
assertThat(unzippedData).isNotNull().hasSize(1);
|
||||
assertThat(inputFile).exists();
|
||||
assertThat(new String(unzippedData.values().iterator().next())).isEqualTo("Spring Integration Rocks!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unzipSingleFileToByteArrayWithDeleteFilesTrue() throws IOException {
|
||||
final Resource resource = this.resourceLoader.getResource("classpath:testzipdata/single.zip");
|
||||
final InputStream is = resource.getInputStream();
|
||||
|
||||
final File inputFile = new File(this.workDir, "unzipSingleFileToByteArray");
|
||||
|
||||
FileOutputStream output = new FileOutputStream(inputFile);
|
||||
IOUtils.copy(is, output);
|
||||
is.close();
|
||||
output.close();
|
||||
|
||||
final Message<File> message = MessageBuilder.withPayload(inputFile).build();
|
||||
|
||||
final UnZipTransformer unZipTransformer = new UnZipTransformer();
|
||||
unZipTransformer.setZipResultType(ZipResultType.BYTE_ARRAY);
|
||||
unZipTransformer.setDeleteFiles(true);
|
||||
unZipTransformer.afterPropertiesSet();
|
||||
|
||||
final Message<?> resultMessage = unZipTransformer.transform(message);
|
||||
|
||||
assertThat(resultMessage).isNotNull();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, byte[]> unzippedData = (Map<String, byte[]>) resultMessage.getPayload();
|
||||
|
||||
assertThat(unzippedData).isNotNull().hasSize(1);
|
||||
assertThat(inputFile).doesNotExist();
|
||||
assertThat(new String(unzippedData.values().iterator().next())).isEqualTo("Spring Integration Rocks!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unzipMultipleFilesAsInputStreamToByteArray() throws IOException {
|
||||
final Resource resource = this.resourceLoader.getResource("classpath:testzipdata/countries.zip");
|
||||
final InputStream is = resource.getInputStream();
|
||||
|
||||
final Message<InputStream> message = MessageBuilder.withPayload(is).build();
|
||||
|
||||
final UnZipTransformer unZipTransformer = new UnZipTransformer();
|
||||
unZipTransformer.setZipResultType(ZipResultType.BYTE_ARRAY);
|
||||
unZipTransformer.afterPropertiesSet();
|
||||
|
||||
final Message<?> resultMessage = unZipTransformer.transform(message);
|
||||
|
||||
assertThat(resultMessage).isNotNull();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, byte[]> unzippedData = (Map<String, byte[]>) resultMessage.getPayload();
|
||||
|
||||
assertThat(unzippedData).isNotNull().hasSize(5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unzipMultipleFilesAsInputStreamWithExpectSingleResultTrue() throws IOException {
|
||||
final Resource resource = this.resourceLoader.getResource("classpath:testzipdata/countries.zip");
|
||||
final InputStream is = resource.getInputStream();
|
||||
|
||||
final Message<InputStream> message = MessageBuilder.withPayload(is).build();
|
||||
|
||||
final UnZipTransformer unZipTransformer = new UnZipTransformer();
|
||||
unZipTransformer.setZipResultType(ZipResultType.BYTE_ARRAY);
|
||||
unZipTransformer.setExpectSingleResult(true);
|
||||
unZipTransformer.afterPropertiesSet();
|
||||
|
||||
|
||||
assertThatExceptionOfType(MessagingException.class)
|
||||
.isThrownBy(() -> unZipTransformer.transform(message))
|
||||
.withStackTraceContaining("The UnZip operation extracted 5 result objects " +
|
||||
"but expectSingleResult was 'true'.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unzipInvalidZipFile() throws IOException {
|
||||
File fileToUnzip = File.createTempFile("test1", "tmp");
|
||||
FileUtils.writeStringToFile(fileToUnzip, "hello world", Charset.defaultCharset());
|
||||
|
||||
UnZipTransformer unZipTransformer = new UnZipTransformer();
|
||||
unZipTransformer.setZipResultType(ZipResultType.BYTE_ARRAY);
|
||||
unZipTransformer.setExpectSingleResult(true);
|
||||
unZipTransformer.afterPropertiesSet();
|
||||
|
||||
Message<File> message = MessageBuilder.withPayload(fileToUnzip).build();
|
||||
|
||||
assertThatExceptionOfType(MessagingException.class)
|
||||
.isThrownBy(() -> unZipTransformer.transform(message))
|
||||
.withStackTraceContaining(String.format("Not a zip file: %s", fileToUnzip.getAbsolutePath()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnzipMaliciousTraversalZipFile() throws IOException {
|
||||
final Resource resource = this.resourceLoader.getResource("classpath:testzipdata/zip-malicious-traversal.zip");
|
||||
final InputStream is = resource.getInputStream();
|
||||
|
||||
final Message<InputStream> message = MessageBuilder.withPayload(is).build();
|
||||
|
||||
final UnZipTransformer unZipTransformer = new UnZipTransformer();
|
||||
unZipTransformer.afterPropertiesSet();
|
||||
|
||||
|
||||
assertThatExceptionOfType(MessageTransformationException.class)
|
||||
.isThrownBy(() -> unZipTransformer.transform(message))
|
||||
.withRootCauseInstanceOf(ZipException.class)
|
||||
.withStackTraceContaining("is trying to leave the target output directory");
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class TestConfiguration {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,246 @@
|
||||
/*
|
||||
* Copyright 2015-2023 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
|
||||
*
|
||||
* https://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.transformer;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.zeroturnaround.zip.ZipUtil;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.integration.zip.ZipHeaders;
|
||||
import org.springframework.messaging.Message;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
* @author Artem Bilan
|
||||
*
|
||||
* @since 6.1
|
||||
*/
|
||||
public class ZipTransformerTests {
|
||||
|
||||
@TempDir
|
||||
public File workDir;
|
||||
|
||||
@Test
|
||||
public void zipString() {
|
||||
final ZipTransformer zipTransformer = new ZipTransformer();
|
||||
zipTransformer.setBeanFactory(mock(BeanFactory.class));
|
||||
zipTransformer.setZipResultType(ZipResultType.BYTE_ARRAY);
|
||||
zipTransformer.afterPropertiesSet();
|
||||
|
||||
final String stringToCompress = "Hello World";
|
||||
|
||||
final Date fileDate = new Date();
|
||||
|
||||
final Message<String> message = MessageBuilder.withPayload(stringToCompress)
|
||||
.setHeader(ZipHeaders.ZIP_ENTRY_FILE_NAME, "test.txt")
|
||||
.setHeader(ZipHeaders.ZIP_ENTRY_LAST_MODIFIED_DATE, fileDate)
|
||||
.build();
|
||||
|
||||
final Message<?> result = zipTransformer.transform(message);
|
||||
|
||||
Object resultPayload = result.getPayload();
|
||||
|
||||
assertThat(resultPayload).isInstanceOf(byte[].class);
|
||||
|
||||
ZipUtil.unpack(new ByteArrayInputStream((byte[]) resultPayload), this.workDir);
|
||||
|
||||
final File unzippedEntry = new File(this.workDir, "test.txt");
|
||||
assertThat(unzippedEntry).exists().isFile();
|
||||
|
||||
//See https://stackoverflow.com/questions/3725662/what-is-the-earliest-timestamp-value-that-is-supported-in-zip-file-format
|
||||
assertThat(unzippedEntry.lastModified())
|
||||
.isGreaterThan(fileDate.getTime() - 3000)
|
||||
.isLessThan(fileDate.getTime() + 3000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipStringCollection() {
|
||||
final ZipTransformer zipTransformer = new ZipTransformer();
|
||||
zipTransformer.setBeanFactory(mock(BeanFactory.class));
|
||||
zipTransformer.setZipResultType(ZipResultType.BYTE_ARRAY);
|
||||
zipTransformer.afterPropertiesSet();
|
||||
|
||||
final String string1ToCompress = "Cartman";
|
||||
final String string2ToCompress = "Kenny";
|
||||
final String string3ToCompress = "Butters";
|
||||
|
||||
final List<String> strings = new ArrayList<>(3);
|
||||
|
||||
strings.add(string1ToCompress);
|
||||
strings.add(string2ToCompress);
|
||||
strings.add(string3ToCompress);
|
||||
|
||||
final Date fileDate = new Date();
|
||||
|
||||
final Message<List<String>> message = MessageBuilder.withPayload(strings)
|
||||
.setHeader(ZipHeaders.ZIP_ENTRY_FILE_NAME, "test.txt")
|
||||
.setHeader(ZipHeaders.ZIP_ENTRY_LAST_MODIFIED_DATE, fileDate)
|
||||
.build();
|
||||
|
||||
final Message<?> result = zipTransformer.transform(message);
|
||||
|
||||
Object resultPayload = result.getPayload();
|
||||
|
||||
assertThat(resultPayload).isInstanceOf(byte[].class);
|
||||
|
||||
ZipUtil.unpack(new ByteArrayInputStream((byte[]) resultPayload), this.workDir);
|
||||
|
||||
File[] files = this.workDir.listFiles();
|
||||
|
||||
assertThat(files).hasSizeGreaterThanOrEqualTo(3);
|
||||
|
||||
final Set<String> expectedFileNames = new HashSet<>();
|
||||
|
||||
expectedFileNames.add("test_1.txt");
|
||||
expectedFileNames.add("test_2.txt");
|
||||
expectedFileNames.add("test_3.txt");
|
||||
|
||||
for (File file : files) {
|
||||
if (file.getName().startsWith("test")) {
|
||||
assertThat(file).exists().isFile();
|
||||
|
||||
//See https://stackoverflow.com/questions/3725662/what-is-the-earliest-timestamp-value-that-is-supported-in-zip-file-format
|
||||
assertThat(file.lastModified())
|
||||
.isLessThan(fileDate.getTime() + 4000)
|
||||
.isGreaterThan(fileDate.getTime() - 4000);
|
||||
|
||||
assertThat(file).hasExtension("txt");
|
||||
|
||||
assertThat(expectedFileNames).contains(file.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipStringToFile() {
|
||||
final ZipTransformer zipTransformer = new ZipTransformer();
|
||||
zipTransformer.setBeanFactory(mock(BeanFactory.class));
|
||||
zipTransformer.afterPropertiesSet();
|
||||
|
||||
final String stringToCompress = "Hello World";
|
||||
|
||||
final String zipEntryFileName = "test.txt";
|
||||
final Message<String> message = MessageBuilder.withPayload(stringToCompress)
|
||||
.setHeader(ZipHeaders.ZIP_ENTRY_FILE_NAME, zipEntryFileName)
|
||||
.build();
|
||||
|
||||
final Message<?> result = zipTransformer.transform(message);
|
||||
|
||||
assertThat(result.getPayload()).isInstanceOf(File.class);
|
||||
|
||||
final File payload = (File) result.getPayload();
|
||||
|
||||
assertThat(payload).hasName(message.getHeaders().getId().toString() + ".msg.zip");
|
||||
assertThat((SpringZipUtils.isValid(payload))).isTrue();
|
||||
|
||||
final byte[] zipEntryData = ZipUtil.unpackEntry(payload, "test.txt");
|
||||
|
||||
assertThat(zipEntryData).isNotNull();
|
||||
assertThat(new String(zipEntryData)).isEqualTo("Hello World");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipFile() {
|
||||
|
||||
ZipTransformer zipTransformer = new ZipTransformer();
|
||||
zipTransformer.setBeanFactory(mock(BeanFactory.class));
|
||||
zipTransformer.setDeleteFiles(true);
|
||||
zipTransformer.afterPropertiesSet();
|
||||
|
||||
final File testFile = createTestFile(10);
|
||||
|
||||
assertThat(testFile).exists();
|
||||
|
||||
final Message<File> message = MessageBuilder.withPayload(testFile).build();
|
||||
|
||||
final Message<?> result = zipTransformer.transform(message);
|
||||
|
||||
assertThat(result.getPayload()).isInstanceOf(File.class);
|
||||
|
||||
final File payload = (File) result.getPayload();
|
||||
|
||||
assertThat(payload).hasName(testFile.getName() + ".zip");
|
||||
assertThat(SpringZipUtils.isValid(payload)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipCollection() {
|
||||
|
||||
final File testFile1 = createTestFile(1);
|
||||
final File testFile2 = createTestFile(2);
|
||||
final File testFile3 = createTestFile(3);
|
||||
final File testFile4 = createTestFile(4);
|
||||
|
||||
assertThat(testFile1).exists();
|
||||
assertThat(testFile2).exists();
|
||||
assertThat(testFile3).exists();
|
||||
assertThat(testFile4).exists();
|
||||
|
||||
final Collection<File> files = new ArrayList<>();
|
||||
|
||||
files.add(testFile1);
|
||||
files.add(testFile2);
|
||||
files.add(testFile3);
|
||||
files.add(testFile4);
|
||||
|
||||
final ZipTransformer zipTransformer = new ZipTransformer();
|
||||
zipTransformer.setBeanFactory(mock(BeanFactory.class));
|
||||
zipTransformer.afterPropertiesSet();
|
||||
|
||||
final Message<Collection<File>> message = MessageBuilder.withPayload(files).build();
|
||||
|
||||
final Message<?> result = zipTransformer.transform(message);
|
||||
|
||||
assertThat(result.getPayload()).isInstanceOf(File.class);
|
||||
|
||||
final File outputZipFile = (File) result.getPayload();
|
||||
|
||||
assertThat(outputZipFile).exists().isFile().hasExtension("zip");
|
||||
assertThat(SpringZipUtils.isValid(outputZipFile)).isTrue();
|
||||
}
|
||||
|
||||
private File createTestFile(int size) {
|
||||
final File testFile = new File(this.workDir, "testdata" + UUID.randomUUID().toString() + ".data");
|
||||
|
||||
try (RandomAccessFile f = new RandomAccessFile(testFile, "rw")) {
|
||||
f.setLength((long) size * 1024 * 1024);
|
||||
}
|
||||
catch (Exception e) {
|
||||
// Ignore
|
||||
}
|
||||
return testFile;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
15
spring-integration-zip/src/test/resources/log4j2-test.xml
Normal file
15
spring-integration-zip/src/test/resources/log4j2-test.xml
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<Appenders>
|
||||
<Console name="STDOUT" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%d %p [%t] [%c] - %m%n" />
|
||||
</Console>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Logger name="org.springframework.integration" level="warn"/>
|
||||
<Logger name="org.springframework.integration.zip" level="warn"/>
|
||||
<Root level="warn">
|
||||
<AppenderRef ref="STDOUT" />
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
Asia
|
||||
@@ -0,0 +1 @@
|
||||
Europe
|
||||
@@ -0,0 +1 @@
|
||||
Germany
|
||||
@@ -0,0 +1 @@
|
||||
France
|
||||
@@ -0,0 +1 @@
|
||||
Poland
|
||||
@@ -0,0 +1 @@
|
||||
Spring Integration Rocks!
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
Spring Integration Rocks!
|
||||
BIN
spring-integration-zip/src/test/resources/testzipdata/single.zip
Normal file
BIN
spring-integration-zip/src/test/resources/testzipdata/single.zip
Normal file
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user