103 lines
2.8 KiB
Java
103 lines
2.8 KiB
Java
package eu.dnetlib.apps.oai2ftp.utils;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
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 FtpClientWrapper {
|
|
|
|
private static final Log log = LogFactory.getLog(FtpClientWrapper.class);
|
|
|
|
private final FTPClient ftp;
|
|
|
|
public FtpClientWrapper(final String server, final boolean secure) {
|
|
this.ftp = ftpConnect(server, secure);
|
|
}
|
|
|
|
private FTPClient ftpConnect(final String server, final boolean secure) {
|
|
try {
|
|
if (StringUtils.isBlank(server)) {
|
|
return new MockFtpClient();
|
|
} else 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 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);
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
disconnect();
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
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);
|
|
disconnect();
|
|
throw new RuntimeException("Error changing or create dir: " + dir, e);
|
|
}
|
|
}
|
|
|
|
public void saveFile(final String filename, final String content) {
|
|
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);
|
|
}
|
|
}
|
|
|
|
}
|