accounting-dashboard-harves.../src/main/java/org/gcube/dataharvest/harvester/VREUsersHarvester.java

61 lines
1.9 KiB
Java

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;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.dataharvest.datamodel.HarvestedData;
import org.gcube.dataharvest.utils.Utils;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Eric Perrone (ISTI - CNR)
* @author Luca Frosini (ISTI - CNR)
*/
public class VREUsersHarvester extends BasicHarvester {
private static Logger logger = LoggerFactory.getLogger(VREUsersHarvester.class);
public VREUsersHarvester(Date start, Date end) throws ParseException {
super(start, end);
}
@Override
public List<HarvestedData> getData() throws Exception {
try {
String context = Utils.getCurrentContext();
int measure = get();
HarvestedData harvest = new HarvestedData(HarvestedData.USERS, context, measure);
logger.debug(harvest.toString());
ArrayList<HarvestedData> data = new ArrayList<HarvestedData>();
data.add(harvest);
return data;
} catch(Exception e) {
throw e;
}
}
private int get() throws MalformedURLException, IOException {
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=";
String token = SecurityTokenProvider.instance.get();
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;
}
}