2018-05-11 17:50:37 +02:00
|
|
|
package org.gcube.dataharvest.harvester;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.text.ParseException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
|
|
|
|
2018-05-18 12:00:10 +02:00
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
2018-05-18 12:00:35 +02:00
|
|
|
import org.gcube.dataharvest.datamodel.HarvestedData;
|
2018-05-11 17:50:37 +02:00
|
|
|
import org.gcube.dataharvest.utils.Utils;
|
2018-05-16 15:08:25 +02:00
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2018-05-11 17:50:37 +02:00
|
|
|
|
|
|
|
public class VreUsersHarvester extends BasicHarvester {
|
2018-05-17 17:21:08 +02:00
|
|
|
|
|
|
|
|
2018-05-11 17:50:37 +02:00
|
|
|
private static Logger logger = LoggerFactory.getLogger(VreUsersHarvester.class);
|
|
|
|
|
|
|
|
public VreUsersHarvester(Date start, Date end) throws ParseException {
|
|
|
|
super(start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-05-18 12:00:35 +02:00
|
|
|
public List<HarvestedData> getData() throws Exception {
|
2018-05-18 12:00:10 +02:00
|
|
|
String context = Utils.getCurrentContext();
|
|
|
|
try {
|
|
|
|
int measure = get();
|
2018-05-18 12:00:35 +02:00
|
|
|
HarvestedData harvest = new HarvestedData(HarvestedData.USERS, context, measure);
|
2018-05-18 12:00:10 +02:00
|
|
|
logger.debug(harvest.toString());
|
2018-05-18 12:00:35 +02:00
|
|
|
ArrayList<HarvestedData> data = new ArrayList<HarvestedData>();
|
2018-05-18 12:00:10 +02:00
|
|
|
data.add(harvest);
|
|
|
|
return data;
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("Error Haversting Context Users for context {}", context, e);
|
|
|
|
throw e;
|
2018-05-11 17:50:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-18 12:00:10 +02:00
|
|
|
private int get() throws MalformedURLException, IOException {
|
2018-05-11 17:50:37 +02:00
|
|
|
int userNumber = 0;
|
|
|
|
// la seguente stringa deve essere letta dinamicamente
|
|
|
|
String url = "https://socialnetworking1.d4science.org/social-networking-library-ws/rest/2/users/get-all-usernames?gcube-token=";
|
2018-05-18 12:00:10 +02:00
|
|
|
String token = SecurityTokenProvider.instance.get();
|
2018-05-11 17:50:37 +02:00
|
|
|
JSONObject jsonObject = new JSONObject(Utils.getJson(url + token));
|
|
|
|
Boolean success = (Boolean) jsonObject.get("success");
|
|
|
|
if (success == false) {
|
|
|
|
String message = "get-all-usernames returned an error. token: " + token;
|
|
|
|
logger.error(message);
|
|
|
|
throw new IOException(message);
|
|
|
|
}
|
|
|
|
userNumber = jsonObject.getJSONArray("result").length();
|
|
|
|
return userNumber;
|
|
|
|
}
|
2018-05-17 17:21:08 +02:00
|
|
|
|
2018-05-11 17:50:37 +02:00
|
|
|
}
|