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 600f228787..7748c57a1b 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 @@ -1036,6 +1036,10 @@ public abstract class AbstractRemoteFileOutboundGateway extends AbstractReply return; } String fileName = getFilename(fileToAdd); + // Some remote file protocols don't include sub-dir into file name. + if (StringUtils.hasText(subDirectory) && !fileName.startsWith(subDirectory)) { + fileName = subDirectory + fileName; + } final boolean isDirectory = isDirectory(file); boolean isDots = hasDots(fileName); if ((this.options.contains(Option.SUBDIRS) || !isDirectory) @@ -1276,10 +1280,7 @@ public abstract class AbstractRemoteFileOutboundGateway extends AbstractReply private File getRemoteFileForMget(Message message, Session session, String remoteDirectory, AbstractFileInfo lsEntry) throws IOException { - String fullFileName = - remoteDirectory != null - ? remoteDirectory + getFilename(lsEntry) - : getFilename(lsEntry); + String fullFileName = getFullFileName(remoteDirectory, lsEntry.getFileInfo()); /* * With recursion, the filename might contain subdirectory information * normalize each file separately. @@ -1289,6 +1290,20 @@ public abstract class AbstractRemoteFileOutboundGateway extends AbstractReply return get(message, session, actualRemoteDirectory, fullFileName, fileName, lsEntry.getFileInfo()); } + /** + * By default, this method contacts the remote directory with the remote file name + * to build a full remote file path. + * The remote file protocol-specific implementation may override this method for other approach. + * @param remoteDirectory the directory remote file belongs. + * @param remoteFile the remote file to take a name and adjust its path according provided remote directory. + * @return the full path for the remote file + */ + protected String getFullFileName(String remoteDirectory, F remoteFile) { + return remoteDirectory != null + ? remoteDirectory + getFilename(remoteFile) + : getFilename(remoteFile); + } + private String getRemoteDirectory(String remoteFilePath, String remoteFilename) { String remoteDir = remoteFilePath.substring(0, remoteFilePath.lastIndexOf(remoteFilename)); if (remoteDir.length() == 0) { diff --git a/spring-integration-smb/src/main/java/org/springframework/integration/smb/outbound/SmbOutboundGateway.java b/spring-integration-smb/src/main/java/org/springframework/integration/smb/outbound/SmbOutboundGateway.java index f8daf2eff8..dd8737f46a 100644 --- a/spring-integration-smb/src/main/java/org/springframework/integration/smb/outbound/SmbOutboundGateway.java +++ b/spring-integration-smb/src/main/java/org/springframework/integration/smb/outbound/SmbOutboundGateway.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 the original author or authors. + * Copyright 2022-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. @@ -16,8 +16,6 @@ package org.springframework.integration.smb.outbound; -import java.net.MalformedURLException; -import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -145,13 +143,14 @@ public class SmbOutboundGateway extends AbstractRemoteFileOutboundGateway file) { + return getFilename(file.getFileInfo()); } @Override - protected String getFilename(AbstractFileInfo file) { - return file.getFilename(); + protected String getFilename(SmbFile file) { + String name = file.getName(); + return name.endsWith("/") ? name.substring(0, name.length() - 1) : name; } @Override @@ -170,14 +169,12 @@ public class SmbOutboundGateway extends AbstractRemoteFileOutboundGateway(dir + "*")); + registration.getInputChannel().send(new GenericMessage<>("*")); Message result = out.receive(10_000); assertThat(result).isNotNull(); @@ -307,12 +306,10 @@ public class SmbTests extends SmbTestSupport { assertThat(localFiles).as("unexpected local files " + localFiles).hasSize(2); for (File file : localFiles) { - assertThat(file.getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/")).contains(dir); + assertThat(file.getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/")) + .matches(".*smbSource/subSmbSource/localTarget\\d.txt"); } - assertThat(localFiles.get(1).getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/")) - .contains(dir + "subSmbSource"); - registration.destroy(); }