1 |
61981
|
michele.ar
|
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 |
|
|
@Override
|
28 |
|
|
public Iterable<String> collect(final InterfaceDescriptor interfaceDescriptor, final String fromDate, final String untilDate)
|
29 |
|
|
throws CollectorServiceException {
|
30 |
|
|
|
31 |
|
|
// It should be https://api.fairsharing.org
|
32 |
|
|
final String baseUrl = interfaceDescriptor.getBaseUrl();
|
33 |
|
|
final String login = interfaceDescriptor.getParams().get("login");
|
34 |
|
|
final String password = interfaceDescriptor.getParams().get("password");
|
35 |
|
|
|
36 |
|
|
final String authCode = login(baseUrl, login, password);
|
37 |
|
|
|
38 |
|
|
log.debug("authCode: " + authCode);
|
39 |
|
|
|
40 |
|
|
if (StringUtils.isBlank(authCode)) { throw new CollectorServiceException("Authorization failed: authCode is empty"); }
|
41 |
|
|
|
42 |
|
|
return () -> new FairSharingIterator(baseUrl, authCode, PAGE_SIZE);
|
43 |
|
|
}
|
44 |
|
|
|
45 |
|
|
private String login(final String baseUrl, final String login, final String password) throws CollectorServiceException {
|
46 |
|
|
final HttpPost req = new HttpPost(baseUrl + "/users/sign_in");
|
47 |
|
|
req.addHeader("Accept", "application/json");
|
48 |
|
|
req.addHeader("Content-Type", "application/json");
|
49 |
|
|
req.setEntity(prepareCredentials(login, password));
|
50 |
|
|
|
51 |
|
|
try (final CloseableHttpClient client = HttpClients.createDefault()) {
|
52 |
|
|
try (final CloseableHttpResponse response = client.execute(req)) {
|
53 |
|
|
final String content = IOUtils.toString(response.getEntity().getContent());
|
54 |
|
|
final JSONObject obj = new JSONObject(content);
|
55 |
|
|
return obj.getString("jwt");
|
56 |
|
|
}
|
57 |
|
|
} catch (final Exception e) {
|
58 |
|
|
throw new CollectorServiceException("Error perfoming call fro login", e);
|
59 |
|
|
}
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
public HttpEntity prepareCredentials(final String login, final String password) throws CollectorServiceException {
|
63 |
|
|
|
64 |
|
|
final JSONObject objUser = new JSONObject();
|
65 |
|
|
objUser.put("login", login);
|
66 |
|
|
objUser.put("password", password);
|
67 |
|
|
|
68 |
|
|
final JSONObject objCredentials = new JSONObject();
|
69 |
|
|
objCredentials.put("user", objUser);
|
70 |
|
|
|
71 |
|
|
try {
|
72 |
|
|
return new StringEntity(objCredentials.toString());
|
73 |
|
|
} catch (final UnsupportedEncodingException e) {
|
74 |
|
|
throw new CollectorServiceException("Error preparing http entity for login");
|
75 |
|
|
}
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
}
|