Skip to content

Commit

Permalink
Code cleanup (#78)
Browse files Browse the repository at this point in the history
* doc: update README

* doc: update pom.xml

* code cleanup
  • Loading branch information
Ronnie Song authored Aug 15, 2019
1 parent b38cffb commit 489d47d
Show file tree
Hide file tree
Showing 23 changed files with 255 additions and 281 deletions.
31 changes: 17 additions & 14 deletions src/main/java/com/psuagilegroup/App.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.psuagilegroup;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.net.ftp.FTPClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class App {

private static void shell(FTPClient ftpClient, connectInfo save) throws IOException {
Expand All @@ -16,19 +19,19 @@ private static void shell(FTPClient ftpClient, connectInfo save) throws IOExcept

HashMap<String, Command> commands = new HashMap<>();
commands.put("", new emptyCommand(ftpClient));
commands.put("logout", new logoutCommand(ftpClient));
commands.put("get", new getCommand(ftpClient));
commands.put("cd", new cdCommand(ftpClient));
commands.put("chmod", new chmodCommand(ftpClient));
commands.put("get", new getCommand(ftpClient));
commands.put("login", new loginCommand(ftpClient));
commands.put("logout", new logoutCommand(ftpClient));
commands.put("ls", new lsCommand(ftpClient));
commands.put("rls", new rlsCommand(ftpClient));
commands.put("rrn", new rrnCommand(ftpClient));
commands.put("rn", new rnCommand(ftpClient));
commands.put("mkdir", new mkdirCommand(ftpClient));
commands.put("put", new putCommand(ftpClient));
commands.put("rls", new rlsCommand(ftpClient));
commands.put("rmdir", new rmdirCommand(ftpClient));
commands.put("login", new loginCommand(ftpClient));
commands.put("rn", new rnCommand(ftpClient));
commands.put("rrm", new rrmCommand(ftpClient));
commands.put("chmod", new chmodCommand(ftpClient));
commands.put("rrn", new rrnCommand(ftpClient));

commands.get("login").run(currentSession, new String[0]);
while (true) {
Expand Down Expand Up @@ -60,9 +63,9 @@ public static void main(String[] args) {
// Variable from the command line argument
// ftpupload.net 21 epiz_24139835 OkmvEHWbl4HFJ8a
connectInfo save = new connectInfo();
if( args.length < 4 ){
save.readInfo();
}else {
if (args.length < 4) {
save.readInfo();
} else {
save.saveInfo(args[0], Integer.parseInt(args[1]), args[2], args[3]);
try {
save.saveInfo();
Expand All @@ -75,7 +78,7 @@ public static void main(String[] args) {

try {
System.out.println();
shell(ftpClient,save);
shell(ftpClient, save);

} catch (IOException e) {
System.out.println("Oops! Something wrong happened here");
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/com/psuagilegroup/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

public abstract class Command {
FTPClient ftpClient;
public Command(FTPClient ftpClient)
{

public Command(FTPClient ftpClient) {
this.ftpClient = ftpClient;
}
public abstract FTPSession run( FTPSession currentSession, String[] args );
public String help(){return "";}

public abstract FTPSession run(FTPSession currentSession, String[] args);

public String help() {
return "";
}

protected void show_Message_fromServer() {
String[] replies = ftpClient.getReplyStrings();
if (replies != null && replies.length > 0) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/psuagilegroup/FTPSession.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.psuagilegroup;

public class FTPSession {
String local_directory="";
String remote_directory="";
String local_directory = "";
String remote_directory = "";

String output="";
String output = "";

connectInfo save;
}
18 changes: 9 additions & 9 deletions src/main/java/com/psuagilegroup/cdCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@
import java.io.IOException;

public class cdCommand extends Command {
public cdCommand(FTPClient ftpClient)
{
public cdCommand(FTPClient ftpClient) {
super(ftpClient);
}

@Override
public String help(){
return "cd\t\tChanges the current directory.";
public String help() {
return "cd\t\t\tChanges the current directory.";
}

@Override
public FTPSession run( FTPSession currentSession, String[] lineSplit ){
public FTPSession run(FTPSession currentSession, String[] lineSplit) {

//FIXME
if (lineSplit.length == 2) {
change_working_directory_on_server(currentSession, lineSplit[1]);
}else {
} else {
currentSession.output = "Error: Invalid Argument";
}
return currentSession;
}

private void change_working_directory_on_server(FTPSession currentSession, String remotePath){
private void change_working_directory_on_server(FTPSession currentSession, String remotePath) {
try {
if(remotePath == ".."){
if (remotePath == "..") {
ftpClient.changeToParentDirectory();
} else {
ftpClient.changeWorkingDirectory(remotePath);
Expand All @@ -38,7 +38,7 @@ private void change_working_directory_on_server(FTPSession currentSession, Strin
}
try {
currentSession.remote_directory = ftpClient.printWorkingDirectory();
}catch(IOException e){
} catch (IOException e) {
e.printStackTrace();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/psuagilegroup/chmodCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public chmodCommand(FTPClient ftpClient) {

@Override
public String help() {
return "chmod\t\tchange mode.";
return "chmod\t\tChanges mode.";
}

@Override
Expand Down
29 changes: 13 additions & 16 deletions src/main/java/com/psuagilegroup/connectInfo.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
package com.psuagilegroup;

import org.apache.commons.net.ftp.FTPClient;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collection;
import java.io.*;

public class connectInfo {
String server;
int port;
String user;
String pass;

public connectInfo(){}
public connectInfo(String server, int port, String user, String pass){
public connectInfo() {
}

public connectInfo(String server, int port, String user, String pass) {
this.server = server;
this.port = port;
this.user = user;
this.pass = pass;
}
public void saveInfo()throws IOException{

public void saveInfo() throws IOException {
BufferedWriter bw = null;
FileWriter fw = null;

Expand All @@ -39,24 +35,25 @@ public void saveInfo()throws IOException{
bw.newLine();
bw.write(this.pass);
bw.close();
}catch(IOException ex){
} catch (IOException ex) {
throw new IOException("Error to write to the file.");
}
}
public void saveInfo(String server, int port, String user, String pass){

public void saveInfo(String server, int port, String user, String pass) {
this.server = server;
this.port = port;
this.user = user;
this.pass = pass;
try {
this.saveInfo();
}catch(IOException EX){
} catch (IOException EX) {
//throw new IOException("Error to write to the file.");
System.out.println(EX);
}
}

public void readInfo(){
public void readInfo() {
BufferedReader br = null;
FileReader fr = null;
try {
Expand All @@ -68,7 +65,7 @@ public void readInfo(){
this.user = br.readLine();
this.pass = br.readLine();
br.close();
}catch(IOException ex) {
} catch (IOException ex) {
System.out.println(ex);
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/com/psuagilegroup/emptyCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@

import org.apache.commons.net.ftp.FTPClient;

public class emptyCommand extends Command
{
public emptyCommand(FTPClient ftpClient)
{
public class emptyCommand extends Command {
public emptyCommand(FTPClient ftpClient) {
super(ftpClient);
}

@Override
public FTPSession run( FTPSession currentSession, String[] args )
{
public FTPSession run(FTPSession currentSession, String[] args) {
return currentSession;
}
}
Loading

0 comments on commit 489d47d

Please sign in to comment.