159 lines
4.7 KiB
Java
159 lines
4.7 KiB
Java
package org.gcube.portal.social_networking_client_library;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import org.apache.commons.lang.Validate;
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
|
import org.gcube.portal.social_networking_client_library.utils.HttpClient;
|
|
import org.json.JSONObject;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
/**
|
|
* Users client.
|
|
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
|
|
*/
|
|
public class UsersClient extends BaseClient{
|
|
|
|
private static final String SUB_SERVICE_PATH = "2/users/";
|
|
private static Logger logger = LoggerFactory.getLogger(UsersClient.class);
|
|
|
|
|
|
public UsersClient() throws Exception {
|
|
super(SUB_SERVICE_PATH);
|
|
}
|
|
|
|
/**
|
|
* Get attribute value
|
|
* @return
|
|
*/
|
|
public String getCustomAttribute(String attributeKey){
|
|
|
|
Validate.isTrue(attributeKey != null, "attribute key cannot be null");
|
|
|
|
logger.debug("Request for attribute value");
|
|
String thisMethodSignature = "get-custom-attribute";
|
|
String request = getServiceEndpoint() + thisMethodSignature + "?attribute=" + attributeKey;
|
|
return HttpClient.get(String.class, request, SecurityTokenProvider.instance.get());
|
|
|
|
}
|
|
|
|
/**
|
|
* Get fullname
|
|
* @return user's fullname
|
|
*/
|
|
public String getFullName(){
|
|
|
|
logger.debug("Request for fullname");
|
|
String thisMethodSignature = "get-fullname";
|
|
String request = getServiceEndpoint() + thisMethodSignature;
|
|
return HttpClient.get(String.class, request, SecurityTokenProvider.instance.get());
|
|
|
|
}
|
|
|
|
/**
|
|
* Get email
|
|
* @return users'email
|
|
*/
|
|
public String getEmail(){
|
|
|
|
logger.debug("Request for email");
|
|
String thisMethodSignature = "get-email";
|
|
String request = getServiceEndpoint() + thisMethodSignature;
|
|
return HttpClient.get(String.class, request, SecurityTokenProvider.instance.get());
|
|
|
|
}
|
|
|
|
/**
|
|
* Get profile
|
|
* @return
|
|
*/
|
|
public JSONObject getProfile(){
|
|
|
|
logger.debug("Request for getting profile");
|
|
String thisMethodSignature = "get-profile";
|
|
String request = getServiceEndpoint() + thisMethodSignature;
|
|
return HttpClient.get(JSONObject.class, request, SecurityTokenProvider.instance.get());
|
|
|
|
}
|
|
|
|
/**
|
|
* Get all usernames in this context
|
|
* @return
|
|
*/
|
|
@SuppressWarnings("unchecked")
|
|
public List<String> getAllUsernamesContext(){
|
|
|
|
logger.debug("Request for getting usernames in this context");
|
|
String thisMethodSignature = "get-all-usernames";
|
|
String request = getServiceEndpoint() + thisMethodSignature;
|
|
return HttpClient.get(new ArrayList<String>(0).getClass(), request, SecurityTokenProvider.instance.get());
|
|
|
|
}
|
|
|
|
/**
|
|
* Get all usernames and fullnames in this context
|
|
* @return
|
|
*/
|
|
@SuppressWarnings("unchecked")
|
|
public Map<String, String> getAllUsernamesFullnamesContext(){
|
|
|
|
logger.debug("Request for getting usernames and fullnames in this context");
|
|
String thisMethodSignature = "get-all-fullnames-and-usernames";
|
|
String request = getServiceEndpoint() + thisMethodSignature;
|
|
return HttpClient.get(new HashMap<String, String>(0).getClass(), request, SecurityTokenProvider.instance.get());
|
|
|
|
}
|
|
|
|
/**
|
|
* Get usernames by global role
|
|
* @return
|
|
*/
|
|
@SuppressWarnings("unchecked")
|
|
public List<String> getAllUsernamesByGlobalRole(String globalRole){
|
|
|
|
Validate.isTrue(globalRole != null, "role cannot be null");
|
|
|
|
logger.debug("Request for getting usernames with global role");
|
|
String thisMethodSignature = "get-usernames-by-global-role";
|
|
String request = getServiceEndpoint() + thisMethodSignature + "?role-name=" + globalRole;
|
|
return HttpClient.get(new ArrayList<String>(0).getClass(), request, SecurityTokenProvider.instance.get());
|
|
|
|
}
|
|
|
|
/**
|
|
* Get usernames by local role
|
|
* @return
|
|
*/
|
|
@SuppressWarnings("unchecked")
|
|
public List<String> getAllUsernamesByLocalRole(String localRole){
|
|
|
|
Validate.isTrue(localRole != null, "role cannot be null");
|
|
|
|
logger.debug("Request for getting usernames with global role");
|
|
String thisMethodSignature = "get-usernames-by-role";
|
|
String request = getServiceEndpoint() + thisMethodSignature + "?role-name=" + localRole;
|
|
return HttpClient.get(new ArrayList<String>(0).getClass(), request, SecurityTokenProvider.instance.get());
|
|
|
|
}
|
|
|
|
/**
|
|
* Check if user exists
|
|
* @return
|
|
*/
|
|
public Boolean userExists(String username){
|
|
|
|
Validate.isTrue(username != null, "username cannot be null");
|
|
|
|
logger.debug("Request for getting usernames with global role");
|
|
String thisMethodSignature = "user-exists";
|
|
String request = getServiceEndpoint() + thisMethodSignature + "?username=" + username;
|
|
return HttpClient.get(Boolean.class, request, SecurityTokenProvider.instance.get());
|
|
|
|
}
|
|
|
|
}
|