simpleOaiCollectorService/src/main/java/eu/dnetlib/apps/Oai2ftp/utils/FtpStorage.java

104 lines
2.9 KiB
Java
Raw Normal View History

package eu.dnetlib.apps.oai2ftp.utils;
2023-05-25 13:21:42 +02:00
import java.io.ByteArrayInputStream;
import java.io.IOException;
2023-05-25 13:21:42 +02:00
import java.io.InputStream;
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;
2023-05-26 13:52:31 +02:00
public class FtpStorage implements StorageClient {
2023-05-26 13:52:31 +02:00
private static final Log log = LogFactory.getLog(FtpStorage.class);
2023-05-25 14:44:19 +02:00
private final FTPClient ftp;
2023-05-26 13:52:31 +02:00
public FtpStorage(final String server, final int port, final boolean secure) {
this.ftp = ftpConnect(server, port, secure);
2023-05-25 14:44:19 +02:00
}
2023-05-26 13:52:31 +02:00
private FTPClient ftpConnect(final String server, final int port, final boolean secure) {
try {
2023-05-26 13:52:31 +02:00
if (secure) {
final FTPSClient ftp = new FTPSClient();
2023-05-26 13:52:31 +02:00
ftp.connect(server, port > 0 ? port : FTPSClient.DEFAULT_FTPS_PORT);
// Set protection buffer size
ftp.execPBSZ(0);
// Set data channel protection to private
ftp.execPROT("P");
return ftp;
} else {
final FTPClient ftp = new FTPClient();
2023-05-26 13:52:31 +02:00
ftp.connect(server, port > 0 ? port : FTPClient.DEFAULT_PORT);
return ftp;
}
} catch (final IOException e) {
log.error("Ftp Connection Failed", e);
throw new RuntimeException(e);
}
}
2023-05-26 13:52:31 +02:00
@Override
2023-05-25 14:44:19 +02:00
public void disconnect() {
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);
}
}
}
2023-05-26 13:52:31 +02:00
@Override
2023-05-25 14:44:19 +02:00
public void login(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);
2023-05-25 14:44:19 +02:00
disconnect();
throw new RuntimeException(e);
}
}
2023-05-26 13:52:31 +02:00
@Override
2023-05-25 14:44:19 +02:00
public boolean changeDir(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);
2023-05-25 14:44:19 +02:00
disconnect();
throw new RuntimeException("Error changing or create dir: " + dir, e);
}
}
2023-05-26 13:52:31 +02:00
@Override
2023-05-25 14:44:19 +02:00
public void saveFile(final String filename, final String content) {
2023-05-25 13:21:42 +02:00
try (InputStream is = new ByteArrayInputStream(content.getBytes())) {
if (log.isDebugEnabled()) {
log.debug("Saving file " + filename);
log.debug(content);
}
if (!ftp.storeFile(filename, is)) {
log.error("Error saving file: " + ftp.getReplyCode() + " - " + ftp.getReplyString());
throw new RuntimeException("Error saving file: " + ftp.getReplyString());
}
} catch (final IOException e) {
log.error("Error saving info file");
throw new RuntimeException("Error saving info file", e);
}
}
}