Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gpimblott/f9eae87a3206fc79ce3511c33edceb78 to your computer and use it in GitHub Desktop.
Save gpimblott/f9eae87a3206fc79ce3511c33edceb78 to your computer and use it in GitHub Desktop.
@Bean
RoutesBuilder ProcessZipFile() {
return new RouteBuilder() {
private ProcessZipFileEntry fileProcessor = new ProcessZipFileEntry();
private MissingFilesProcessor missingFileProcessor = new MissingFilesProcessor();
@Override
public void configure() throws Exception {
// This is the location to look for the metadata file
// we move it to the 'staging' directory first
// once processed we more to 'completed'
// NB - The locking isn't sufficient for a multi-process application here
String fileEndpoint = "file://" + inputDir +
"?include=" + metaFileNameRegex +
"&preMove=" + stagingDir +
"&move=" + completedDir +
"&charset=ascii&readLock=changed";
// The import directory for the zip files + filename
String zipFileEndpoint = "file://" + inputDir + "/";
// Base route to check for new meta-data csv files
// 1) clear out the array we will use to keep the list of exected files from the meta-data
// 2) Use the route 'processMetaFile' to process the csv and populate our array of expected files
// 3) Use a custom file loader to load the zip data file, use route 'processZipContents' to process
// the individual items in the zip
// 4) Log that we have completed the processing of this dataset
// 5) Waits for next file
from(fileEndpoint)
.id("MainRoute")
.setProperty("expectedFiles").constant(new ArrayList<ItemHolder>())
.to("direct:processMetaFile")
.bean(new LoadZipFileProcessor(zipFileEndpoint,
"direct:processZipContents", errorDir, stagingDir, completedDir, 10 * 1000))
.bean( missingFileProcessor )
.log("All done")
.end();
// Read the meta-data file and extract the list of files we expect into an array
// The 'expectedFiles' array is stored in a property in the route for use later
from("direct:processMetaFile")
.id("ProcessMetaFile")
.log("Processing ${file:name}")
.split(body().tokenize("\n"))
.streaming()
.process(exchange -> {
List files = (List) exchange.getProperty("expectedFiles");
String line = exchange.getIn().getBody(String.class);
files.add(new ItemHolder(line));
});
// Process the Zip file
// 1) Extract each of the pdf files from the zip archive
// 2) Don't load the whole file - use streaming mode - just in case it's big
// 3) If the extracted file is in the manifest then use our custom 'processor'
// 4) Otherwise - Use the 'unknownFile' route to handle the unexpected file
// 5) All done
from("direct:processZipContents")
.id("ProcessZipFile")
.split(new ZipSplitter())
.streaming()
.choice()
.when(new IsInManifest())
.bean(fileProcessor)
.otherwise()
.to("direct:unknownFile")
.end();
// Stub error handling for unknown files
// 1) Log the error
// 2) Write the PDF to the error directory
from("direct:unknownFile")
.id("UnknownFile")
.log("Unknown file found in zip : ${header.camelFileName}")
.to("file://" + errorDir + "?fileName=${header.camelFileName}");
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment