Project

General

Profile

1
package eu.dnetlib.msro.workers.aggregation.collect.plugins.sftp;
2

    
3
import java.util.Set;
4
import java.util.stream.Stream;
5

    
6
import com.google.common.base.Splitter;
7
import com.google.common.collect.Sets;
8
import eu.dnetlib.miscutils.streams.DnetStreamSupport;
9
import eu.dnetlib.msro.workers.aggregation.collect.plugins.CollectorPlugin;
10
import eu.dnetlib.msro.workers.aggregation.collect.plugins.DnetCollectorParam;
11
import eu.dnetlib.msro.workers.aggregation.collect.plugins.DnetCollectorPlugin;
12
import eu.dnetlib.msro.workers.aggregation.collect.plugins.ProtocolParameterType;
13
import eu.dnetlib.msro.workers.aggregation.objects.InterfaceDescriptor;
14
import eu.dnetlib.msro.workflows.nodes.collect.CollectException;
15

    
16
import org.springframework.stereotype.Component;
17

    
18
/**
19
 * Created by andrea on 11/01/16.
20
 */
21
@Component
22
@DnetCollectorPlugin(value = "sftp", parameters = {
23
		@DnetCollectorParam("username"),
24
		@DnetCollectorParam("password"),
25
		@DnetCollectorParam(value = "recursive", type = ProtocolParameterType.BOOLEAN),
26
		@DnetCollectorParam(value = "extensions", type = ProtocolParameterType.LIST)
27
})
28
public class SftpCollectorPlugin implements CollectorPlugin {
29

    
30
	private SftpIteratorFactory sftpIteratorFactory;
31

    
32
	@Override
33
	public Stream<String> collect(final InterfaceDescriptor interfaceDescriptor, final String fromDate, final String toDate)
34
			throws CollectException {
35
		final String baseUrl = interfaceDescriptor.getBaseUrl();
36
		final String username = interfaceDescriptor.getParams().get("username");
37
		final String password = interfaceDescriptor.getParams().get("password");
38
		final String recursive = interfaceDescriptor.getParams().get("recursive");
39
		final String extensions = interfaceDescriptor.getParams().get("extensions");
40

    
41
		if ((baseUrl == null) || baseUrl.isEmpty()) { throw new CollectException("Param 'baseurl' is null or empty"); }
42
		if ((username == null) || username.isEmpty()) { throw new CollectException("Param 'username' is null or empty"); }
43
		if ((password == null) || password.isEmpty()) { throw new CollectException("Param 'password' is null or empty"); }
44
		if ((recursive == null) || recursive.isEmpty()) { throw new CollectException("Param 'recursive' is null or empty"); }
45
		if ((extensions == null) || extensions.isEmpty()) { throw new CollectException("Param 'extensions' is null or empty"); }
46
		if ((fromDate != null) && !fromDate.matches("\\d{4}-\\d{2}-\\d{2}")) { throw new CollectException("Invalid date (YYYY-MM-DD): " + fromDate); }
47

    
48
		// final int fromDateIntSeconds =
49

    
50
		final boolean isRecursive = "true".equals(recursive);
51

    
52
		final Set<String> extensionsSet = parseSet(extensions);
53

    
54
		return DnetStreamSupport.stream(getSftpIteratorFactory().newIterator(baseUrl, username, password, isRecursive, extensionsSet, fromDate));
55
	}
56

    
57
	private Set<String> parseSet(final String extensions) {
58
		return Sets.newHashSet(Splitter.on(",").omitEmptyStrings().trimResults().split(extensions));
59
	}
60

    
61
	public SftpIteratorFactory getSftpIteratorFactory() {
62
		return sftpIteratorFactory;
63
	}
64

    
65
	public void setSftpIteratorFactory(final SftpIteratorFactory sftpIteratorFactory) {
66
		this.sftpIteratorFactory = sftpIteratorFactory;
67
	}
68
}
(1-1/3)