package org.gcube.gcat.social; import java.net.HttpURLConnection; 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.common.authorization.utils.manager.SecretManager; 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.utils.Constants; import org.gcube.gcat.utils.HTTPUtility; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author Luca Frosini (ISTI - CNR) */ public class SocialMessage extends Thread { private static final Logger logger = LoggerFactory.getLogger(SocialMessage.class); public static final String ITEM_URL = "Item URL"; // https://wiki.gcube-system.org/gcube/Social_Networking_Service#Send_a_message protected static final String SOCIAL_SERVICE_SEND_MESSAGE_PATH = "/2/messages/write-message"; protected static final String RESPONSE_SUCCESS_KEY = "success"; protected static final String RESPONSE_MESSAGE_KEY = "message"; protected final ObjectMapper objectMapper; private final String token; protected Message message; public SocialMessage() throws Exception { super(); this.objectMapper = new ObjectMapper(); SecretManager secretManager = SecretManagerProvider.instance.get(); this.token = secretManager.getCurrentSecretHolder().getSecrets().first().getToken(); } public Message getMessage() { return message; } public void setMessage(Message message) { this.message = message; } @Override public void run() { try { logger.info("Going to send Message {}", message); // write message sendSocialMessage(); } catch(Exception e) { logger.error("Error while executing post creation actions", e); } } public void sendSocialMessage() { try { String basePath = SocialService.getSocialService().getServiceBasePath(); if(basePath == null) { logger.info("Unable to send a message because there is no social networking service available"); return; } basePath = basePath.endsWith("/") ? basePath : basePath + "/"; String messageString = objectMapper.writeValueAsString(message); logger.debug("The message that is going to be send is\n{}", messageString); GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(basePath); gxhttpStringRequest.from(Constants.CATALOGUE_NAME); gxhttpStringRequest.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); gxhttpStringRequest.setSecurityToken(token); gxhttpStringRequest.path(SOCIAL_SERVICE_SEND_MESSAGE_PATH); HttpURLConnection httpURLConnection = gxhttpStringRequest.post(messageString); String ret = HTTPUtility.getResultAsString(httpURLConnection); JsonNode jsonNode = objectMapper.readTree(ret); if(jsonNode.get(RESPONSE_SUCCESS_KEY).asBoolean()) { logger.info("Message sent : {}", messageString); } else { logger.info("Failed to write the message {}. Reason {}", messageString, jsonNode.get(RESPONSE_MESSAGE_KEY).asText()); } } catch(Exception e) { logger.error("Unable to send the message : " + message.toString(), e); } } }