diff --git a/src/main/java/jftp/client/FtpClient.java b/src/main/java/jftp/client/FtpClient.java index b752ac9..851b198 100644 --- a/src/main/java/jftp/client/FtpClient.java +++ b/src/main/java/jftp/client/FtpClient.java @@ -56,6 +56,14 @@ public void disconnect() { } } + private void connectClientAndCheckStatus() throws SocketException, IOException, FtpException { + + ftpClient.connect(host, port); + + if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) + throw new FtpException(String.format(STATUS_ERROR_MESSAGE, host, port)); + } + private void login() throws IOException, FtpException { boolean hasLoggedIn = ftpClient.login(userCredentials.getUsername(), userCredentials.getPassword()); @@ -72,12 +80,4 @@ private void setSpecificModesOnClient() throws IOException { ftpClient.setControlKeepAliveTimeout(FIVE_MINUTES); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); } - - private void connectClientAndCheckStatus() throws SocketException, IOException, FtpException { - - ftpClient.connect(host, port); - - if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) - throw new FtpException(String.format(STATUS_ERROR_MESSAGE, host, port)); - } } diff --git a/src/main/java/jftp/client/SftpClient.java b/src/main/java/jftp/client/SftpClient.java index 3ea8547..6a7f4dd 100644 --- a/src/main/java/jftp/client/SftpClient.java +++ b/src/main/java/jftp/client/SftpClient.java @@ -51,12 +51,6 @@ public void disconnect() { session.disconnect(); } - private void openChannelFromSession() throws JSchException { - - channel = session.openChannel(SFTP); - channel.connect(); - } - private void configureSessionAndConnect() throws JSchException { session = jsch.getSession(userCredentials.getUsername(), host, port); @@ -65,4 +59,10 @@ private void configureSessionAndConnect() throws JSchException { session.connect(); } + + private void openChannelFromSession() throws JSchException { + + channel = session.openChannel(SFTP); + channel.connect(); + } } diff --git a/src/main/java/jftp/connection/Connection.java b/src/main/java/jftp/connection/Connection.java index 6462d7a..51b1d74 100644 --- a/src/main/java/jftp/connection/Connection.java +++ b/src/main/java/jftp/connection/Connection.java @@ -9,13 +9,13 @@ public interface Connection { void changeDirectory(String directory) throws FtpException; - String printWorkingDirectory() throws FtpException; + void download(String remoteFilePath, String localDirectory) throws FtpException; List listFiles() throws FtpException; List listFiles(String path) throws FtpException; - void download(String remoteFilePath, String localDirectory) throws FtpException; + String printWorkingDirectory() throws FtpException; void upload(String localFilePath, String remoteDirectory) throws FtpException; } diff --git a/src/main/java/jftp/connection/FtpConnection.java b/src/main/java/jftp/connection/FtpConnection.java index aafb404..6572039 100644 --- a/src/main/java/jftp/connection/FtpConnection.java +++ b/src/main/java/jftp/connection/FtpConnection.java @@ -50,15 +50,27 @@ public void changeDirectory(String directory) throws FtpException { } @Override - public String printWorkingDirectory() throws FtpException { + public void download(String remoteFilePath, String localDirectory) throws FtpException { + + String localDestination = determinePath(remoteFilePath, localDirectory); try { - return client.printWorkingDirectory(); + OutputStream outputStream = fileStreamFactory.createOutputStream(localDestination); + + boolean hasDownloaded = client.retrieveFile(remoteFilePath, outputStream); + + outputStream.close(); + + ensureFileHasSuccessfullyDownloaded(hasDownloaded); + + } catch (FileNotFoundException e) { + + throw new FtpException(String.format(FILE_STREAM_OPEN_FAIL_MESSAGE, localDestination), e); } catch (IOException e) { - throw new FtpException("Unable to print the working directory", e); + throw new FtpException(String.format(FILE_DOWNLOAD_FAILURE_MESSAGE, remoteFilePath), e); } } @@ -97,27 +109,15 @@ public List listFiles(String remotePath) throws FtpException { } @Override - public void download(String remoteFilePath, String localDirectory) throws FtpException { - - String localDestination = determinePath(remoteFilePath, localDirectory); + public String printWorkingDirectory() throws FtpException { try { - OutputStream outputStream = fileStreamFactory.createOutputStream(localDestination); - - boolean hasDownloaded = client.retrieveFile(remoteFilePath, outputStream); - - outputStream.close(); - - ensureFileHasSuccessfullyDownloaded(hasDownloaded); - - } catch (FileNotFoundException e) { - - throw new FtpException(String.format(FILE_STREAM_OPEN_FAIL_MESSAGE, localDestination), e); + return client.printWorkingDirectory(); } catch (IOException e) { - throw new FtpException(String.format(FILE_DOWNLOAD_FAILURE_MESSAGE, remoteFilePath), e); + throw new FtpException("Unable to print the working directory", e); } } @@ -153,18 +153,18 @@ private String determinePath(String sourcePathWithName, String targetPathWithout return safePath + targetPath.getFileSystem().getSeparator() + fileName; } - private void ensureFileHasSuccessfullyUploaded(boolean hasUploaded) { - - if (!hasUploaded) - throw new FtpException("Upload failed."); - } - private void ensureFileHasSuccessfullyDownloaded(boolean hasDownloaded) { if (!hasDownloaded) throw new FtpException("Server returned failure while downloading."); } + private void ensureFileHasSuccessfullyUploaded(boolean hasUploaded) { + + if (!hasUploaded) + throw new FtpException("Upload failed."); + } + private FtpFile toFtpFile(FTPFile ftpFile, String filePath) throws IOException { String name = ftpFile.getName(); diff --git a/src/main/java/jftp/connection/SftpConnection.java b/src/main/java/jftp/connection/SftpConnection.java index 014a58c..288fb99 100644 --- a/src/main/java/jftp/connection/SftpConnection.java +++ b/src/main/java/jftp/connection/SftpConnection.java @@ -47,15 +47,15 @@ public void changeDirectory(String directory) throws FtpException { } @Override - public String printWorkingDirectory() throws FtpException { - + public void download(String remoteFilePath, String localDirectory) throws FtpException { + try { - - return channel.pwd(); - + + channel.get(remoteFilePath, localDirectory); + } catch (SftpException e) { - throw new FtpException("Unable to print the working directory", e); + throw new FtpException("Unable to download file " + remoteFilePath, e); } } @@ -95,15 +95,15 @@ public List listFiles(String remotePath) throws FtpException { } @Override - public void download(String remoteFilePath, String localDirectory) throws FtpException { - + public String printWorkingDirectory() throws FtpException { + try { - - channel.get(remoteFilePath, localDirectory); - + + return channel.pwd(); + } catch (SftpException e) { - throw new FtpException("Unable to download file " + remoteFilePath, e); + throw new FtpException("Unable to print the working directory", e); } } diff --git a/src/test/java/jftp/connection/FtpConnectionTest.java b/src/test/java/jftp/connection/FtpConnectionTest.java index d897862..333d885 100644 --- a/src/test/java/jftp/connection/FtpConnectionTest.java +++ b/src/test/java/jftp/connection/FtpConnectionTest.java @@ -337,11 +337,15 @@ private FTPFile[] createRemoteFTPFiles() { when(file.getName()).thenReturn("File " + (i + 1)); when(file.getSize()).thenReturn((long) (i + 1) * 1000); when(file.getTimestamp()).thenReturn(calendar); - when(file.isDirectory()).thenReturn((i + 1) % 2 == 0 ? true : false); + when(file.isDirectory()).thenReturn(setTrueIfNumberIsEven(i)); files[i] = file; } return files; } + + private boolean setTrueIfNumberIsEven(int i) { + return (i + 1) % 2 == 0 ? true : false; + } }