gcat/src/main/java/org/gcube/gcat/social/SocialUsers.java

49 lines
1.8 KiB
Java

package org.gcube.gcat.social;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.common.authorization.utils.socialservice.SocialService;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
import org.gcube.gcat.utils.HTTPUtility;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class SocialUsers {
protected static final String GET_USERNAMES_BY_ROLE = "2/users/get-usernames-by-role";
protected static final String GET_USERNAMES_BY_ROLE_ROLE_NAME_PARAMETER = "role-name";
protected static final String GET_USERNAMES_BY_ROLE_RESULT_KEY = "result";
public static Set<String> getUsernamesByRole(String roleName) throws Exception {
String socialServiceBasePath = SocialService.getSocialService().getServiceBasePath();
GXHTTPStringRequest gxhttpStringRequest = HTTPUtility.createGXHTTPStringRequest(socialServiceBasePath,
GET_USERNAMES_BY_ROLE, false);
Map<String,String> parameters = new HashMap<>();
parameters.put(GET_USERNAMES_BY_ROLE_ROLE_NAME_PARAMETER, roleName);
gxhttpStringRequest.queryParams(parameters);
HttpURLConnection httpURLConnection = gxhttpStringRequest.get();
String ret = HTTPUtility.getResultAsString(httpURLConnection);
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(ret);
ArrayNode arrayNode = (ArrayNode) jsonNode.get(GET_USERNAMES_BY_ROLE_RESULT_KEY);
Set<String> usernames = new HashSet<>();
for(JsonNode node : arrayNode) {
usernames.add(node.asText());
}
return usernames;
}
}