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
		if (StringUtils.isBlank(queryParams)) {throw new CollectorServiceException("Param 'queryParams' is null or empty");}
46
		if (StringUtils.isBlank(entityXpath)) {throw new CollectorServiceException("Param 'entityXpath' is null or empty");}
47

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

    
68
}
(1-1/2)