Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-8691: Support age FTP, SMB over time filters #8725

Closed
wants to merge 10 commits into from
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,8 @@ project('spring-integration-sftp') {

testImplementation project(':spring-integration-event')
testImplementation project(':spring-integration-file').sourceSets.test.output

testRuntimeOnly 'net.i2p.crypto:eddsa:0.3.0'
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright 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.file.filters;

import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import org.springframework.lang.Nullable;

/**
* The {@link FileListFilter} implementation to filter those files which
* lastModified is less than the {@link #age} in comparison
* with the {@link Instant#now()}.
* When {@link #discardCallback} is provided, it called for all the rejected files.
*
* @param <F> the file
*
* @author Adama Sorho
*
* @since 6.2
*/
public abstract class AbstractLastModifiedFileListFilter<F> implements DiscardAwareFileListFilter<F> {

protected static final long ONE_SECOND = 1000;

private static final long DEFAULT_AGE = 60;

private Duration age = Duration.ofSeconds(DEFAULT_AGE);

@Nullable
private Consumer<F> discardCallback;

public AbstractLastModifiedFileListFilter() {
}

public AbstractLastModifiedFileListFilter(Duration age) {
this.age = age;
}

/**
* Set the age that files have to be before being passed by this filter.
* If lastModified plus {@link #age} is before the {@link Instant#now()}, the file
* is filtered.
* Defaults to 60 seconds.
* @param age the Duration.
*/
public void setAge(Duration age) {
this.age = age;
}

/**
* Set the age that files have to be before being passed by this filter.
* If lastModified plus {@link #age} is before the {@link Instant#now()}, the file
* is filtered.
* Defaults to 60 seconds.
* @param age the age in seconds.
*/
public void setAge(long age) {
setAge(Duration.ofSeconds(age));
}

@Override
public void addDiscardCallback(@Nullable Consumer<F> discardCallback) {
this.discardCallback = discardCallback;
}

@Override
public List<F> filterFiles(F[] files) {
List<F> list = new ArrayList<>();
Instant now = Instant.now();
for (F file: files) {
if (fileIsAged(file, now)) {
list.add(file);
}
else if (this.discardCallback != null) {
this.discardCallback.accept(file);
}
}

return list;
}

@Override
public boolean accept(F file) {
if (fileIsAged(file, Instant.now())) {
return true;
}
else if (this.discardCallback != null) {
this.discardCallback.accept(file);
}

return false;
}

private boolean fileIsAged(F file, Instant now) {
return getLastModified(file).plus(this.age).isBefore(now);
}

@Override
public boolean supportsSingleFileFiltering() {
return true;
}

protected abstract Instant getLastModified(F remoteFile);

protected Duration getAgeDuration() {
return this.age;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2022 the original author or authors.
* 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.
Expand All @@ -18,51 +18,41 @@

import java.io.File;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import org.springframework.lang.Nullable;

/**
* The {@link FileListFilter} implementation to filter those files which
* {@link File#lastModified()} is less than the {@link #age} in comparison
* {@link File#lastModified()} is less than the age in comparison
* with the current time.
* <p>
* The resolution is done in seconds.
* <p>
* When {@link #discardCallback} is provided, it called for all the
* When discardCallback {@link #addDiscardCallback(Consumer)} is provided, it called for all the
* rejected files.
*
* @author Gary Russell
* @author Artem Bilan
* @author Adama Sorho
*
* @since 4.2
*
*/
public class LastModifiedFileListFilter implements DiscardAwareFileListFilter<File> {

private static final long ONE_SECOND = 1000;

private static final long DEFAULT_AGE = 60;

private volatile long age = DEFAULT_AGE;

@Nullable
private Consumer<File> discardCallback;
public class LastModifiedFileListFilter extends AbstractLastModifiedFileListFilter<File> {

public LastModifiedFileListFilter() {
super();
}

/**
* Construct a {@link LastModifiedFileListFilter} instance with provided {@link #age}.
* Construct a {@link LastModifiedFileListFilter} instance with provided age.
* Defaults to 60 seconds.
* @param age the age in seconds.
* @since 5.0
*/
public LastModifiedFileListFilter(long age) {
this.age = age;
super(Duration.ofSeconds(age));
}

/**
Expand All @@ -72,76 +62,27 @@ public LastModifiedFileListFilter(long age) {
* Defaults to 60 seconds.
* @param age the age
* @param unit the timeUnit.
*
* @deprecated since 6.2 in favor of {@link #setAge(Duration)}
*/
@Deprecated(since = "6.2", forRemoval = true)
public void setAge(long age, TimeUnit unit) {
artembilan marked this conversation as resolved.
Show resolved Hide resolved
this.age = unit.toSeconds(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
* is filtered. The resolution is seconds.
* Defaults to 60 seconds.
* @param age the age
* @since 5.1.3
*/
public void setAge(Duration age) {
setAge(age.getSeconds());
setAge(unit.toSeconds(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
* is filtered. The resolution is seconds.
* Defaults to 60 seconds.
* @param age the age
* @return the age in seconds.
*
* @deprecated since 6.2
*/
public void setAge(long age) {
setAge(age, TimeUnit.SECONDS);
}

@Deprecated(since = "6.2", forRemoval = true)
public long getAge() {
artembilan marked this conversation as resolved.
Show resolved Hide resolved
return this.age;
}

@Override
public void addDiscardCallback(@Nullable Consumer<File> discardCallbackToSet) {
this.discardCallback = discardCallbackToSet;
}

@Override
public List<File> filterFiles(File[] files) {
List<File> list = new ArrayList<>();
long now = System.currentTimeMillis() / ONE_SECOND;
for (File file : files) {
if (fileIsAged(file, now)) {
list.add(file);
}
else if (this.discardCallback != null) {
this.discardCallback.accept(file);
}
}
return list;
}

@Override
public boolean accept(File file) {
if (fileIsAged(file, System.currentTimeMillis() / ONE_SECOND)) {
return true;
}
else if (this.discardCallback != null) {
this.discardCallback.accept(file);
}
return false;
}

private boolean fileIsAged(File file, long now) {
return file.lastModified() / ONE_SECOND + this.age <= now;
return getAgeDuration().getSeconds();
}

@Override
public boolean supportsSingleFileFiltering() {
return true;
protected Instant getLastModified(File file) {
return Instant.ofEpochSecond(file.lastModified() / ONE_SECOND);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2022 the original author or authors.
* 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.
Expand All @@ -18,7 +18,6 @@

import java.io.File;
import java.io.FileOutputStream;
import java.util.concurrent.TimeUnit;

import org.junit.Rule;
import org.junit.Test;
Expand All @@ -29,6 +28,7 @@
/**
* @author Gary Russell
* @author Artem Bilan
* @author Adama Sorho
* @since 4.2
*
*/
Expand All @@ -40,7 +40,7 @@ public class LastModifiedFileListFilterTests {
@Test
public void testAge() throws Exception {
LastModifiedFileListFilter filter = new LastModifiedFileListFilter();
filter.setAge(60, TimeUnit.SECONDS);
filter.setAge(60);
File foo = this.folder.newFile();
FileOutputStream fileOutputStream = new FileOutputStream(foo);
fileOutputStream.write("x".getBytes());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 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.ftp.filters;

import java.time.Duration;
import java.time.Instant;
import java.util.function.Consumer;

import org.apache.commons.net.ftp.FTPFile;

import org.springframework.integration.file.filters.AbstractLastModifiedFileListFilter;

/**
* The {@link org.springframework.integration.file.filters.FileListFilter} implementation to filter those files which
* {@link FTPFile#getTimestampInstant()} is less than the age in comparison
* with the {@link Instant#now()}.
* When discardCallback {@link #addDiscardCallback(Consumer)} is provided, it called for all the rejected files.
*
* @author Adama Sorho
*
* @since 6.2
*/
public class FtpLastModifiedFileListFilter extends AbstractLastModifiedFileListFilter<FTPFile> {

public FtpLastModifiedFileListFilter() {
super();
}

/**
* Construct a {@link FtpLastModifiedFileListFilter} instance with provided age.
* Defaults to 60 seconds.
* @param age the age in seconds.
*/
public FtpLastModifiedFileListFilter(long age) {
this(Duration.ofSeconds(age));
}

/**
* Construct a {@link FtpLastModifiedFileListFilter} instance with provided age.
* Defaults to 60 seconds.
* @param age the Duration
*/
public FtpLastModifiedFileListFilter(Duration age) {
super(age);
}

@Override
protected Instant getLastModified(FTPFile remoteFile) {
return remoteFile.getTimestampInstant();
}

}
Loading