diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/session/FtpSession.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/session/FtpSession.java index cb5f7848a4..344670aee4 100644 --- a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/session/FtpSession.java +++ b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/session/FtpSession.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-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. @@ -111,6 +111,10 @@ public class FtpSession implements Session { if (!this.readingRaw.compareAndSet(true, false)) { throw new IOException("Raw read is not in process"); } + if (FTPReply.isNegativePermanent(this.client.getReplyCode())) { + // The 'readRaw()' has failed - nothing to complete. + return true; + } if (this.client.completePendingCommand()) { int replyCode = this.client.getReplyCode(); if (LOGGER.isDebugEnabled()) { diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound/FtpServerOutboundTests.java b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound/FtpServerOutboundTests.java index 4b79754ead..51b9510679 100644 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound/FtpServerOutboundTests.java +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound/FtpServerOutboundTests.java @@ -758,6 +758,22 @@ public class FtpServerOutboundTests extends FtpTestSupport { this.config.latch = null; } + @Test + void finalizeRawIsOkEvenIfReadRawIsNot() throws IOException { + Session session = this.sessionFactory.getSession(); + IOException expectedException = null; + try (InputStream stream = session.readRaw("no_such_file")) { + stream.read(); // Just to avoid empty 'try' block + } + catch (IOException ex) { + expectedException = ex; + } + finally { + assertThat(session.finalizeRaw()).isTrue(); + } + assertThat(expectedException).hasMessage("Failed to obtain InputStream for remote file no_such_file: 550"); + } + private void resetSessionCache() { ((CachingSessionFactory) this.sessionFactory).resetCache(); }