INTEXT-40 Add ZIP Transformer

* Add zip-transformer
* Add unzip-transformer
* Add UnZipResultSplitter
* Add sample

For reference see: https://jira.springsource.org/browse/INTEXT-40
This commit is contained in:
Gunnar Hillert
2015-06-15 17:40:00 -05:00
parent ba52da8d0c
commit 416cc39da4
62 changed files with 4694 additions and 1 deletions

View File

@@ -0,0 +1,84 @@
/*
* Copyright 2015 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.samples.zip;
import java.util.Scanner;
import org.apache.log4j.Logger;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Starts the Spring Context and will initialize the Spring Integration routes.
*
* @author Gunnar Hillert
* @since 1.0
*
*/
public final class Main {
private static final Logger LOGGER = Logger.getLogger(Main.class);
private Main() { }
/**
* Load the Spring Integration Application Context
*
* @param args - command line arguments
*/
public static void main(final String... args) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("\n========================================================="
+ "\n "
+ "\n Welcome to the Spring Integration Zip Sample "
+ "\n "
+ "\n For more information please visit: "
+ "\n http://www.springsource.org/spring-integration "
+ "\n "
+ "\n=========================================================" );
}
final AbstractApplicationContext context =
new ClassPathXmlApplicationContext("classpath:META-INF/spring/integration/*-context.xml");
context.registerShutdownHook();
SpringIntegrationUtils.displayDirectories(context);
final Scanner scanner = new Scanner(System.in);
if (LOGGER.isInfoEnabled()) {
LOGGER.info("\n========================================================="
+ "\n "
+ "\n Please press 'q + Enter' to quit the application. "
+ "\n "
+ "\n=========================================================" );
}
while (!scanner.hasNext("q")) {
//Do nothing unless user presses 'q' to quit.
}
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Exiting application...bye.");
}
System.exit(0);
}
}

View File

@@ -0,0 +1,91 @@
/*
* Copyright 2015 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.samples.zip;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.ApplicationContext;
import org.springframework.expression.Expression;
import org.springframework.integration.file.FileReadingMessageSource;
import org.springframework.integration.file.FileWritingMessageHandler;
/**
* Displays the names of the input and output directories.
*
* @author Gunnar Hillert
* @since 1.0
*
*/
public final class SpringIntegrationUtils {
private static final Log logger = LogFactory.getLog(SpringIntegrationUtils.class);
private SpringIntegrationUtils() { }
/**
* Helper Method to dynamically determine and display input and output
* directories as defined in the Spring Integration context.
*
* @param context Spring Application Context
*/
public static void displayDirectories(final ApplicationContext context) {
final Map<String, FileReadingMessageSource> fileReadingMessageSources = context.getBeansOfType(FileReadingMessageSource.class);
final List<String> inputDirectories = new ArrayList<String>();
for (FileReadingMessageSource source : fileReadingMessageSources.values()) {
final File inDir = (File) new DirectFieldAccessor(source).getPropertyValue("directory");
inputDirectories.add(inDir.getAbsolutePath());
}
final Map<String, FileWritingMessageHandler> fileWritingMessageHandlers = context.getBeansOfType(FileWritingMessageHandler.class);
final List<String> outputDirectories = new ArrayList<String>();
for (final FileWritingMessageHandler messageHandler : fileWritingMessageHandlers.values()) {
final Expression outDir = (Expression) new DirectFieldAccessor(messageHandler).getPropertyValue("destinationDirectoryExpression");
outputDirectories.add(outDir.getExpressionString());
}
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("\n=========================================================");
stringBuilder.append("\n");
for (final String inputDirectory : inputDirectories) {
stringBuilder.append("\n Intput directory is: '" + inputDirectory + "'");
}
for (final String outputDirectory : outputDirectories) {
stringBuilder.append("\n Output directory is: '" + outputDirectory + "'");
}
stringBuilder.append("\n\n=========================================================");
logger.info(stringBuilder.toString());
}
}

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2015 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.samples.zip;
import java.io.File;
import java.io.IOException;
import java.util.Locale;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.file.FileHeaders;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
/**
* This Spring Integration transformation handler takes the input file, converts
* the file into a string, converts the file contents into an upper-case string
* and then sets a few Spring Integration message headers.
*
* @author Gunnar Hillert
* @since 1.0
*/
public class TransformationHandler {
/**
* Actual Spring Integration transformation handler.
*
* @param inputMessage Spring Integration input message
* @return New Spring Integration message with updated headers
*/
@Transformer
public Message<String> handleFile(final Message<File> inputMessage) {
final File inputFile = inputMessage.getPayload();
final String filename = inputFile.getName();
final String fileExtension = FilenameUtils.getExtension(filename);
final String inputAsString;
try {
inputAsString = FileUtils.readFileToString(inputFile);
} catch (IOException e) {
throw new IllegalStateException(e);
}
final Message<String> message = MessageBuilder.withPayload(inputAsString.toUpperCase(Locale.ENGLISH))
.setHeader(FileHeaders.FILENAME, filename)
.setHeader(FileHeaders.ORIGINAL_FILE, inputFile)
.setHeader("file_size", inputFile.length())
.setHeader("file_extension", fileExtension)
.build();
return message;
}
}

View File

@@ -0,0 +1,39 @@
<?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-file="http://www.springframework.org/schema/integration/file"
xmlns:int-zip="http://www.springframework.org/schema/integration/zip"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/zip http://www.springframework.org/schema/integration/zip/spring-integration-zip.xsd">
<int-file:inbound-channel-adapter id="zipFilesIn"
directory="file:input-zip">
<int:poller id="poller" fixed-rate="5000" max-messages-per-poll="10" />
</int-file:inbound-channel-adapter>
<int-file:inbound-channel-adapter id="uncompressedFilesIn" directory="file:input-uncompressed">
<int:poller id="poller" fixed-rate="5000" max-messages-per-poll="10" />
</int-file:inbound-channel-adapter>
<int-zip:zip-transformer id="zipFiles" input-channel="uncompressedFilesIn"
output-channel="zipFilesOut" />
<int:chain id="unzipFiles" input-channel="zipFilesIn" output-channel="decompressedFilesOut">
<int-zip:unzip-transformer result-type="BYTE_ARRAY"/>
<int:splitter method="splitUnzippedMap">
<bean class="org.springframework.integration.zip.transformer.splitter.UnZipResultSplitter"/>
</int:splitter>
</int:chain>
<int-file:outbound-channel-adapter
id="decompressedFilesOut" directory="file:target/output/decompressedFilesOut"
delete-source-files="true" />
<int-file:outbound-channel-adapter
id="zipFilesOut" directory="file:target/output/zipFilesOut"
delete-source-files="true" />
</beans>

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss.SSS} %-5p [%t][%c] %m%n" />
</layout>
</appender>
<!-- Loggers -->
<logger name="org.springframework.integration">
<level value="warn" />
</logger>
<logger name="org.springframework.integration.samples.zip">
<level value="info" />
</logger>
<!-- Root Logger -->
<root>
<priority value="warn" />
<appender-ref ref="console" />
</root>
</log4j:configuration>