From e966cfe41fd8db0e9adf9339deff06535987a3db Mon Sep 17 00:00:00 2001 From: Adama Sorho Date: Fri, 1 Sep 2023 23:56:31 -0500 Subject: [PATCH 01/10] GH-8691: Support age FTP, SMB over time filters --- .../LastModifiedFTPFileListFilter.java | 139 ++++++++++++++++++ .../LastModifiedFTPFileListFilterTests.java | 78 ++++++++++ .../LastModifiedSmbFileListFilter.java | 139 ++++++++++++++++++ .../LastModifiedSmbFileListFilterTests.java | 76 ++++++++++ 4 files changed, 432 insertions(+) create mode 100644 spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilter.java create mode 100644 spring-integration-ftp/src/test/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilterTests.java create mode 100644 spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilter.java create mode 100644 spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilterTests.java diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilter.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilter.java new file mode 100644 index 00000000000..c843384dc72 --- /dev/null +++ b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilter.java @@ -0,0 +1,139 @@ +/* + * 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.ftp.filters; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; + +import org.apache.commons.net.ftp.FTPFile; + +import org.springframework.integration.file.filters.DiscardAwareFileListFilter; +import org.springframework.lang.Nullable; + +/** + * The {@link org.springframework.integration.file.filters.FileListFilter} implementation to filter those files which + * {@link FTPFile#getTimestamp()} is less than the {@link #age} in comparison + * with the current time. + *

+ * The resolution is done in seconds. + *

+ * When {@link #discardCallback} is provided, it called for all the rejected files. + * + * @author Adama Sorho + * + * @since 6.2 + */ +public class LastModifiedFTPFileListFilter implements DiscardAwareFileListFilter { + + private static final long ONE_SECOND = 1000; + private static final long DEFAULT_AGE = 60; + private volatile long age = DEFAULT_AGE; + + @Nullable + private Consumer discardCallback; + + public LastModifiedFTPFileListFilter() { + } + + /** + * Construct a {@link LastModifiedFTPFileListFilter} instance with provided {@link #age}. + * Defaults to 60 seconds. + * @param age the age in seconds. + */ + public LastModifiedFTPFileListFilter(long age) { + this.age = age; + } + + /** + * Set the age that the files have to be before being passed by this filter. + * If {@link FTPFile#getTimestamp()} plus age is greater than the current time, the file + * is filtered. The resolution is seconds. + * Defaults to 60 seconds. + * @param age the age in seconds. + * @param unit the timeUnit. + */ + public void setAge(long age, TimeUnit unit) { + this.age = unit.toSeconds(age); + } + + /** + * Set the age that files have to be before being passed by this filter. + * If {@link FTPFile#getTimestamp()} plus age is greater than the current time, the file + * is filtered. The resolution is seconds. + * Defaults to 60 seconds. + * @param age the age in seconds. + */ + public void setAge(Duration age) { + setAge(age.getSeconds()); + } + + /** + * Set the age that files have to be before being passed by this filter. + * If {@link FTPFile#getTimestamp()} plus age is greater than the current time, the file + * is filtered. The resolution is seconds. + * Defaults to 60 seconds. + * @param age the age in seconds. + */ + public void setAge(long age) { + setAge(age, TimeUnit.SECONDS); + } + + @Override + public void addDiscardCallback(@Nullable Consumer discardCallback) { + this.discardCallback = discardCallback; + } + + @Override + public List filterFiles(FTPFile[] files) { + List list = new ArrayList<>(); + long now = System.currentTimeMillis() / ONE_SECOND; + for (FTPFile file: files) { + if (fileIsAged(file, now)) { + list.add(file); + } + else if (this.discardCallback != null) { + this.discardCallback.accept(file); + } + } + + return list; + } + + @Override + public boolean accept(FTPFile file) { + if (fileIsAged(file, System.currentTimeMillis() / ONE_SECOND)) { + return true; + } + else if (this.discardCallback != null) { + this.discardCallback.accept(file); + } + + return false; + } + + private boolean fileIsAged(FTPFile file, long now) { + return file.getTimestamp().getTimeInMillis() / ONE_SECOND + this.age <= now; + } + + @Override + public boolean supportsSingleFileFiltering() { + return true; + } +} diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilterTests.java b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilterTests.java new file mode 100644 index 00000000000..bf8c877f055 --- /dev/null +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilterTests.java @@ -0,0 +1,78 @@ +/* + * 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.ftp.filters; + +import java.util.Calendar; +import java.util.concurrent.TimeUnit; + +import org.apache.commons.net.ftp.FTPFile; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.integration.file.remote.session.SessionFactory; +import org.springframework.integration.ftp.FtpTestSupport; +import org.springframework.integration.ftp.session.FtpRemoteFileTemplate; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Adama Sorho + * @since 6.2 + */ +@SpringJUnitConfig +@DirtiesContext +public class LastModifiedFTPFileListFilterTests extends FtpTestSupport { + + @Autowired + private FtpRemoteFileTemplate ftpRemoteFileTemplate; + + @Test + public void testAge() { + LastModifiedFTPFileListFilter filter = new LastModifiedFTPFileListFilter(); + filter.setAge(60, TimeUnit.SECONDS); + FTPFile[] files = ftpRemoteFileTemplate.list("ftpSource"); + assertThat(files.length).isGreaterThan(0); + assertThat(filter.filterFiles(files)).hasSize(0); + FTPFile ftpFile = files[1]; + assertThat(filter.accept(ftpFile)).isFalse(); + + // Make a file as of yesterday's + final Calendar calendar = Calendar.getInstance(); + calendar.add(Calendar.DATE, -1); + ftpFile.setTimestamp(calendar); + assertThat(filter.filterFiles(files)).hasSize(1); + assertThat(filter.accept(ftpFile)).isTrue(); + } + + @Configuration + public static class Config { + + @Bean + public SessionFactory ftpFileSessionFactory() { + return LastModifiedFTPFileListFilterTests.sessionFactory(); + } + + @Bean + public FtpRemoteFileTemplate ftpRemoteFileTemplate() { + return new FtpRemoteFileTemplate(ftpFileSessionFactory()); + } + } +} diff --git a/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilter.java b/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilter.java new file mode 100644 index 00000000000..b32a4f6ce81 --- /dev/null +++ b/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilter.java @@ -0,0 +1,139 @@ +/* + * 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.smb.filters; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; + +import jcifs.smb.SmbFile; + +import org.springframework.integration.file.filters.DiscardAwareFileListFilter; +import org.springframework.lang.Nullable; + +/** + * The {@link org.springframework.integration.file.filters.FileListFilter} implementation to filter those files which + * {@link SmbFile#getLastModified()} is less than the {@link #age} in comparison + * with the current time. + *

+ * The resolution is done in seconds. + *

+ * When {@link #discardCallback} is provided, it called for all the rejected files. + * + * @author Adama Sorho + * + * @since 6.2 + */ +public class LastModifiedSmbFileListFilter implements DiscardAwareFileListFilter { + + private static final long ONE_SECOND = 1000; + private static final long DEFAULT_AGE = 60; + private volatile long age = DEFAULT_AGE; + + @Nullable + private Consumer discardCallback; + + public LastModifiedSmbFileListFilter() { + } + + /** + * Construct a {@link LastModifiedSmbFileListFilter} instance with provided {@link #age}. + * Defaults to 60 seconds. + * @param age the age in seconds. + */ + public LastModifiedSmbFileListFilter(long age) { + this.age = age; + } + + /** + * Set the age that the files have to be before being passed by this filter. + * If {@link SmbFile#getLastModified()} plus age is greater than the current time, the file + * is filtered. The resolution is seconds. + * Defaults to 60 seconds. + * @param age the age in seconds. + * @param unit the timeUnit. + */ + public void setAge(long age, TimeUnit unit) { + this.age = unit.toSeconds(age); + } + + /** + * Set the age that the files have to be before being passed by this filter. + * If {@link SmbFile#getLastModified()} plus age is greater than the current time, the file + * is filtered. The resolution is seconds. + * Defaults to 60 seconds. + * @param age the age in seconds. + */ + public void setAge(Duration age) { + setAge(age.getSeconds()); + } + + /** + * Set the age that the files have to be before being passed by this filter. + * If {@link SmbFile#getLastModified()} plus age is greater than the current time, the file + * is filtered. The resolution is seconds. + * Defaults to 60 seconds. + * @param age the age in seconds. + */ + public void setAge(long age) { + setAge(age, TimeUnit.SECONDS); + } + + @Override + public void addDiscardCallback(@Nullable Consumer discardCallback) { + this.discardCallback = discardCallback; + } + + @Override + public List filterFiles(SmbFile[] files) { + List list = new ArrayList<>(); + long now = System.currentTimeMillis() / ONE_SECOND; + for (SmbFile file: files) { + if (fileIsAged(file, now)) { + list.add(file); + } + else if (this.discardCallback != null) { + this.discardCallback.accept(file); + } + } + + return list; + } + + @Override + public boolean accept(SmbFile file) { + if (fileIsAged(file, System.currentTimeMillis() / ONE_SECOND)) { + return true; + } + else if (this.discardCallback != null) { + this.discardCallback.accept(file); + } + + return false; + } + + private boolean fileIsAged(SmbFile file, long now) { + return file.getLastModified() / ONE_SECOND + this.age <= now; + } + + @Override + public boolean supportsSingleFileFiltering() { + return true; + } +} diff --git a/spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilterTests.java b/spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilterTests.java new file mode 100644 index 00000000000..5f8c400b2f3 --- /dev/null +++ b/spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilterTests.java @@ -0,0 +1,76 @@ +/* + * 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.smb.filters; + +import java.util.concurrent.TimeUnit; + +import jcifs.smb.SmbException; +import jcifs.smb.SmbFile; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.integration.file.remote.session.SessionFactory; +import org.springframework.integration.smb.SmbTestSupport; +import org.springframework.integration.smb.session.SmbRemoteFileTemplate; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Adama Sorho + * @since 6.2 + */ +@SpringJUnitConfig +@DirtiesContext +public class LastModifiedSmbFileListFilterTests extends SmbTestSupport { + + @Autowired + private SmbRemoteFileTemplate smbRemoteFileTemplate; + + @Test + public void testAge() throws SmbException { + LastModifiedSmbFileListFilter filter = new LastModifiedSmbFileListFilter(); + filter.setAge(60, TimeUnit.SECONDS); + SmbFile[] files = smbRemoteFileTemplate.list("smbSource"); + assertThat(files.length).isGreaterThan(0); + assertThat(filter.filterFiles(files)).hasSize(0); + SmbFile smbFile = files[1]; + assertThat(filter.accept(smbFile)).isFalse(); + + // Make a file as of yesterday's + smbFile.setLastModified(System.currentTimeMillis() - 1000 * 60 * 60 * 24); + assertThat(filter.filterFiles(files)).hasSize(1); + assertThat(filter.accept(smbFile)).isTrue(); + } + + @Configuration + public static class Config { + + @Bean + public SessionFactory smbFileSessionFactory() { + return LastModifiedSmbFileListFilterTests.sessionFactory(); + } + + @Bean + public SmbRemoteFileTemplate smbRemoteFileTemplate() { + return new SmbRemoteFileTemplate(smbFileSessionFactory()); + } + } +} From 9071998f47ce1d4348b6a87f02eb16d48c7258a5 Mon Sep 17 00:00:00 2001 From: Adama Sorho Date: Sat, 2 Sep 2023 18:36:06 -0500 Subject: [PATCH 02/10] GH-8691: Support age for sftp module --- build.gradle | 2 + .../LastModifiedSftpFileListFilter.java | 138 ++++++++++++++++++ .../LastModifiedSftpFileListFilterTests.java | 76 ++++++++++ 3 files changed, 216 insertions(+) create mode 100644 spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilter.java create mode 100644 spring-integration-sftp/src/test/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilterTests.java diff --git a/build.gradle b/build.gradle index 880a1994092..bc583e571bd 100644 --- a/build.gradle +++ b/build.gradle @@ -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' } } diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilter.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilter.java new file mode 100644 index 00000000000..9ab2f601ce7 --- /dev/null +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilter.java @@ -0,0 +1,138 @@ +/* + * Copyright 2013-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.sftp.filters; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; + +import org.apache.sshd.sftp.client.SftpClient; + +import org.springframework.integration.file.filters.DiscardAwareFileListFilter; +import org.springframework.lang.Nullable; + +/** + * The {@link org.springframework.integration.file.filters.FileListFilter} implementation to filter those files which + * {@link SftpClient.Attributes#getModifyTime()} is less than the {@link #age} in comparison + * with the current time. + *

+ * The resolution is done in seconds. + *

+ * When {@link #discardCallback} is provided, it called for all the rejected files. + * + * @author Adama Sorho + * + * @since 6.2 + */ +public class LastModifiedSftpFileListFilter implements DiscardAwareFileListFilter { + private static final long ONE_SECOND = 1000; + private static final long DEFAULT_AGE = 60; + private volatile long age = DEFAULT_AGE; + + @Nullable + private Consumer discardCallback; + + public LastModifiedSftpFileListFilter() { + } + + /** + * Construct a {@link LastModifiedSftpFileListFilter} instance with provided {@link #age}. + * Defaults to 60 seconds. + * @param age the age in seconds. + */ + public LastModifiedSftpFileListFilter(long age) { + this.age = age; + } + + /** + * Set the age that the files have to be before being passed by this filter. + * If {@link SftpClient.Attributes#getModifyTime()} plus age is greater than the current time, the file + * is filtered. The resolution is seconds. + * Defaults to 60 seconds. + * @param age the age in seconds. + * @param unit the timeUnit. + */ + public void setAge(long age, TimeUnit unit) { + this.age = unit.toSeconds(age); + } + + /** + * Set the age that the files have to be before being passed by this filter. + * If {@link SftpClient.Attributes#getModifyTime()} plus age is greater than the current time, the file + * is filtered. The resolution is seconds. + * Defaults to 60 seconds. + * @param age the age in seconds. + */ + public void setAge(Duration age) { + setAge(age.getSeconds()); + } + + /** + * Set the age that the files have to be before being passed by this filter. + * If {@link SftpClient.Attributes#getModifyTime()} plus age is greater than the current time, the file + * is filtered. The resolution is seconds. + * Defaults to 60 seconds. + * @param age the age in seconds. + */ + public void setAge(long age) { + setAge(age, TimeUnit.SECONDS); + } + + @Override + public void addDiscardCallback(@Nullable Consumer discardCallback) { + this.discardCallback = discardCallback; + } + + @Override + public List filterFiles(SftpClient.DirEntry[] files) { + List list = new ArrayList<>(); + long now = System.currentTimeMillis() / ONE_SECOND; + for (SftpClient.DirEntry file: files) { + if (fileIsAged(file, now)) { + list.add(file); + } + else if (this.discardCallback != null) { + this.discardCallback.accept(file); + } + } + + return list; + } + + @Override + public boolean accept(SftpClient.DirEntry file) { + if (fileIsAged(file, System.currentTimeMillis() / ONE_SECOND)) { + return true; + } + else if (this.discardCallback != null) { + this.discardCallback.accept(file); + } + + return false; + } + + private boolean fileIsAged(SftpClient.DirEntry file, long now) { + return file.getAttributes().getModifyTime().to(TimeUnit.SECONDS) + this.age <= now; + } + + @Override + public boolean supportsSingleFileFiltering() { + return true; + } +} diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilterTests.java b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilterTests.java new file mode 100644 index 00000000000..baf72bbb566 --- /dev/null +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilterTests.java @@ -0,0 +1,76 @@ +/* + * 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.sftp.filters; + +import java.nio.file.attribute.FileTime; + +import org.apache.sshd.sftp.client.SftpClient; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.integration.file.remote.session.SessionFactory; +import org.springframework.integration.sftp.SftpTestSupport; +import org.springframework.integration.sftp.session.SftpRemoteFileTemplate; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Adama Sorho + * @since 6.2 + */ +@SpringJUnitConfig +@DirtiesContext +public class LastModifiedSftpFileListFilterTests extends SftpTestSupport { + + @Autowired + private SftpRemoteFileTemplate sftpRemoteFileTemplate; + + @Test + public void testAge() { + LastModifiedSftpFileListFilter filter = new LastModifiedSftpFileListFilter(); + filter.setAge(60); + SftpClient.DirEntry[] files = sftpRemoteFileTemplate.list("sftpSource"); + assertThat(files.length).isGreaterThan(0); + assertThat(filter.filterFiles(files)).hasSize(0); + SftpClient.DirEntry sftFile = files[1]; + assertThat(filter.accept(sftFile)).isFalse(); + + // Make a file as of yesterday's + final FileTime fileTime = FileTime.fromMillis(System.currentTimeMillis() - 1000 * 60 * 60 * 24); + sftFile.getAttributes().setModifyTime(fileTime); + assertThat(filter.filterFiles(files)).hasSize(1); + assertThat(filter.accept(sftFile)).isTrue(); + } + + @Configuration + public static class Config { + + @Bean + public SessionFactory sftpFileSessionFactory() { + return LastModifiedSftpFileListFilterTests.sessionFactory(); + } + + @Bean + public SftpRemoteFileTemplate sftpRemoteFileTemplate() { + return new SftpRemoteFileTemplate(sftpFileSessionFactory()); + } + } +} From fd6ec9e2d5253d206b5ac1795a44328a3736fd4f Mon Sep 17 00:00:00 2001 From: Adama Sorho Date: Thu, 7 Sep 2023 02:33:45 -0500 Subject: [PATCH 03/10] GH-8691: update tests and class names --- ...ava => FtpLastModifiedFileListFilter.java} | 58 ++++++++------ .../FtpLastModifiedFileListFilterTests.java | 56 +++++++++++++ .../LastModifiedFTPFileListFilterTests.java | 78 ------------------- ...va => SftpLastModifiedFileListFilter.java} | 55 +++++++------ .../LastModifiedSftpFileListFilterTests.java | 76 ------------------ .../SftpLastModifiedFileListFilterTests.java | 57 ++++++++++++++ ...ava => SmbLastModifiedFileListFilter.java} | 13 ++-- .../LastModifiedSmbFileListFilterTests.java | 76 ------------------ .../SmbLastModifiedFileListFilterTests.java | 52 +++++++++++++ 9 files changed, 236 insertions(+), 285 deletions(-) rename spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/{LastModifiedFTPFileListFilter.java => FtpLastModifiedFileListFilter.java} (66%) create mode 100644 spring-integration-ftp/src/test/java/org/springframework/integration/ftp/filters/FtpLastModifiedFileListFilterTests.java delete mode 100644 spring-integration-ftp/src/test/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilterTests.java rename spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/{LastModifiedSftpFileListFilter.java => SftpLastModifiedFileListFilter.java} (68%) delete mode 100644 spring-integration-sftp/src/test/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilterTests.java create mode 100644 spring-integration-sftp/src/test/java/org/springframework/integration/sftp/filters/SftpLastModifiedFileListFilterTests.java rename spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/{LastModifiedSmbFileListFilter.java => SmbLastModifiedFileListFilter.java} (93%) delete mode 100644 spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilterTests.java create mode 100644 spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilterTests.java diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilter.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/FtpLastModifiedFileListFilter.java similarity index 66% rename from spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilter.java rename to spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/FtpLastModifiedFileListFilter.java index c843384dc72..3b62053bf20 100644 --- a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilter.java +++ b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/FtpLastModifiedFileListFilter.java @@ -17,6 +17,7 @@ package org.springframework.integration.ftp.filters; import java.time.Duration; +import java.time.Instant; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; @@ -29,70 +30,76 @@ /** * The {@link org.springframework.integration.file.filters.FileListFilter} implementation to filter those files which - * {@link FTPFile#getTimestamp()} is less than the {@link #age} in comparison - * with the current time. - *

- * The resolution is done in seconds. - *

+ * {@link FTPFile#getTimestampInstant()} 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. * * @author Adama Sorho * * @since 6.2 */ -public class LastModifiedFTPFileListFilter implements DiscardAwareFileListFilter { +public class FtpLastModifiedFileListFilter implements DiscardAwareFileListFilter { - private static final long ONE_SECOND = 1000; private static final long DEFAULT_AGE = 60; - private volatile long age = DEFAULT_AGE; + + private Duration age = Duration.ofSeconds(DEFAULT_AGE); @Nullable private Consumer discardCallback; - public LastModifiedFTPFileListFilter() { + public FtpLastModifiedFileListFilter() { } /** - * Construct a {@link LastModifiedFTPFileListFilter} instance with provided {@link #age}. + * Construct a {@link FtpLastModifiedFileListFilter} instance with provided {@link #age}. * Defaults to 60 seconds. * @param age the age in seconds. */ - public LastModifiedFTPFileListFilter(long age) { + public FtpLastModifiedFileListFilter(long age) { + this(Duration.ofSeconds(age)); + } + + /** + * Construct a {@link FtpLastModifiedFileListFilter} instance with provided {@link #age}. + * Defaults to 60 seconds. + * @param age the Duration + */ + public FtpLastModifiedFileListFilter(Duration age) { this.age = age; } /** * Set the age that the files have to be before being passed by this filter. - * If {@link FTPFile#getTimestamp()} plus age is greater than the current time, the file - * is filtered. The resolution is seconds. + * If {@link FTPFile#getTimestampInstant()} plus {@link #age} is before the {@link Instant#now()}, the file + * is filtered. * Defaults to 60 seconds. * @param age the age in seconds. * @param unit the timeUnit. */ public void setAge(long age, TimeUnit unit) { - this.age = unit.toSeconds(age); + setAge(Duration.ofSeconds(unit.toSeconds(age))); } /** * Set the age that files have to be before being passed by this filter. - * If {@link FTPFile#getTimestamp()} plus age is greater than the current time, the file - * is filtered. The resolution is seconds. + * If {@link FTPFile#getTimestampInstant()} plus {@link #age} is before the {@link Instant#now()}, the file + * is filtered. * Defaults to 60 seconds. - * @param age the age in seconds. + * @param age the Duration. */ public void setAge(Duration age) { - setAge(age.getSeconds()); + this.age = age; } /** * Set the age that files have to be before being passed by this filter. - * If {@link FTPFile#getTimestamp()} plus age is greater than the current time, the file - * is filtered. The resolution is seconds. + * If {@link FTPFile#getTimestampInstant()} 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(age, TimeUnit.SECONDS); + setAge(Duration.ofSeconds(age)); } @Override @@ -103,7 +110,7 @@ public void addDiscardCallback(@Nullable Consumer discardCallback) { @Override public List filterFiles(FTPFile[] files) { List list = new ArrayList<>(); - long now = System.currentTimeMillis() / ONE_SECOND; + Instant now = Instant.now(); for (FTPFile file: files) { if (fileIsAged(file, now)) { list.add(file); @@ -118,7 +125,7 @@ else if (this.discardCallback != null) { @Override public boolean accept(FTPFile file) { - if (fileIsAged(file, System.currentTimeMillis() / ONE_SECOND)) { + if (fileIsAged(file, Instant.now())) { return true; } else if (this.discardCallback != null) { @@ -128,12 +135,13 @@ else if (this.discardCallback != null) { return false; } - private boolean fileIsAged(FTPFile file, long now) { - return file.getTimestamp().getTimeInMillis() / ONE_SECOND + this.age <= now; + private boolean fileIsAged(FTPFile file, Instant now) { + return file.getTimestampInstant().plus(this.age).isBefore(now); } @Override public boolean supportsSingleFileFiltering() { return true; } + } diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/filters/FtpLastModifiedFileListFilterTests.java b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/filters/FtpLastModifiedFileListFilterTests.java new file mode 100644 index 00000000000..e2591ed1256 --- /dev/null +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/filters/FtpLastModifiedFileListFilterTests.java @@ -0,0 +1,56 @@ +/* + * 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.ftp.filters; + +import java.util.Calendar; +import java.util.concurrent.TimeUnit; + +import org.apache.commons.net.ftp.FTPFile; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Adama Sorho + * @since 6.2 + */ +public class FtpLastModifiedFileListFilterTests { + + @Test + public void testAge() { + FtpLastModifiedFileListFilter filter = new FtpLastModifiedFileListFilter(); + filter.setAge(60, TimeUnit.SECONDS); + FTPFile ftpFile1 = new FTPFile(); + ftpFile1.setName("foo"); + ftpFile1.setTimestamp(Calendar.getInstance()); + FTPFile ftpFile2 = new FTPFile(); + ftpFile2.setName("bar"); + ftpFile2.setTimestamp(Calendar.getInstance()); + FTPFile[] files = new FTPFile[] {ftpFile1, ftpFile2}; + assertThat(filter.filterFiles(files)).hasSize(0); + assertThat(filter.accept(ftpFile2)).isFalse(); + + // Make a file as of yesterday's + final Calendar calendar = Calendar.getInstance(); + calendar.add(Calendar.DATE, -1); + ftpFile2.setTimestamp(calendar); + + assertThat(filter.filterFiles(files)).hasSize(1); + assertThat(filter.accept(ftpFile2)).isTrue(); + } + +} diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilterTests.java b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilterTests.java deleted file mode 100644 index bf8c877f055..00000000000 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilterTests.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * 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.ftp.filters; - -import java.util.Calendar; -import java.util.concurrent.TimeUnit; - -import org.apache.commons.net.ftp.FTPFile; -import org.junit.jupiter.api.Test; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.integration.file.remote.session.SessionFactory; -import org.springframework.integration.ftp.FtpTestSupport; -import org.springframework.integration.ftp.session.FtpRemoteFileTemplate; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * @author Adama Sorho - * @since 6.2 - */ -@SpringJUnitConfig -@DirtiesContext -public class LastModifiedFTPFileListFilterTests extends FtpTestSupport { - - @Autowired - private FtpRemoteFileTemplate ftpRemoteFileTemplate; - - @Test - public void testAge() { - LastModifiedFTPFileListFilter filter = new LastModifiedFTPFileListFilter(); - filter.setAge(60, TimeUnit.SECONDS); - FTPFile[] files = ftpRemoteFileTemplate.list("ftpSource"); - assertThat(files.length).isGreaterThan(0); - assertThat(filter.filterFiles(files)).hasSize(0); - FTPFile ftpFile = files[1]; - assertThat(filter.accept(ftpFile)).isFalse(); - - // Make a file as of yesterday's - final Calendar calendar = Calendar.getInstance(); - calendar.add(Calendar.DATE, -1); - ftpFile.setTimestamp(calendar); - assertThat(filter.filterFiles(files)).hasSize(1); - assertThat(filter.accept(ftpFile)).isTrue(); - } - - @Configuration - public static class Config { - - @Bean - public SessionFactory ftpFileSessionFactory() { - return LastModifiedFTPFileListFilterTests.sessionFactory(); - } - - @Bean - public FtpRemoteFileTemplate ftpRemoteFileTemplate() { - return new FtpRemoteFileTemplate(ftpFileSessionFactory()); - } - } -} diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilter.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/SftpLastModifiedFileListFilter.java similarity index 68% rename from spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilter.java rename to spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/SftpLastModifiedFileListFilter.java index 9ab2f601ce7..8d1c8359a78 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilter.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/SftpLastModifiedFileListFilter.java @@ -16,7 +16,9 @@ package org.springframework.integration.sftp.filters; +import java.nio.file.attribute.FileTime; import java.time.Duration; +import java.time.Instant; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; @@ -29,69 +31,71 @@ /** * The {@link org.springframework.integration.file.filters.FileListFilter} implementation to filter those files which - * {@link SftpClient.Attributes#getModifyTime()} is less than the {@link #age} in comparison - * with the current time. - *

- * The resolution is done in seconds. - *

+ * {@link FileTime#toInstant()} 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. * * @author Adama Sorho * * @since 6.2 */ -public class LastModifiedSftpFileListFilter implements DiscardAwareFileListFilter { - private static final long ONE_SECOND = 1000; +public class SftpLastModifiedFileListFilter implements DiscardAwareFileListFilter { + private static final long DEFAULT_AGE = 60; - private volatile long age = DEFAULT_AGE; + + private Duration age = Duration.ofSeconds(DEFAULT_AGE); @Nullable private Consumer discardCallback; - public LastModifiedSftpFileListFilter() { + public SftpLastModifiedFileListFilter() { } /** - * Construct a {@link LastModifiedSftpFileListFilter} instance with provided {@link #age}. + * Construct a {@link SftpLastModifiedFileListFilter} instance with provided {@link #age}. * Defaults to 60 seconds. * @param age the age in seconds. */ - public LastModifiedSftpFileListFilter(long age) { + public SftpLastModifiedFileListFilter(long age) { + this(Duration.ofSeconds(age)); + } + + public SftpLastModifiedFileListFilter(Duration age) { this.age = age; } /** * Set the age that the files have to be before being passed by this filter. - * If {@link SftpClient.Attributes#getModifyTime()} plus age is greater than the current time, the file - * is filtered. The resolution is seconds. + * If {@link FileTime#toInstant()} plus {@link #age} is before the {@link Instant#now()}, the file + * is filtered. * Defaults to 60 seconds. * @param age the age in seconds. * @param unit the timeUnit. */ public void setAge(long age, TimeUnit unit) { - this.age = unit.toSeconds(age); + setAge(Duration.ofSeconds(unit.toSeconds(age))); } /** * Set the age that the files have to be before being passed by this filter. - * If {@link SftpClient.Attributes#getModifyTime()} plus age is greater than the current time, the file - * is filtered. The resolution is seconds. + * If {@link FileTime#toInstant()} plus {@link #age} is before the {@link Instant#now()}, the file + * is filtered. * Defaults to 60 seconds. - * @param age the age in seconds. + * @param age The Duration. */ public void setAge(Duration age) { - setAge(age.getSeconds()); + this.age = age; } /** * Set the age that the files have to be before being passed by this filter. - * If {@link SftpClient.Attributes#getModifyTime()} plus age is greater than the current time, the file - * is filtered. The resolution is seconds. + * If {@link FileTime#toInstant()} 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(age, TimeUnit.SECONDS); + setAge(Duration.ofSeconds(age)); } @Override @@ -102,7 +106,7 @@ public void addDiscardCallback(@Nullable Consumer discardCa @Override public List filterFiles(SftpClient.DirEntry[] files) { List list = new ArrayList<>(); - long now = System.currentTimeMillis() / ONE_SECOND; + Instant now = Instant.now(); for (SftpClient.DirEntry file: files) { if (fileIsAged(file, now)) { list.add(file); @@ -117,7 +121,7 @@ else if (this.discardCallback != null) { @Override public boolean accept(SftpClient.DirEntry file) { - if (fileIsAged(file, System.currentTimeMillis() / ONE_SECOND)) { + if (fileIsAged(file, Instant.now())) { return true; } else if (this.discardCallback != null) { @@ -127,12 +131,13 @@ else if (this.discardCallback != null) { return false; } - private boolean fileIsAged(SftpClient.DirEntry file, long now) { - return file.getAttributes().getModifyTime().to(TimeUnit.SECONDS) + this.age <= now; + private boolean fileIsAged(SftpClient.DirEntry file, Instant now) { + return file.getAttributes().getModifyTime().toInstant().plus(this.age).isBefore(now); } @Override public boolean supportsSingleFileFiltering() { return true; } + } diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilterTests.java b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilterTests.java deleted file mode 100644 index baf72bbb566..00000000000 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilterTests.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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.sftp.filters; - -import java.nio.file.attribute.FileTime; - -import org.apache.sshd.sftp.client.SftpClient; -import org.junit.jupiter.api.Test; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.integration.file.remote.session.SessionFactory; -import org.springframework.integration.sftp.SftpTestSupport; -import org.springframework.integration.sftp.session.SftpRemoteFileTemplate; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * @author Adama Sorho - * @since 6.2 - */ -@SpringJUnitConfig -@DirtiesContext -public class LastModifiedSftpFileListFilterTests extends SftpTestSupport { - - @Autowired - private SftpRemoteFileTemplate sftpRemoteFileTemplate; - - @Test - public void testAge() { - LastModifiedSftpFileListFilter filter = new LastModifiedSftpFileListFilter(); - filter.setAge(60); - SftpClient.DirEntry[] files = sftpRemoteFileTemplate.list("sftpSource"); - assertThat(files.length).isGreaterThan(0); - assertThat(filter.filterFiles(files)).hasSize(0); - SftpClient.DirEntry sftFile = files[1]; - assertThat(filter.accept(sftFile)).isFalse(); - - // Make a file as of yesterday's - final FileTime fileTime = FileTime.fromMillis(System.currentTimeMillis() - 1000 * 60 * 60 * 24); - sftFile.getAttributes().setModifyTime(fileTime); - assertThat(filter.filterFiles(files)).hasSize(1); - assertThat(filter.accept(sftFile)).isTrue(); - } - - @Configuration - public static class Config { - - @Bean - public SessionFactory sftpFileSessionFactory() { - return LastModifiedSftpFileListFilterTests.sessionFactory(); - } - - @Bean - public SftpRemoteFileTemplate sftpRemoteFileTemplate() { - return new SftpRemoteFileTemplate(sftpFileSessionFactory()); - } - } -} diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/filters/SftpLastModifiedFileListFilterTests.java b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/filters/SftpLastModifiedFileListFilterTests.java new file mode 100644 index 00000000000..5d8608bad55 --- /dev/null +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/filters/SftpLastModifiedFileListFilterTests.java @@ -0,0 +1,57 @@ +/* + * 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.sftp.filters; + +import java.nio.file.attribute.FileTime; +import java.time.Instant; + +import org.apache.sshd.sftp.client.SftpClient; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Adama Sorho + * @since 6.2 + */ +public class SftpLastModifiedFileListFilterTests { + + @Test + public void testAge() { + SftpLastModifiedFileListFilter filter = new SftpLastModifiedFileListFilter(); + filter.setAge(60); + SftpClient.Attributes attributes1 = new SftpClient.Attributes(); + attributes1.setModifyTime(FileTime.from(Instant.now())); + SftpClient.Attributes attributes2 = new SftpClient.Attributes(); + attributes2.setModifyTime(FileTime.from(Instant.now())); + + SftpClient.DirEntry sftpFile1 = new SftpClient.DirEntry("foo", "foo", attributes1); + SftpClient.DirEntry sftpFile2 = new SftpClient.DirEntry("bar", "bar", attributes2); + SftpClient.DirEntry[] files = new SftpClient.DirEntry[] {sftpFile1, sftpFile2}; + + assertThat(filter.filterFiles(files)).hasSize(0); + assertThat(filter.accept(sftpFile2)).isFalse(); + + + // Make a file as of yesterday's + final FileTime fileTime = FileTime.fromMillis(System.currentTimeMillis() - 1000 * 60 * 60 * 24); + sftpFile2.getAttributes().setModifyTime(fileTime); + assertThat(filter.filterFiles(files)).hasSize(1); + assertThat(filter.accept(sftpFile2)).isTrue(); + } + +} diff --git a/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilter.java b/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilter.java similarity index 93% rename from spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilter.java rename to spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilter.java index b32a4f6ce81..1bc8cd7f8c2 100644 --- a/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilter.java +++ b/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilter.java @@ -40,24 +40,26 @@ * * @since 6.2 */ -public class LastModifiedSmbFileListFilter implements DiscardAwareFileListFilter { +public class SmbLastModifiedFileListFilter implements DiscardAwareFileListFilter { private static final long ONE_SECOND = 1000; + private static final long DEFAULT_AGE = 60; - private volatile long age = DEFAULT_AGE; + + private long age = DEFAULT_AGE; @Nullable private Consumer discardCallback; - public LastModifiedSmbFileListFilter() { + public SmbLastModifiedFileListFilter() { } /** - * Construct a {@link LastModifiedSmbFileListFilter} instance with provided {@link #age}. + * Construct a {@link SmbLastModifiedFileListFilter} instance with provided {@link #age}. * Defaults to 60 seconds. * @param age the age in seconds. */ - public LastModifiedSmbFileListFilter(long age) { + public SmbLastModifiedFileListFilter(long age) { this.age = age; } @@ -136,4 +138,5 @@ private boolean fileIsAged(SmbFile file, long now) { public boolean supportsSingleFileFiltering() { return true; } + } diff --git a/spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilterTests.java b/spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilterTests.java deleted file mode 100644 index 5f8c400b2f3..00000000000 --- a/spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilterTests.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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.smb.filters; - -import java.util.concurrent.TimeUnit; - -import jcifs.smb.SmbException; -import jcifs.smb.SmbFile; -import org.junit.jupiter.api.Test; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.integration.file.remote.session.SessionFactory; -import org.springframework.integration.smb.SmbTestSupport; -import org.springframework.integration.smb.session.SmbRemoteFileTemplate; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * @author Adama Sorho - * @since 6.2 - */ -@SpringJUnitConfig -@DirtiesContext -public class LastModifiedSmbFileListFilterTests extends SmbTestSupport { - - @Autowired - private SmbRemoteFileTemplate smbRemoteFileTemplate; - - @Test - public void testAge() throws SmbException { - LastModifiedSmbFileListFilter filter = new LastModifiedSmbFileListFilter(); - filter.setAge(60, TimeUnit.SECONDS); - SmbFile[] files = smbRemoteFileTemplate.list("smbSource"); - assertThat(files.length).isGreaterThan(0); - assertThat(filter.filterFiles(files)).hasSize(0); - SmbFile smbFile = files[1]; - assertThat(filter.accept(smbFile)).isFalse(); - - // Make a file as of yesterday's - smbFile.setLastModified(System.currentTimeMillis() - 1000 * 60 * 60 * 24); - assertThat(filter.filterFiles(files)).hasSize(1); - assertThat(filter.accept(smbFile)).isTrue(); - } - - @Configuration - public static class Config { - - @Bean - public SessionFactory smbFileSessionFactory() { - return LastModifiedSmbFileListFilterTests.sessionFactory(); - } - - @Bean - public SmbRemoteFileTemplate smbRemoteFileTemplate() { - return new SmbRemoteFileTemplate(smbFileSessionFactory()); - } - } -} diff --git a/spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilterTests.java b/spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilterTests.java new file mode 100644 index 00000000000..dc8bc5e6423 --- /dev/null +++ b/spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilterTests.java @@ -0,0 +1,52 @@ +/* + * 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.smb.filters; + +import java.util.concurrent.TimeUnit; + +import jcifs.smb.SmbFile; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * @author Adama Sorho + * @since 6.2 + */ +public class SmbLastModifiedFileListFilterTests { + + @Test + public void testAge() { + SmbLastModifiedFileListFilter filter = new SmbLastModifiedFileListFilter(); + filter.setAge(60, TimeUnit.SECONDS); + SmbFile smbFile1 = mock(SmbFile.class); + when(smbFile1.getLastModified()).thenReturn(System.currentTimeMillis()); + SmbFile smbFile2 = mock(SmbFile.class); + when(smbFile2.getLastModified()).thenReturn(System.currentTimeMillis()); + SmbFile smbFile3 = mock(SmbFile.class); + + // Make a file as of yesterday's + when(smbFile3.getLastModified()).thenReturn(System.currentTimeMillis() - 1000 * 60 * 60 * 24); + SmbFile[] files = new SmbFile[] {smbFile1, smbFile2, smbFile3}; + assertThat(filter.filterFiles(files)).hasSize(1); + assertThat(filter.accept(smbFile1)).isFalse(); + assertThat(filter.accept(smbFile3)).isTrue(); + } + +} From 66c1315f49b280c174877a08cf1268c38c22a718 Mon Sep 17 00:00:00 2001 From: Adama Sorho Date: Mon, 11 Sep 2023 14:09:36 -0500 Subject: [PATCH 04/10] GH-8691: Remove setAge with TimeUnit Turned out we don't need anymore since we're using Duration for age in FtpLastModifiedFileListFilter and SftpLastModifiedFileListFilter. --- .../filters/FtpLastModifiedFileListFilter.java | 15 +-------------- .../FtpLastModifiedFileListFilterTests.java | 4 +--- .../filters/SftpLastModifiedFileListFilter.java | 15 +-------------- .../SftpLastModifiedFileListFilterTests.java | 3 +-- .../filters/SmbLastModifiedFileListFilter.java | 2 +- .../SmbLastModifiedFileListFilterTests.java | 4 ++-- 6 files changed, 7 insertions(+), 36 deletions(-) diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/FtpLastModifiedFileListFilter.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/FtpLastModifiedFileListFilter.java index 3b62053bf20..b24a32fc8e1 100644 --- a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/FtpLastModifiedFileListFilter.java +++ b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/FtpLastModifiedFileListFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2023 the original author or authors. + * 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. @@ -20,7 +20,6 @@ import java.time.Instant; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.TimeUnit; import java.util.function.Consumer; import org.apache.commons.net.ftp.FTPFile; @@ -68,18 +67,6 @@ public FtpLastModifiedFileListFilter(Duration age) { this.age = age; } - /** - * Set the age that the files have to be before being passed by this filter. - * If {@link FTPFile#getTimestampInstant()} plus {@link #age} is before the {@link Instant#now()}, the file - * is filtered. - * Defaults to 60 seconds. - * @param age the age in seconds. - * @param unit the timeUnit. - */ - public void setAge(long age, TimeUnit unit) { - setAge(Duration.ofSeconds(unit.toSeconds(age))); - } - /** * Set the age that files have to be before being passed by this filter. * If {@link FTPFile#getTimestampInstant()} plus {@link #age} is before the {@link Instant#now()}, the file diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/filters/FtpLastModifiedFileListFilterTests.java b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/filters/FtpLastModifiedFileListFilterTests.java index e2591ed1256..b0a9465fff6 100644 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/filters/FtpLastModifiedFileListFilterTests.java +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/filters/FtpLastModifiedFileListFilterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2023 the original author or authors. + * 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. @@ -17,7 +17,6 @@ package org.springframework.integration.ftp.filters; import java.util.Calendar; -import java.util.concurrent.TimeUnit; import org.apache.commons.net.ftp.FTPFile; import org.junit.jupiter.api.Test; @@ -33,7 +32,6 @@ public class FtpLastModifiedFileListFilterTests { @Test public void testAge() { FtpLastModifiedFileListFilter filter = new FtpLastModifiedFileListFilter(); - filter.setAge(60, TimeUnit.SECONDS); FTPFile ftpFile1 = new FTPFile(); ftpFile1.setName("foo"); ftpFile1.setTimestamp(Calendar.getInstance()); diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/SftpLastModifiedFileListFilter.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/SftpLastModifiedFileListFilter.java index 8d1c8359a78..fe7a33dc745 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/SftpLastModifiedFileListFilter.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/SftpLastModifiedFileListFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2023 the original author or authors. + * 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. @@ -21,7 +21,6 @@ import java.time.Instant; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.TimeUnit; import java.util.function.Consumer; import org.apache.sshd.sftp.client.SftpClient; @@ -64,18 +63,6 @@ public SftpLastModifiedFileListFilter(Duration age) { this.age = age; } - /** - * Set the age that the files have to be before being passed by this filter. - * If {@link FileTime#toInstant()} plus {@link #age} is before the {@link Instant#now()}, the file - * is filtered. - * Defaults to 60 seconds. - * @param age the age in seconds. - * @param unit the timeUnit. - */ - public void setAge(long age, TimeUnit unit) { - setAge(Duration.ofSeconds(unit.toSeconds(age))); - } - /** * Set the age that the files have to be before being passed by this filter. * If {@link FileTime#toInstant()} plus {@link #age} is before the {@link Instant#now()}, the file diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/filters/SftpLastModifiedFileListFilterTests.java b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/filters/SftpLastModifiedFileListFilterTests.java index 5d8608bad55..2dca9440153 100644 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/filters/SftpLastModifiedFileListFilterTests.java +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/filters/SftpLastModifiedFileListFilterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2023 the original author or authors. + * 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. @@ -33,7 +33,6 @@ public class SftpLastModifiedFileListFilterTests { @Test public void testAge() { SftpLastModifiedFileListFilter filter = new SftpLastModifiedFileListFilter(); - filter.setAge(60); SftpClient.Attributes attributes1 = new SftpClient.Attributes(); attributes1.setModifyTime(FileTime.from(Instant.now())); SftpClient.Attributes attributes2 = new SftpClient.Attributes(); diff --git a/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilter.java b/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilter.java index 1bc8cd7f8c2..1847887f97a 100644 --- a/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilter.java +++ b/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2023 the original author or authors. + * 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. diff --git a/spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilterTests.java b/spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilterTests.java index dc8bc5e6423..78ca7dc4ed3 100644 --- a/spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilterTests.java +++ b/spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2023 the original author or authors. + * 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. @@ -34,7 +34,7 @@ public class SmbLastModifiedFileListFilterTests { @Test public void testAge() { SmbLastModifiedFileListFilter filter = new SmbLastModifiedFileListFilter(); - filter.setAge(60, TimeUnit.SECONDS); + filter.setAge(80, TimeUnit.SECONDS); SmbFile smbFile1 = mock(SmbFile.class); when(smbFile1.getLastModified()).thenReturn(System.currentTimeMillis()); SmbFile smbFile2 = mock(SmbFile.class); From dbc3e1d169319950ad50384eb512689431a38514 Mon Sep 17 00:00:00 2001 From: Adama Sorho Date: Mon, 11 Sep 2023 15:27:59 -0500 Subject: [PATCH 05/10] GH-8691: Add changes to the docs --- .../antora/modules/ROOT/pages/ftp/inbound.adoc | 17 +++++++++++++++++ .../antora/modules/ROOT/pages/sftp/inbound.adoc | 17 +++++++++++++++++ .../antora/modules/ROOT/pages/smb.adoc | 17 +++++++++++++++++ .../antora/modules/ROOT/pages/whats-new.adoc | 6 ++++++ 4 files changed, 57 insertions(+) diff --git a/src/reference/antora/modules/ROOT/pages/ftp/inbound.adoc b/src/reference/antora/modules/ROOT/pages/ftp/inbound.adoc index 9541ea524b2..98429ecbc5d 100644 --- a/src/reference/antora/modules/ROOT/pages/ftp/inbound.adoc +++ b/src/reference/antora/modules/ROOT/pages/ftp/inbound.adoc @@ -98,6 +98,23 @@ You should also understand that the FTP inbound channel adapter is a polling con Therefore, you must configure a poller (by using either a global default or a local sub-element). Once a file has been transferred, a message with a `java.io.File` as its payload is generated and sent to the channel identified by the `channel` attribute. +Starting with version 6.2, you can filter FTP files based on last-modified strategy using `FtpLastModifiedFileListFilter`. +This filter can be configured with an `age` property so that only files older than this value are passed by the filter. +The age defaults to 60 seconds, but you should choose an age that is large enough to avoid picking up a file early (due to, say, network glitches). +The following example shows how to configure a `FtpLastModifiedFileListFilter`: + +[source, java] +---- +@Bean +public FtpLastModifiedFileListFilter ftpLastModifiedFileListFilter() { + FtpLastModifiedFileListFilter filter = new FtpLastModifiedFileListFilter(); + + filter.setAge(Duration.ofSeconds(180)); + + return filter; +} +---- + [[more-on-file-filtering-and-incomplete-files]] == More on File Filtering and Incomplete Files diff --git a/src/reference/antora/modules/ROOT/pages/sftp/inbound.adoc b/src/reference/antora/modules/ROOT/pages/sftp/inbound.adoc index f4578152e1b..783983059a1 100644 --- a/src/reference/antora/modules/ROOT/pages/sftp/inbound.adoc +++ b/src/reference/antora/modules/ROOT/pages/sftp/inbound.adoc @@ -95,6 +95,23 @@ SFTP inbound channel adapter is a polling consumer. Therefore, you must configure a poller (either a global default or a local element). Once the file has been transferred to a local directory, a message with `java.io.File` as its payload type is generated and sent to the channel identified by the `channel` attribute. +Starting with version 6.2, you can filter SFTP files based on last-modified strategy using `SftpLastModifiedFileListFilter`. +This filter can be configured with an `age` property so that only files older than this value are passed by the filter. +The age defaults to 60 seconds, but you should choose an age that is large enough to avoid picking up a file early (due to, say, network glitches). +The following example shows how to configure a `SftpLastModifiedFileListFilter`: + +[source, java] +---- +@Bean +public SftpLastModifiedFileListFilter sftpLastModifiedFileListFilter() { + SftpLastModifiedFileListFilter filter = new SftpLastModifiedFileListFilter(); + + filter.setAge(Duration.ofSeconds(180)); + + return filter; +} +---- + [[more-on-file-filtering-and-large-files]] == More on File Filtering and Large Files diff --git a/src/reference/antora/modules/ROOT/pages/smb.adoc b/src/reference/antora/modules/ROOT/pages/smb.adoc index 2e4472971d0..ced20accd9e 100644 --- a/src/reference/antora/modules/ROOT/pages/smb.adoc +++ b/src/reference/antora/modules/ROOT/pages/smb.adoc @@ -140,6 +140,23 @@ public MessageSource smbMessageSource() { For XML configuration the `` component is provided. +Starting with version 6.2, you can filter SMB files based on last-modified strategy using `SmbLastModifiedFileListFilter`. +This filter can be configured with an `age` property so that only files older than this value are passed by the filter. +The age defaults to 60 seconds, but you should choose an age that is large enough to avoid picking up a file early (due to, say, network glitches). +The following example shows how to configure a `SmbLastModifiedFileListFilter`: + +[source, java] +---- +@Bean +public SmbLastModifiedFileListFilter smbLastModifiedFileListFilter() { + SmbLastModifiedFileListFilter filter = new SmbLastModifiedFileListFilter(); + + filter.setAge(180); + + return filter; +} +---- + [[configuring-with-the-java-dsl]] === Configuring with the Java DSL diff --git a/src/reference/antora/modules/ROOT/pages/whats-new.adoc b/src/reference/antora/modules/ROOT/pages/whats-new.adoc index 5c4630c7535..d1c800482c6 100644 --- a/src/reference/antora/modules/ROOT/pages/whats-new.adoc +++ b/src/reference/antora/modules/ROOT/pages/whats-new.adoc @@ -66,3 +66,9 @@ See xref:jdbc/message-store.adoc#jdbc-db-init[Initializing the Database] for mor A new option `setCreateIndexes(boolean)` has been introduced in `AbstractConfigurableMongoDbMessageStore` to disable the auto indexes creation. See xref:mongodb.adoc#mongodb-message-store[MongoDB Message Store] for an example. + +[[x6.2-remote-files]] +=== Remote Files Support Changes + +`FtpLastModifiedFileListFilter`, `SftpLastModifiedFileListFilter` and `SmbLastModifiedFileListFilter` have been introduced to allow files filtering based on a last-modified strategy respectively for `FTP`, `SFTP` and `SMB`. +See xref:ftp/inbound.adoc#ftp-inbound[FTP Inbound Channel Adapter], xref:sftp/inbound.adoc#sftp-inbound[SFTP Inbound Channel Adapter] and xref:smb.adoc#smb-inbound[SMB Inbound Channel Adapter]. From 4723fe8c0578a9ace9a11e2abc0e3e0737156430 Mon Sep 17 00:00:00 2001 From: Adama Sorho Date: Fri, 1 Sep 2023 23:56:31 -0500 Subject: [PATCH 06/10] GH-8691: Support age FTP, SMB over time filters --- .../LastModifiedFTPFileListFilter.java | 139 ++++++++++++++++++ .../LastModifiedFTPFileListFilterTests.java | 78 ++++++++++ .../LastModifiedSmbFileListFilter.java | 139 ++++++++++++++++++ .../LastModifiedSmbFileListFilterTests.java | 76 ++++++++++ 4 files changed, 432 insertions(+) create mode 100644 spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilter.java create mode 100644 spring-integration-ftp/src/test/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilterTests.java create mode 100644 spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilter.java create mode 100644 spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilterTests.java diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilter.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilter.java new file mode 100644 index 00000000000..c843384dc72 --- /dev/null +++ b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilter.java @@ -0,0 +1,139 @@ +/* + * 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.ftp.filters; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; + +import org.apache.commons.net.ftp.FTPFile; + +import org.springframework.integration.file.filters.DiscardAwareFileListFilter; +import org.springframework.lang.Nullable; + +/** + * The {@link org.springframework.integration.file.filters.FileListFilter} implementation to filter those files which + * {@link FTPFile#getTimestamp()} is less than the {@link #age} in comparison + * with the current time. + *

+ * The resolution is done in seconds. + *

+ * When {@link #discardCallback} is provided, it called for all the rejected files. + * + * @author Adama Sorho + * + * @since 6.2 + */ +public class LastModifiedFTPFileListFilter implements DiscardAwareFileListFilter { + + private static final long ONE_SECOND = 1000; + private static final long DEFAULT_AGE = 60; + private volatile long age = DEFAULT_AGE; + + @Nullable + private Consumer discardCallback; + + public LastModifiedFTPFileListFilter() { + } + + /** + * Construct a {@link LastModifiedFTPFileListFilter} instance with provided {@link #age}. + * Defaults to 60 seconds. + * @param age the age in seconds. + */ + public LastModifiedFTPFileListFilter(long age) { + this.age = age; + } + + /** + * Set the age that the files have to be before being passed by this filter. + * If {@link FTPFile#getTimestamp()} plus age is greater than the current time, the file + * is filtered. The resolution is seconds. + * Defaults to 60 seconds. + * @param age the age in seconds. + * @param unit the timeUnit. + */ + public void setAge(long age, TimeUnit unit) { + this.age = unit.toSeconds(age); + } + + /** + * Set the age that files have to be before being passed by this filter. + * If {@link FTPFile#getTimestamp()} plus age is greater than the current time, the file + * is filtered. The resolution is seconds. + * Defaults to 60 seconds. + * @param age the age in seconds. + */ + public void setAge(Duration age) { + setAge(age.getSeconds()); + } + + /** + * Set the age that files have to be before being passed by this filter. + * If {@link FTPFile#getTimestamp()} plus age is greater than the current time, the file + * is filtered. The resolution is seconds. + * Defaults to 60 seconds. + * @param age the age in seconds. + */ + public void setAge(long age) { + setAge(age, TimeUnit.SECONDS); + } + + @Override + public void addDiscardCallback(@Nullable Consumer discardCallback) { + this.discardCallback = discardCallback; + } + + @Override + public List filterFiles(FTPFile[] files) { + List list = new ArrayList<>(); + long now = System.currentTimeMillis() / ONE_SECOND; + for (FTPFile file: files) { + if (fileIsAged(file, now)) { + list.add(file); + } + else if (this.discardCallback != null) { + this.discardCallback.accept(file); + } + } + + return list; + } + + @Override + public boolean accept(FTPFile file) { + if (fileIsAged(file, System.currentTimeMillis() / ONE_SECOND)) { + return true; + } + else if (this.discardCallback != null) { + this.discardCallback.accept(file); + } + + return false; + } + + private boolean fileIsAged(FTPFile file, long now) { + return file.getTimestamp().getTimeInMillis() / ONE_SECOND + this.age <= now; + } + + @Override + public boolean supportsSingleFileFiltering() { + return true; + } +} diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilterTests.java b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilterTests.java new file mode 100644 index 00000000000..bf8c877f055 --- /dev/null +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilterTests.java @@ -0,0 +1,78 @@ +/* + * 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.ftp.filters; + +import java.util.Calendar; +import java.util.concurrent.TimeUnit; + +import org.apache.commons.net.ftp.FTPFile; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.integration.file.remote.session.SessionFactory; +import org.springframework.integration.ftp.FtpTestSupport; +import org.springframework.integration.ftp.session.FtpRemoteFileTemplate; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Adama Sorho + * @since 6.2 + */ +@SpringJUnitConfig +@DirtiesContext +public class LastModifiedFTPFileListFilterTests extends FtpTestSupport { + + @Autowired + private FtpRemoteFileTemplate ftpRemoteFileTemplate; + + @Test + public void testAge() { + LastModifiedFTPFileListFilter filter = new LastModifiedFTPFileListFilter(); + filter.setAge(60, TimeUnit.SECONDS); + FTPFile[] files = ftpRemoteFileTemplate.list("ftpSource"); + assertThat(files.length).isGreaterThan(0); + assertThat(filter.filterFiles(files)).hasSize(0); + FTPFile ftpFile = files[1]; + assertThat(filter.accept(ftpFile)).isFalse(); + + // Make a file as of yesterday's + final Calendar calendar = Calendar.getInstance(); + calendar.add(Calendar.DATE, -1); + ftpFile.setTimestamp(calendar); + assertThat(filter.filterFiles(files)).hasSize(1); + assertThat(filter.accept(ftpFile)).isTrue(); + } + + @Configuration + public static class Config { + + @Bean + public SessionFactory ftpFileSessionFactory() { + return LastModifiedFTPFileListFilterTests.sessionFactory(); + } + + @Bean + public FtpRemoteFileTemplate ftpRemoteFileTemplate() { + return new FtpRemoteFileTemplate(ftpFileSessionFactory()); + } + } +} diff --git a/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilter.java b/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilter.java new file mode 100644 index 00000000000..b32a4f6ce81 --- /dev/null +++ b/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilter.java @@ -0,0 +1,139 @@ +/* + * 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.smb.filters; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; + +import jcifs.smb.SmbFile; + +import org.springframework.integration.file.filters.DiscardAwareFileListFilter; +import org.springframework.lang.Nullable; + +/** + * The {@link org.springframework.integration.file.filters.FileListFilter} implementation to filter those files which + * {@link SmbFile#getLastModified()} is less than the {@link #age} in comparison + * with the current time. + *

+ * The resolution is done in seconds. + *

+ * When {@link #discardCallback} is provided, it called for all the rejected files. + * + * @author Adama Sorho + * + * @since 6.2 + */ +public class LastModifiedSmbFileListFilter implements DiscardAwareFileListFilter { + + private static final long ONE_SECOND = 1000; + private static final long DEFAULT_AGE = 60; + private volatile long age = DEFAULT_AGE; + + @Nullable + private Consumer discardCallback; + + public LastModifiedSmbFileListFilter() { + } + + /** + * Construct a {@link LastModifiedSmbFileListFilter} instance with provided {@link #age}. + * Defaults to 60 seconds. + * @param age the age in seconds. + */ + public LastModifiedSmbFileListFilter(long age) { + this.age = age; + } + + /** + * Set the age that the files have to be before being passed by this filter. + * If {@link SmbFile#getLastModified()} plus age is greater than the current time, the file + * is filtered. The resolution is seconds. + * Defaults to 60 seconds. + * @param age the age in seconds. + * @param unit the timeUnit. + */ + public void setAge(long age, TimeUnit unit) { + this.age = unit.toSeconds(age); + } + + /** + * Set the age that the files have to be before being passed by this filter. + * If {@link SmbFile#getLastModified()} plus age is greater than the current time, the file + * is filtered. The resolution is seconds. + * Defaults to 60 seconds. + * @param age the age in seconds. + */ + public void setAge(Duration age) { + setAge(age.getSeconds()); + } + + /** + * Set the age that the files have to be before being passed by this filter. + * If {@link SmbFile#getLastModified()} plus age is greater than the current time, the file + * is filtered. The resolution is seconds. + * Defaults to 60 seconds. + * @param age the age in seconds. + */ + public void setAge(long age) { + setAge(age, TimeUnit.SECONDS); + } + + @Override + public void addDiscardCallback(@Nullable Consumer discardCallback) { + this.discardCallback = discardCallback; + } + + @Override + public List filterFiles(SmbFile[] files) { + List list = new ArrayList<>(); + long now = System.currentTimeMillis() / ONE_SECOND; + for (SmbFile file: files) { + if (fileIsAged(file, now)) { + list.add(file); + } + else if (this.discardCallback != null) { + this.discardCallback.accept(file); + } + } + + return list; + } + + @Override + public boolean accept(SmbFile file) { + if (fileIsAged(file, System.currentTimeMillis() / ONE_SECOND)) { + return true; + } + else if (this.discardCallback != null) { + this.discardCallback.accept(file); + } + + return false; + } + + private boolean fileIsAged(SmbFile file, long now) { + return file.getLastModified() / ONE_SECOND + this.age <= now; + } + + @Override + public boolean supportsSingleFileFiltering() { + return true; + } +} diff --git a/spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilterTests.java b/spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilterTests.java new file mode 100644 index 00000000000..5f8c400b2f3 --- /dev/null +++ b/spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilterTests.java @@ -0,0 +1,76 @@ +/* + * 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.smb.filters; + +import java.util.concurrent.TimeUnit; + +import jcifs.smb.SmbException; +import jcifs.smb.SmbFile; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.integration.file.remote.session.SessionFactory; +import org.springframework.integration.smb.SmbTestSupport; +import org.springframework.integration.smb.session.SmbRemoteFileTemplate; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Adama Sorho + * @since 6.2 + */ +@SpringJUnitConfig +@DirtiesContext +public class LastModifiedSmbFileListFilterTests extends SmbTestSupport { + + @Autowired + private SmbRemoteFileTemplate smbRemoteFileTemplate; + + @Test + public void testAge() throws SmbException { + LastModifiedSmbFileListFilter filter = new LastModifiedSmbFileListFilter(); + filter.setAge(60, TimeUnit.SECONDS); + SmbFile[] files = smbRemoteFileTemplate.list("smbSource"); + assertThat(files.length).isGreaterThan(0); + assertThat(filter.filterFiles(files)).hasSize(0); + SmbFile smbFile = files[1]; + assertThat(filter.accept(smbFile)).isFalse(); + + // Make a file as of yesterday's + smbFile.setLastModified(System.currentTimeMillis() - 1000 * 60 * 60 * 24); + assertThat(filter.filterFiles(files)).hasSize(1); + assertThat(filter.accept(smbFile)).isTrue(); + } + + @Configuration + public static class Config { + + @Bean + public SessionFactory smbFileSessionFactory() { + return LastModifiedSmbFileListFilterTests.sessionFactory(); + } + + @Bean + public SmbRemoteFileTemplate smbRemoteFileTemplate() { + return new SmbRemoteFileTemplate(smbFileSessionFactory()); + } + } +} From 651666ddd499050d502c87891dc04057f74d5b81 Mon Sep 17 00:00:00 2001 From: Adama Sorho Date: Sat, 2 Sep 2023 18:36:06 -0500 Subject: [PATCH 07/10] GH-8691: Support age for sftp module --- .../LastModifiedSftpFileListFilter.java | 138 ++++++++++++++++++ .../LastModifiedSftpFileListFilterTests.java | 76 ++++++++++ 2 files changed, 214 insertions(+) create mode 100644 spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilter.java create mode 100644 spring-integration-sftp/src/test/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilterTests.java diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilter.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilter.java new file mode 100644 index 00000000000..9ab2f601ce7 --- /dev/null +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilter.java @@ -0,0 +1,138 @@ +/* + * Copyright 2013-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.sftp.filters; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; + +import org.apache.sshd.sftp.client.SftpClient; + +import org.springframework.integration.file.filters.DiscardAwareFileListFilter; +import org.springframework.lang.Nullable; + +/** + * The {@link org.springframework.integration.file.filters.FileListFilter} implementation to filter those files which + * {@link SftpClient.Attributes#getModifyTime()} is less than the {@link #age} in comparison + * with the current time. + *

+ * The resolution is done in seconds. + *

+ * When {@link #discardCallback} is provided, it called for all the rejected files. + * + * @author Adama Sorho + * + * @since 6.2 + */ +public class LastModifiedSftpFileListFilter implements DiscardAwareFileListFilter { + private static final long ONE_SECOND = 1000; + private static final long DEFAULT_AGE = 60; + private volatile long age = DEFAULT_AGE; + + @Nullable + private Consumer discardCallback; + + public LastModifiedSftpFileListFilter() { + } + + /** + * Construct a {@link LastModifiedSftpFileListFilter} instance with provided {@link #age}. + * Defaults to 60 seconds. + * @param age the age in seconds. + */ + public LastModifiedSftpFileListFilter(long age) { + this.age = age; + } + + /** + * Set the age that the files have to be before being passed by this filter. + * If {@link SftpClient.Attributes#getModifyTime()} plus age is greater than the current time, the file + * is filtered. The resolution is seconds. + * Defaults to 60 seconds. + * @param age the age in seconds. + * @param unit the timeUnit. + */ + public void setAge(long age, TimeUnit unit) { + this.age = unit.toSeconds(age); + } + + /** + * Set the age that the files have to be before being passed by this filter. + * If {@link SftpClient.Attributes#getModifyTime()} plus age is greater than the current time, the file + * is filtered. The resolution is seconds. + * Defaults to 60 seconds. + * @param age the age in seconds. + */ + public void setAge(Duration age) { + setAge(age.getSeconds()); + } + + /** + * Set the age that the files have to be before being passed by this filter. + * If {@link SftpClient.Attributes#getModifyTime()} plus age is greater than the current time, the file + * is filtered. The resolution is seconds. + * Defaults to 60 seconds. + * @param age the age in seconds. + */ + public void setAge(long age) { + setAge(age, TimeUnit.SECONDS); + } + + @Override + public void addDiscardCallback(@Nullable Consumer discardCallback) { + this.discardCallback = discardCallback; + } + + @Override + public List filterFiles(SftpClient.DirEntry[] files) { + List list = new ArrayList<>(); + long now = System.currentTimeMillis() / ONE_SECOND; + for (SftpClient.DirEntry file: files) { + if (fileIsAged(file, now)) { + list.add(file); + } + else if (this.discardCallback != null) { + this.discardCallback.accept(file); + } + } + + return list; + } + + @Override + public boolean accept(SftpClient.DirEntry file) { + if (fileIsAged(file, System.currentTimeMillis() / ONE_SECOND)) { + return true; + } + else if (this.discardCallback != null) { + this.discardCallback.accept(file); + } + + return false; + } + + private boolean fileIsAged(SftpClient.DirEntry file, long now) { + return file.getAttributes().getModifyTime().to(TimeUnit.SECONDS) + this.age <= now; + } + + @Override + public boolean supportsSingleFileFiltering() { + return true; + } +} diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilterTests.java b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilterTests.java new file mode 100644 index 00000000000..baf72bbb566 --- /dev/null +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilterTests.java @@ -0,0 +1,76 @@ +/* + * 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.sftp.filters; + +import java.nio.file.attribute.FileTime; + +import org.apache.sshd.sftp.client.SftpClient; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.integration.file.remote.session.SessionFactory; +import org.springframework.integration.sftp.SftpTestSupport; +import org.springframework.integration.sftp.session.SftpRemoteFileTemplate; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Adama Sorho + * @since 6.2 + */ +@SpringJUnitConfig +@DirtiesContext +public class LastModifiedSftpFileListFilterTests extends SftpTestSupport { + + @Autowired + private SftpRemoteFileTemplate sftpRemoteFileTemplate; + + @Test + public void testAge() { + LastModifiedSftpFileListFilter filter = new LastModifiedSftpFileListFilter(); + filter.setAge(60); + SftpClient.DirEntry[] files = sftpRemoteFileTemplate.list("sftpSource"); + assertThat(files.length).isGreaterThan(0); + assertThat(filter.filterFiles(files)).hasSize(0); + SftpClient.DirEntry sftFile = files[1]; + assertThat(filter.accept(sftFile)).isFalse(); + + // Make a file as of yesterday's + final FileTime fileTime = FileTime.fromMillis(System.currentTimeMillis() - 1000 * 60 * 60 * 24); + sftFile.getAttributes().setModifyTime(fileTime); + assertThat(filter.filterFiles(files)).hasSize(1); + assertThat(filter.accept(sftFile)).isTrue(); + } + + @Configuration + public static class Config { + + @Bean + public SessionFactory sftpFileSessionFactory() { + return LastModifiedSftpFileListFilterTests.sessionFactory(); + } + + @Bean + public SftpRemoteFileTemplate sftpRemoteFileTemplate() { + return new SftpRemoteFileTemplate(sftpFileSessionFactory()); + } + } +} From 7794676d2a34e9d26eaff361bf0eda4db1b4dc78 Mon Sep 17 00:00:00 2001 From: Adama Sorho Date: Thu, 7 Sep 2023 02:33:45 -0500 Subject: [PATCH 08/10] GH-8691: update tests and class names --- .../LastModifiedFTPFileListFilter.java | 139 ------------------ .../LastModifiedFTPFileListFilterTests.java | 78 ---------- .../LastModifiedSftpFileListFilter.java | 138 ----------------- .../LastModifiedSftpFileListFilterTests.java | 76 ---------- .../LastModifiedSmbFileListFilter.java | 139 ------------------ .../LastModifiedSmbFileListFilterTests.java | 76 ---------- 6 files changed, 646 deletions(-) delete mode 100644 spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilter.java delete mode 100644 spring-integration-ftp/src/test/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilterTests.java delete mode 100644 spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilter.java delete mode 100644 spring-integration-sftp/src/test/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilterTests.java delete mode 100644 spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilter.java delete mode 100644 spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilterTests.java diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilter.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilter.java deleted file mode 100644 index c843384dc72..00000000000 --- a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilter.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * 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.ftp.filters; - -import java.time.Duration; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.TimeUnit; -import java.util.function.Consumer; - -import org.apache.commons.net.ftp.FTPFile; - -import org.springframework.integration.file.filters.DiscardAwareFileListFilter; -import org.springframework.lang.Nullable; - -/** - * The {@link org.springframework.integration.file.filters.FileListFilter} implementation to filter those files which - * {@link FTPFile#getTimestamp()} is less than the {@link #age} in comparison - * with the current time. - *

- * The resolution is done in seconds. - *

- * When {@link #discardCallback} is provided, it called for all the rejected files. - * - * @author Adama Sorho - * - * @since 6.2 - */ -public class LastModifiedFTPFileListFilter implements DiscardAwareFileListFilter { - - private static final long ONE_SECOND = 1000; - private static final long DEFAULT_AGE = 60; - private volatile long age = DEFAULT_AGE; - - @Nullable - private Consumer discardCallback; - - public LastModifiedFTPFileListFilter() { - } - - /** - * Construct a {@link LastModifiedFTPFileListFilter} instance with provided {@link #age}. - * Defaults to 60 seconds. - * @param age the age in seconds. - */ - public LastModifiedFTPFileListFilter(long age) { - this.age = age; - } - - /** - * Set the age that the files have to be before being passed by this filter. - * If {@link FTPFile#getTimestamp()} plus age is greater than the current time, the file - * is filtered. The resolution is seconds. - * Defaults to 60 seconds. - * @param age the age in seconds. - * @param unit the timeUnit. - */ - public void setAge(long age, TimeUnit unit) { - this.age = unit.toSeconds(age); - } - - /** - * Set the age that files have to be before being passed by this filter. - * If {@link FTPFile#getTimestamp()} plus age is greater than the current time, the file - * is filtered. The resolution is seconds. - * Defaults to 60 seconds. - * @param age the age in seconds. - */ - public void setAge(Duration age) { - setAge(age.getSeconds()); - } - - /** - * Set the age that files have to be before being passed by this filter. - * If {@link FTPFile#getTimestamp()} plus age is greater than the current time, the file - * is filtered. The resolution is seconds. - * Defaults to 60 seconds. - * @param age the age in seconds. - */ - public void setAge(long age) { - setAge(age, TimeUnit.SECONDS); - } - - @Override - public void addDiscardCallback(@Nullable Consumer discardCallback) { - this.discardCallback = discardCallback; - } - - @Override - public List filterFiles(FTPFile[] files) { - List list = new ArrayList<>(); - long now = System.currentTimeMillis() / ONE_SECOND; - for (FTPFile file: files) { - if (fileIsAged(file, now)) { - list.add(file); - } - else if (this.discardCallback != null) { - this.discardCallback.accept(file); - } - } - - return list; - } - - @Override - public boolean accept(FTPFile file) { - if (fileIsAged(file, System.currentTimeMillis() / ONE_SECOND)) { - return true; - } - else if (this.discardCallback != null) { - this.discardCallback.accept(file); - } - - return false; - } - - private boolean fileIsAged(FTPFile file, long now) { - return file.getTimestamp().getTimeInMillis() / ONE_SECOND + this.age <= now; - } - - @Override - public boolean supportsSingleFileFiltering() { - return true; - } -} diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilterTests.java b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilterTests.java deleted file mode 100644 index bf8c877f055..00000000000 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/filters/LastModifiedFTPFileListFilterTests.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * 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.ftp.filters; - -import java.util.Calendar; -import java.util.concurrent.TimeUnit; - -import org.apache.commons.net.ftp.FTPFile; -import org.junit.jupiter.api.Test; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.integration.file.remote.session.SessionFactory; -import org.springframework.integration.ftp.FtpTestSupport; -import org.springframework.integration.ftp.session.FtpRemoteFileTemplate; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * @author Adama Sorho - * @since 6.2 - */ -@SpringJUnitConfig -@DirtiesContext -public class LastModifiedFTPFileListFilterTests extends FtpTestSupport { - - @Autowired - private FtpRemoteFileTemplate ftpRemoteFileTemplate; - - @Test - public void testAge() { - LastModifiedFTPFileListFilter filter = new LastModifiedFTPFileListFilter(); - filter.setAge(60, TimeUnit.SECONDS); - FTPFile[] files = ftpRemoteFileTemplate.list("ftpSource"); - assertThat(files.length).isGreaterThan(0); - assertThat(filter.filterFiles(files)).hasSize(0); - FTPFile ftpFile = files[1]; - assertThat(filter.accept(ftpFile)).isFalse(); - - // Make a file as of yesterday's - final Calendar calendar = Calendar.getInstance(); - calendar.add(Calendar.DATE, -1); - ftpFile.setTimestamp(calendar); - assertThat(filter.filterFiles(files)).hasSize(1); - assertThat(filter.accept(ftpFile)).isTrue(); - } - - @Configuration - public static class Config { - - @Bean - public SessionFactory ftpFileSessionFactory() { - return LastModifiedFTPFileListFilterTests.sessionFactory(); - } - - @Bean - public FtpRemoteFileTemplate ftpRemoteFileTemplate() { - return new FtpRemoteFileTemplate(ftpFileSessionFactory()); - } - } -} diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilter.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilter.java deleted file mode 100644 index 9ab2f601ce7..00000000000 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilter.java +++ /dev/null @@ -1,138 +0,0 @@ -/* - * Copyright 2013-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.sftp.filters; - -import java.time.Duration; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.TimeUnit; -import java.util.function.Consumer; - -import org.apache.sshd.sftp.client.SftpClient; - -import org.springframework.integration.file.filters.DiscardAwareFileListFilter; -import org.springframework.lang.Nullable; - -/** - * The {@link org.springframework.integration.file.filters.FileListFilter} implementation to filter those files which - * {@link SftpClient.Attributes#getModifyTime()} is less than the {@link #age} in comparison - * with the current time. - *

- * The resolution is done in seconds. - *

- * When {@link #discardCallback} is provided, it called for all the rejected files. - * - * @author Adama Sorho - * - * @since 6.2 - */ -public class LastModifiedSftpFileListFilter implements DiscardAwareFileListFilter { - private static final long ONE_SECOND = 1000; - private static final long DEFAULT_AGE = 60; - private volatile long age = DEFAULT_AGE; - - @Nullable - private Consumer discardCallback; - - public LastModifiedSftpFileListFilter() { - } - - /** - * Construct a {@link LastModifiedSftpFileListFilter} instance with provided {@link #age}. - * Defaults to 60 seconds. - * @param age the age in seconds. - */ - public LastModifiedSftpFileListFilter(long age) { - this.age = age; - } - - /** - * Set the age that the files have to be before being passed by this filter. - * If {@link SftpClient.Attributes#getModifyTime()} plus age is greater than the current time, the file - * is filtered. The resolution is seconds. - * Defaults to 60 seconds. - * @param age the age in seconds. - * @param unit the timeUnit. - */ - public void setAge(long age, TimeUnit unit) { - this.age = unit.toSeconds(age); - } - - /** - * Set the age that the files have to be before being passed by this filter. - * If {@link SftpClient.Attributes#getModifyTime()} plus age is greater than the current time, the file - * is filtered. The resolution is seconds. - * Defaults to 60 seconds. - * @param age the age in seconds. - */ - public void setAge(Duration age) { - setAge(age.getSeconds()); - } - - /** - * Set the age that the files have to be before being passed by this filter. - * If {@link SftpClient.Attributes#getModifyTime()} plus age is greater than the current time, the file - * is filtered. The resolution is seconds. - * Defaults to 60 seconds. - * @param age the age in seconds. - */ - public void setAge(long age) { - setAge(age, TimeUnit.SECONDS); - } - - @Override - public void addDiscardCallback(@Nullable Consumer discardCallback) { - this.discardCallback = discardCallback; - } - - @Override - public List filterFiles(SftpClient.DirEntry[] files) { - List list = new ArrayList<>(); - long now = System.currentTimeMillis() / ONE_SECOND; - for (SftpClient.DirEntry file: files) { - if (fileIsAged(file, now)) { - list.add(file); - } - else if (this.discardCallback != null) { - this.discardCallback.accept(file); - } - } - - return list; - } - - @Override - public boolean accept(SftpClient.DirEntry file) { - if (fileIsAged(file, System.currentTimeMillis() / ONE_SECOND)) { - return true; - } - else if (this.discardCallback != null) { - this.discardCallback.accept(file); - } - - return false; - } - - private boolean fileIsAged(SftpClient.DirEntry file, long now) { - return file.getAttributes().getModifyTime().to(TimeUnit.SECONDS) + this.age <= now; - } - - @Override - public boolean supportsSingleFileFiltering() { - return true; - } -} diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilterTests.java b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilterTests.java deleted file mode 100644 index baf72bbb566..00000000000 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilterTests.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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.sftp.filters; - -import java.nio.file.attribute.FileTime; - -import org.apache.sshd.sftp.client.SftpClient; -import org.junit.jupiter.api.Test; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.integration.file.remote.session.SessionFactory; -import org.springframework.integration.sftp.SftpTestSupport; -import org.springframework.integration.sftp.session.SftpRemoteFileTemplate; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * @author Adama Sorho - * @since 6.2 - */ -@SpringJUnitConfig -@DirtiesContext -public class LastModifiedSftpFileListFilterTests extends SftpTestSupport { - - @Autowired - private SftpRemoteFileTemplate sftpRemoteFileTemplate; - - @Test - public void testAge() { - LastModifiedSftpFileListFilter filter = new LastModifiedSftpFileListFilter(); - filter.setAge(60); - SftpClient.DirEntry[] files = sftpRemoteFileTemplate.list("sftpSource"); - assertThat(files.length).isGreaterThan(0); - assertThat(filter.filterFiles(files)).hasSize(0); - SftpClient.DirEntry sftFile = files[1]; - assertThat(filter.accept(sftFile)).isFalse(); - - // Make a file as of yesterday's - final FileTime fileTime = FileTime.fromMillis(System.currentTimeMillis() - 1000 * 60 * 60 * 24); - sftFile.getAttributes().setModifyTime(fileTime); - assertThat(filter.filterFiles(files)).hasSize(1); - assertThat(filter.accept(sftFile)).isTrue(); - } - - @Configuration - public static class Config { - - @Bean - public SessionFactory sftpFileSessionFactory() { - return LastModifiedSftpFileListFilterTests.sessionFactory(); - } - - @Bean - public SftpRemoteFileTemplate sftpRemoteFileTemplate() { - return new SftpRemoteFileTemplate(sftpFileSessionFactory()); - } - } -} diff --git a/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilter.java b/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilter.java deleted file mode 100644 index b32a4f6ce81..00000000000 --- a/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilter.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * 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.smb.filters; - -import java.time.Duration; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.TimeUnit; -import java.util.function.Consumer; - -import jcifs.smb.SmbFile; - -import org.springframework.integration.file.filters.DiscardAwareFileListFilter; -import org.springframework.lang.Nullable; - -/** - * The {@link org.springframework.integration.file.filters.FileListFilter} implementation to filter those files which - * {@link SmbFile#getLastModified()} is less than the {@link #age} in comparison - * with the current time. - *

- * The resolution is done in seconds. - *

- * When {@link #discardCallback} is provided, it called for all the rejected files. - * - * @author Adama Sorho - * - * @since 6.2 - */ -public class LastModifiedSmbFileListFilter implements DiscardAwareFileListFilter { - - private static final long ONE_SECOND = 1000; - private static final long DEFAULT_AGE = 60; - private volatile long age = DEFAULT_AGE; - - @Nullable - private Consumer discardCallback; - - public LastModifiedSmbFileListFilter() { - } - - /** - * Construct a {@link LastModifiedSmbFileListFilter} instance with provided {@link #age}. - * Defaults to 60 seconds. - * @param age the age in seconds. - */ - public LastModifiedSmbFileListFilter(long age) { - this.age = age; - } - - /** - * Set the age that the files have to be before being passed by this filter. - * If {@link SmbFile#getLastModified()} plus age is greater than the current time, the file - * is filtered. The resolution is seconds. - * Defaults to 60 seconds. - * @param age the age in seconds. - * @param unit the timeUnit. - */ - public void setAge(long age, TimeUnit unit) { - this.age = unit.toSeconds(age); - } - - /** - * Set the age that the files have to be before being passed by this filter. - * If {@link SmbFile#getLastModified()} plus age is greater than the current time, the file - * is filtered. The resolution is seconds. - * Defaults to 60 seconds. - * @param age the age in seconds. - */ - public void setAge(Duration age) { - setAge(age.getSeconds()); - } - - /** - * Set the age that the files have to be before being passed by this filter. - * If {@link SmbFile#getLastModified()} plus age is greater than the current time, the file - * is filtered. The resolution is seconds. - * Defaults to 60 seconds. - * @param age the age in seconds. - */ - public void setAge(long age) { - setAge(age, TimeUnit.SECONDS); - } - - @Override - public void addDiscardCallback(@Nullable Consumer discardCallback) { - this.discardCallback = discardCallback; - } - - @Override - public List filterFiles(SmbFile[] files) { - List list = new ArrayList<>(); - long now = System.currentTimeMillis() / ONE_SECOND; - for (SmbFile file: files) { - if (fileIsAged(file, now)) { - list.add(file); - } - else if (this.discardCallback != null) { - this.discardCallback.accept(file); - } - } - - return list; - } - - @Override - public boolean accept(SmbFile file) { - if (fileIsAged(file, System.currentTimeMillis() / ONE_SECOND)) { - return true; - } - else if (this.discardCallback != null) { - this.discardCallback.accept(file); - } - - return false; - } - - private boolean fileIsAged(SmbFile file, long now) { - return file.getLastModified() / ONE_SECOND + this.age <= now; - } - - @Override - public boolean supportsSingleFileFiltering() { - return true; - } -} diff --git a/spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilterTests.java b/spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilterTests.java deleted file mode 100644 index 5f8c400b2f3..00000000000 --- a/spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/LastModifiedSmbFileListFilterTests.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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.smb.filters; - -import java.util.concurrent.TimeUnit; - -import jcifs.smb.SmbException; -import jcifs.smb.SmbFile; -import org.junit.jupiter.api.Test; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.integration.file.remote.session.SessionFactory; -import org.springframework.integration.smb.SmbTestSupport; -import org.springframework.integration.smb.session.SmbRemoteFileTemplate; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * @author Adama Sorho - * @since 6.2 - */ -@SpringJUnitConfig -@DirtiesContext -public class LastModifiedSmbFileListFilterTests extends SmbTestSupport { - - @Autowired - private SmbRemoteFileTemplate smbRemoteFileTemplate; - - @Test - public void testAge() throws SmbException { - LastModifiedSmbFileListFilter filter = new LastModifiedSmbFileListFilter(); - filter.setAge(60, TimeUnit.SECONDS); - SmbFile[] files = smbRemoteFileTemplate.list("smbSource"); - assertThat(files.length).isGreaterThan(0); - assertThat(filter.filterFiles(files)).hasSize(0); - SmbFile smbFile = files[1]; - assertThat(filter.accept(smbFile)).isFalse(); - - // Make a file as of yesterday's - smbFile.setLastModified(System.currentTimeMillis() - 1000 * 60 * 60 * 24); - assertThat(filter.filterFiles(files)).hasSize(1); - assertThat(filter.accept(smbFile)).isTrue(); - } - - @Configuration - public static class Config { - - @Bean - public SessionFactory smbFileSessionFactory() { - return LastModifiedSmbFileListFilterTests.sessionFactory(); - } - - @Bean - public SmbRemoteFileTemplate smbRemoteFileTemplate() { - return new SmbRemoteFileTemplate(smbFileSessionFactory()); - } - } -} From e6b17c71989332f12db3f1141b1c29394856dfef Mon Sep 17 00:00:00 2001 From: Adama Sorho Date: Wed, 13 Sep 2023 12:23:51 -0500 Subject: [PATCH 09/10] GH-8691: Introduce AbstractLastModifiedFileListFilter --- .../AbstractLastModifiedFileListFilter.java | 124 ++++++++++++++++++ .../filters/LastModifiedFileListFilter.java | 92 ++----------- .../FtpLastModifiedFileListFilter.java | 88 ++----------- .../SftpLastModifiedFileListFilter.java | 87 ++---------- .../SmbLastModifiedFileListFilter.java | 100 ++------------ .../SmbLastModifiedFileListFilterTests.java | 4 +- .../modules/ROOT/pages/ftp/inbound.adoc | 14 +- .../modules/ROOT/pages/sftp/inbound.adoc | 14 +- .../antora/modules/ROOT/pages/smb.adoc | 16 +-- 9 files changed, 174 insertions(+), 365 deletions(-) create mode 100644 spring-integration-file/src/main/java/org/springframework/integration/file/filters/AbstractLastModifiedFileListFilter.java diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/filters/AbstractLastModifiedFileListFilter.java b/spring-integration-file/src/main/java/org/springframework/integration/file/filters/AbstractLastModifiedFileListFilter.java new file mode 100644 index 00000000000..04626bcc8ba --- /dev/null +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/filters/AbstractLastModifiedFileListFilter.java @@ -0,0 +1,124 @@ +/* + * 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 the file + * + * @author Adama Sorho + * + * @since 6.2 + */ +public abstract class AbstractLastModifiedFileListFilter implements DiscardAwareFileListFilter { + + private static final long DEFAULT_AGE = 60; + + private Duration age = Duration.ofSeconds(DEFAULT_AGE); + + @Nullable + private Consumer 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 discardCallback) { + this.discardCallback = discardCallback; + } + + @Override + public List filterFiles(F[] files) { + List 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 getCurrentAge() { + return this.age; + } +} diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/filters/LastModifiedFileListFilter.java b/spring-integration-file/src/main/java/org/springframework/integration/file/filters/LastModifiedFileListFilter.java index 553a024e5f1..03955c94b18 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/filters/LastModifiedFileListFilter.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/filters/LastModifiedFileListFilter.java @@ -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. @@ -18,51 +18,43 @@ 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. *

* The resolution is done in seconds. *

- * 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 { +public class LastModifiedFileListFilter extends AbstractLastModifiedFileListFilter { private static final long ONE_SECOND = 1000; - private static final long DEFAULT_AGE = 60; - - private volatile long age = DEFAULT_AGE; - - @Nullable - private Consumer discardCallback; - 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)); } /** @@ -74,74 +66,16 @@ public LastModifiedFileListFilter(long age) { * @param unit the timeUnit. */ public void setAge(long age, TimeUnit unit) { - 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()); - } - - /** - * 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 - */ - public void setAge(long age) { - setAge(age, TimeUnit.SECONDS); + setAge(unit.toSeconds(age)); } public long getAge() { - return this.age; - } - - @Override - public void addDiscardCallback(@Nullable Consumer discardCallbackToSet) { - this.discardCallback = discardCallbackToSet; - } - - @Override - public List filterFiles(File[] files) { - List 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 getCurrentAge().getSeconds(); } @Override - public boolean supportsSingleFileFiltering() { - return true; + protected Instant getLastModified(File file) { + return Instant.ofEpochSecond(file.lastModified() / ONE_SECOND); } } diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/FtpLastModifiedFileListFilter.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/FtpLastModifiedFileListFilter.java index b24a32fc8e1..515479aa907 100644 --- a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/FtpLastModifiedFileListFilter.java +++ b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/filters/FtpLastModifiedFileListFilter.java @@ -18,39 +18,30 @@ import java.time.Duration; import java.time.Instant; -import java.util.ArrayList; -import java.util.List; import java.util.function.Consumer; import org.apache.commons.net.ftp.FTPFile; -import org.springframework.integration.file.filters.DiscardAwareFileListFilter; -import org.springframework.lang.Nullable; +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 {@link #age} in comparison + * {@link FTPFile#getTimestampInstant()} is less than the age in comparison * with the {@link Instant#now()}. - * When {@link #discardCallback} is provided, it called for all the rejected files. + * When discardCallback {@link #addDiscardCallback(Consumer)} is provided, it called for all the rejected files. * * @author Adama Sorho * * @since 6.2 */ -public class FtpLastModifiedFileListFilter implements DiscardAwareFileListFilter { - - private static final long DEFAULT_AGE = 60; - - private Duration age = Duration.ofSeconds(DEFAULT_AGE); - - @Nullable - private Consumer discardCallback; +public class FtpLastModifiedFileListFilter extends AbstractLastModifiedFileListFilter { public FtpLastModifiedFileListFilter() { + super(); } /** - * Construct a {@link FtpLastModifiedFileListFilter} instance with provided {@link #age}. + * Construct a {@link FtpLastModifiedFileListFilter} instance with provided age. * Defaults to 60 seconds. * @param age the age in seconds. */ @@ -59,76 +50,17 @@ public FtpLastModifiedFileListFilter(long age) { } /** - * Construct a {@link FtpLastModifiedFileListFilter} instance with provided {@link #age}. + * Construct a {@link FtpLastModifiedFileListFilter} instance with provided age. * Defaults to 60 seconds. * @param age the Duration */ public FtpLastModifiedFileListFilter(Duration age) { - this.age = age; - } - - /** - * Set the age that files have to be before being passed by this filter. - * If {@link FTPFile#getTimestampInstant()} 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 {@link FTPFile#getTimestampInstant()} 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 discardCallback) { - this.discardCallback = discardCallback; - } - - @Override - public List filterFiles(FTPFile[] files) { - List list = new ArrayList<>(); - Instant now = Instant.now(); - for (FTPFile file: files) { - if (fileIsAged(file, now)) { - list.add(file); - } - else if (this.discardCallback != null) { - this.discardCallback.accept(file); - } - } - - return list; - } - - @Override - public boolean accept(FTPFile file) { - if (fileIsAged(file, Instant.now())) { - return true; - } - else if (this.discardCallback != null) { - this.discardCallback.accept(file); - } - - return false; - } - - private boolean fileIsAged(FTPFile file, Instant now) { - return file.getTimestampInstant().plus(this.age).isBefore(now); + super(age); } @Override - public boolean supportsSingleFileFiltering() { - return true; + protected Instant getLastModified(FTPFile remoteFile) { + return remoteFile.getTimestampInstant(); } } diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/SftpLastModifiedFileListFilter.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/SftpLastModifiedFileListFilter.java index fe7a33dc745..d2960cb6d2d 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/SftpLastModifiedFileListFilter.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/filters/SftpLastModifiedFileListFilter.java @@ -19,39 +19,30 @@ import java.nio.file.attribute.FileTime; import java.time.Duration; import java.time.Instant; -import java.util.ArrayList; -import java.util.List; import java.util.function.Consumer; import org.apache.sshd.sftp.client.SftpClient; -import org.springframework.integration.file.filters.DiscardAwareFileListFilter; -import org.springframework.lang.Nullable; +import org.springframework.integration.file.filters.AbstractLastModifiedFileListFilter; /** * The {@link org.springframework.integration.file.filters.FileListFilter} implementation to filter those files which - * {@link FileTime#toInstant()} is less than the {@link #age} in comparison + * {@link FileTime#toInstant()} is less than the age in comparison * with the {@link Instant#now()}. - * When {@link #discardCallback} is provided, it called for all the rejected files. + * When discardCallback {@link #addDiscardCallback(Consumer)} is provided, it called for all the rejected files. * * @author Adama Sorho * * @since 6.2 */ -public class SftpLastModifiedFileListFilter implements DiscardAwareFileListFilter { - - private static final long DEFAULT_AGE = 60; - - private Duration age = Duration.ofSeconds(DEFAULT_AGE); - - @Nullable - private Consumer discardCallback; +public class SftpLastModifiedFileListFilter extends AbstractLastModifiedFileListFilter { public SftpLastModifiedFileListFilter() { + super(); } /** - * Construct a {@link SftpLastModifiedFileListFilter} instance with provided {@link #age}. + * Construct a {@link SftpLastModifiedFileListFilter} instance with provided age. * Defaults to 60 seconds. * @param age the age in seconds. */ @@ -59,72 +50,18 @@ public SftpLastModifiedFileListFilter(long age) { this(Duration.ofSeconds(age)); } - public SftpLastModifiedFileListFilter(Duration age) { - this.age = age; - } - /** - * Set the age that the files have to be before being passed by this filter. - * If {@link FileTime#toInstant()} plus {@link #age} is before the {@link Instant#now()}, the file - * is filtered. + * Construct a {@link SftpLastModifiedFileListFilter} instance with provided age. * Defaults to 60 seconds. - * @param age The Duration. + * @param age the Duration */ - public void setAge(Duration age) { - this.age = age; - } - - /** - * Set the age that the files have to be before being passed by this filter. - * If {@link FileTime#toInstant()} 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 discardCallback) { - this.discardCallback = discardCallback; - } - - @Override - public List filterFiles(SftpClient.DirEntry[] files) { - List list = new ArrayList<>(); - Instant now = Instant.now(); - for (SftpClient.DirEntry file: files) { - if (fileIsAged(file, now)) { - list.add(file); - } - else if (this.discardCallback != null) { - this.discardCallback.accept(file); - } - } - - return list; - } - - @Override - public boolean accept(SftpClient.DirEntry file) { - if (fileIsAged(file, Instant.now())) { - return true; - } - else if (this.discardCallback != null) { - this.discardCallback.accept(file); - } - - return false; - } - - private boolean fileIsAged(SftpClient.DirEntry file, Instant now) { - return file.getAttributes().getModifyTime().toInstant().plus(this.age).isBefore(now); + public SftpLastModifiedFileListFilter(Duration age) { + super(age); } @Override - public boolean supportsSingleFileFiltering() { - return true; + protected Instant getLastModified(SftpClient.DirEntry remoteFile) { + return remoteFile.getAttributes().getModifyTime().toInstant(); } } diff --git a/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilter.java b/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilter.java index 1847887f97a..55e036e6ca9 100644 --- a/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilter.java +++ b/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilter.java @@ -17,126 +17,46 @@ package org.springframework.integration.smb.filters; import java.time.Duration; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.TimeUnit; +import java.time.Instant; import java.util.function.Consumer; import jcifs.smb.SmbFile; -import org.springframework.integration.file.filters.DiscardAwareFileListFilter; -import org.springframework.lang.Nullable; +import org.springframework.integration.file.filters.AbstractLastModifiedFileListFilter; /** * The {@link org.springframework.integration.file.filters.FileListFilter} implementation to filter those files which - * {@link SmbFile#getLastModified()} is less than the {@link #age} in comparison + * {@link SmbFile#getLastModified()} is less than the age in comparison * with the current time. *

* The resolution is done in seconds. *

- * When {@link #discardCallback} is provided, it called for all the rejected files. + * When discardCallback {@link #addDiscardCallback(Consumer)} is provided, it called for all the rejected files. * * @author Adama Sorho * * @since 6.2 */ -public class SmbLastModifiedFileListFilter implements DiscardAwareFileListFilter { +public class SmbLastModifiedFileListFilter extends AbstractLastModifiedFileListFilter { private static final long ONE_SECOND = 1000; - private static final long DEFAULT_AGE = 60; - - private long age = DEFAULT_AGE; - - @Nullable - private Consumer discardCallback; - public SmbLastModifiedFileListFilter() { + super(); } /** - * Construct a {@link SmbLastModifiedFileListFilter} instance with provided {@link #age}. + * Construct a {@link SmbLastModifiedFileListFilter} instance with provided age. * Defaults to 60 seconds. * @param age the age in seconds. */ public SmbLastModifiedFileListFilter(long age) { - this.age = age; - } - - /** - * Set the age that the files have to be before being passed by this filter. - * If {@link SmbFile#getLastModified()} plus age is greater than the current time, the file - * is filtered. The resolution is seconds. - * Defaults to 60 seconds. - * @param age the age in seconds. - * @param unit the timeUnit. - */ - public void setAge(long age, TimeUnit unit) { - this.age = unit.toSeconds(age); - } - - /** - * Set the age that the files have to be before being passed by this filter. - * If {@link SmbFile#getLastModified()} plus age is greater than the current time, the file - * is filtered. The resolution is seconds. - * Defaults to 60 seconds. - * @param age the age in seconds. - */ - public void setAge(Duration age) { - setAge(age.getSeconds()); - } - - /** - * Set the age that the files have to be before being passed by this filter. - * If {@link SmbFile#getLastModified()} plus age is greater than the current time, the file - * is filtered. The resolution is seconds. - * Defaults to 60 seconds. - * @param age the age in seconds. - */ - public void setAge(long age) { - setAge(age, TimeUnit.SECONDS); - } - - @Override - public void addDiscardCallback(@Nullable Consumer discardCallback) { - this.discardCallback = discardCallback; - } - - @Override - public List filterFiles(SmbFile[] files) { - List list = new ArrayList<>(); - long now = System.currentTimeMillis() / ONE_SECOND; - for (SmbFile file: files) { - if (fileIsAged(file, now)) { - list.add(file); - } - else if (this.discardCallback != null) { - this.discardCallback.accept(file); - } - } - - return list; - } - - @Override - public boolean accept(SmbFile file) { - if (fileIsAged(file, System.currentTimeMillis() / ONE_SECOND)) { - return true; - } - else if (this.discardCallback != null) { - this.discardCallback.accept(file); - } - - return false; - } - - private boolean fileIsAged(SmbFile file, long now) { - return file.getLastModified() / ONE_SECOND + this.age <= now; + super(Duration.ofSeconds(age)); } @Override - public boolean supportsSingleFileFiltering() { - return true; + protected Instant getLastModified(SmbFile remoteFile) { + return Instant.ofEpochSecond(remoteFile.getLastModified() / ONE_SECOND); } } diff --git a/spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilterTests.java b/spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilterTests.java index 78ca7dc4ed3..f330b42f33f 100644 --- a/spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilterTests.java +++ b/spring-integration-smb/src/test/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilterTests.java @@ -16,8 +16,6 @@ package org.springframework.integration.smb.filters; -import java.util.concurrent.TimeUnit; - import jcifs.smb.SmbFile; import org.junit.jupiter.api.Test; @@ -34,7 +32,7 @@ public class SmbLastModifiedFileListFilterTests { @Test public void testAge() { SmbLastModifiedFileListFilter filter = new SmbLastModifiedFileListFilter(); - filter.setAge(80, TimeUnit.SECONDS); + filter.setAge(80); SmbFile smbFile1 = mock(SmbFile.class); when(smbFile1.getLastModified()).thenReturn(System.currentTimeMillis()); SmbFile smbFile2 = mock(SmbFile.class); diff --git a/src/reference/antora/modules/ROOT/pages/ftp/inbound.adoc b/src/reference/antora/modules/ROOT/pages/ftp/inbound.adoc index 98429ecbc5d..4f0eff27344 100644 --- a/src/reference/antora/modules/ROOT/pages/ftp/inbound.adoc +++ b/src/reference/antora/modules/ROOT/pages/ftp/inbound.adoc @@ -101,19 +101,7 @@ Once a file has been transferred, a message with a `java.io.File` as its payload Starting with version 6.2, you can filter FTP files based on last-modified strategy using `FtpLastModifiedFileListFilter`. This filter can be configured with an `age` property so that only files older than this value are passed by the filter. The age defaults to 60 seconds, but you should choose an age that is large enough to avoid picking up a file early (due to, say, network glitches). -The following example shows how to configure a `FtpLastModifiedFileListFilter`: - -[source, java] ----- -@Bean -public FtpLastModifiedFileListFilter ftpLastModifiedFileListFilter() { - FtpLastModifiedFileListFilter filter = new FtpLastModifiedFileListFilter(); - - filter.setAge(Duration.ofSeconds(180)); - - return filter; -} ----- +Please, look into its Javadoc for more info. [[more-on-file-filtering-and-incomplete-files]] == More on File Filtering and Incomplete Files diff --git a/src/reference/antora/modules/ROOT/pages/sftp/inbound.adoc b/src/reference/antora/modules/ROOT/pages/sftp/inbound.adoc index 783983059a1..c7319f69c30 100644 --- a/src/reference/antora/modules/ROOT/pages/sftp/inbound.adoc +++ b/src/reference/antora/modules/ROOT/pages/sftp/inbound.adoc @@ -98,19 +98,7 @@ Once the file has been transferred to a local directory, a message with `java.io Starting with version 6.2, you can filter SFTP files based on last-modified strategy using `SftpLastModifiedFileListFilter`. This filter can be configured with an `age` property so that only files older than this value are passed by the filter. The age defaults to 60 seconds, but you should choose an age that is large enough to avoid picking up a file early (due to, say, network glitches). -The following example shows how to configure a `SftpLastModifiedFileListFilter`: - -[source, java] ----- -@Bean -public SftpLastModifiedFileListFilter sftpLastModifiedFileListFilter() { - SftpLastModifiedFileListFilter filter = new SftpLastModifiedFileListFilter(); - - filter.setAge(Duration.ofSeconds(180)); - - return filter; -} ----- +Please, look into its Javadoc for more info. [[more-on-file-filtering-and-large-files]] == More on File Filtering and Large Files diff --git a/src/reference/antora/modules/ROOT/pages/smb.adoc b/src/reference/antora/modules/ROOT/pages/smb.adoc index ced20accd9e..381b86f02f5 100644 --- a/src/reference/antora/modules/ROOT/pages/smb.adoc +++ b/src/reference/antora/modules/ROOT/pages/smb.adoc @@ -142,20 +142,8 @@ For XML configuration the `` component is provi Starting with version 6.2, you can filter SMB files based on last-modified strategy using `SmbLastModifiedFileListFilter`. This filter can be configured with an `age` property so that only files older than this value are passed by the filter. -The age defaults to 60 seconds, but you should choose an age that is large enough to avoid picking up a file early (due to, say, network glitches). -The following example shows how to configure a `SmbLastModifiedFileListFilter`: - -[source, java] ----- -@Bean -public SmbLastModifiedFileListFilter smbLastModifiedFileListFilter() { - SmbLastModifiedFileListFilter filter = new SmbLastModifiedFileListFilter(); - - filter.setAge(180); - - return filter; -} ----- +The age defaults to 60 seconds, but you should choose an age that is large enough to avoid picking up a file early (due to, say, network glitches. +Please, look into its Javadoc for more info. [[configuring-with-the-java-dsl]] === Configuring with the Java DSL From 66dc9c677a506216b4de270e86c0aad32e082efb Mon Sep 17 00:00:00 2001 From: Adama Sorho Date: Wed, 13 Sep 2023 21:46:48 -0500 Subject: [PATCH 10/10] GH-8691: Deprecate unnecessary methods --- .../filters/AbstractLastModifiedFileListFilter.java | 4 +++- .../file/filters/LastModifiedFileListFilter.java | 13 ++++++++++--- .../filters/LastModifiedFileListFilterTests.java | 6 +++--- .../smb/filters/SmbLastModifiedFileListFilter.java | 2 -- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/filters/AbstractLastModifiedFileListFilter.java b/spring-integration-file/src/main/java/org/springframework/integration/file/filters/AbstractLastModifiedFileListFilter.java index 04626bcc8ba..9f0a1686f4c 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/filters/AbstractLastModifiedFileListFilter.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/filters/AbstractLastModifiedFileListFilter.java @@ -38,6 +38,8 @@ */ public abstract class AbstractLastModifiedFileListFilter implements DiscardAwareFileListFilter { + protected static final long ONE_SECOND = 1000; + private static final long DEFAULT_AGE = 60; private Duration age = Duration.ofSeconds(DEFAULT_AGE); @@ -118,7 +120,7 @@ public boolean supportsSingleFileFiltering() { protected abstract Instant getLastModified(F remoteFile); - protected Duration getCurrentAge() { + protected Duration getAgeDuration() { return this.age; } } diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/filters/LastModifiedFileListFilter.java b/spring-integration-file/src/main/java/org/springframework/integration/file/filters/LastModifiedFileListFilter.java index 03955c94b18..1b7297fc3f0 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/filters/LastModifiedFileListFilter.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/filters/LastModifiedFileListFilter.java @@ -41,8 +41,6 @@ */ public class LastModifiedFileListFilter extends AbstractLastModifiedFileListFilter { - private static final long ONE_SECOND = 1000; - public LastModifiedFileListFilter() { super(); } @@ -64,13 +62,22 @@ 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) { setAge(unit.toSeconds(age)); } + /** + * @return the age in seconds. + * + * @deprecated since 6.2 + */ + @Deprecated(since = "6.2", forRemoval = true) public long getAge() { - return getCurrentAge().getSeconds(); + return getAgeDuration().getSeconds(); } @Override diff --git a/spring-integration-file/src/test/java/org/springframework/integration/file/filters/LastModifiedFileListFilterTests.java b/spring-integration-file/src/test/java/org/springframework/integration/file/filters/LastModifiedFileListFilterTests.java index 49a7ec6ca0f..8d7ebc71a8a 100644 --- a/spring-integration-file/src/test/java/org/springframework/integration/file/filters/LastModifiedFileListFilterTests.java +++ b/spring-integration-file/src/test/java/org/springframework/integration/file/filters/LastModifiedFileListFilterTests.java @@ -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. @@ -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; @@ -29,6 +28,7 @@ /** * @author Gary Russell * @author Artem Bilan + * @author Adama Sorho * @since 4.2 * */ @@ -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()); diff --git a/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilter.java b/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilter.java index 55e036e6ca9..350e2246646 100644 --- a/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilter.java +++ b/spring-integration-smb/src/main/java/org/springframework/integration/smb/filters/SmbLastModifiedFileListFilter.java @@ -39,8 +39,6 @@ */ public class SmbLastModifiedFileListFilter extends AbstractLastModifiedFileListFilter { - private static final long ONE_SECOND = 1000; - public SmbLastModifiedFileListFilter() { super(); }