FTP: source cleanup (added finals, indention for easier diffs)

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@4559 6c8d7289-2bf4-0310-a012-ef5d649a1542
This commit is contained in:
danielr 2008-03-14 12:35:53 +00:00
parent fa1090113d
commit 1a7870df0d
2 changed files with 2328 additions and 2001 deletions

File diff suppressed because it is too large Load Diff

View File

@ -44,7 +44,6 @@
// the intact and unchanged copyright notice.
// Contributions and changes to the program code must be marked as such.
package de.anomic.plasma.crawler;
import java.io.ByteArrayOutputStream;
@ -65,96 +64,85 @@ import de.anomic.server.logging.serverLog;
public class plasmaFTPLoader {
private plasmaSwitchboard sb;
private serverLog log;
public plasmaFTPLoader(plasmaSwitchboard sb, serverLog log) {
private final plasmaSwitchboard sb;
private final serverLog log;
public plasmaFTPLoader(final plasmaSwitchboard sb, final serverLog log) {
this.sb = sb;
this.log = log;
}
protected plasmaHTCache.Entry createCacheEntry(plasmaCrawlEntry entry, String mimeType, Date fileDate) {
return plasmaHTCache.newEntry(
new Date(),
entry.depth(),
entry.url(),
entry.name(),
"OK",
new ResourceInfo(
entry.url(),
sb.getURL(entry.referrerhash()),
mimeType,
fileDate
),
entry.initiator(),
sb.profilesActiveCrawls.getEntry(entry.profileHandle())
);
protected plasmaHTCache.Entry createCacheEntry(final plasmaCrawlEntry entry, final String mimeType,
final Date fileDate) {
return plasmaHTCache.newEntry(new Date(), entry.depth(), entry.url(), entry.name(), "OK", new ResourceInfo(
entry.url(), sb.getURL(entry.referrerhash()), mimeType, fileDate), entry.initiator(),
sb.profilesActiveCrawls.getEntry(entry.profileHandle()));
}
public plasmaHTCache.Entry load(plasmaCrawlEntry entry) {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
PrintStream out = new PrintStream(bout);
ByteArrayOutputStream berr = new ByteArrayOutputStream();
PrintStream err = new PrintStream(berr);
public plasmaHTCache.Entry load(final plasmaCrawlEntry entry) {
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
final PrintStream out = new PrintStream(bout);
final ByteArrayOutputStream berr = new ByteArrayOutputStream();
final PrintStream err = new PrintStream(berr);
// create a new ftp client
ftpc ftpClient = new ftpc(System.in, out, err);
final ftpc ftpClient = new ftpc(System.in, out, err);
// get username and password
String userInfo = entry.url().getUserInfo();
final String userInfo = entry.url().getUserInfo();
String userName = "anonymous", userPwd = "anonymous";
if (userInfo != null) {
int pos = userInfo.indexOf(":");
final int pos = userInfo.indexOf(":");
if (pos != -1) {
userName = userInfo.substring(0,pos);
userPwd = userInfo.substring(pos+1);
userName = userInfo.substring(0, pos);
userPwd = userInfo.substring(pos + 1);
}
}
}
// get server name, port and file path
String host = entry.url().getHost();
final String host = entry.url().getHost();
String fullPath = entry.url().getPath();
int port = entry.url().getPort();
final int port = entry.url().getPort();
plasmaHTCache.Entry htCache = null;
try {
// open a connection to the ftp server
if (port == -1) {
if (port == -1) {
ftpClient.exec("open " + host, false);
} else {
ftpClient.exec("open " + host + " " + port, false);
}
// login to the server
ftpClient.exec("user " + userName + " " + userPwd, false);
ftpClient.exec("user " + userName + " " + userPwd, false);
// change transfer mode to binary
ftpClient.exec("binary", false);
// determine filename and path
String file, path;
String file, path;
if (fullPath.endsWith("/")) {
file = "";
path = fullPath;
} else {
int pos = fullPath.lastIndexOf("/");
final int pos = fullPath.lastIndexOf("/");
if (pos == -1) {
file = fullPath;
path = "/";
} else {
path = fullPath.substring(0,pos+1);
file = fullPath.substring(pos+1);
} else {
path = fullPath.substring(0, pos + 1);
file = fullPath.substring(pos + 1);
}
}
}
// testing if the specified file is a directory
if (file.length() > 0) {
ftpClient.exec("cd \"" + path + "\"", false);
// testing if the current name is a directoy
boolean isFolder = ftpClient.isFolder(file);
final boolean isFolder = ftpClient.isFolder(file);
if (isFolder) {
fullPath = fullPath + "/";
file = "";
@ -162,11 +150,11 @@ public class plasmaFTPLoader {
}
// creating a cache file object
File cacheFile = plasmaHTCache.getCachePath(entry.url());
final File cacheFile = plasmaHTCache.getCachePath(entry.url());
// TODO: aborting download if content is to long ...
// TODO: invalid file path check
// TODO: invalid file path check
// testing if the file already exists
if (cacheFile.isFile()) {
@ -179,7 +167,7 @@ public class plasmaFTPLoader {
String mimeType;
Date fileDate;
if (file.length() == 0) {
if (file.length() == 0) {
// getting the dirlist
mimeType = "text/html";
fileDate = new Date();
@ -188,24 +176,27 @@ public class plasmaFTPLoader {
htCache = createCacheEntry(entry, mimeType, fileDate);
// generate the dirlist
StringBuffer dirList = ftpClient.dirhtml(fullPath);
final StringBuffer dirList = ftpClient.dirhtml(fullPath);
if (dirList != null && dirList.length() > 0) try {
// write it into a file
PrintWriter writer = new PrintWriter(new FileOutputStream(cacheFile),false);
writer.write(dirList.toString());
writer.flush();
writer.close();
} catch (Exception e) {
this.log.logInfo("Unable to write dirlist for URL " + entry.url().toString());
htCache = null;
if (dirList != null && dirList.length() > 0) {
try {
// write it into a file
final PrintWriter writer = new PrintWriter(new FileOutputStream(cacheFile), false);
writer.write(dirList.toString());
writer.flush();
writer.close();
} catch (final Exception e) {
log.logInfo("Unable to write dirlist for URL " + entry.url().toString());
htCache = null;
}
}
} else {
// determine the mimetype of the resource
String extension = plasmaParser.getFileExt(entry.url());
final String extension = plasmaParser.getFileExt(entry.url());
mimeType = plasmaParser.getMimeTypeByFileExt(extension);
// if the mimetype and file extension is supported we start to download the file
// if the mimetype and file extension is supported we start to
// download the file
if (plasmaParser.supportedContent(plasmaParser.PARSER_MODE_CRAWLER, entry.url(), mimeType)) {
// TODO: determine the real file date
@ -215,14 +206,16 @@ public class plasmaFTPLoader {
htCache = createCacheEntry(entry, mimeType, fileDate);
// change into working directory
//ftpClient.exec("cd \"" + path + "\"", false);
// ftpClient.exec("cd \"" + path + "\"", false);
// download the remote file
ftpClient.exec("get \"" + fullPath + "\" \"" + cacheFile.getAbsolutePath() + "\"", false);
} else {
// if the response has not the right file type then reject file
this.log.logInfo("REJECTED WRONG MIME/EXT TYPE " + mimeType + " for URL " + entry.url().toString());
sb.crawlQueues.errorURL.newEntry(entry, null, new Date(), 1, plasmaCrawlEURL.DENIED_WRONG_MIMETYPE_OR_EXT);
// if the response has not the right file type then reject
// file
log.logInfo("REJECTED WRONG MIME/EXT TYPE " + mimeType + " for URL " + entry.url().toString());
sb.crawlQueues.errorURL.newEntry(entry, null, new Date(), 1,
plasmaCrawlEURL.DENIED_WRONG_MIMETYPE_OR_EXT);
return null;
}
}
@ -230,23 +223,25 @@ public class plasmaFTPLoader {
// pass the downloaded resource to the cache manager
if (berr.size() > 0 || htCache == null) {
// if the response has not the right file type then reject file
this.log.logWarning("Unable to download URL " + entry.url().toString() + "\nErrorlog: " + berr.toString());
sb.crawlQueues.errorURL.newEntry(entry, null, new Date(), 1, plasmaCrawlEURL.DENIED_SERVER_DOWNLOAD_ERROR);
log.logWarning("Unable to download URL " + entry.url().toString() + "\nErrorlog: " + berr.toString());
sb.crawlQueues.errorURL.newEntry(entry, null, new Date(), 1,
plasmaCrawlEURL.DENIED_SERVER_DOWNLOAD_ERROR);
// an error has occured. cleanup
if (cacheFile.exists()) cacheFile.delete();
if (cacheFile.exists()) {
cacheFile.delete();
}
} else {
// announce the file
plasmaHTCache.writeFileAnnouncement(cacheFile);
plasmaHTCache.writeFileAnnouncement(cacheFile);
}
return htCache;
} finally {
// closing connection
ftpClient.exec("close", false);
ftpClient.exec("exit", false);
}
ftpClient.exec("exit", false);
}
}
}