Skip to content

Commit

Permalink
Sorted all methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Greatrex committed May 8, 2014
1 parent f1389f2 commit 6f27a95
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 53 deletions.
16 changes: 8 additions & 8 deletions src/main/java/jftp/client/FtpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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));
}
}
12 changes: 6 additions & 6 deletions src/main/java/jftp/client/SftpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -65,4 +59,10 @@ private void configureSessionAndConnect() throws JSchException {

session.connect();
}

private void openChannelFromSession() throws JSchException {

channel = session.openChannel(SFTP);
channel.connect();
}
}
4 changes: 2 additions & 2 deletions src/main/java/jftp/connection/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<FtpFile> listFiles() throws FtpException;

List<FtpFile> 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;
}
48 changes: 24 additions & 24 deletions src/main/java/jftp/connection/FtpConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -97,27 +109,15 @@ public List<FtpFile> 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);
}
}

Expand Down Expand Up @@ -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();
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/jftp/connection/SftpConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -95,15 +95,15 @@ public List<FtpFile> 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);
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/test/java/jftp/connection/FtpConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 6f27a95

Please sign in to comment.