diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/RemoteFileUtils.java b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/RemoteFileUtils.java index ff9f8e52ca..6ceb92f942 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/RemoteFileUtils.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/RemoteFileUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -28,6 +28,7 @@ import org.springframework.integration.file.remote.session.Session; * Utility methods for supporting remote file operations. * * @author Gary Russell + * @author Artem Bilan * * @since 3.0 * @@ -46,13 +47,11 @@ public final class RemoteFileUtils { * @param logger The logger. * @throws IOException Any IOException. */ - public static synchronized void makeDirectories(String path, Session session, String remoteFileSeparator, + public static void makeDirectories(String path, Session session, String remoteFileSeparator, Log logger) throws IOException { if (!session.exists(path)) { - int nextSeparatorIndex = path.lastIndexOf(remoteFileSeparator); - if (nextSeparatorIndex > -1) { List pathsToCreate = new LinkedList<>(); while (nextSeparatorIndex > -1) { @@ -71,13 +70,19 @@ public final class RemoteFileUtils { if (logger.isDebugEnabled()) { logger.debug("Creating '" + pathToCreate + "'"); } - session.mkdir(pathToCreate); + tryCreateRemoteDirectory(session, pathToCreate); } } else { - session.mkdir(path); + tryCreateRemoteDirectory(session, path); } } } + private static void tryCreateRemoteDirectory(Session session, String path) throws IOException { + if (!session.mkdir(path)) { + throw new IOException("Could not create a remote directory: " + path); + } + } + } diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java index 7dfd58cefa..deb458534b 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -121,6 +121,7 @@ public abstract class AbstractRemoteFileOutboundGateway extends AbstractReply MessageSessionCallback messageSessionCallback) { this(new RemoteFileTemplate<>(sessionFactory), messageSessionCallback); + remoteFileTemplateExplicitlySet(false); } /** @@ -165,6 +166,7 @@ public abstract class AbstractRemoteFileOutboundGateway extends AbstractReply @Nullable String expression) { this(new RemoteFileTemplate<>(sessionFactory), command, expression); + remoteFileTemplateExplicitlySet(false); } /** @@ -516,6 +518,9 @@ public abstract class AbstractRemoteFileOutboundGateway extends AbstractReply } populateBeanFactoryIntoComponentsIfAny(); + if (!this.remoteFileTemplateExplicitlySet) { + this.remoteFileTemplate.afterPropertiesSet(); + } } private void populateBeanFactoryIntoComponentsIfAny() { diff --git a/spring-integration-file/src/test/java/org/springframework/integration/file/remote/gateway/RemoteFileOutboundGatewayTests.java b/spring-integration-file/src/test/java/org/springframework/integration/file/remote/gateway/RemoteFileOutboundGatewayTests.java index eb2a19dc55..55c73cdbe2 100644 --- a/spring-integration-file/src/test/java/org/springframework/integration/file/remote/gateway/RemoteFileOutboundGatewayTests.java +++ b/spring-integration-file/src/test/java/org/springframework/integration/file/remote/gateway/RemoteFileOutboundGatewayTests.java @@ -295,7 +295,7 @@ public class RemoteFileOutboundGatewayTests { final List madeDirs = new ArrayList<>(); doAnswer(invocation -> { madeDirs.add(invocation.getArgument(0)); - return null; + return true; }).when(session).mkdir(anyString()); when(sessionFactory.getSession()).thenReturn(session); Message requestMessage = MessageBuilder.withPayload("foo") @@ -939,7 +939,6 @@ public class RemoteFileOutboundGatewayTests { Session session = mock(Session.class); TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "mput", "payload"); gw.setRemoteDirectoryExpression(new LiteralExpression("foo/")); - gw.setBeanFactory(mock(BeanFactory.class)); gw.afterPropertiesSet(); when(sessionFactory.getSession()).thenReturn(session); final AtomicReference written = new AtomicReference<>(); diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SftpSession.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SftpSession.java index ce4ebaf4bf..c3bb4438f3 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SftpSession.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SftpSession.java @@ -252,8 +252,10 @@ public class SftpSession implements Session { try { this.channel.mkdir(remoteDirectory); } - catch (SftpException e) { - throw new NestedIOException("failed to create remote directory '" + remoteDirectory + "'.", e); + catch (SftpException ex) { + if (ex.id != ChannelSftp.SSH_FX_FAILURE || !exists(remoteDirectory)) { + throw new NestedIOException("failed to create remote directory '" + remoteDirectory + "'.", ex); + } } return true; } diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpOutboundTests.java b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpOutboundTests.java index d172cf5080..0f0f2cb4c7 100644 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpOutboundTests.java +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpOutboundTests.java @@ -219,7 +219,7 @@ public class SftpOutboundTests { final List madeDirs = new ArrayList<>(); doAnswer(invocation -> { madeDirs.add(invocation.getArgument(0)); - return null; + return true; }).when(session).mkdir(anyString()); handler.handleMessage(new GenericMessage<>("qux")); assertThat(madeDirs.size()).isEqualTo(3);