Project

General

Profile

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

    
6
import com.google.gson.Gson;
7
import eu.dnetlib.data.collector.plugin.AbstractCollectorPlugin;
8
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
9
import eu.dnetlib.data.collector.rmi.InterfaceDescriptor;
10
import org.apache.commons.lang3.StringUtils;
11
import org.json.JSONObject;
12

    
13
import java.util.Map;
14

    
15
/**
16
 * @author 	js, Andreas Czerniak
17
 * @date 	2020-04-09
18
 *
19
 */
20
public class RestCollectorPlugin extends AbstractCollectorPlugin {
21

    
22
	@Override
23
	public Iterable<String> collect(InterfaceDescriptor ifDescriptor, String arg1, String arg2)
24
			throws CollectorServiceException {
25
		final String baseUrl = ifDescriptor.getBaseUrl();
26
		final String resumptionType = ifDescriptor.getParams().get("resumptionType");
27
		final String resumptionParam = ifDescriptor.getParams().get("resumptionParam");
28
		final String resumptionXpath = ifDescriptor.getParams().get("resumptionXpath");
29
		final String resultTotalXpath = ifDescriptor.getParams().get("resultTotalXpath");
30
		final String resultFormatParam = ifDescriptor.getParams().get("resultFormatParam");
31
		final String resultFormatValue = ifDescriptor.getParams().get("resultFormatValue");
32
		final String resultSizeParam = ifDescriptor.getParams().get("resultSizeParam");
33
		final String resultSizeValue = (StringUtils.isBlank(ifDescriptor.getParams().get("resultSizeValue"))) ? "100" : ifDescriptor.getParams().get("resultSizeValue");
34
        final String queryParams = ifDescriptor.getParams().get("queryParams");
35
		final String entityXpath = ifDescriptor.getParams().get("entityXpath");
36
		final String authMethod = ifDescriptor.getParams().get("authMethod");
37
		final String authToken = ifDescriptor.getParams().get("authToken");
38
		final String requestHeaderMap = ifDescriptor.getParams().get("requestHeaderMap");
39
		Gson gson = new Gson();
40
		Map<String, String> requestHeaders = gson.fromJson(requestHeaderMap, Map.class);
41

    
42

    
43
		if (StringUtils.isBlank(baseUrl)) {throw new CollectorServiceException("Param 'baseUrl' is null or empty");}
44
		if (StringUtils.isBlank(resumptionType)) {throw new CollectorServiceException("Param 'resumptionType' is null or empty");}
45
		if (StringUtils.isBlank(resumptionParam)) {throw new CollectorServiceException("Param 'resumptionParam' is null or empty");}
46
		// if (StringUtils.isBlank(resumptionXpath)) {throw new CollectorServiceException("Param 'resumptionXpath' is null or empty");}
47
		// if (StringUtils.isBlank(resultTotalXpath)) {throw new CollectorServiceException("Param 'resultTotalXpath' is null or empty");}
48
		// resultFormatParam can be emtpy because some Rest-APIs doesn't like this argument in the query
49
		//if (StringUtils.isBlank(resultFormatParam)) {throw new CollectorServiceException("Param 'resultFormatParam' is null, empty or whitespace");}
50
		if (StringUtils.isBlank(resultFormatValue)) {throw new CollectorServiceException("Param 'resultFormatValue' is null or empty");}
51
		// if (StringUtils.isBlank(resultSizeParam)) {throw new CollectorServiceException("Param 'resultSizeParam' is null or empty");}
52
		// prevent resumptionType: discover -- if (Integer.valueOf(resultSizeValue) <= 1) {throw new CollectorServiceException("Param 'resultSizeValue' is less than 2");}
53

    
54
		// queryParams could be empty like for DRIS+ API from euroCRIS
55
		//if (StringUtils.isBlank(queryParams)) {throw new CollectorServiceException("Param 'queryParams' is null or empty");}
56
		if (StringUtils.isBlank(entityXpath)) {throw new CollectorServiceException("Param 'entityXpath' is null or empty");}
57

    
58
		String resFormat = ifDescriptor.getParams().get("resultOutputFormat");
59
		final String resultOutputFormat = StringUtils.isNotBlank(resFormat) ? resFormat.toLowerCase() : resultFormatValue.toLowerCase();
60
		
61
		return () -> new RestIterator(
62
				baseUrl,
63
				resumptionType,
64
				resumptionParam,
65
				resumptionXpath,
66
				resultTotalXpath,
67
				resultFormatParam,
68
				resultFormatValue,
69
				resultSizeParam,
70
                resultSizeValue,
71
				queryParams,
72
				entityXpath,
73
				authMethod,
74
				authToken,
75
				resultOutputFormat, requestHeaders);
76
	}
77

    
78
}
(1-1/2)