Skip to content

Commit

Permalink
FTP:
Browse files Browse the repository at this point in the history
- report connection status (to break if no connection possible)
- fixed isFolder()
- additional error output
- fixed paths with encoded symbols (ie. a%20file.txt)
- refactoring


git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@4567 6c8d7289-2bf4-0310-a012-ef5d649a1542
  • Loading branch information
danielr committed Mar 15, 2008
1 parent 8206419 commit f51bad8
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 117 deletions.
149 changes: 95 additions & 54 deletions source/de/anomic/net/ftpc.java
Expand Up @@ -249,11 +249,11 @@ public boolean exec(String command, final boolean promptIt) {
.booleanValue());
} catch (final InvocationTargetException e) {
if (e.getMessage() == null) {
} else if (ControlSocket == null) {
} else if (notConnected()) {
// the error was probably caused because there is no
// connection
errPrintln("not connected. no effect.");
e.printStackTrace();
e.printStackTrace(err);
return ret;
} else {
errPrintln("ftp internal exception: target exception " + e);
Expand All @@ -266,7 +266,7 @@ public boolean exec(String command, final boolean promptIt) {
// consider first that the user attempted to execute a java
// command from
// the current path; either local or remote
if (ControlSocket == null) {
if (notConnected()) {
// try a local exec
try {
javaexec(cmd);
Expand Down Expand Up @@ -470,7 +470,7 @@ public boolean CD() {
errPrintln("Syntax: CD <path>");
return true;
}
if (ControlSocket == null) {
if (notConnected()) {
return LCD();
}
try {
Expand Down Expand Up @@ -531,7 +531,7 @@ public boolean DEL() {
errPrintln("Syntax: DEL <file>");
return true;
}
if (ControlSocket == null) {
if (notConnected()) {
return LDEL();
}
try {
Expand All @@ -551,7 +551,7 @@ public boolean DIR() {
errPrintln("Syntax: DIR [<path>|<file>]");
return true;
}
if (ControlSocket == null) {
if (notConnected()) {
return LDIR();
}
try {
Expand All @@ -575,18 +575,21 @@ public boolean DISCONNECT() {
} catch (final IOException e) {
errPrintln("Connection to server lost.");
}
ControlSocket = null;
DataSocketActive = null;
DataSocketPassive = null;
clientInput = null;
clientOutput = null;
try {
closeConnection();
} catch (final IOException e) {
ControlSocket = null;
DataSocketActive = null;
DataSocketPassive = null;
clientInput = null;
clientOutput = null;
}
prompt = "ftp [local]>";
return true;
}

private String quit() throws IOException {

// send delete command
send("QUIT");

// read status reply
Expand All @@ -595,24 +598,7 @@ private String quit() throws IOException {
throw new IOException(reply);
}

// cleanup
if (ControlSocket != null) {
clientOutput.close();
clientInput.close();
ControlSocket.close();
ControlSocket = null;
}

if (DataSocketActive != null) {
DataSocketActive.close();
DataSocketActive = null;
}
if (DataSocketPassive != null) {
DataSocketPassive.close();
DataSocketPassive = null; // "Once a socket has been closed, it is
// not available for further networking
// use"
}
closeConnection();

return reply;
}
Expand All @@ -633,8 +619,8 @@ public boolean GET() {
final File local = absoluteLocalFile(localFilename);

if (local.exists()) {
errPrintln("Error: local file " + local.toString() + " already exists.");
errPrintln(logPrefix + " File " + remote + " not retrieved. Local file unchanged.");
errPrintln("Error: local file " + local.toString() + " already exists.\n" + " File " + remote
+ " not retrieved. Local file unchanged.");
} else {
if (withoutLocalFile) {
retrieveFilesRecursively(remote, false);
Expand Down Expand Up @@ -732,7 +718,8 @@ public boolean isFolder(final String path) {
}
// check if we actually changed into the folder
final String changedPath = pwd();
if (!(changedPath.equals(path) || changedPath.equals(currentFolder + "/" + path))) {
if (!(changedPath.equals(path) || changedPath.equals(currentFolder
+ (currentFolder.endsWith("/") ? "" : "/") + path))) {
throw new IOException("folder is '" + changedPath + "' should be '" + path + "'");
}
// return to last folder
Expand Down Expand Up @@ -1172,7 +1159,7 @@ private entryInfo parseListData(final String line) {
final String dateString = tokens.group(3) + " " + tokens.group(4) + " " + year + " " + time;
try {
date = lsDateFormat.parse(dateString);
} catch (ParseException e) {
} catch (final ParseException e) {
errPrintln(logPrefix + "---- Error: not ls date-format '" + dateString + "': " + e.getMessage());
date = new Date();
}
Expand Down Expand Up @@ -1391,7 +1378,7 @@ public boolean LS() {
errPrintln("Syntax: LS [<path>|<file>]");
return true;
}
if (ControlSocket == null) {
if (notConnected()) {
return LLS();
}
try {
Expand All @@ -1415,7 +1402,6 @@ private void printElements(final List<String> list) {
outPrintln("---- v---v---v---v---v---v---v---v---v---v---v---v---v---v---v---v---v---v---v");
for (final String element : list) {
outPrintln(element);
outPrintln("--> " + parseListData(element));
}
outPrintln("---- ^---^---^---^---^---^---^---^---^---^---^---^---^---^---^---^---^---^---^");
}
Expand Down Expand Up @@ -1477,7 +1463,7 @@ public boolean MKDIR() {
errPrintln("Syntax: MKDIR <folder-name>");
return true;
}
if (ControlSocket == null) {
if (notConnected()) {
return LMKDIR();
}
try {
Expand Down Expand Up @@ -1545,7 +1531,7 @@ public boolean MV() {
errPrintln("Syntax: MV <from> <to>");
return true;
}
if (ControlSocket == null) {
if (notConnected()) {
return LMV();
}
try {
Expand Down Expand Up @@ -1604,7 +1590,7 @@ public boolean OPEN() {
outPrintln("---- Connection to " + cmd[1] + " established.");
prompt = "ftp [" + cmd[1] + "]>";
} catch (final IOException e) {
errPrintln("Error: connecting " + cmd[1] + " on port " + port + " failed.");
errPrintln("Error: connecting " + cmd[1] + " on port " + port + " failed: " + e.getMessage());
}
return true;
}
Expand All @@ -1614,17 +1600,56 @@ private void open(final String host, final int port) throws IOException {
exec("close", false); // close any existing connections first
}

ControlSocket = new Socket(host, port);
ControlSocket.setSoTimeout(getTimeout());
clientInput = new BufferedReader(new InputStreamReader(ControlSocket.getInputStream()));
clientOutput = new DataOutputStream(new BufferedOutputStream(ControlSocket.getOutputStream()));
try {
ControlSocket = new Socket(host, port);
ControlSocket.setSoTimeout(getTimeout());
clientInput = new BufferedReader(new InputStreamReader(ControlSocket.getInputStream()));
clientOutput = new DataOutputStream(new BufferedOutputStream(ControlSocket.getOutputStream()));

// read and return server message
this.host = host;
this.port = port;
remotemessage = receive();
if ((remotemessage != null) && (remotemessage.length() > 3)) {
remotemessage = remotemessage.substring(4);
}
} catch (final IOException e) {
// if a connection was opened, it should not be used
closeConnection();
throw new IOException(e);
}
}

/**
* @return
*/
public boolean notConnected() {
return ControlSocket == null;
}

// read and return server message
this.host = host;
this.port = port;
remotemessage = receive();
if ((remotemessage != null) && (remotemessage.length() > 3)) {
remotemessage = remotemessage.substring(4);
/**
* close all sockets
*
* @throws IOException
*/
private void closeConnection() throws IOException {
// cleanup
if (ControlSocket != null) {
clientOutput.close();
clientInput.close();
ControlSocket.close();
ControlSocket = null;
}

if (DataSocketActive != null) {
DataSocketActive.close();
DataSocketActive = null;
}
if (DataSocketPassive != null) {
DataSocketPassive.close();
DataSocketPassive = null; // "Once a socket has been closed, it is
// not available for further networking
// use"
}
}

Expand Down Expand Up @@ -1658,7 +1683,7 @@ public boolean PWD() {
errPrintln("Syntax: PWD (no parameter)");
return true;
}
if (ControlSocket == null) {
if (notConnected()) {
return LPWD();
}
try {
Expand Down Expand Up @@ -1701,7 +1726,7 @@ public boolean RMDIR() {
errPrintln("Syntax: RMDIR <folder-name>");
return true;
}
if (ControlSocket == null) {
if (notConnected()) {
return LRMDIR();
}
try {
Expand All @@ -1713,7 +1738,7 @@ public boolean RMDIR() {
}

public boolean QUIT() {
if (ControlSocket != null) {
if (!notConnected()) {
exec("close", false);
}
return false;
Expand Down Expand Up @@ -1780,7 +1805,7 @@ public boolean USER() {
login(cmd[1], cmd[2]);
outPrintln("---- Granted access for user " + cmd[1] + ".");
} catch (final IOException e) {
errPrintln("Error: authorization of user " + cmd[1] + " failed.");
errPrintln("Error: authorization of user " + cmd[1] + " failed: " + e.getMessage());
}
return true;
}
Expand Down Expand Up @@ -2354,6 +2379,7 @@ private void put(final String fileName, final String fileDest) throws IOExceptio
* @throws IOException
*/
private void login(final String account, final String password) throws IOException {
unsetLoginData();

// send user name
send("USER " + account);
Expand All @@ -2380,6 +2406,15 @@ private void login(final String account, final String password) throws IOExcepti
setLoginData(account, password, reply);
}

/**
* we are authorized to use the server
*
* @return
*/
public boolean isLoggedIn() {
return (account != null && password != null && remotegreeting != null);
}

/**
* remember username and password which were used to login
*
Expand All @@ -2394,6 +2429,12 @@ private void setLoginData(final String account, final String password, final Str
remotegreeting = reply;
}

private void unsetLoginData() {
account = null;
password = null;
remotegreeting = null;
}

public void sys() throws IOException {
// send system command
send("SYST");
Expand Down Expand Up @@ -2451,7 +2492,7 @@ public void setDataTimeoutByMaxFilesize(final int maxFilesize) {
* @param timeout
* in seconds, 0 = infinite
*/
public void setDataSocketTimeout(int timeout) {
public void setDataSocketTimeout(final int timeout) {
DataSocketTimeout = timeout;

try {
Expand Down

0 comments on commit f51bad8

Please sign in to comment.