83 lines
2.1 KiB
Java
83 lines
2.1 KiB
Java
package eu.dnetlib.apps.oai2ftp.utils;
|
|
|
|
import java.io.IOException;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
import org.apache.commons.logging.LogFactory;
|
|
import org.apache.commons.net.ftp.FTPClient;
|
|
import org.apache.commons.net.ftp.FTPSClient;
|
|
|
|
public class FtpUtils {
|
|
|
|
private static final Log log = LogFactory.getLog(FtpUtils.class);
|
|
|
|
public static FTPClient ftpConnect(final String server, final boolean secure) {
|
|
try {
|
|
if (secure) {
|
|
final FTPSClient ftp = new FTPSClient();
|
|
ftp.connect(server);
|
|
// Set protection buffer size
|
|
ftp.execPBSZ(0);
|
|
// Set data channel protection to private
|
|
ftp.execPROT("P");
|
|
return ftp;
|
|
} else {
|
|
final FTPClient ftp = new FTPClient();
|
|
ftp.connect(server);
|
|
return ftp;
|
|
}
|
|
} catch (final IOException e) {
|
|
log.error("Ftp Connection Failed", e);
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
public static void ftpLogin(final FTPClient ftp, final String user, final String password) {
|
|
try {
|
|
if (!ftp.login(user, password)) { throw new RuntimeException("FTP login failed"); }
|
|
|
|
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
|
|
ftp.enterLocalPassiveMode();
|
|
ftp.setBufferSize(1024);
|
|
|
|
log.info("Ftp logged");
|
|
} catch (final IOException e) {
|
|
log.error("Ftp Login Failed", e);
|
|
ftpDisconnect(ftp);
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
public static void ftpDisconnect(final FTPClient ftp) {
|
|
if (ftp != null && ftp.isConnected()) {
|
|
try {
|
|
ftp.disconnect();
|
|
log.info("Ftp Disconnected");
|
|
} catch (final IOException e) {
|
|
log.error("Ftp Disconnection Failed");
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static boolean changeDir(final FTPClient ftp, final String dir) {
|
|
try {
|
|
if (!ftp.changeWorkingDirectory(dir)) {
|
|
ftp.makeDirectory(dir);
|
|
return ftp.changeWorkingDirectory(dir);
|
|
}
|
|
return true;
|
|
} catch (final IOException e) {
|
|
log.error("Error changing or create dir: " + dir);
|
|
ftpDisconnect(ftp);
|
|
throw new RuntimeException("Error changing or create dir: " + dir, e);
|
|
}
|
|
}
|
|
|
|
public static void saveRecord(final String id, final String body) {
|
|
// TODO Auto-generated method stub
|
|
|
|
}
|
|
|
|
}
|