From c23d2fc5b7092621e8c27d0a9fd80331b9aa5b55 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Mon, 19 Sep 2022 17:45:43 +0200 Subject: [PATCH] Migrate code to use social-service-client refs #23151 --- .../org/gcube/gcat/oldutils/Validator.java | 5 +- .../org/gcube/gcat/social/SocialPost.java | 59 +++---------------- .../org/gcube/gcat/social/SocialUsers.java | 34 +---------- 3 files changed, 14 insertions(+), 84 deletions(-) diff --git a/src/main/java/org/gcube/gcat/oldutils/Validator.java b/src/main/java/org/gcube/gcat/oldutils/Validator.java index e448e54..d548e71 100644 --- a/src/main/java/org/gcube/gcat/oldutils/Validator.java +++ b/src/main/java/org/gcube/gcat/oldutils/Validator.java @@ -34,7 +34,6 @@ import org.gcube.gcat.persistence.ckan.CKANPackage; import org.gcube.gcat.persistence.ckan.CKANUser; import org.gcube.gcat.persistence.ckan.CKANUtility; import org.gcube.gcat.profile.MetadataUtility; -import org.gcube.gcat.social.SocialPost; import org.geojson.GeoJsonObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -53,6 +52,8 @@ public class Validator { public static final int MAX_TAG_CHARS = 100; + public static final String ITEM_URL = "Item URL"; + protected ObjectMapper mapper; public Validator() { @@ -90,7 +91,7 @@ public class Validator { CustomField cf = new CustomField(object); if(cf.getKey().equals(CKANPackage.EXTRAS_KEY_VALUE_SYSTEM_TYPE)) { metadataTypeCF = cf; - } else if(cf.getKey().equals(SocialPost.ITEM_URL)) { + } else if(cf.getKey().equals(ITEM_URL)) { continue; } else { customFields.add(cf); diff --git a/src/main/java/org/gcube/gcat/social/SocialPost.java b/src/main/java/org/gcube/gcat/social/SocialPost.java index 2caadaf..aac42ef 100644 --- a/src/main/java/org/gcube/gcat/social/SocialPost.java +++ b/src/main/java/org/gcube/gcat/social/SocialPost.java @@ -1,25 +1,18 @@ package org.gcube.gcat.social; import java.io.StringWriter; -import java.net.HttpURLConnection; import java.util.ArrayList; import java.util.List; -import javax.ws.rs.core.HttpHeaders; -import javax.ws.rs.core.MediaType; - 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.com.fasterxml.jackson.databind.node.ObjectNode; import org.gcube.common.authorization.utils.manager.SecretManagerProvider; -import org.gcube.common.authorization.utils.socialservice.SocialService; -import org.gcube.common.gxhttp.request.GXHTTPStringRequest; import org.gcube.gcat.api.configuration.CatalogueConfiguration; import org.gcube.gcat.configuration.CatalogueConfigurationFactory; import org.gcube.gcat.persistence.ckan.CKANUserCache; -import org.gcube.gcat.utils.Constants; -import org.gcube.gcat.utils.HTTPUtility; +import org.gcube.portal.databook.shared.Feed; +import org.gcube.social_networking.social_networking_client_library.PostClient; +import org.gcube.social_networking.socialnetworking.model.beans.PostInputBean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,22 +23,9 @@ public class SocialPost extends Thread { private static final Logger logger = LoggerFactory.getLogger(SocialPost.class); - public static final String ITEM_URL = "Item URL"; - - // https://wiki.gcube-system.org/gcube/Social_Networking_Service#Write_application_post_2 - protected static final String SOCIAL_SERVICE_WRITE_APPLICATION_POST_PATH = "/2/posts/write-post-app"; - - // String.format(NOTIFICATION_MESSAGE, fullName, title, url) protected static final String NOTIFICATION_MESSAGE = "%s just published the item \"%s\"\n" + "Please find it at %s\n"; - protected static final String SOCIAL_POST_TEXT_KEY = "text"; - protected static final String SOCIAL_POST_ENABLE_NOTIFICATION_KEY = "enable_notification"; - protected static final String SOCIAL_POST_RESPONSE_SUCCESS_KEY = "success"; - protected static final String SOCIAL_POST_RESPONSE_MESSAGE_KEY = "message"; - - protected final ObjectMapper objectMapper; - protected String itemID; protected String itemURL; protected String itemTitle; @@ -54,7 +34,6 @@ public class SocialPost extends Thread { public SocialPost() throws Exception { super(); - this.objectMapper = new ObjectMapper(); } public String getItemID() { @@ -149,12 +128,6 @@ public class SocialPost extends Thread { try { String fullName = CKANUserCache.getCurrrentCKANUser().getNameSurname(); - String basePath = SocialService.getSocialService().getServiceBasePath(); - if(basePath == null) { - logger.info("Unable to write a post because there is no social networking service available"); - return; - } - basePath = basePath.endsWith("/") ? basePath : basePath + "/"; StringWriter messageWriter = new StringWriter(); messageWriter.append(String.format(NOTIFICATION_MESSAGE, fullName, itemTitle, itemURL)); @@ -173,28 +146,12 @@ public class SocialPost extends Thread { logger.debug("The post that is going to be written is\n{}", message); - ObjectNode objectNode = objectMapper.createObjectNode(); - objectNode.put(SOCIAL_POST_TEXT_KEY, message); - objectNode.put(SOCIAL_POST_ENABLE_NOTIFICATION_KEY, notifyUsers); + PostClient postClient = new PostClient(); + PostInputBean postInputBean = new PostInputBean(); + postInputBean.setEnablenotification(notifyUsers); + postInputBean.setText(message); + Feed feed = postClient.writeApplicationPost(postInputBean); - // Do not use ApplicationMode class here because is a thread and change the current token could impact - // on the other threads. - - GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(basePath); - gxhttpStringRequest.from(Constants.CATALOGUE_NAME); - gxhttpStringRequest.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); - gxhttpStringRequest.setSecurityToken(Constants.getCatalogueSecret().getToken()); - gxhttpStringRequest.path(SOCIAL_SERVICE_WRITE_APPLICATION_POST_PATH); - - HttpURLConnection httpURLConnection = gxhttpStringRequest.post(objectMapper.writeValueAsString(objectNode)); - String ret = HTTPUtility.getResultAsString(httpURLConnection); - JsonNode jsonNode = objectMapper.readTree(ret); - if(jsonNode.get(SOCIAL_POST_RESPONSE_SUCCESS_KEY).asBoolean()) { - logger.info("Post written : {}", message); - } else { - logger.info("Failed to write the post {}. Reason {}", message, - jsonNode.get(SOCIAL_POST_RESPONSE_MESSAGE_KEY).asText()); - } } catch(Exception e) { logger.error("Unable to send Social Post", e); } diff --git a/src/main/java/org/gcube/gcat/social/SocialUsers.java b/src/main/java/org/gcube/gcat/social/SocialUsers.java index a2f3cd3..25fe68f 100644 --- a/src/main/java/org/gcube/gcat/social/SocialUsers.java +++ b/src/main/java/org/gcube/gcat/social/SocialUsers.java @@ -1,47 +1,19 @@ 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; +import org.gcube.social_networking.social_networking_client_library.UserClient; /** * @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 getUsernamesByRole(String roleName) throws Exception { - String socialServiceBasePath = SocialService.getSocialService().getServiceBasePath(); - - GXHTTPStringRequest gxhttpStringRequest = HTTPUtility.createGXHTTPStringRequest(socialServiceBasePath, - GET_USERNAMES_BY_ROLE, false); - Map 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 usernames = new HashSet<>(); - for(JsonNode node : arrayNode) { - usernames.add(node.asText()); - } - + UserClient userClient = new UserClient(); + Set usernames = new HashSet<>(userClient.getAllUsernamesByLocalRole(roleName)); return usernames; }