-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-8691: Support age for sftp module
- Loading branch information
1 parent
4723fe8
commit 651666d
Showing
2 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
...ain/java/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
* <p> | ||
* The resolution is done in seconds. | ||
* </p> | ||
* When {@link #discardCallback} is provided, it called for all the rejected files. | ||
* | ||
* @author Adama Sorho | ||
* | ||
* @since 6.2 | ||
*/ | ||
public class LastModifiedSftpFileListFilter implements DiscardAwareFileListFilter<SftpClient.DirEntry> { | ||
private static final long ONE_SECOND = 1000; | ||
private static final long DEFAULT_AGE = 60; | ||
private volatile long age = DEFAULT_AGE; | ||
|
||
@Nullable | ||
private Consumer<SftpClient.DirEntry> 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<SftpClient.DirEntry> discardCallback) { | ||
this.discardCallback = discardCallback; | ||
} | ||
|
||
@Override | ||
public List<SftpClient.DirEntry> filterFiles(SftpClient.DirEntry[] files) { | ||
List<SftpClient.DirEntry> 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; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...ava/org/springframework/integration/sftp/filters/LastModifiedSftpFileListFilterTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<SftpClient.DirEntry> sftpFileSessionFactory() { | ||
return LastModifiedSftpFileListFilterTests.sessionFactory(); | ||
} | ||
|
||
@Bean | ||
public SftpRemoteFileTemplate sftpRemoteFileTemplate() { | ||
return new SftpRemoteFileTemplate(sftpFileSessionFactory()); | ||
} | ||
} | ||
} |