|
1 | 1 | /* |
2 | | - * Copyright 2016-2022 the original author or authors. |
| 2 | + * Copyright 2016-2023 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
16 | 16 |
|
17 | 17 | package org.springframework.integration.sftp.inbound; |
18 | 18 |
|
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
19 | 21 | import java.io.InputStream; |
| 22 | +import java.nio.charset.StandardCharsets; |
20 | 23 | import java.time.Duration; |
21 | 24 | import java.util.Arrays; |
22 | 25 | import java.util.Comparator; |
23 | 26 | import java.util.concurrent.ConcurrentHashMap; |
24 | 27 | import java.util.concurrent.ConcurrentMap; |
25 | 28 |
|
| 29 | +import org.apache.commons.io.FileUtils; |
26 | 30 | import org.apache.sshd.sftp.client.SftpClient; |
27 | 31 | import org.junit.jupiter.api.Test; |
28 | 32 |
|
|
39 | 43 | import org.springframework.integration.endpoint.SourcePollingChannelAdapter; |
40 | 44 | import org.springframework.integration.file.FileHeaders; |
41 | 45 | import org.springframework.integration.file.filters.AcceptAllFileListFilter; |
| 46 | +import org.springframework.integration.file.filters.ChainFileListFilter; |
42 | 47 | import org.springframework.integration.file.remote.session.SessionFactory; |
43 | 48 | import org.springframework.integration.metadata.SimpleMetadataStore; |
44 | 49 | import org.springframework.integration.scheduling.PollerMetadata; |
45 | 50 | import org.springframework.integration.sftp.SftpTestSupport; |
46 | 51 | import org.springframework.integration.sftp.filters.SftpPersistentAcceptOnceFileListFilter; |
| 52 | +import org.springframework.integration.sftp.filters.SftpSimplePatternFileListFilter; |
| 53 | +import org.springframework.integration.sftp.filters.SftpSystemMarkerFilePresentFileListFilter; |
47 | 54 | import org.springframework.integration.sftp.session.SftpFileInfo; |
48 | 55 | import org.springframework.integration.sftp.session.SftpRemoteFileTemplate; |
49 | 56 | import org.springframework.integration.transformer.StreamTransformer; |
@@ -168,6 +175,69 @@ public void testMaxFetchLambdaFilter() throws Exception { |
168 | 175 | StaticMessageHeaderAccessor.getCloseableResource(received).close(); |
169 | 176 | } |
170 | 177 |
|
| 178 | + |
| 179 | + @Test |
| 180 | + public void maxFetchIsAdjustedWhenNoSupportsSingleFileFiltering() throws Exception { |
| 181 | + SftpStreamingMessageSource messageSource = buildSource(); |
| 182 | + ChainFileListFilter<SftpClient.DirEntry> chainFileListFilter = new ChainFileListFilter<>(); |
| 183 | + SftpSystemMarkerFilePresentFileListFilter sftpSystemMarkerFilePresentFileListFilter = |
| 184 | + new SftpSystemMarkerFilePresentFileListFilter( |
| 185 | + new SftpSimplePatternFileListFilter("*"), ".trg"); |
| 186 | + SftpPersistentAcceptOnceFileListFilter sftpPersistentAcceptOnceFileListFilter = |
| 187 | + new SftpPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "prefix"); |
| 188 | + chainFileListFilter.addFilter(sftpSystemMarkerFilePresentFileListFilter); |
| 189 | + chainFileListFilter.addFilter(sftpPersistentAcceptOnceFileListFilter); |
| 190 | + messageSource.setFilter(chainFileListFilter); |
| 191 | + messageSource.setMaxFetchSize(5); |
| 192 | + messageSource.afterPropertiesSet(); |
| 193 | + messageSource.start(); |
| 194 | + |
| 195 | + addFileAndTrigger("file001"); |
| 196 | + addFileAndTrigger("file002"); |
| 197 | + |
| 198 | + Message<InputStream> received = messageSource.receive(); |
| 199 | + assertThat(received).isNotNull(); |
| 200 | + assertThat(received.getHeaders().get(FileHeaders.REMOTE_FILE)).isEqualTo("file001"); |
| 201 | + |
| 202 | + received = messageSource.receive(); |
| 203 | + assertThat(received).isNotNull(); |
| 204 | + assertThat(received.getHeaders().get(FileHeaders.REMOTE_FILE)).isEqualTo("file002"); |
| 205 | + |
| 206 | + addFileAndTrigger("file003"); |
| 207 | + addFileAndTrigger("file004"); |
| 208 | + addFileAndTrigger("file005"); |
| 209 | + addFileAndTrigger("file006"); |
| 210 | + addFileAndTrigger("file007"); |
| 211 | + |
| 212 | + received = messageSource.receive(); |
| 213 | + assertThat(received).isNotNull(); |
| 214 | + assertThat(received.getHeaders().get(FileHeaders.REMOTE_FILE)).isEqualTo("file003"); |
| 215 | + |
| 216 | + received = messageSource.receive(); |
| 217 | + assertThat(received).isNotNull(); |
| 218 | + assertThat(received.getHeaders().get(FileHeaders.REMOTE_FILE)).isEqualTo("file004"); |
| 219 | + |
| 220 | + received = messageSource.receive(); |
| 221 | + assertThat(received).isNotNull(); |
| 222 | + assertThat(received.getHeaders().get(FileHeaders.REMOTE_FILE)).isEqualTo("file005"); |
| 223 | + |
| 224 | + received = messageSource.receive(); |
| 225 | + assertThat(received).isNotNull(); |
| 226 | + assertThat(received.getHeaders().get(FileHeaders.REMOTE_FILE)).isEqualTo("file006"); |
| 227 | + |
| 228 | + received = messageSource.receive(); |
| 229 | + assertThat(received).isNotNull(); |
| 230 | + assertThat(received.getHeaders().get(FileHeaders.REMOTE_FILE)).isEqualTo("file007"); |
| 231 | + } |
| 232 | + |
| 233 | + private void addFileAndTrigger(String filename) throws IOException { |
| 234 | + File file = new File(this.sourceRemoteDirectory, filename); |
| 235 | + FileUtils.writeStringToFile(file, "source1", StandardCharsets.UTF_8); |
| 236 | + |
| 237 | + file = new File(this.sourceRemoteDirectory, filename + ".trg"); |
| 238 | + file.createNewFile(); |
| 239 | + } |
| 240 | + |
171 | 241 | private SftpStreamingMessageSource buildSource() { |
172 | 242 | SftpStreamingMessageSource messageSource = |
173 | 243 | new SftpStreamingMessageSource(this.config.template(), |
|
0 commit comments