package org.gcube.gcat.social; import java.net.HttpURLConnection; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.ws.rs.InternalServerErrorException; import org.gcube.common.gxhttp.request.GXHTTPStringRequest; import org.gcube.gcat.api.moderation.Moderated; import org.gcube.gcat.utils.HTTPUtility; 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; /** * @author Luca Frosini (ISTI - CNR) */ public class PortalUser { protected static final String RESPONSE_SUCCESS_KEY = "success"; protected static final String RESPONSE_MESSAGE_KEY = "message"; protected static final String RESPONSE_RESULT_KEY = "result"; protected static final String SOCIAL_SERVICE_GET_OAUTH_USER_PROFILE_PATH = "2/users/get-oauth-profile"; // This key contains the fullname //protected static final String OAUTH_USER_PROFILE_FULLNAME_KEY = "name"; protected static final String OAUTH_USER_PROFILE_NAME_KEY = "given_name"; protected static final String OAUTH_USER_PROFILE_SURNAME_KEY = "family_name"; protected static final String OAUTH_USER_PROFILE_EMAIL_KEY = "email"; protected static final String OAUTH_USER_PROFILE_JOB_TITLE_KEY = "job_title"; protected static final String OAUTH_USER_PROFILE_ROLES_KEY = "roles"; protected final ObjectMapper objectMapper; // private JsonNode gCubeUserProfile; protected JsonNode oAuthUserProfile; protected String name; protected String surname; protected String eMail; protected String jobTitle; protected List roles; protected Boolean catalogueModerator; public PortalUser() { this.objectMapper = new ObjectMapper(); } public JsonNode getOAuthUserProfile() { if(oAuthUserProfile == null) { try { String socialServiceBasePath = SocialService.getSocialService().getServiceBasePath(); GXHTTPStringRequest gxhttpStringRequest = HTTPUtility.createGXHTTPStringRequest(socialServiceBasePath, SOCIAL_SERVICE_GET_OAUTH_USER_PROFILE_PATH, false); HttpURLConnection httpURLConnection = gxhttpStringRequest.get(); String ret = HTTPUtility.getResultAsString(httpURLConnection); oAuthUserProfile = objectMapper.readTree(ret); } catch(Exception e) { throw new InternalServerErrorException("Unable to retrive Infrastructure User Information from Social Service", e); } } return oAuthUserProfile; } public String getSurnameName() { return String.format("%s %s", getSurname(), getName()); } public String getNameSurname() { return String.format("%s %s", getName(), getSurname()); } public String getName() { if(name == null) { name = getOAuthUserProfile().get(OAUTH_USER_PROFILE_NAME_KEY).asText(); } return name; } public String getSurname() { if(surname == null) { surname = getOAuthUserProfile().get(OAUTH_USER_PROFILE_SURNAME_KEY).asText(); } return surname; } public String getEMail() { if(eMail == null) { eMail = getOAuthUserProfile().get(OAUTH_USER_PROFILE_EMAIL_KEY).asText(); } return eMail; } public List getRoles() { if(roles == null) { JsonNode jsonNode = getOAuthUserProfile().get(OAUTH_USER_PROFILE_ROLES_KEY); roles = new ArrayList(); if(jsonNode.isArray()) { ArrayNode arrayNode = (ArrayNode) jsonNode; if(arrayNode.size() > 0) { Iterator iterator = arrayNode.iterator(); while(iterator.hasNext()) { roles.add(iterator.next().asText()); } } } } return roles; } public boolean isCatalogueModerator() { if(catalogueModerator == null) { catalogueModerator = getRoles().contains(Moderated.CATALOGUE_MODERATOR); } return catalogueModerator; } public String getJobTitle() { if(jobTitle == null) { jobTitle = getOAuthUserProfile().get(OAUTH_USER_PROFILE_JOB_TITLE_KEY).asText(); } return jobTitle; } }