INT-1614 moved file copy code up to base class, subclasses now only implement isFile and getFilename methods
This commit is contained in:
@@ -16,19 +16,11 @@
|
||||
|
||||
package org.springframework.integration.ftp.inbound;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.apache.commons.net.ftp.FTPFile;
|
||||
|
||||
import org.springframework.integration.MessagingException;
|
||||
import org.springframework.integration.file.remote.session.Session;
|
||||
import org.springframework.integration.file.remote.session.SessionFactory;
|
||||
import org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer;
|
||||
import org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizingMessageSource;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
/**
|
||||
* An FTP-adapter implementation of {@link org.springframework.integration.file.synchronization.AbstractInboundRemoteFileSystemSychronizer}
|
||||
@@ -47,51 +39,13 @@ public class FtpInboundFileSynchronizer extends AbstractInboundFileSynchronizer<
|
||||
|
||||
|
||||
@Override
|
||||
protected boolean copyFileToLocalDirectory(String remoteDirectoryPath, FTPFile ftpFile, File localDirectory, Session session) throws IOException {
|
||||
if (!ftpFile.isFile()) {
|
||||
return false;
|
||||
}
|
||||
String remoteFileName = ftpFile.getName();
|
||||
String localFileName = localDirectory.getPath() + "/" + remoteFileName;
|
||||
File localFile = new File(localFileName);
|
||||
if (!localFile.exists()) {
|
||||
String tempFileName = localFileName + AbstractInboundFileSynchronizingMessageSource.INCOMPLETE_EXTENSION;
|
||||
File file = new File(tempFileName);
|
||||
FileOutputStream fileOutputStream = new FileOutputStream(file);
|
||||
try {
|
||||
InputStream inputStream = session.get(remoteFileName);
|
||||
if (inputStream == null) {
|
||||
return false;
|
||||
}
|
||||
FileCopyUtils.copy(inputStream, fileOutputStream);
|
||||
if (this.shouldDeleteSourceFile) {
|
||||
this.deleteRemoteFile(session, ftpFile);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
if (e instanceof RuntimeException){
|
||||
throw (RuntimeException) e;
|
||||
}
|
||||
else {
|
||||
throw new MessagingException("Failed to copy file", e);
|
||||
}
|
||||
}
|
||||
finally {
|
||||
fileOutputStream.close();
|
||||
}
|
||||
file.renameTo(localFile);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
protected boolean isFile(FTPFile file) {
|
||||
return file.isFile();
|
||||
}
|
||||
|
||||
// TODO: make this an abstract method in the base class once the code that calls this is refactored upward
|
||||
private void deleteRemoteFile(Session session, FTPFile ftpFile) {
|
||||
if ((ftpFile != null) && session.rm(ftpFile.getName())) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("deleted " + ftpFile.getName());
|
||||
}
|
||||
}
|
||||
@Override
|
||||
protected String getFilename(FTPFile file) {
|
||||
return file.getName();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.integration.ftp.session;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
@@ -27,7 +26,6 @@ import org.apache.commons.net.ftp.FTPFile;
|
||||
|
||||
import org.springframework.integration.file.remote.session.Session;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
@@ -72,9 +70,9 @@ public class FtpSession implements Session {
|
||||
}
|
||||
}
|
||||
|
||||
public InputStream get(String source) {
|
||||
public InputStream get(String path) {
|
||||
try {
|
||||
InputStream inputStream = this.client.retrieveFileStream(source);
|
||||
InputStream inputStream = this.client.retrieveFileStream(path);
|
||||
this.client.completePendingCommand();
|
||||
return inputStream;
|
||||
}
|
||||
@@ -86,31 +84,15 @@ public class FtpSession implements Session {
|
||||
}
|
||||
}
|
||||
|
||||
public void put(InputStream inputStream, String destination) {
|
||||
String originalWorkDir = null;
|
||||
public void put(InputStream inputStream, String path) {
|
||||
Assert.notNull(inputStream, "inputStream must not be null");
|
||||
Assert.notNull(path, "path must not be null");
|
||||
try {
|
||||
String fileName = StringUtils.getFilename(destination);
|
||||
int startOfFileName = destination.lastIndexOf(File.separatorChar);
|
||||
|
||||
if (startOfFileName > 0) {
|
||||
originalWorkDir = client.printWorkingDirectory();
|
||||
String pathname = destination.substring(0, startOfFileName);
|
||||
client.changeWorkingDirectory(pathname);
|
||||
}
|
||||
this.client.storeFile(fileName, inputStream);
|
||||
this.client.storeFile(path, inputStream);
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException("failed to copy file", e);
|
||||
}
|
||||
finally {
|
||||
if (originalWorkDir != null){
|
||||
try {
|
||||
this.client.changeWorkingDirectory(originalWorkDir);
|
||||
} catch (IOException ioex) {
|
||||
throw new IllegalStateException("failed to change working directories ", ioex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
|
||||
@@ -127,12 +127,10 @@ public class FtpSendingMessageHandlerTest {
|
||||
when(ftpClient.changeWorkingDirectory(Mockito.anyString())).thenReturn(true);
|
||||
when(ftpClient.printWorkingDirectory()).thenReturn("remote-target-dir");
|
||||
when(ftpClient.storeFile(Mockito.anyString(), Mockito.any(InputStream.class))).thenAnswer(new Answer<Boolean>() {
|
||||
public Boolean answer(InvocationOnMock invocation)
|
||||
throws Throwable {
|
||||
public Boolean answer(InvocationOnMock invocation) throws Throwable {
|
||||
String fileName = (String) invocation.getArguments()[0];
|
||||
InputStream fis = (InputStream) invocation.getArguments()[1];
|
||||
String workingDirectory = ftpClient.printWorkingDirectory();
|
||||
FileCopyUtils.copy(fis, new FileOutputStream(workingDirectory + File.separator + fileName));
|
||||
FileCopyUtils.copy(fis, new FileOutputStream(fileName));
|
||||
return true;
|
||||
}
|
||||
});
|
||||
@@ -142,4 +140,5 @@ public class FtpSendingMessageHandlerTest {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user