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

48 lines
1.2 KiB
Java

package eu.dnetlib.apps.oai2ftp.utils;
import java.net.URI;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class StorageClientFactory {
@Value("${oai2ftp.conf.storage.url}")
private URI storageUrl;
@Value("${oai2ftp.conf.storage.user}")
private String storageUser;
@Value("${oai2ftp.conf.storage.password}")
private String storagePassword;
public StorageClient newClient() {
final String protocol = storageUrl.getScheme();
final String host = storageUrl.getHost();
final int port = storageUrl.getPort();
final String path = storageUrl.getPath();
StorageClient client;
if (protocol.equalsIgnoreCase("ftp")) {
client = new FtpStorage(host, port, false);
} else if (protocol.equalsIgnoreCase("ftps")) {
client = new FtpStorage(host, port, true);
} else if (protocol.equalsIgnoreCase("file")) {
client = new LocalStorage();
} else {
throw new RuntimeException("Invalid storage protocol: " + protocol + " (valid protocol are: file, ftp and ftps)");
}
client.login(storageUser, storagePassword);
client.changeDir(path);
return client;
}
public String getStorageUrlAsString() {
return storageUrl.toString();
}
}