Project

General

Profile

1
package eu.dnetlib.data.collector.plugins.fairsharing;
2

    
3
import java.io.UnsupportedEncodingException;
4

    
5
import org.apache.commons.io.IOUtils;
6
import org.apache.commons.lang3.StringUtils;
7
import org.apache.commons.logging.Log;
8
import org.apache.commons.logging.LogFactory;
9
import org.apache.http.HttpEntity;
10
import org.apache.http.client.methods.CloseableHttpResponse;
11
import org.apache.http.client.methods.HttpPost;
12
import org.apache.http.entity.StringEntity;
13
import org.apache.http.impl.client.CloseableHttpClient;
14
import org.apache.http.impl.client.HttpClients;
15
import org.json.JSONObject;
16

    
17
import eu.dnetlib.data.collector.plugin.AbstractCollectorPlugin;
18
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
19
import eu.dnetlib.data.collector.rmi.InterfaceDescriptor;
20

    
21
public class FairSharingPlugin extends AbstractCollectorPlugin {
22

    
23
	private static final int PAGE_SIZE = 100;
24

    
25
	private static final Log log = LogFactory.getLog(FairSharingPlugin.class); // NOPMD by marko on 11/24/08 5:02 PM
26

    
27
	// Suggested values:
28
	// baseUrl = https://api.fairsharing.org
29
	// XPATH_ID = /record/id
30

    
31
	@Override
32
	public Iterable<String> collect(final InterfaceDescriptor interfaceDescriptor, final String fromDate, final String untilDate)
33
		throws CollectorServiceException {
34

    
35
		final String baseUrl = interfaceDescriptor.getBaseUrl();
36
		final String login = interfaceDescriptor.getParams().get("login");
37
		final String password = interfaceDescriptor.getParams().get("password");
38

    
39
		final String authCode = login(baseUrl, login, password);
40

    
41
		log.debug("authCode: " + authCode);
42

    
43
		if (StringUtils.isBlank(authCode)) { throw new CollectorServiceException("Authorization failed: authCode is empty"); }
44

    
45
		return () -> new FairSharingIterator(baseUrl, authCode, PAGE_SIZE);
46
	}
47

    
48
	private String login(final String baseUrl, final String login, final String password) throws CollectorServiceException {
49
		final HttpPost req = new HttpPost(baseUrl + "/users/sign_in");
50
		req.addHeader("Accept", "application/json");
51
		req.addHeader("Content-Type", "application/json");
52
		req.setEntity(prepareCredentials(login, password));
53

    
54
		try (final CloseableHttpClient client = HttpClients.createDefault()) {
55
			try (final CloseableHttpResponse response = client.execute(req)) {
56
				final String content = IOUtils.toString(response.getEntity().getContent());
57
				final JSONObject obj = new JSONObject(content);
58
				return obj.getString("jwt");
59
			}
60
		} catch (final Exception e) {
61
			throw new CollectorServiceException("Error perfoming login", e);
62
		}
63
	}
64

    
65
	public HttpEntity prepareCredentials(final String login, final String password) throws CollectorServiceException {
66

    
67
		final JSONObject objUser = new JSONObject();
68
		objUser.put("login", login);
69
		objUser.put("password", password);
70

    
71
		final JSONObject objCredentials = new JSONObject();
72
		objCredentials.put("user", objUser);
73

    
74
		try {
75
			return new StringEntity(objCredentials.toString());
76
		} catch (final UnsupportedEncodingException e) {
77
			throw new CollectorServiceException("Error preparing http entity for login");
78
		}
79
	}
80

    
81
}
(2-2/2)