Project

General

Profile

1
/**
2
 * 
3
 */
4
package eu.dnetlib.data.collector.plugins.rest;
5

    
6
import eu.dnetlib.data.collector.plugin.AbstractCollectorPlugin;
7
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
8
import eu.dnetlib.data.collector.rmi.InterfaceDescriptor;
9
import org.apache.commons.lang3.StringUtils;
10

    
11
/**
12
 * @author js, Andreas Czerniak
13
 *
14
 */
15
public class RestCollectorPlugin extends AbstractCollectorPlugin {
16

    
17
	@Override
18
	public Iterable<String> collect(InterfaceDescriptor ifDescriptor, String arg1, String arg2)
19
			throws CollectorServiceException {
20
		final String baseUrl = ifDescriptor.getBaseUrl();
21
		final String resumptionType = ifDescriptor.getParams().get("resumptionType");
22
		final String resumptionParam = ifDescriptor.getParams().get("resumptionParam");
23
		final String resumptionXpath = ifDescriptor.getParams().get("resumptionXpath");
24
		final String resultTotalXpath = ifDescriptor.getParams().get("resultTotalXpath");
25
		final String resultFormatParam = ifDescriptor.getParams().get("resultFormatParam");
26
		final String resultFormatValue = ifDescriptor.getParams().get("resultFormatValue");
27
		final String resultSizeParam = ifDescriptor.getParams().get("resultSizeParam");
28
		final String resultSizeValue = (StringUtils.isBlank(ifDescriptor.getParams().get("resultSizeValue"))) ? "100" : ifDescriptor.getParams().get("resultSizeValue");
29
        final String queryParams = ifDescriptor.getParams().get("queryParams");
30
		final String entityXpath = ifDescriptor.getParams().get("entityXpath");
31
		final String authMethod = ifDescriptor.getParams().get("authMethod");
32
		final String authToken = ifDescriptor.getParams().get("authToken");
33
		final String resultOffsetParam = ifDescriptor.getParams().get("resultOffsetParam");
34

    
35
		
36
		if (StringUtils.isBlank(baseUrl)) {throw new CollectorServiceException("Param 'baseUrl' is null or empty");}
37
		if (StringUtils.isBlank(resumptionType)) {throw new CollectorServiceException("Param 'resumptionType' is null or empty");}
38
		if (StringUtils.isBlank(resumptionParam)) {throw new CollectorServiceException("Param 'resumptionParam' is null or empty");}
39
		// if (StringUtils.isBlank(resumptionXpath)) {throw new CollectorServiceException("Param 'resumptionXpath' is null or empty");}
40
		// if (StringUtils.isBlank(resultTotalXpath)) {throw new CollectorServiceException("Param 'resultTotalXpath' is null or empty");}
41
		// resultFormatParam can be emtpy because some Rest-APIs doesn't like this argument in the query
42
                //if (StringUtils.isBlank(resultFormatParam)) {throw new CollectorServiceException("Param 'resultFormatParam' is null, empty or whitespace");}
43
		if (StringUtils.isBlank(resultFormatValue)) {throw new CollectorServiceException("Param 'resultFormatValue' is null or empty");}
44
//		if (StringUtils.isBlank(resultSizeParam)) {throw new CollectorServiceException("Param 'resultSizeParam' is null or empty");}
45
                // prevent resumptionType: discover -- if (Integer.valueOf(resultSizeValue) <= 1) {throw new CollectorServiceException("Param 'resultSizeValue' is less than 2");}
46
                if (StringUtils.isBlank(queryParams)) {throw new CollectorServiceException("Param 'queryParams' is null or empty");}
47
		if (StringUtils.isBlank(entityXpath)) {throw new CollectorServiceException("Param 'entityXpath' is null or empty");}
48
		
49
		return () -> new RestIterator(
50
				baseUrl,
51
				resumptionType,
52
				resumptionParam,
53
				resumptionXpath,
54
				resultTotalXpath,
55
				resultFormatParam,
56
				resultFormatValue,
57
				resultSizeParam,
58
                resultSizeValue,
59
				queryParams,
60
				entityXpath,
61
				authMethod,
62
				authToken);
63
	}
64

    
65
}
(1-1/2)