INT-832: Add File Relative Path to Message Headers

JIRA: https://jira.spring.io/browse/INT-832

When we scan directory recursively for files (e.g. `WatchServiceDirectoryScanner`), it can be useful to get access to the relative path from the `Message`, e.g. on the `FileWritingMessageHandler` side to restore the original structure

* Add a `FileHeaders.FILENAME` into the outbound `Message` from the `FileReadingMessageSource`
* The result of that header is like a removal of leading `this.directory.getAbsolutePath()` in the target `File.getAbsolutePath()`.
In case of not recursion we get just only regular file name.

* Introduce `FileHeaders.RELATIVE_PATH`
* Populated `FileHeaders.RELATIVE_PATH`, `FileHeaders.FILENAME`, `FileHeaders.ORIGINAL_FILE` in the `FileReadingMessageSource`
* File `FileTailingMessageProducerSupport` to populate `FileHeaders` properly
* Introduce ctor for the `LastModifiedFileListFilter` for better Java configuration experience
* Add docs for changes

Doc Polishing
This commit is contained in:
Artem Bilan
2016-09-20 15:09:36 -04:00
committed by Gary Russell
parent 7d9c65a108
commit 8f0fa3468f
12 changed files with 108 additions and 19 deletions

View File

@@ -22,6 +22,7 @@ package org.springframework.integration.file;
*
* @author Mark Fisher
* @author Gary Russell
* @author Artem Bilan
*/
public abstract class FileHeaders {
@@ -29,6 +30,8 @@ public abstract class FileHeaders {
public static final String FILENAME = PREFIX + "name";
public static final String RELATIVE_PATH = PREFIX + "relativePath";
public static final String ORIGINAL_FILE = PREFIX + "originalFile";
public static final String REMOTE_DIRECTORY = PREFIX + "remoteDirectory";

View File

@@ -39,6 +39,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -361,7 +362,13 @@ public class FileReadingMessageSource extends IntegrationObjectSupport implement
}
if (file != null) {
message = this.getMessageBuilderFactory().withPayload(file).build();
message = getMessageBuilderFactory().withPayload(file)
.setHeader(FileHeaders.RELATIVE_PATH, file.getAbsolutePath()
.replaceFirst(Matcher.quoteReplacement(this.directory.getAbsolutePath() + File.separator),
""))
.setHeader(FileHeaders.FILENAME, file.getName())
.setHeader(FileHeaders.ORIGINAL_FILE, file)
.build();
if (logger.isInfoEnabled()) {
logger.info("Created message: [" + message + "]");
}

View File

@@ -29,6 +29,7 @@ import java.util.concurrent.TimeUnit;
* The resolution is done in seconds.
*
* @author Gary Russell
* @author Artem Bilan
* @since 4.2
*
*/
@@ -42,6 +43,19 @@ public class LastModifiedFileListFilter implements FileListFilter<File> {
return this.age;
}
public LastModifiedFileListFilter() {
}
/**
* Construct a {@link LastModifiedFileListFilter} instance with provided {@link #age}.
* Defaults to 60 seconds.
* @param age the age in seconds.
* @since 5.0
*/
public LastModifiedFileListFilter(long age) {
this.age = age;
}
/**
* Set the age that files have to be before being passed by this filter.
* If {@link File#lastModified()} plus age is greater than the current time, the file

View File

@@ -32,6 +32,7 @@ import org.springframework.util.Assert;
* Base class for file tailing inbound adapters.
*
* @author Gary Russell
* @author Artem Bilan
* @since 3.0
*
*/
@@ -101,7 +102,8 @@ public abstract class FileTailingMessageProducerSupport extends MessageProducerS
protected void send(String line) {
Message<?> message = this.getMessageBuilderFactory().withPayload(line)
.setHeader(FileHeaders.FILENAME, this.file.getAbsolutePath())
.setHeader(FileHeaders.FILENAME, this.file.getName())
.setHeader(FileHeaders.ORIGINAL_FILE, this.file)
.build();
super.sendMessage(message);
}