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
 * @date 	2020-04-09
14
 *
15
 */
16
public class RestCollectorPlugin extends AbstractCollectorPlugin {
17

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

    
35
		if (StringUtils.isBlank(baseUrl)) {throw new CollectorServiceException("Param 'baseUrl' is null or empty");}
36
		if (StringUtils.isBlank(resumptionType)) {throw new CollectorServiceException("Param 'resumptionType' is null or empty");}
37
		if (StringUtils.isBlank(resumptionParam)) {throw new CollectorServiceException("Param 'resumptionParam' is null or empty");}
38
		// if (StringUtils.isBlank(resumptionXpath)) {throw new CollectorServiceException("Param 'resumptionXpath' is null or empty");}
39
		// if (StringUtils.isBlank(resultTotalXpath)) {throw new CollectorServiceException("Param 'resultTotalXpath' is null or empty");}
40
		// resultFormatParam can be emtpy because some Rest-APIs doesn't like this argument in the query
41
		//if (StringUtils.isBlank(resultFormatParam)) {throw new CollectorServiceException("Param 'resultFormatParam' is null, empty or whitespace");}
42
		if (StringUtils.isBlank(resultFormatValue)) {throw new CollectorServiceException("Param 'resultFormatValue' is null or empty");}
43
		// if (StringUtils.isBlank(resultSizeParam)) {throw new CollectorServiceException("Param 'resultSizeParam' is null or empty");}
44
		// prevent resumptionType: discover -- if (Integer.valueOf(resultSizeValue) <= 1) {throw new CollectorServiceException("Param 'resultSizeValue' is less than 2");}
45

    
46
		// queryParams could be empty like for DRIS+ API from euroCRIS
47
		//if (StringUtils.isBlank(queryParams)) {throw new CollectorServiceException("Param 'queryParams' is null or empty");}
48
		if (StringUtils.isBlank(entityXpath)) {throw new CollectorServiceException("Param 'entityXpath' is null or empty");}
49

    
50
		String resFormat = ifDescriptor.getParams().get("resultOutputFormat");
51
		final String resultOutputFormat = StringUtils.isNotBlank(resFormat) ? resFormat.toLowerCase() : resultFormatValue.toLowerCase();
52
		
53
		return () -> new RestIterator(
54
				baseUrl,
55
				resumptionType,
56
				resumptionParam,
57
				resumptionXpath,
58
				resultTotalXpath,
59
				resultFormatParam,
60
				resultFormatValue,
61
				resultSizeParam,
62
                resultSizeValue,
63
				queryParams,
64
				entityXpath,
65
				authMethod,
66
				authToken,
67
				resultOutputFormat);
68
	}
69

    
70
}
(1-1/2)