Skip to content

Commit 23b1a9f

Browse files
artembilangaryrussell
authored andcommitted
GH-3168: Fix FtpSession warning on logout
Fixes #3168 * Call `this.client.noop()` instead of `this.client.isConnected()` to really check that client has a live connection with the server before calling a `this.client.logout()` * Wrap `this.client.logout()` into a `try..catch` to be sure that we wil call a `this.client.disconnect()` even if `logout()` fails for some reason. * Change `WARN` logs about `Session.close()` into a `DEBUG` level - when we have a problem with closing session because of server disconnect reason we have no any control to do with a situation **Cherry-pick to 5.2.x**
1 parent 1bd9954 commit 23b1a9f

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

spring-integration-ftp/src/main/java/org/springframework/integration/ftp/session/FtpSession.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -152,16 +152,21 @@ public void append(InputStream inputStream, String path) throws IOException {
152152
@Override
153153
public void close() {
154154
try {
155-
if (this.readingRaw.get() && !finalizeRaw() && LOGGER.isWarnEnabled()) {
156-
LOGGER.warn("Finalize on readRaw() returned false for " + this);
155+
if (this.readingRaw.get() && !finalizeRaw() && LOGGER.isDebugEnabled()) {
156+
LOGGER.debug("Finalize on readRaw() returned false for " + this);
157157
}
158-
if (this.client.isConnected()) {
159-
this.client.logout();
158+
if (isOpen()) {
159+
try {
160+
this.client.logout();
161+
}
162+
catch (IOException ex) {
163+
LOGGER.debug("failed to logout FTPClient", ex);
164+
}
160165
}
161166
this.client.disconnect();
162167
}
163-
catch (Exception e) {
164-
LOGGER.warn("failed to disconnect FTPClient", e);
168+
catch (Exception ex) {
169+
LOGGER.debug("failed to disconnect FTPClient", ex);
165170
}
166171
}
167172

@@ -170,7 +175,8 @@ public boolean isOpen() {
170175
try {
171176
this.client.noop();
172177
}
173-
catch (Exception e) {
178+
catch (Exception ex) {
179+
LOGGER.debug("failed to noop FTPClient", ex);
174180
return false;
175181
}
176182
return true;

spring-integration-ftp/src/test/java/org/springframework/integration/ftp/session/SessionFactoryTests.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
2222
import static org.assertj.core.api.Assertions.fail;
2323
import static org.mockito.Mockito.doReturn;
24+
import static org.mockito.Mockito.doThrow;
2425
import static org.mockito.Mockito.mock;
26+
import static org.mockito.Mockito.times;
2527
import static org.mockito.Mockito.verify;
2628

2729
import java.lang.reflect.Field;
@@ -69,7 +71,6 @@ protected FTPClient createClientInstance() {
6971
sessionFactory.setDataTimeout(789);
7072
doReturn(200).when(client).getReplyCode();
7173
doReturn(true).when(client).login("foo", null);
72-
doReturn(true).when(client).isConnected();
7374
FtpSession session = sessionFactory.getSession();
7475
verify(client).setConnectTimeout(123);
7576
verify(client).setDefaultTimeout(456);
@@ -79,6 +80,13 @@ protected FTPClient createClientInstance() {
7980

8081
verify(client).logout();
8182
verify(client).disconnect();
83+
84+
doThrow(RuntimeException.class).when(client).noop();
85+
86+
session.close();
87+
88+
verify(client).logout();
89+
verify(client, times(2)).disconnect();
8290
}
8391

8492
@Test

0 commit comments

Comments
 (0)