GH-2820: Close session in FtpRemFileTempl.exists

Fixes: https://github.com/spring-projects/spring-integration/issues/2820

The current `FtpRemoteFileTemplate.exists()` implementation obtains a
`Session` for the `NLST_AND_DIRS` mode, but doesn't close it.

* Fix the `FtpRemoteFileTemplate.exists()` method to delegate to `super`
for the proper interaction with a `Session`

**Cherry-pick to 5.1.x, 5.0.x & 4.3.x**
This commit is contained in:
Artem Bilan
2019-03-18 18:51:16 -04:00
committed by Gary Russell
parent 00ca87fe40
commit 4aea880cef
2 changed files with 29 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-2019 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.
@@ -92,7 +92,7 @@ public class FtpRemoteFileTemplate extends RemoteFileTemplate<FTPFile> {
return !ObjectUtils.isEmpty(names);
case NLST_AND_DIRS:
return getSession().exists(path);
return FtpRemoteFileTemplate.super.exists(path);
default:
throw new IllegalStateException("Unsupported 'existsMode': " +

View File

@@ -33,6 +33,7 @@ import org.apache.commons.net.ftp.FTPFile;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -41,7 +42,10 @@ import org.springframework.integration.file.DefaultFileNameGenerator;
import org.springframework.integration.file.remote.ClientCallbackWithoutResult;
import org.springframework.integration.file.remote.SessionCallbackWithoutResult;
import org.springframework.integration.file.remote.session.SessionFactory;
import org.springframework.integration.file.support.FileExistsMode;
import org.springframework.integration.ftp.FtpTestSupport;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.integration.util.SimplePool;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.context.ContextConfiguration;
@@ -60,7 +64,7 @@ public class FtpRemoteFileTemplateTests extends FtpTestSupport {
private SessionFactory<FTPFile> sessionFactory;
@Test
public void testINT3412AppendStatRmdir() throws IOException {
public void testINT3412AppendStatRmdir() {
FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(sessionFactory);
DefaultFileNameGenerator fileNameGenerator = new DefaultFileNameGenerator();
fileNameGenerator.setExpression("'foobar.txt'");
@@ -90,7 +94,7 @@ public class FtpRemoteFileTemplateTests extends FtpTestSupport {
assertEquals(0, files.length);
assertTrue(session.rmdir("foo/"));
});
assertFalse(template.getSession().exists("foo"));
assertFalse(template.exists("foo"));
}
@Test
@@ -118,6 +122,27 @@ public class FtpRemoteFileTemplateTests extends FtpTestSupport {
newFile.delete();
}
@Test
public void testConnectionClosedAfterExists() throws Exception {
FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(this.sessionFactory);
template.setRemoteDirectoryExpression(new LiteralExpression("/"));
template.setExistsMode(FtpRemoteFileTemplate.ExistsMode.NLST_AND_DIRS);
template.setBeanFactory(mock(BeanFactory.class));
template.afterPropertiesSet();
File file = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write("foo".getBytes());
fileOutputStream.close();
template.send(new GenericMessage<>(file), FileExistsMode.IGNORE);
File newFile = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
assertTrue(file.renameTo(newFile));
file.delete();
newFile.delete();
SimplePool<?> pool = TestUtils.getPropertyValue(this.sessionFactory, "pool", SimplePool.class);
assertEquals(0, pool.getActiveCount());
}
@Configuration
public static class Config {