html-dirlist can now also be generated from existing connections

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@2493 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
orbiter 18 years ago
parent 7930839594
commit d4c5e2af01

@ -114,7 +114,10 @@ public class ftpc {
String[] cmd; String[] cmd;
// session parameters
File currentPath; File currentPath;
String account, password, host, remotemessage, remotegreeting, remotesystem;
int port;
public ftpc() { public ftpc() {
this(System.in, System.out, System.err); this(System.in, System.out, System.err);
@ -136,6 +139,13 @@ public class ftpc {
this.currentPath = new File(this.currentPath.getCanonicalPath()); this.currentPath = new File(this.currentPath.getCanonicalPath());
} catch (IOException e) {} } catch (IOException e) {}
this.account = null;
this.password = null;
this.host = null;
this.port = -1;
this.remotemessage = null;
this.remotegreeting = null;
this.remotesystem = null;
} }
public void shell(String server) { public void shell(String server) {
@ -1200,7 +1210,7 @@ cd ..
return true; return true;
} }
private String open(String host, int port) throws IOException { private void open(String host, int port) throws IOException {
if (ControlSocket != null) exec("close",false); // close any existing connections first if (ControlSocket != null) exec("close",false); // close any existing connections first
ControlSocket = new Socket(host, port); ControlSocket = new Socket(host, port);
@ -1209,7 +1219,10 @@ cd ..
clientOutput = new DataOutputStream(new BufferedOutputStream(ControlSocket.getOutputStream())); clientOutput = new DataOutputStream(new BufferedOutputStream(ControlSocket.getOutputStream()));
// read and return server message // read and return server message
return receive(); this.host = host;
this.port = port;
this.remotemessage = receive();
if ((remotemessage != null) && (remotemessage.length() > 3)) remotemessage = remotemessage.substring(4);
} }
public boolean PROMPT() { public boolean PROMPT() {
@ -1756,14 +1769,19 @@ cd ..
} }
private String login(String account, String password) throws IOException { private void login(String account, String password) throws IOException {
// send user name // send user name
send("USER " + account); send("USER " + account);
String reply = receive(); String reply = receive();
if (Integer.parseInt(reply.substring(0, 1)) == 4) throw new IOException(reply); if (Integer.parseInt(reply.substring(0, 1)) == 4) throw new IOException(reply);
if (Integer.parseInt(reply.substring(0, 1)) == 2) return reply; if (Integer.parseInt(reply.substring(0, 1)) == 2) {
this.account = account;
this.password = password;
this.remotegreeting = reply;
return;
}
// send password // send password
send("PASS " + password); send("PASS " + password);
@ -1771,10 +1789,12 @@ cd ..
reply = receive(); reply = receive();
if (Integer.parseInt(reply.substring(0, 1)) != 2) throw new IOException(reply); if (Integer.parseInt(reply.substring(0, 1)) != 2) throw new IOException(reply);
return reply; this.account = account;
this.password = password;
this.remotegreeting = reply;
} }
public String sys() throws IOException { public void sys() throws IOException {
// send system command // send system command
send("SYST"); send("SYST");
@ -1783,7 +1803,7 @@ cd ..
if (Integer.parseInt(systemType.substring(0, 1)) != 2) throw new IOException(systemType); if (Integer.parseInt(systemType.substring(0, 1)) != 2) throw new IOException(systemType);
// exclude status code from reply // exclude status code from reply
return systemType.substring(4); this.remotesystem = systemType.substring(4);
} }
@ -1889,26 +1909,41 @@ cd ..
} }
} }
public static StringBuffer dirhtml(String host, String remotePath) { public StringBuffer dirhtml(String remotePath) {
return dirhtml(host, 21, remotePath, "anonymous", "anomic"); // returns a directory listing using an existing connection
try {
Vector list = list(remotePath, true);
if (this.remotesystem == null) sys();
String base = "ftp://" + ((account.equals("anonymous")) ? "" : (account + ":" + password + "@")) + host + ((port == 21) ? "" : (":" + port)) + ((remotePath.charAt(0) == '/') ? "" : "/") + remotePath;
return dirhtml(base, this.remotemessage, this.remotegreeting, this.remotesystem, list);
} catch (java.security.AccessControlException e) {
return null;
} catch (IOException e) {
return null;
}
} }
public static StringBuffer dirhtml(String host, int port, String remotePath, String account, String password) { public static StringBuffer dirhtml(String host, int port, String remotePath, String account, String password) {
// opens a new connection and returns a directory listing as html
try { try {
ftpc c = new ftpc(System.in, null, System.err); ftpc c = new ftpc(System.in, null, System.err);
String servermessage = c.open(host, port); c.open(host, port);
if ((servermessage != null) && (servermessage.length() > 3)) servermessage = servermessage.substring(4); c.login(account, password);
String greeting = c.login(account, password); c.sys();
String system = c.sys(); StringBuffer page = c.dirhtml(remotePath);
Vector list = c.list(remotePath, true);
c.quit(); c.quit();
return page;
} catch (java.security.AccessControlException e) {
return null;
} catch (IOException e) {
return null;
}
}
//System.out.println("servermessage=" + servermessage); public static StringBuffer dirhtml(String base, String servermessage, String greeting, String system, Vector list) {
//System.out.println("greeting=" + greeting); // this creates the html output from collected strings
StringBuffer page = new StringBuffer(1024); StringBuffer page = new StringBuffer(1024);
String base = "ftp://" + ((account.equals("anonymous")) ? "" : (account + ":" + password + "@")) + host + ((port == 21) ? "" : (":" + port)) + ((remotePath.charAt(0) == '/') ? "" : "/") + remotePath;
String title = "Index of " + base; String title = "Index of " + base;
// find position of filename // find position of filename
@ -1946,13 +1981,7 @@ cd ..
page.append(" <pre>System info: \"" + system + "\"</pre>\n"); page.append(" <pre>System info: \"" + system + "\"</pre>\n");
page.append("</body></html>\n"); page.append("</body></html>\n");
//System.out.println(new String(page));
return page; return page;
} catch (java.security.AccessControlException e) {
return null;
} catch (IOException e) {
return null;
}
} }
public static void dirAnonymous(String host, public static void dirAnonymous(String host,

Loading…
Cancel
Save